@@ -46,8 +46,9 @@ fun RowScope.ComputeDifferencesButton(
46
46
onClick = {
47
47
try {
48
48
if (referenceIsOlderThanCurrent(state)) {
49
- createThumbnailVideos(state)
50
- calculateVideoDifferences(scope, state, errorDialogText, showDialog)
49
+ scope.launch {
50
+ runComputation(scope, state, errorDialogText, showDialog)
51
+ }
51
52
} else {
52
53
showConfirmDialog.value = true
53
54
}
@@ -80,8 +81,9 @@ fun RowScope.ComputeDifferencesButton(
80
81
text = " The reference video is newer than the current video. Are you sure you want to continue?" ,
81
82
showDialog = showConfirmDialog.value,
82
83
onConfirm = {
83
- createThumbnailVideos(state)
84
- calculateVideoDifferences(scope, state, errorDialogText, showDialog)
84
+ scope.launch {
85
+ runComputation(scope, state, errorDialogText, showDialog)
86
+ }
85
87
showConfirmDialog.value = false
86
88
},
87
89
onCancel = {
@@ -90,6 +92,31 @@ fun RowScope.ComputeDifferencesButton(
90
92
)
91
93
}
92
94
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
+
93
120
fun createThumbnailVideos (state : MutableState <AppState >) {
94
121
// create the thumbnail videos
95
122
val tempReference = createThumbnailVideo(state.value.videoReferencePath!! , 0.25f )
@@ -99,49 +126,42 @@ fun createThumbnailVideos(state: MutableState<AppState>) {
99
126
}
100
127
101
128
private fun calculateVideoDifferences (
102
- scope : CoroutineScope ,
103
129
state : MutableState <AppState >,
104
130
errorDialogText : MutableState <String ?>,
105
- isLoading : MutableState <Boolean >,
106
131
) {
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
+ }
134
146
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
+ }
139
157
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
144
161
}
162
+
163
+ // set the sequence
164
+ state.value = state.value.copy(sequenceObj = generator.getSequence())
145
165
}
146
166
147
167
fun getVideoCreationDate (videoPath : String ): Long {
0 commit comments