288 lines
10 KiB
Kotlin
288 lines
10 KiB
Kotlin
/*
|
|
* Copyright (c) 2023 New Vector Ltd
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
package io.element.android.appnav
|
|
|
|
import android.content.Intent
|
|
import android.os.Parcelable
|
|
import androidx.compose.foundation.layout.Box
|
|
import androidx.compose.foundation.layout.fillMaxSize
|
|
import androidx.compose.runtime.Composable
|
|
import androidx.compose.ui.Alignment
|
|
import androidx.compose.ui.Modifier
|
|
import androidx.lifecycle.lifecycleScope
|
|
import com.bumble.appyx.core.composable.Children
|
|
import com.bumble.appyx.core.modality.BuildContext
|
|
import com.bumble.appyx.core.node.Node
|
|
import com.bumble.appyx.core.node.node
|
|
import com.bumble.appyx.core.plugin.Plugin
|
|
import com.bumble.appyx.core.plugin.plugins
|
|
import com.bumble.appyx.core.state.MutableSavedStateMap
|
|
import com.bumble.appyx.navmodel.backstack.BackStack
|
|
import com.bumble.appyx.navmodel.backstack.operation.pop
|
|
import com.bumble.appyx.navmodel.backstack.operation.push
|
|
import dagger.assisted.Assisted
|
|
import dagger.assisted.AssistedInject
|
|
import io.element.android.anvilannotations.ContributesNode
|
|
import io.element.android.appnav.di.MatrixClientsHolder
|
|
import io.element.android.appnav.intent.IntentResolver
|
|
import io.element.android.appnav.intent.ResolvedIntent
|
|
import io.element.android.appnav.root.RootPresenter
|
|
import io.element.android.appnav.root.RootView
|
|
import io.element.android.features.login.api.LoginUserStory
|
|
import io.element.android.features.login.api.oidc.OidcAction
|
|
import io.element.android.features.login.api.oidc.OidcActionFlow
|
|
import io.element.android.features.preferences.api.CacheService
|
|
import io.element.android.features.rageshake.api.bugreport.BugReportEntryPoint
|
|
import io.element.android.libraries.architecture.BackstackNode
|
|
import io.element.android.libraries.architecture.animation.rememberDefaultTransitionHandler
|
|
import io.element.android.libraries.architecture.createNode
|
|
import io.element.android.libraries.architecture.waitForChildAttached
|
|
import io.element.android.libraries.deeplink.DeeplinkData
|
|
import io.element.android.libraries.designsystem.theme.components.CircularProgressIndicator
|
|
import io.element.android.libraries.di.AppScope
|
|
import io.element.android.libraries.matrix.api.auth.MatrixAuthenticationService
|
|
import io.element.android.libraries.matrix.api.core.SessionId
|
|
import kotlinx.coroutines.flow.distinctUntilChanged
|
|
|
|
import kotlinx.coroutines.flow.Flow
|
|
import kotlinx.coroutines.flow.combine
|
|
import kotlinx.coroutines.flow.launchIn
|
|
import kotlinx.coroutines.flow.map
|
|
import kotlinx.coroutines.flow.onEach
|
|
import kotlinx.coroutines.flow.onStart
|
|
import kotlinx.parcelize.Parcelize
|
|
import timber.log.Timber
|
|
import java.util.UUID
|
|
|
|
@ContributesNode(AppScope::class)
|
|
class RootFlowNode @AssistedInject constructor(
|
|
@Assisted val buildContext: BuildContext,
|
|
@Assisted plugins: List<Plugin>,
|
|
private val authenticationService: MatrixAuthenticationService,
|
|
private val cacheService: CacheService,
|
|
private val matrixClientsHolder: MatrixClientsHolder,
|
|
private val presenter: RootPresenter,
|
|
private val bugReportEntryPoint: BugReportEntryPoint,
|
|
private val intentResolver: IntentResolver,
|
|
private val oidcActionFlow: OidcActionFlow,
|
|
private val loginUserStory: LoginUserStory,
|
|
) :
|
|
BackstackNode<RootFlowNode.NavTarget>(
|
|
backstack = BackStack(
|
|
initialElement = NavTarget.SplashScreen,
|
|
savedStateMap = buildContext.savedStateMap,
|
|
),
|
|
buildContext = buildContext,
|
|
plugins = plugins
|
|
) {
|
|
|
|
override fun onBuilt() {
|
|
matrixClientsHolder.restore(buildContext.savedStateMap)
|
|
super.onBuilt()
|
|
observeLoggedInState()
|
|
}
|
|
|
|
override fun onSaveInstanceState(state: MutableSavedStateMap) {
|
|
super.onSaveInstanceState(state)
|
|
matrixClientsHolder.save(state)
|
|
}
|
|
|
|
private fun observeLoggedInState() {
|
|
combine(
|
|
cacheService.onClearedCacheEventFlow(),
|
|
isUserLoggedInFlow(),
|
|
) { _, isLoggedIn -> isLoggedIn }
|
|
.onEach { isLoggedIn ->
|
|
Timber.v("isLoggedIn=$isLoggedIn")
|
|
if (isLoggedIn) {
|
|
tryToRestoreLatestSession(
|
|
onSuccess = { switchToLoggedInFlow(it) },
|
|
onFailure = { switchToNotLoggedInFlow() }
|
|
)
|
|
} else {
|
|
switchToNotLoggedInFlow()
|
|
}
|
|
}
|
|
.launchIn(lifecycleScope)
|
|
}
|
|
|
|
|
|
private fun switchToLoggedInFlow(sessionId: SessionId) {
|
|
backstack.safeRoot(NavTarget.LoggedInFlow(sessionId))
|
|
}
|
|
|
|
private fun isUserLoggedInFlow(): Flow<Boolean> {
|
|
return combine(
|
|
authenticationService.isLoggedIn(),
|
|
loginUserStory.loginFlowIsDone
|
|
) { isLoggedIn, loginFlowIsDone ->
|
|
isLoggedIn && loginFlowIsDone
|
|
}
|
|
.distinctUntilChanged()
|
|
}
|
|
|
|
private fun switchToNotLoggedInFlow() {
|
|
matrixClientsHolder.removeAll()
|
|
backstack.safeRoot(NavTarget.NotLoggedInFlow)
|
|
}
|
|
|
|
private suspend fun restoreSessionIfNeeded(
|
|
sessionId: SessionId,
|
|
onFailure: () -> Unit = {},
|
|
onSuccess: (SessionId) -> Unit = {},
|
|
) {
|
|
// If the session is already known it'll be restored by the node hierarchy
|
|
if (matrixClientsHolder.knowSession(sessionId)) {
|
|
Timber.v("Session $sessionId already alive, no need to restore.")
|
|
return
|
|
}
|
|
authenticationService.restoreSession(sessionId)
|
|
.onSuccess { matrixClient ->
|
|
matrixClientsHolder.add(matrixClient)
|
|
Timber.v("Succeed to restore session $sessionId")
|
|
onSuccess(sessionId)
|
|
}
|
|
.onFailure {
|
|
Timber.v("Failed to restore session $sessionId")
|
|
onFailure()
|
|
}
|
|
}
|
|
|
|
private suspend fun tryToRestoreLatestSession(
|
|
onSuccess: (SessionId) -> Unit = {},
|
|
onFailure: () -> Unit = {}
|
|
) {
|
|
val latestSessionId = authenticationService.getLatestSessionId()
|
|
if (latestSessionId == null) {
|
|
onFailure()
|
|
return
|
|
}
|
|
restoreSessionIfNeeded(latestSessionId, onFailure, onSuccess)
|
|
}
|
|
|
|
private fun onOpenBugReport() {
|
|
backstack.push(NavTarget.BugReport)
|
|
}
|
|
|
|
@Composable
|
|
override fun View(modifier: Modifier) {
|
|
val state = presenter.present()
|
|
RootView(
|
|
state = state,
|
|
modifier = modifier,
|
|
onOpenBugReport = this::onOpenBugReport,
|
|
) {
|
|
Children(
|
|
navModel = backstack,
|
|
// Animate opening the bug report screen
|
|
transitionHandler = rememberDefaultTransitionHandler(),
|
|
)
|
|
}
|
|
}
|
|
|
|
sealed interface NavTarget : Parcelable {
|
|
@Parcelize
|
|
object SplashScreen : NavTarget
|
|
|
|
@Parcelize
|
|
object NotLoggedInFlow : NavTarget
|
|
|
|
@Parcelize
|
|
data class LoggedInFlow(
|
|
val sessionId: SessionId,
|
|
val navId: UUID = UUID.randomUUID(),
|
|
) : NavTarget
|
|
|
|
@Parcelize
|
|
object BugReport : NavTarget
|
|
}
|
|
|
|
override fun resolve(navTarget: NavTarget, buildContext: BuildContext): Node {
|
|
return when (navTarget) {
|
|
is NavTarget.LoggedInFlow -> {
|
|
val matrixClient = matrixClientsHolder.getOrNull(navTarget.sessionId) ?: return splashNode(buildContext).also {
|
|
Timber.w("Couldn't find any session, go through SplashScreen")
|
|
}
|
|
val inputs = LoggedInFlowNode.Inputs(matrixClient)
|
|
val callback = object : LoggedInFlowNode.Callback {
|
|
override fun onOpenBugReport() {
|
|
backstack.push(NavTarget.BugReport)
|
|
}
|
|
}
|
|
val nodeLifecycleCallbacks = plugins<NodeLifecycleCallback>()
|
|
createNode<LoggedInFlowNode>(buildContext, plugins = listOf(inputs, callback) + nodeLifecycleCallbacks)
|
|
}
|
|
NavTarget.NotLoggedInFlow -> createNode<NotLoggedInFlowNode>(buildContext)
|
|
NavTarget.SplashScreen -> splashNode(buildContext)
|
|
NavTarget.BugReport -> {
|
|
val callback = object : BugReportEntryPoint.Callback {
|
|
override fun onBugReportSent() {
|
|
backstack.pop()
|
|
}
|
|
}
|
|
bugReportEntryPoint
|
|
.nodeBuilder(this, buildContext)
|
|
.callback(callback)
|
|
.build()
|
|
}
|
|
}
|
|
}
|
|
|
|
private fun splashNode(buildContext: BuildContext) = node(buildContext) {
|
|
Box(modifier = it.fillMaxSize(), contentAlignment = Alignment.Center) {
|
|
CircularProgressIndicator()
|
|
}
|
|
}
|
|
|
|
suspend fun handleIntent(intent: Intent) {
|
|
val resolvedIntent = intentResolver.resolve(intent) ?: return
|
|
when (resolvedIntent) {
|
|
is ResolvedIntent.Navigation -> navigateTo(resolvedIntent.deeplinkData)
|
|
is ResolvedIntent.Oidc -> onOidcAction(resolvedIntent.oidcAction)
|
|
}
|
|
}
|
|
|
|
private suspend fun navigateTo(deeplinkData: DeeplinkData) {
|
|
Timber.d("Navigating to $deeplinkData")
|
|
attachSession(deeplinkData.sessionId)
|
|
.apply {
|
|
when (deeplinkData) {
|
|
is DeeplinkData.Root -> attachRoot()
|
|
is DeeplinkData.Room -> attachRoom(deeplinkData)
|
|
is DeeplinkData.InviteList -> attachInviteList(deeplinkData)
|
|
}
|
|
}
|
|
}
|
|
|
|
private fun onOidcAction(oidcAction: OidcAction) {
|
|
oidcActionFlow.post(oidcAction)
|
|
}
|
|
|
|
private suspend fun attachSession(sessionId: SessionId): LoggedInFlowNode {
|
|
//TODO handle multi-session
|
|
return waitForChildAttached { navTarget ->
|
|
navTarget is NavTarget.LoggedInFlow && navTarget.sessionId == sessionId
|
|
}
|
|
}
|
|
|
|
private fun CacheService.onClearedCacheEventFlow(): Flow<Unit> {
|
|
return clearedCacheEventFlow
|
|
.onEach { sessionId -> matrixClientsHolder.remove(sessionId) }
|
|
.map { }
|
|
.onStart { emit((Unit)) }
|
|
}
|
|
}
|