-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NF: simplify catchingLifecycleScope #12011
Conversation
ee77290
to
f440484
Compare
Currently there are two parallel usages of coroutines - @divyansh-dxn has been exploring them to migrate the CollectionTasks, and I have been exploring them to make the backend integration easier. We thus currently have two different CoroutineHelpers.kt, and some overlapping functionality that should eventually be merged. I took a similar approach of adding this to an activity in my file as well, but maybe a fragment would be better (I don't know enough about Android's UI to properly understand the difference): Where we also differ is that my version applies special handling to some standard classes of errors, instead of relying on a message needing to be passed in. The backend code typically returns translated error messages for us already for example. |
I agree with the changes. Since at the time of adding it dae's changes were also unmerged I had to create a new file in parallel. Now the code can be moved to a single file. I would be good to create the Fragment.catchingLifecycleScope on top of dae's implementation and remove the redundant part. |
fun FragmentActivity.launchCatchingTask(
errorMessage: String? = null,
block: suspend CoroutineScope.() -> Unit
): Job {
return lifecycle.coroutineScope.launch {
try {
block()
} catch (exc: BackendInterruptedException) {
Timber.e("caught: %s", exc, errorMessage)
showSimpleSnackbar(this@launchCatchingTask, exc.localizedMessage, false)
} catch (exc: BackendException) {
Timber.e("caught: %s", exc, errorMessage)
showError(this@launchCatchingTask, exc.localizedMessage!!)
} catch (exc: Exception) {
Timber.e("caught: %s", exc, errorMessage)
showError(this@launchCatchingTask, exc.toString())
}
}
}
fun Fragment.launchCatchingTask(
errorMessage: String? = null,
block: suspend CoroutineScope.() -> Unit
) : Job = requireActivity().launchCatchingTask(errorMessage,block) I think this could be better in |
f440484
to
a387c82
Compare
Done. |
a387c82
to
87ae4d3
Compare
Was the a specific reason this code differs from the one I linked above? It loses the i18n messages from the backend, shows "an error occurred" when the user interrupts an operation instead of just "Interrupted", and doesn't wait for the user to confirm when an error has occurred, making it hard for them to screenshot it or copy it into a report. Why not combine the fragment and error message change with the existing function? |
Right now, my goal was to simplify code and add an helper method. Then my second goal was to limit the number of files. |
…tions Change based on #12011 (comment)
d37cc0f
to
a186536
Compare
Not sure what the intent is here, but it definitely took on conflicts |
daa7bba
to
2ba2b83
Compare
@@ -376,7 +376,7 @@ class Statistics : NavigationDrawerActivity(), DeckSelectionListener, SubtitleLi | |||
private fun createChart() { | |||
val statisticsActivity = requireActivity() as Statistics | |||
val taskHandler = statisticsActivity.taskHandler | |||
statisticsJob = viewLifecycleOwner.catchingLifecycleScope(requireActivity()) { | |||
statisticsJob = catchingLifecycleScope(requireActivity()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should have the error message instead of requireActivity()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was already using requireActivity
The goal of this PR was to simplify code, introduction useful utils for future code, and not do any actual change to behavior of the app.
So, even if you're right (I don't know, I have little context here), I don't believe it belongs to this PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we define Activity.catchingLifecycleScope
so we can remove the requireActivity()
call here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
By the way, why do we need to put taskHandler
value outside of the catchingLifecycleScope
if anyone know? Hopefully maybe @divyansh-dxn
2ba2b83
to
752e0a8
Compare
My understanding is that this method will often be called either on fragments, and using the fragment's activity. So it seems to make sense to simplify it, ensuring that we avoid a parameter that will probably never change. Actually, since activities are also LifecycleOwner, it may be possible to ensure this method is even simpler, by defining it on activities instead of defining it on LifecycleOwner. Unless we have a reason to use an activity distinct from the LifecycleOwner. Also cleaning from function I viewed at the same time
752e0a8
to
2a1d865
Compare
My understanding is that this method will often be called either on fragments,
and using the fragment's activity. So it seems to make sense to simplify it,
ensuring that we avoid a parameter that will probably never change.
Actually, since activities are also LifecycleOwner, it may be possible to ensure
this method is even simpler, by defining it on activities instead of defining it
on LifecycleOwner. Unless we have a reason to use an activity distinct from the
LifecycleOwner.
Also cleaning from function I viewed at the same time