NavigationStateService : do not throw error but just log and returns
This commit is contained in:
@@ -25,9 +25,9 @@ import io.element.android.libraries.matrix.api.core.SessionId
|
|||||||
import io.element.android.libraries.matrix.api.core.SpaceId
|
import io.element.android.libraries.matrix.api.core.SpaceId
|
||||||
import io.element.android.libraries.matrix.api.core.ThreadId
|
import io.element.android.libraries.matrix.api.core.ThreadId
|
||||||
import io.element.android.services.appnavstate.api.AppForegroundStateService
|
import io.element.android.services.appnavstate.api.AppForegroundStateService
|
||||||
import io.element.android.services.appnavstate.api.NavigationState
|
|
||||||
import io.element.android.services.appnavstate.api.AppNavigationStateService
|
|
||||||
import io.element.android.services.appnavstate.api.AppNavigationState
|
import io.element.android.services.appnavstate.api.AppNavigationState
|
||||||
|
import io.element.android.services.appnavstate.api.AppNavigationStateService
|
||||||
|
import io.element.android.services.appnavstate.api.NavigationState
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
@@ -50,16 +50,15 @@ class DefaultAppNavigationStateService @Inject constructor(
|
|||||||
|
|
||||||
private val state = MutableStateFlow(
|
private val state = MutableStateFlow(
|
||||||
AppNavigationState(
|
AppNavigationState(
|
||||||
navigationState = NavigationState.Root,
|
navigationState = NavigationState.Root,
|
||||||
isInForeground = true,
|
isInForeground = true,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
override val appNavigationState: StateFlow<AppNavigationState> = state
|
override val appNavigationState: StateFlow<AppNavigationState> = state
|
||||||
|
|
||||||
init {
|
init {
|
||||||
coroutineScope.launch {
|
coroutineScope.launch {
|
||||||
appForegroundStateService.start()
|
appForegroundStateService.start()
|
||||||
|
|
||||||
appForegroundStateService.isInForeground.collect { isInForeground ->
|
appForegroundStateService.isInForeground.collect { isInForeground ->
|
||||||
state.getAndUpdate { it.copy(isInForeground = isInForeground) }
|
state.getAndUpdate { it.copy(isInForeground = isInForeground) }
|
||||||
}
|
}
|
||||||
@@ -83,7 +82,7 @@ class DefaultAppNavigationStateService @Inject constructor(
|
|||||||
val currentValue = state.value.navigationState
|
val currentValue = state.value.navigationState
|
||||||
Timber.tag(loggerTag.value).d("Navigating to space $spaceId. Current state: $currentValue")
|
Timber.tag(loggerTag.value).d("Navigating to space $spaceId. Current state: $currentValue")
|
||||||
val newValue: NavigationState.Space = when (currentValue) {
|
val newValue: NavigationState.Space = when (currentValue) {
|
||||||
NavigationState.Root -> error("onNavigateToSession() must be called first")
|
NavigationState.Root -> return logError("onNavigateToSession()")
|
||||||
is NavigationState.Session -> NavigationState.Space(owner, spaceId, currentValue)
|
is NavigationState.Session -> NavigationState.Space(owner, spaceId, currentValue)
|
||||||
is NavigationState.Space -> NavigationState.Space(owner, spaceId, currentValue.parentSession)
|
is NavigationState.Space -> NavigationState.Space(owner, spaceId, currentValue.parentSession)
|
||||||
is NavigationState.Room -> NavigationState.Space(owner, spaceId, currentValue.parentSpace.parentSession)
|
is NavigationState.Room -> NavigationState.Space(owner, spaceId, currentValue.parentSpace.parentSession)
|
||||||
@@ -96,8 +95,8 @@ class DefaultAppNavigationStateService @Inject constructor(
|
|||||||
val currentValue = state.value.navigationState
|
val currentValue = state.value.navigationState
|
||||||
Timber.tag(loggerTag.value).d("Navigating to room $roomId. Current state: $currentValue")
|
Timber.tag(loggerTag.value).d("Navigating to room $roomId. Current state: $currentValue")
|
||||||
val newValue: NavigationState.Room = when (currentValue) {
|
val newValue: NavigationState.Room = when (currentValue) {
|
||||||
NavigationState.Root -> error("onNavigateToSession() must be called first")
|
NavigationState.Root -> return logError("onNavigateToSession()")
|
||||||
is NavigationState.Session -> error("onNavigateToSpace() must be called first")
|
is NavigationState.Session -> return logError("onNavigateToSpace()")
|
||||||
is NavigationState.Space -> NavigationState.Room(owner, roomId, currentValue)
|
is NavigationState.Space -> NavigationState.Room(owner, roomId, currentValue)
|
||||||
is NavigationState.Room -> NavigationState.Room(owner, roomId, currentValue.parentSpace)
|
is NavigationState.Room -> NavigationState.Room(owner, roomId, currentValue.parentSpace)
|
||||||
is NavigationState.Thread -> NavigationState.Room(owner, roomId, currentValue.parentRoom.parentSpace)
|
is NavigationState.Thread -> NavigationState.Room(owner, roomId, currentValue.parentRoom.parentSpace)
|
||||||
@@ -109,9 +108,9 @@ class DefaultAppNavigationStateService @Inject constructor(
|
|||||||
val currentValue = state.value.navigationState
|
val currentValue = state.value.navigationState
|
||||||
Timber.tag(loggerTag.value).d("Navigating to thread $threadId. Current state: $currentValue")
|
Timber.tag(loggerTag.value).d("Navigating to thread $threadId. Current state: $currentValue")
|
||||||
val newValue: NavigationState.Thread = when (currentValue) {
|
val newValue: NavigationState.Thread = when (currentValue) {
|
||||||
NavigationState.Root -> error("onNavigateToSession() must be called first")
|
NavigationState.Root -> return logError("onNavigateToSession()")
|
||||||
is NavigationState.Session -> error("onNavigateToSpace() must be called first")
|
is NavigationState.Session -> return logError("onNavigateToSpace()")
|
||||||
is NavigationState.Space -> error("onNavigateToRoom() must be called first")
|
is NavigationState.Space -> return logError("onNavigateToRoom()")
|
||||||
is NavigationState.Room -> NavigationState.Thread(owner, threadId, currentValue)
|
is NavigationState.Room -> NavigationState.Thread(owner, threadId, currentValue)
|
||||||
is NavigationState.Thread -> NavigationState.Thread(owner, threadId, currentValue.parentRoom)
|
is NavigationState.Thread -> NavigationState.Thread(owner, threadId, currentValue.parentRoom)
|
||||||
}
|
}
|
||||||
@@ -123,10 +122,10 @@ class DefaultAppNavigationStateService @Inject constructor(
|
|||||||
Timber.tag(loggerTag.value).d("Leaving thread. Current state: $currentValue")
|
Timber.tag(loggerTag.value).d("Leaving thread. Current state: $currentValue")
|
||||||
if (!currentValue.assertOwner(owner)) return
|
if (!currentValue.assertOwner(owner)) return
|
||||||
val newValue: NavigationState.Room = when (currentValue) {
|
val newValue: NavigationState.Room = when (currentValue) {
|
||||||
NavigationState.Root -> error("onNavigateToSession() must be called first")
|
NavigationState.Root -> return logError("onNavigateToSession()")
|
||||||
is NavigationState.Session -> error("onNavigateToSpace() must be called first")
|
is NavigationState.Session -> return logError("onNavigateToSpace()")
|
||||||
is NavigationState.Space -> error("onNavigateToRoom() must be called first")
|
is NavigationState.Space -> return logError("onNavigateToRoom()")
|
||||||
is NavigationState.Room -> error("onNavigateToThread() must be called first")
|
is NavigationState.Room -> return logError("onNavigateToThread()")
|
||||||
is NavigationState.Thread -> currentValue.parentRoom
|
is NavigationState.Thread -> currentValue.parentRoom
|
||||||
}
|
}
|
||||||
state.getAndUpdate { it.copy(navigationState = newValue) }
|
state.getAndUpdate { it.copy(navigationState = newValue) }
|
||||||
@@ -137,9 +136,9 @@ class DefaultAppNavigationStateService @Inject constructor(
|
|||||||
Timber.tag(loggerTag.value).d("Leaving room. Current state: $currentValue")
|
Timber.tag(loggerTag.value).d("Leaving room. Current state: $currentValue")
|
||||||
if (!currentValue.assertOwner(owner)) return
|
if (!currentValue.assertOwner(owner)) return
|
||||||
val newValue: NavigationState.Space = when (currentValue) {
|
val newValue: NavigationState.Space = when (currentValue) {
|
||||||
NavigationState.Root -> error("onNavigateToSession() must be called first")
|
NavigationState.Root -> return logError("onNavigateToSession()")
|
||||||
is NavigationState.Session -> error("onNavigateToSpace() must be called first")
|
is NavigationState.Session -> return logError("onNavigateToSpace()")
|
||||||
is NavigationState.Space -> error("onNavigateToRoom() must be called first")
|
is NavigationState.Space -> return logError("onNavigateToRoom()")
|
||||||
is NavigationState.Room -> currentValue.parentSpace
|
is NavigationState.Room -> currentValue.parentSpace
|
||||||
is NavigationState.Thread -> currentValue.parentRoom.parentSpace
|
is NavigationState.Thread -> currentValue.parentRoom.parentSpace
|
||||||
}
|
}
|
||||||
@@ -151,8 +150,8 @@ class DefaultAppNavigationStateService @Inject constructor(
|
|||||||
Timber.tag(loggerTag.value).d("Leaving space. Current state: $currentValue")
|
Timber.tag(loggerTag.value).d("Leaving space. Current state: $currentValue")
|
||||||
if (!currentValue.assertOwner(owner)) return
|
if (!currentValue.assertOwner(owner)) return
|
||||||
val newValue: NavigationState.Session = when (currentValue) {
|
val newValue: NavigationState.Session = when (currentValue) {
|
||||||
NavigationState.Root -> error("onNavigateToSession() must be called first")
|
NavigationState.Root -> return logError("onNavigateToSession()")
|
||||||
is NavigationState.Session -> error("onNavigateToSpace() must be called first")
|
is NavigationState.Session -> return logError("onNavigateToSpace()")
|
||||||
is NavigationState.Space -> currentValue.parentSession
|
is NavigationState.Space -> currentValue.parentSession
|
||||||
is NavigationState.Room -> currentValue.parentSpace.parentSession
|
is NavigationState.Room -> currentValue.parentSpace.parentSession
|
||||||
is NavigationState.Thread -> currentValue.parentRoom.parentSpace.parentSession
|
is NavigationState.Thread -> currentValue.parentRoom.parentSpace.parentSession
|
||||||
@@ -167,6 +166,10 @@ class DefaultAppNavigationStateService @Inject constructor(
|
|||||||
state.getAndUpdate { it.copy(navigationState = NavigationState.Root) }
|
state.getAndUpdate { it.copy(navigationState = NavigationState.Root) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun logError(logPrefix: String) {
|
||||||
|
Timber.tag(loggerTag.value).w("$logPrefix must be call first, can't leave.")
|
||||||
|
}
|
||||||
|
|
||||||
private fun NavigationState.assertOwner(owner: String): Boolean {
|
private fun NavigationState.assertOwner(owner: String): Boolean {
|
||||||
if (this.owner != owner) {
|
if (this.owner != owner) {
|
||||||
Timber.tag(loggerTag.value).d("Can't leave current state as the owner is not the same (current = ${this.owner}, new = $owner)")
|
Timber.tag(loggerTag.value).d("Can't leave current state as the owner is not the same (current = ${this.owner}, new = $owner)")
|
||||||
|
|||||||
Reference in New Issue
Block a user