2020-12-31 13:15:13 +01:00
|
|
|
package sushi.hardcore.droidfs
|
2020-07-28 22:25:10 +02:00
|
|
|
|
|
|
|
import android.widget.TextView
|
|
|
|
import android.widget.Toast
|
|
|
|
import androidx.appcompat.app.AlertDialog
|
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
|
|
import sushi.hardcore.droidfs.widgets.ColoredAlertDialogBuilder
|
|
|
|
|
2020-11-03 17:22:09 +01:00
|
|
|
abstract class LoadingTask(val activity: AppCompatActivity, loadingMessageResId: Int) {
|
2020-07-28 22:25:10 +02:00
|
|
|
private val dialogLoadingView = activity.layoutInflater.inflate(R.layout.dialog_loading, null)
|
|
|
|
private val dialogLoading: AlertDialog = ColoredAlertDialogBuilder(activity)
|
|
|
|
.setView(dialogLoadingView)
|
|
|
|
.setTitle(R.string.loading)
|
|
|
|
.setCancelable(false)
|
|
|
|
.create()
|
2020-08-07 19:25:16 +02:00
|
|
|
private var isStopped = false
|
2020-07-28 22:25:10 +02:00
|
|
|
init {
|
|
|
|
dialogLoadingView.findViewById<TextView>(R.id.text_message).text = activity.getString(loadingMessageResId)
|
|
|
|
startTask()
|
|
|
|
}
|
|
|
|
abstract fun doTask(activity: AppCompatActivity)
|
|
|
|
open fun doFinally(activity: AppCompatActivity){}
|
|
|
|
private fun startTask() {
|
|
|
|
dialogLoading.show()
|
|
|
|
Thread {
|
|
|
|
doTask(activity)
|
2020-08-07 19:25:16 +02:00
|
|
|
if (!isStopped){
|
|
|
|
dialogLoading.dismiss()
|
|
|
|
}
|
2020-07-28 22:25:10 +02:00
|
|
|
activity.runOnUiThread { doFinally(activity) }
|
|
|
|
}.start()
|
|
|
|
}
|
2020-11-03 17:22:09 +01:00
|
|
|
fun stopTask(onUiThread: (() -> Unit)?){
|
2020-08-07 19:25:16 +02:00
|
|
|
isStopped = true
|
2020-07-28 22:25:10 +02:00
|
|
|
dialogLoading.dismiss()
|
2020-08-07 19:25:16 +02:00
|
|
|
onUiThread?.let {
|
|
|
|
activity.runOnUiThread {
|
|
|
|
onUiThread()
|
|
|
|
}
|
2020-07-28 22:25:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
protected fun stopTaskWithToast(stringId: Int){
|
|
|
|
stopTask { Toast.makeText(activity, stringId, Toast.LENGTH_SHORT).show() }
|
|
|
|
}
|
|
|
|
}
|