Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions app/src/main/kotlin/com/wire/android/ui/WireActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ import com.wire.android.util.debug.LocalFeatureVisibilityFlags
import com.wire.android.util.launchUpdateTheApp
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.receiveAsFlow
Expand Down Expand Up @@ -259,13 +260,21 @@ class WireActivity : BaseActivity() {
val snackbarHostState = remember { SnackbarHostState() }
val currentUserId = viewModel.globalAppState.currentUserId
val appGraph = LocalContext.current.wireApplicationGraph
val sessionViewModelGraph = remember(appGraph, currentUserId) {
val currentSessionViewModelGraph = remember(appGraph, currentUserId) {
currentUserId?.let { appGraph.sessionViewModelGraph }
}
var retainedSessionViewModelGraph by remember(appGraph) {
mutableStateOf<AppSessionViewModelGraph?>(null)
}
LaunchedEffect(currentSessionViewModelGraph) {
currentSessionViewModelGraph?.let {
retainedSessionViewModelGraph = it
}
}
val authenticationViewModelGraph = remember(appGraph) {
appGraph.authenticationViewModelGraph
}
val activityViewModels = sessionViewModelGraph?.let {
val activityViewModels = currentSessionViewModelGraph?.let {
wireActivityScopedViewModels(it)
}

Expand Down Expand Up @@ -305,6 +314,14 @@ class WireActivity : BaseActivity() {
?.destination
?.route
?.getBaseRoute()
val effectiveBaseRoute = currentBaseRoute ?: startDestination.baseRoute
LaunchedEffect(currentUserId, effectiveBaseRoute) {
if (currentUserId == null && effectiveBaseRoute in authenticationGraphRoutes) {
delay(SESSION_GRAPH_RELEASE_DELAY_MILLIS)
retainedSessionViewModelGraph = null
Comment on lines +319 to +321
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit concerned about relying on a fixed 500ms delay, as it may not be sufficient on slower devices
can we rely on completion of the logout flow ?

}
}
val sessionViewModelGraph = currentSessionViewModelGraph ?: retainedSessionViewModelGraph
val metroViewModelGraph = rememberMetroViewModelGraph(
currentBaseRoute = currentBaseRoute,
startDestinationBaseRoute = startDestination.baseRoute,
Expand Down Expand Up @@ -879,6 +896,7 @@ class WireActivity : BaseActivity() {
companion object {
private const val HANDLED_DEEPLINK_FLAG = "deeplink_handled_flag_key"
private const val ORIGINAL_SAVED_INTENT_FLAG = "original_saved_intent"
private const val SESSION_GRAPH_RELEASE_DELAY_MILLIS = 500L
private const val TAG = "WireActivity"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ inline fun <reified VM> authenticationViewModel(
metroViewModel<AuthenticationViewModelGraph, VM>(
viewModelStoreOwner = viewModelStoreOwner,
key = key,
scopeKeyOverride = AUTHENTICATION_VIEW_MODEL_SCOPE_KEY,
) {
authenticationViewModelFactory.create()
}
Expand All @@ -75,10 +76,13 @@ inline fun <reified VM> authenticationSavedStateViewModel(
metroSavedStateViewModel<AuthenticationViewModelGraph, VM>(
viewModelStoreOwner = viewModelStoreOwner,
key = key,
scopeKeyOverride = AUTHENTICATION_VIEW_MODEL_SCOPE_KEY,
) { savedStateHandle ->
authenticationViewModelFactory.create(savedStateHandle)
}

const val AUTHENTICATION_VIEW_MODEL_SCOPE_KEY = "authentication"

@Composable
fun welcomeViewModel(): WelcomeViewModel = authenticationSavedStateViewModel { welcomeViewModel(it) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ inline fun <reified Graph, reified VM> metroViewModel(
"No ViewModelStoreOwner was provided via LocalViewModelStoreOwner"
},
key: String? = null,
scopeKeyOverride: String? = null,
crossinline create: Graph.() -> VM,
): VM where Graph : MetroViewModelGraph, VM : ViewModel {
val graph = checkNotNull(LocalMetroViewModelGraph.current as? Graph) {
Expand All @@ -59,7 +60,7 @@ inline fun <reified Graph, reified VM> metroViewModel(
val scopedKey = scopedMetroViewModelKey(
defaultKey = VM::class.qualifiedName,
key = key,
scopeKey = graph.viewModelScopeKey,
scopeKey = scopeKeyOverride ?: graph.viewModelScopeKey,
)
val factory = remember(graph) {
viewModelFactory {
Expand All @@ -82,6 +83,7 @@ inline fun <reified Graph, reified VM> metroSavedStateViewModel(
"No ViewModelStoreOwner was provided via LocalViewModelStoreOwner"
},
key: String? = null,
scopeKeyOverride: String? = null,
crossinline create: Graph.(SavedStateHandle) -> VM,
): VM where Graph : MetroViewModelGraph, VM : ViewModel {
val graph = checkNotNull(LocalMetroViewModelGraph.current as? Graph) {
Expand All @@ -90,7 +92,7 @@ inline fun <reified Graph, reified VM> metroSavedStateViewModel(
val scopedKey = scopedMetroViewModelKey(
defaultKey = VM::class.qualifiedName,
key = key,
scopeKey = graph.viewModelScopeKey,
scopeKey = scopeKeyOverride ?: graph.viewModelScopeKey,
)
val factory = remember(graph) {
viewModelFactory {
Expand Down
Loading