2020-12-31 13:15:13 +01:00
|
|
|
package sushi.hardcore.droidfs
|
2020-07-28 22:25:10 +02:00
|
|
|
|
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
2022-04-20 15:17:33 +02:00
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
import kotlinx.coroutines.withContext
|
|
|
|
import sushi.hardcore.droidfs.databinding.DialogLoadingBinding
|
2021-11-09 11:12:09 +01:00
|
|
|
import sushi.hardcore.droidfs.widgets.CustomAlertDialogBuilder
|
2020-07-28 22:25:10 +02:00
|
|
|
|
2022-04-20 15:17:33 +02:00
|
|
|
abstract class LoadingTask<T>(val activity: AppCompatActivity, themeValue: String, loadingMessageResId: Int) {
|
|
|
|
private val dialogLoading = CustomAlertDialogBuilder(activity, themeValue)
|
|
|
|
.setView(
|
|
|
|
DialogLoadingBinding.inflate(activity.layoutInflater).apply {
|
|
|
|
textMessage.text = activity.getString(loadingMessageResId)
|
|
|
|
}.root
|
|
|
|
)
|
2020-07-28 22:25:10 +02:00
|
|
|
.setTitle(R.string.loading)
|
|
|
|
.setCancelable(false)
|
|
|
|
.create()
|
2022-04-20 15:17:33 +02:00
|
|
|
|
|
|
|
abstract suspend fun doTask(): T
|
|
|
|
|
|
|
|
fun startTask(scope: CoroutineScope, onDone: (T) -> Unit) {
|
2020-07-28 22:25:10 +02:00
|
|
|
dialogLoading.show()
|
2022-04-20 15:17:33 +02:00
|
|
|
scope.launch {
|
|
|
|
val result = withContext(Dispatchers.IO) {
|
|
|
|
doTask()
|
2020-08-07 19:25:16 +02:00
|
|
|
}
|
2022-04-20 15:17:33 +02:00
|
|
|
dialogLoading.dismiss()
|
|
|
|
onDone(result)
|
2020-07-28 22:25:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|