@@ -8,7 +8,7 @@ import android.view.ViewGroup
88import androidx.fragment.app.DialogFragment
99import com.dan.timelapse.databinding.BusyDialogBinding
1010
11- class BusyDialog ( private var message : String ): DialogFragment() {
11+ class BusyDialog ( private var message : String , private var progress : Int , private var total : Int ): DialogFragment() {
1212
1313 companion object {
1414 private const val FRAGMENT_TAG = " busy"
@@ -29,27 +29,15 @@ class BusyDialog( private var message: String): DialogFragment() {
2929 }
3030 }
3131
32- fun show (message : String ) {
32+ fun show (message : String , progress : Int = -1, total : Int = -1 ) {
3333 runSafe {
3434 if (null == currentDialog) {
35- val dialog = BusyDialog (message)
35+ val dialog = BusyDialog (message, progress, total )
3636 dialog.isCancelable = false
3737 dialog.show(activity.supportFragmentManager, FRAGMENT_TAG )
3838 currentDialog = dialog
3939 } else {
40- currentDialog?.binding?.textBusyMessage?.text = message
41- }
42- }
43- }
44-
45- fun updateProgress (progress : Int , total : Int ) {
46- if (total <= 0 || progress < 0 || progress > total) return
47-
48- runSafe {
49- currentDialog?.binding?.progressBar?.apply {
50- this .max = total
51- this .progress = progress
52- this .isIndeterminate = false
40+ currentDialog?.update(message, progress, total)
5341 }
5442 }
5543 }
@@ -64,10 +52,34 @@ class BusyDialog( private var message: String): DialogFragment() {
6452
6553 private var binding: BusyDialogBinding ? = null
6654
55+ fun update (message : String , progress : Int = -1, total : Int = -1) {
56+ this .message = message
57+ this .progress = progress
58+ this .total = total
59+ update()
60+ }
61+
62+ private fun update () {
63+ val infinite = progress < 0 || total <= 0 || progress > total
64+ val title = if (progress < 0 ) message else " $message ($progress )"
65+ binding?.let {
66+ it.textBusyMessage.text = title
67+ if (infinite) {
68+ it.progressBar.isIndeterminate = true
69+ } else {
70+ it.progressBar.progress = 0
71+ it.progressBar.max = total
72+ it.progressBar.progress = progress
73+ it.progressBar.isIndeterminate = false
74+ }
75+ }
76+ }
77+
6778 override fun onCreateView (inflater : LayoutInflater , container : ViewGroup ? , savedInstanceState : Bundle ? ): View {
6879 val binding = BusyDialogBinding .inflate( inflater )
6980 binding.textBusyMessage.text = message
7081 this .binding = binding
82+ update()
7183 return binding.root
7284 }
7385}
0 commit comments