Fix cancelling dashboard jobs (#2395)

This commit is contained in:
Mikołaj Pich 2024-01-14 16:45:30 +01:00 committed by GitHub
parent 9ececeb4e9
commit 976eb5a772
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 8 deletions

View File

@ -1,6 +1,16 @@
package io.github.wulkanowy.data
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.emitAll
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.takeWhile
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import timber.log.Timber
@ -131,7 +141,7 @@ inline fun <ResultType, RequestType> networkBoundResource(
query().map { Resource.Success(filterResult(it)) }
} catch (throwable: Throwable) {
onFetchFailed(throwable)
query().map { Resource.Error(throwable) }
flowOf(Resource.Error(throwable))
}
} else {
query().map { Resource.Success(filterResult(it)) }
@ -165,7 +175,7 @@ inline fun <ResultType, RequestType, T> networkBoundResource(
query().map { Resource.Success(mapResult(it)) }
} catch (throwable: Throwable) {
onFetchFailed(throwable)
query().map { Resource.Error(throwable) }
flowOf(Resource.Error(throwable))
}
} else {
query().map { Resource.Success(mapResult(it)) }

View File

@ -76,7 +76,7 @@ class CaptchaDialog : BaseDialogFragment<DialogCaptchaBinding>() {
runCatching { parentFragmentManager.setFragmentResult(CAPTCHA_SUCCESS, bundleOf()) }
.onFailure { Timber.e(it) }
showMessage(getString(R.string.captcha_verified_message))
dismiss()
dismissAllowingStateLoss()
}
override fun onDestroy() {

View File

@ -324,7 +324,7 @@ class DashboardPresenter @Inject constructor(
) { luckyNumberResource, messageResource, attendanceResource ->
val resList = listOf(luckyNumberResource, messageResource, attendanceResource)
DashboardItem.HorizontalGroup(
resList to DashboardItem.HorizontalGroup(
isLoading = resList.any { it is Resource.Loading },
error = resList.map { it.errorOrNull }.let { errors ->
if (errors.all { it != null }) {
@ -349,9 +349,9 @@ class DashboardPresenter @Inject constructor(
)
})
}
.filterNot { it.isLoading && forceRefresh }
.filterNot { (_, it) -> it.isLoading && forceRefresh }
.distinctUntilChanged()
.onEach {
.onEach { (_, it) ->
updateData(it, forceRefresh)
if (it.isLoading) {
@ -369,7 +369,7 @@ class DashboardPresenter @Inject constructor(
)
errorHandler.dispatch(it)
}
.launch("horizontal_group ${if (forceRefresh) "-forceRefresh" else ""}")
.launchWithUniqueRefreshJob("horizontal_group", forceRefresh)
}
private fun loadGrades(student: Student, forceRefresh: Boolean) {
@ -862,6 +862,28 @@ class DashboardPresenter @Inject constructor(
onEach {
if (it is Resource.Success) {
cancelJobs(jobName)
} else if (it is Resource.Error) {
cancelJobs(jobName)
}
}.launch(jobName)
} else {
launch(jobName)
}
}
@JvmName("launchWithUniqueRefreshJobHorizontalGroup")
private fun Flow<Pair<List<Resource<*>>, *>>.launchWithUniqueRefreshJob(
name: String,
forceRefresh: Boolean
) {
val jobName = if (forceRefresh) "$name-forceRefresh" else name
if (forceRefresh) {
onEach { (resources, _) ->
if (resources.all { it is Resource.Success<*> }) {
cancelJobs(jobName)
} else if (resources.any { it is Resource.Error<*> }) {
cancelJobs(jobName)
}
}.launch(jobName)
} else {