diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/arch/BaseViewModel.kt b/app/apk/src/main/java/com/topjohnwu/magisk/arch/BaseViewModel.kt index c9a162c69..26d1e93f1 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/arch/BaseViewModel.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/arch/BaseViewModel.kt @@ -6,13 +6,16 @@ import androidx.lifecycle.ViewModel import com.topjohnwu.magisk.core.AppContext import com.topjohnwu.magisk.core.ktx.toast import com.topjohnwu.magisk.ui.navigation.Route -import kotlinx.coroutines.flow.MutableSharedFlow -import kotlinx.coroutines.flow.SharedFlow +import kotlinx.coroutines.channels.Channel +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.receiveAsFlow abstract class BaseViewModel : ViewModel() { - private val _navEvents = MutableSharedFlow(extraBufferCapacity = 1) - val navEvents: SharedFlow = _navEvents + // Keep requests until the UI collects them: one can arrive before the screen's collector + // has (re)started, e.g. from an activity result delivered right after a recreation + private val _navEvents = Channel(Channel.BUFFERED) + val navEvents: Flow = _navEvents.receiveAsFlow() fun showToast(@StringRes resId: Int) { AppContext.toast(resId, Toast.LENGTH_SHORT) @@ -23,6 +26,6 @@ abstract class BaseViewModel : ViewModel() { } fun navigateTo(route: Route) { - _navEvents.tryEmit(route) + _navEvents.trySend(route) } }