Skip to content

Commit 71b96da

Browse files
committed
Small changes to progress dialog
1 parent 4945ca6 commit 71b96da

File tree

2 files changed

+38
-25
lines changed

2 files changed

+38
-25
lines changed

app/src/main/java/com/dan/videostab/BusyDialog.kt

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import android.view.ViewGroup
88
import androidx.fragment.app.DialogFragment
99
import com.dan.videostab.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
}

app/src/main/java/com/dan/videostab/MainFragment.kt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ import kotlin.math.sin
2727

2828
class MainFragment(activity: MainActivity) : AppFragment(activity) {
2929
companion object {
30+
const val TITLE_ANALYSE = "Analyse"
31+
const val TITLE_STABILIZE = "Stabilize"
32+
const val TITLE_SAVE = "Save"
33+
3034
const val INTENT_OPEN_VIDEO = 2
3135
const val INTENT_OPEN_IMAGES = 3
3236
const val INTENT_OPEN_FOLDER = 4
@@ -246,13 +250,13 @@ class MainFragment(activity: MainActivity) : AppFragment(activity) {
246250

247251

248252
private fun handleStabilize() {
249-
runAsync("Stabilize") {
253+
runAsync(TITLE_STABILIZE) {
250254
stabApplyAsync()
251255
}
252256
}
253257

254258
private fun handleSave() {
255-
runAsync("Save") {
259+
runAsync(TITLE_SAVE) {
256260
saveAsync()
257261
}
258262
}
@@ -335,12 +339,11 @@ class MainFragment(activity: MainActivity) : AppFragment(activity) {
335339
var a = 0.0
336340

337341
framesInput.forEachFrame { index, size, readFrame ->
338-
if (firstFrame.empty()) firstFrame = readFrame.clone()
339342
cvtColor(readFrame, frames[currentIndex], COLOR_BGR2GRAY)
340343
val scale = scaleForAnalyse(frames[currentIndex])
341344

342345
frameCounter++
343-
BusyDialog.updateProgress(index, size)
346+
BusyDialog.show(TITLE_ANALYSE, index, size)
344347

345348
if (!frames[prevIndex].empty()) {
346349
// Detect features in previous frame
@@ -465,8 +468,6 @@ class MainFragment(activity: MainActivity) : AppFragment(activity) {
465468
}
466469

467470
private fun stabApplyAsync() {
468-
BusyDialog.show("Smooth movements")
469-
470471
val framesInput = this.framesInput ?: return
471472
val trajectory = videoTrajectory ?: return
472473

@@ -571,9 +572,8 @@ class MainFragment(activity: MainActivity) : AppFragment(activity) {
571572
val frameStabilized = Mat()
572573
val t = Mat(2, 3, CV_64F)
573574

574-
BusyDialog.show("Stabilize")
575575
framesInput.forEachFrame { index, size, frame ->
576-
BusyDialog.updateProgress(index, size)
576+
BusyDialog.show(TITLE_STABILIZE, index, size)
577577

578578
transforms.getTransform(index, t)
579579
warpAffine(frame, frameStabilized, t, frame.size())
@@ -633,6 +633,7 @@ class MainFragment(activity: MainActivity) : AppFragment(activity) {
633633
}
634634

635635
firstFrame = framesInput.firstFrame()
636+
scaleForAnalyse(firstFrame)
636637
}
637638
}
638639

@@ -668,7 +669,7 @@ class MainFragment(activity: MainActivity) : AppFragment(activity) {
668669

669670
private fun stabAnalyse() {
670671
videoTrajectory = null
671-
runAsync("Analyse") { stabAnalyzeAsync() }
672+
runAsync(TITLE_ANALYSE) { stabAnalyzeAsync() }
672673
}
673674

674675
private fun updateView() {

0 commit comments

Comments
 (0)