Skip to content

Commit 7231926

Browse files
committed
feat(gui): run comput. & thumbnail vid. creation concurrently
Signed-off-by: Anton Kriese <[email protected]>
1 parent 5b83727 commit 7231926

File tree

1 file changed

+61
-41
lines changed

1 file changed

+61
-41
lines changed

GUI/src/main/kotlin/ui/components/selectVideoScreen/ComputeDifferencesButton.kt

Lines changed: 61 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ fun RowScope.ComputeDifferencesButton(
4646
onClick = {
4747
try {
4848
if (referenceIsOlderThanCurrent(state)) {
49-
createThumbnailVideos(state)
50-
calculateVideoDifferences(scope, state, errorDialogText, showDialog)
49+
scope.launch {
50+
runComputation(scope, state, errorDialogText, showDialog)
51+
}
5152
} else {
5253
showConfirmDialog.value = true
5354
}
@@ -80,8 +81,9 @@ fun RowScope.ComputeDifferencesButton(
8081
text = "The reference video is newer than the current video. Are you sure you want to continue?",
8182
showDialog = showConfirmDialog.value,
8283
onConfirm = {
83-
createThumbnailVideos(state)
84-
calculateVideoDifferences(scope, state, errorDialogText, showDialog)
84+
scope.launch {
85+
runComputation(scope, state, errorDialogText, showDialog)
86+
}
8587
showConfirmDialog.value = false
8688
},
8789
onCancel = {
@@ -90,6 +92,31 @@ fun RowScope.ComputeDifferencesButton(
9092
)
9193
}
9294

95+
suspend fun runComputation(
96+
scope: CoroutineScope,
97+
state: MutableState<AppState>,
98+
errorDialogText: MutableState<String?>,
99+
isLoading: MutableState<Boolean>,
100+
) {
101+
isLoading.value = true
102+
val computeJob =
103+
scope.launch(Dispatchers.Default) {
104+
calculateVideoDifferences(state, errorDialogText)
105+
}
106+
107+
computeJob.invokeOnCompletion { isLoading.value = false }
108+
109+
val videoScaleJob =
110+
scope.launch(Dispatchers.Default) {
111+
createThumbnailVideos(state)
112+
}
113+
114+
// wait for both jobs to finish before transitioning to the diff screen
115+
listOf(computeJob, videoScaleJob).joinAll()
116+
117+
state.value = state.value.copy(screen = Screen.DiffScreen, hasUnsavedChanges = true)
118+
}
119+
93120
fun createThumbnailVideos(state: MutableState<AppState>) {
94121
// create the thumbnail videos
95122
val tempReference = createThumbnailVideo(state.value.videoReferencePath!!, 0.25f)
@@ -99,49 +126,42 @@ fun createThumbnailVideos(state: MutableState<AppState>) {
99126
}
100127

101128
private fun calculateVideoDifferences(
102-
scope: CoroutineScope,
103129
state: MutableState<AppState>,
104130
errorDialogText: MutableState<String?>,
105-
isLoading: MutableState<Boolean>,
106131
) {
107-
scope.launch(Dispatchers.Default) {
108-
isLoading.value = true
109-
AlgorithmExecutionState.getInstance().reset()
110-
111-
// generate the differences
112-
lateinit var generator: DifferenceGeneratorWrapper
113-
try {
114-
generator = DifferenceGeneratorWrapper(state)
115-
} catch (e: DifferenceGeneratorException) {
116-
errorDialogText.value = e.toString()
117-
return@launch
118-
} catch (e: Exception) {
119-
errorDialogText.value = "An unexpected exception was thrown when creating" +
120-
"the DifferenceGenerator instance:\n\n${e.message}"
121-
return@launch
122-
}
123-
124-
try {
125-
generator.getDifferences(state.value.outputPath!!)
126-
} catch (e: DifferenceGeneratorStoppedException) {
127-
println("stopped by canceling...")
128-
return@launch
129-
} catch (e: Exception) {
130-
errorDialogText.value = "An unexpected exception was thrown when running" +
131-
"the difference computation:\n\n${e.message}"
132-
return@launch
133-
}
132+
AlgorithmExecutionState.getInstance().reset()
133+
134+
// generate the differences
135+
lateinit var generator: DifferenceGeneratorWrapper
136+
try {
137+
generator = DifferenceGeneratorWrapper(state)
138+
} catch (e: DifferenceGeneratorException) {
139+
errorDialogText.value = e.toString()
140+
return
141+
} catch (e: Exception) {
142+
errorDialogText.value = "An unexpected exception was thrown when creating" +
143+
"the DifferenceGenerator instance:\n\n${e.message}"
144+
return
145+
}
134146

135-
// check for cancellation one last time before switching to the diff screen
136-
if (!AlgorithmExecutionState.getInstance().isAlive()) {
137-
return@launch
138-
}
147+
try {
148+
generator.getDifferences(state.value.outputPath!!)
149+
} catch (e: DifferenceGeneratorStoppedException) {
150+
println("stopped by canceling...")
151+
return
152+
} catch (e: Exception) {
153+
errorDialogText.value = "An unexpected exception was thrown when running" +
154+
"the difference computation:\n\n${e.message}"
155+
return
156+
}
139157

140-
// set the sequence and screen
141-
state.value = state.value.copy(sequenceObj = generator.getSequence(), screen = Screen.DiffScreen, hasUnsavedChanges = true)
142-
}.invokeOnCompletion {
143-
isLoading.value = false
158+
// check for cancellation one last time before switching to the diff screen
159+
if (!AlgorithmExecutionState.getInstance().isAlive()) {
160+
return
144161
}
162+
163+
// set the sequence
164+
state.value = state.value.copy(sequenceObj = generator.getSequence())
145165
}
146166

147167
fun getVideoCreationDate(videoPath: String): Long {

0 commit comments

Comments
 (0)