DroidFS/app/src/main/java/sushi/hardcore/droidfs/LoadingTask.kt

34 lines
1.1 KiB
Kotlin
Raw Normal View History

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()
}
2022-04-20 15:17:33 +02:00
dialogLoading.dismiss()
onDone(result)
2020-07-28 22:25:10 +02:00
}
}
}