Fix scenario copy cancellation#847
Conversation
|
Hi and thank you for your contribution. Let me know if its ok with you. I can also do the changes myself if needed :) |
| withContext(NonCancellable) { | ||
| if (isSmart) smartRepository.addScenarioCopy(scenarioId, name) | ||
| else dumbRepository.addDumbScenarioCopy(scenarioId, name) | ||
| } |
There was a problem hiding this comment.
While this works, i don't think that's the proper way to fix this.
A better approach would be to delegate the threading responsability to the smart & dumb repositories, allowing to tie the copy to the app lifecycle.
So basically, by adding the following parameter to both repositories:
@Singleton
class DumbRepository @Inject constructor(
@Dispatcher(IO) ioDispatcher: CoroutineDispatcher,
private val dumbScenarioDataSource: DumbScenarioDataSource,
) : IDumbRepository {
private val coroutineScopeIo: CoroutineScope =
CoroutineScope(SupervisorJob() + ioDispatcher)
fun addDumbScenarioCopy(scenarioDbId: Long, copyName: String, onCopyCompleted: () -> Unit): Long? {
coroutineScopeIo.launch {
dumbScenarioDao.getDumbScenariosWithAction(scenarioDbId)?.let { scenarioWithActions ->
addDumbScenarioCopy(scenarioWithActions, copyName)
onCopyCompleted()
}
}
}
}
f7daf69 to
1196b75
Compare
|
Thanks for the review. I pushed an update that follows your suggested direction: the scenario copy work is now launched from repository-owned IO scopes for both smart and dumb scenarios, and the ViewModel only requests the copy and handles completion back on the main dispatcher. I have not performed device/build testing after this amendment. I am also completely fine with either this amended approach or with you making further changes directly to the PR if you prefer a slightly different implementation style. |
Summary
Why
The dialog previously used the default
setPositiveButtonbehavior, so pressing OK dismissed the dialog immediately. Since the copy coroutine is owned by the dialog view model, that could cancel the copy work before it completed.Validation
:smartautoclicker:compileFDroidDebugKotlin