mirror of
https://github.com/wulkanowy/wulkanowy.git
synced 2025-01-19 00:26:45 -06:00
Fix cancelling dashboard jobs (#2395)
This commit is contained in:
parent
9ececeb4e9
commit
976eb5a772
@ -1,6 +1,16 @@
|
|||||||
package io.github.wulkanowy.data
|
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.Mutex
|
||||||
import kotlinx.coroutines.sync.withLock
|
import kotlinx.coroutines.sync.withLock
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
@ -131,7 +141,7 @@ inline fun <ResultType, RequestType> networkBoundResource(
|
|||||||
query().map { Resource.Success(filterResult(it)) }
|
query().map { Resource.Success(filterResult(it)) }
|
||||||
} catch (throwable: Throwable) {
|
} catch (throwable: Throwable) {
|
||||||
onFetchFailed(throwable)
|
onFetchFailed(throwable)
|
||||||
query().map { Resource.Error(throwable) }
|
flowOf(Resource.Error(throwable))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
query().map { Resource.Success(filterResult(it)) }
|
query().map { Resource.Success(filterResult(it)) }
|
||||||
@ -165,7 +175,7 @@ inline fun <ResultType, RequestType, T> networkBoundResource(
|
|||||||
query().map { Resource.Success(mapResult(it)) }
|
query().map { Resource.Success(mapResult(it)) }
|
||||||
} catch (throwable: Throwable) {
|
} catch (throwable: Throwable) {
|
||||||
onFetchFailed(throwable)
|
onFetchFailed(throwable)
|
||||||
query().map { Resource.Error(throwable) }
|
flowOf(Resource.Error(throwable))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
query().map { Resource.Success(mapResult(it)) }
|
query().map { Resource.Success(mapResult(it)) }
|
||||||
|
@ -76,7 +76,7 @@ class CaptchaDialog : BaseDialogFragment<DialogCaptchaBinding>() {
|
|||||||
runCatching { parentFragmentManager.setFragmentResult(CAPTCHA_SUCCESS, bundleOf()) }
|
runCatching { parentFragmentManager.setFragmentResult(CAPTCHA_SUCCESS, bundleOf()) }
|
||||||
.onFailure { Timber.e(it) }
|
.onFailure { Timber.e(it) }
|
||||||
showMessage(getString(R.string.captcha_verified_message))
|
showMessage(getString(R.string.captcha_verified_message))
|
||||||
dismiss()
|
dismissAllowingStateLoss()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
|
@ -324,7 +324,7 @@ class DashboardPresenter @Inject constructor(
|
|||||||
) { luckyNumberResource, messageResource, attendanceResource ->
|
) { luckyNumberResource, messageResource, attendanceResource ->
|
||||||
val resList = listOf(luckyNumberResource, messageResource, attendanceResource)
|
val resList = listOf(luckyNumberResource, messageResource, attendanceResource)
|
||||||
|
|
||||||
DashboardItem.HorizontalGroup(
|
resList to DashboardItem.HorizontalGroup(
|
||||||
isLoading = resList.any { it is Resource.Loading },
|
isLoading = resList.any { it is Resource.Loading },
|
||||||
error = resList.map { it.errorOrNull }.let { errors ->
|
error = resList.map { it.errorOrNull }.let { errors ->
|
||||||
if (errors.all { it != null }) {
|
if (errors.all { it != null }) {
|
||||||
@ -349,9 +349,9 @@ class DashboardPresenter @Inject constructor(
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
.filterNot { it.isLoading && forceRefresh }
|
.filterNot { (_, it) -> it.isLoading && forceRefresh }
|
||||||
.distinctUntilChanged()
|
.distinctUntilChanged()
|
||||||
.onEach {
|
.onEach { (_, it) ->
|
||||||
updateData(it, forceRefresh)
|
updateData(it, forceRefresh)
|
||||||
|
|
||||||
if (it.isLoading) {
|
if (it.isLoading) {
|
||||||
@ -369,7 +369,7 @@ class DashboardPresenter @Inject constructor(
|
|||||||
)
|
)
|
||||||
errorHandler.dispatch(it)
|
errorHandler.dispatch(it)
|
||||||
}
|
}
|
||||||
.launch("horizontal_group ${if (forceRefresh) "-forceRefresh" else ""}")
|
.launchWithUniqueRefreshJob("horizontal_group", forceRefresh)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun loadGrades(student: Student, forceRefresh: Boolean) {
|
private fun loadGrades(student: Student, forceRefresh: Boolean) {
|
||||||
@ -862,6 +862,28 @@ class DashboardPresenter @Inject constructor(
|
|||||||
onEach {
|
onEach {
|
||||||
if (it is Resource.Success) {
|
if (it is Resource.Success) {
|
||||||
cancelJobs(jobName)
|
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)
|
}.launch(jobName)
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user