mirror of
https://github.com/kvorschk/Magisk.git
synced 2026-12-04 19:10:47 -05:00
app: buffer navigation events until collected
ViewModels requested navigation through a SharedFlow without replay, so a request was silently dropped when no screen was collecting it. This happens when an activity result arrives right after the activity was recreated: the launcher delivers it before the screen's collector has started. E.g. after a configuration change while picking an image to patch, the patch dialog closed but the patching screen never opened. Use a buffered Channel instead, so requests wait for the collector. Assisted-by: Claude Opus 5.5
This commit is contained in:
@@ -6,13 +6,16 @@ import androidx.lifecycle.ViewModel
|
|||||||
import com.topjohnwu.magisk.core.AppContext
|
import com.topjohnwu.magisk.core.AppContext
|
||||||
import com.topjohnwu.magisk.core.ktx.toast
|
import com.topjohnwu.magisk.core.ktx.toast
|
||||||
import com.topjohnwu.magisk.ui.navigation.Route
|
import com.topjohnwu.magisk.ui.navigation.Route
|
||||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
import kotlinx.coroutines.channels.Channel
|
||||||
import kotlinx.coroutines.flow.SharedFlow
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.receiveAsFlow
|
||||||
|
|
||||||
abstract class BaseViewModel : ViewModel() {
|
abstract class BaseViewModel : ViewModel() {
|
||||||
|
|
||||||
private val _navEvents = MutableSharedFlow<Route>(extraBufferCapacity = 1)
|
// Keep requests until the UI collects them: one can arrive before the screen's collector
|
||||||
val navEvents: SharedFlow<Route> = _navEvents
|
// has (re)started, e.g. from an activity result delivered right after a recreation
|
||||||
|
private val _navEvents = Channel<Route>(Channel.BUFFERED)
|
||||||
|
val navEvents: Flow<Route> = _navEvents.receiveAsFlow()
|
||||||
|
|
||||||
fun showToast(@StringRes resId: Int) {
|
fun showToast(@StringRes resId: Int) {
|
||||||
AppContext.toast(resId, Toast.LENGTH_SHORT)
|
AppContext.toast(resId, Toast.LENGTH_SHORT)
|
||||||
@@ -23,6 +26,6 @@ abstract class BaseViewModel : ViewModel() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun navigateTo(route: Route) {
|
fun navigateTo(route: Route) {
|
||||||
_navEvents.tryEmit(route)
|
_navEvents.trySend(route)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user