Multi accounts - experimental first implementation (#5285)

* Multi account - Do not reset analytics store on sign out.

Else when 1 of many accounts is removed, the analytics opt in screen is displayed again.

* Multi accounts - first implementation.

* Multi accounts - Prevent user from logging twice with the same account

* Multi accounts - ignore automatic GoBack in case of error.

* Multi accounts - update first view when adding an account.

* Rename method storeData to addSession.

* Multi accounts - handle account switch when coming from a notification

* Multi accounts - handle login link when there is already an account.

* Multi accounts - handle click on push history for not current account.

* Multi accounts - improve layout and add preview.

* Add accountselect modules

* Multi accounts - incoming share with account selection

* Multi accounts - check the feature flag before allowing login using login link.

* Multi accounts - swipe on account icon

* Cleanup

* Multi accounts - fix other implementation of SessionStore

* Multi accounts - fix PreferencesRootPresenterTest

* Multi accounts - Add test on AccountSelectPresenter

* Multi accounts - Fix test on HomePresenter - WIP

* Update database to be able to sort accounts by creation date.

* Add unit test on takeCurrentUserWithNeighbors

* Fix test and improve code.

* Add exception

* Multi accounts - handle permalink

* Code quality

* Multi accounts - localization

* Fix issue after rebase on develop

* Fix issue after rebase on develop

* Fix tests

* Fix tests

* Fix tests

* Fix tests

* Update Multi accounts flag details.

* Add missing test on DatabaseSessionStore

* Add missing preview on LoginModeView

* Remove dead code.

* Add missing preview on PushHistoryView

* Document API.

* Rename API and update test.

* Remove MatrixAuthenticationService.loggedInStateFlow()

* Update screenshots

* Remove unused import

* Add exception

* Fix compilation issue after rebase on develop.

* Update screenshots

* Fix test

* Avoid calling getLatestSession() twice

* Rename `matrixUserAndNeighbors` to `currentUserAndNeighbors`

* Extract code to its own class.

* Add comment to clarify the code.

* Init current user profile with what we now have in the database.

It allows having the cached data (user display name and avatar) when starting the application when no network is available.

* Let the RustMatrixClient update the profile in the session database

* Fix test.

* When logging out from Pin code screen, logout from all the sessions.

tom

* Make PushData.clientSecret mandatory.
Also do not restore the last session as a fallback, it can lead to error in a multi account context, or even when a ghost pusher send a Push.

* Change test in RustMatrixAuthenticationServiceTest

* Do not use MatrixAuthenticationService in RootFlowNode, only use SessionStore

* Remove MatrixAuthenticationService.getLatestSessionId()

* Fix compilation issue after merging develop

* Add test on DefaultAccountSelectEntryPoint

* Fix compilation issue after merging develop

* Introduce LoggedInAccountSwitcherNode, to improve animation when switching between accounts.

* Rename Node to follow naming convention.

* Fix navigation issue after login.

* Remove unused import

* Revert "Fix navigation issue after login."

This reverts commit e409630856d7a7e741548016d7afe174ff1b40f7.

* Revert "Rename Node to follow naming convention."

This reverts commit 883b1f37c7207512d9f6605749977ad9045846a1.

* Revert "Introduce LoggedInAccountSwitcherNode, to improve animation when switching between accounts."

This reverts commit 9c698ff8152aceb5fd2b8b5ab5f609d28de64d24.

* Metro now have `@AssistedInject`.

* Update screenshots

* Introduce DelegateTransitionHandler and use it in RootFlowNode

---------

Co-authored-by: ElementBot <android@element.io>
Co-authored-by: ganfra <francoisg@element.io>
This commit is contained in:
Benoit Marty 2025-09-26 15:45:06 +02:00 committed by GitHub
parent a8c4d5d019
commit 1e546335df
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
117 changed files with 2161 additions and 281 deletions

View file

@ -7,8 +7,13 @@
package io.element.android.libraries.troubleshoot.impl.history
import io.element.android.libraries.matrix.api.core.EventId
import io.element.android.libraries.matrix.api.core.RoomId
import io.element.android.libraries.matrix.api.core.SessionId
sealed interface PushHistoryEvents {
data class SetShowOnlyErrors(val showOnlyErrors: Boolean) : PushHistoryEvents
data class Reset(val requiresConfirmation: Boolean) : PushHistoryEvents
data class NavigateTo(val sessionId: SessionId, val roomId: RoomId, val eventId: EventId) : PushHistoryEvents
data object ClearDialog : PushHistoryEvents
}

View file

@ -20,7 +20,6 @@ import io.element.android.annotations.ContributesNode
import io.element.android.libraries.di.SessionScope
import io.element.android.libraries.matrix.api.core.EventId
import io.element.android.libraries.matrix.api.core.RoomId
import io.element.android.libraries.matrix.api.core.SessionId
import io.element.android.libraries.troubleshoot.api.PushHistoryEntryPoint
import io.element.android.services.analytics.api.ScreenTracker
@ -29,21 +28,23 @@ import io.element.android.services.analytics.api.ScreenTracker
class PushHistoryNode(
@Assisted buildContext: BuildContext,
@Assisted plugins: List<Plugin>,
private val presenter: PushHistoryPresenter,
presenterFactory: PushHistoryPresenter.Factory,
private val screenTracker: ScreenTracker,
) : Node(buildContext, plugins = plugins) {
) : Node(buildContext, plugins = plugins), PushHistoryNavigator {
private fun onDone() {
plugins<PushHistoryEntryPoint.Callback>().forEach {
it.onDone()
}
}
private fun onItemClick(sessionId: SessionId, roomId: RoomId, eventId: EventId) {
override fun navigateTo(roomId: RoomId, eventId: EventId) {
plugins<PushHistoryEntryPoint.Callback>().forEach {
it.onItemClick(sessionId, roomId, eventId)
it.navigateTo(roomId, eventId)
}
}
private val presenter = presenterFactory.create(this)
@Composable
override fun View(modifier: Modifier) {
screenTracker.TrackScreen(MobileScreen.ScreenName.NotificationTroubleshoot)
@ -51,7 +52,6 @@ class PushHistoryNode(
PushHistoryView(
state = state,
onBackClick = ::onDone,
onItemClick = ::onItemClick,
modifier = modifier,
)
}

View file

@ -14,18 +14,36 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import dev.zacsweers.metro.Inject
import dev.zacsweers.metro.Assisted
import dev.zacsweers.metro.AssistedFactory
import dev.zacsweers.metro.AssistedInject
import io.element.android.libraries.architecture.AsyncAction
import io.element.android.libraries.architecture.Presenter
import io.element.android.libraries.matrix.api.MatrixClient
import io.element.android.libraries.matrix.api.core.EventId
import io.element.android.libraries.matrix.api.core.RoomId
import io.element.android.libraries.push.api.PushService
import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
@Inject
fun interface PushHistoryNavigator {
fun navigateTo(roomId: RoomId, eventId: EventId)
}
@AssistedInject
class PushHistoryPresenter(
@Assisted private val pushHistoryNavigator: PushHistoryNavigator,
private val pushService: PushService,
matrixClient: MatrixClient,
) : Presenter<PushHistoryState> {
@AssistedFactory
fun interface Factory {
fun create(pushHistoryNavigator: PushHistoryNavigator): PushHistoryPresenter
}
private val sessionId = matrixClient.sessionId
@Composable
override fun present(): PushHistoryState {
val coroutineScope = rememberCoroutineScope()
@ -41,6 +59,7 @@ class PushHistoryPresenter(
}
}.collectAsState(emptyList())
var resetAction: AsyncAction<Unit> by remember { mutableStateOf(AsyncAction.Uninitialized) }
var showNotSameAccountError by remember { mutableStateOf(false) }
fun handleEvents(event: PushHistoryEvents) {
when (event) {
@ -60,6 +79,14 @@ class PushHistoryPresenter(
}
PushHistoryEvents.ClearDialog -> {
resetAction = AsyncAction.Uninitialized
showNotSameAccountError = false
}
is PushHistoryEvents.NavigateTo -> {
if (event.sessionId != sessionId) {
showNotSameAccountError = true
} else {
pushHistoryNavigator.navigateTo(event.roomId, event.eventId)
}
}
}
}
@ -69,6 +96,7 @@ class PushHistoryPresenter(
pushHistoryItems = pushHistory.toImmutableList(),
showOnlyErrors = showOnlyErrors,
resetAction = resetAction,
showNotSameAccountError = showNotSameAccountError,
eventSink = ::handleEvents
)
}

View file

@ -16,5 +16,6 @@ data class PushHistoryState(
val pushHistoryItems: ImmutableList<PushHistoryItem>,
val showOnlyErrors: Boolean,
val resetAction: AsyncAction<Unit>,
val showNotSameAccountError: Boolean,
val eventSink: (PushHistoryEvents) -> Unit,
)

View file

@ -40,6 +40,9 @@ open class PushHistoryStateProvider : PreviewParameterProvider<PushHistoryState>
aPushHistoryState(
resetAction = AsyncAction.ConfirmingNoParams,
),
aPushHistoryState(
showNotSameAccountError = true,
),
)
}
@ -48,12 +51,14 @@ fun aPushHistoryState(
pushHistoryItems: List<PushHistoryItem> = emptyList(),
showOnlyErrors: Boolean = false,
resetAction: AsyncAction<Unit> = AsyncAction.Uninitialized,
showNotSameAccountError: Boolean = false,
eventSink: (PushHistoryEvents) -> Unit = {},
) = PushHistoryState(
pushCounter = pushCounter,
pushHistoryItems = pushHistoryItems.toImmutableList(),
showOnlyErrors = showOnlyErrors,
resetAction = resetAction,
showNotSameAccountError = showNotSameAccountError,
eventSink = eventSink,
)

View file

@ -37,6 +37,7 @@ import io.element.android.compound.tokens.generated.CompoundIcons
import io.element.android.libraries.designsystem.components.async.AsyncActionView
import io.element.android.libraries.designsystem.components.button.BackButton
import io.element.android.libraries.designsystem.components.dialogs.ConfirmationDialog
import io.element.android.libraries.designsystem.components.dialogs.ErrorDialog
import io.element.android.libraries.designsystem.components.list.ListItemContent
import io.element.android.libraries.designsystem.preview.ElementPreview
import io.element.android.libraries.designsystem.preview.PreviewsDayNight
@ -48,9 +49,6 @@ import io.element.android.libraries.designsystem.theme.components.ListItem
import io.element.android.libraries.designsystem.theme.components.Scaffold
import io.element.android.libraries.designsystem.theme.components.Text
import io.element.android.libraries.designsystem.theme.components.TopAppBar
import io.element.android.libraries.matrix.api.core.EventId
import io.element.android.libraries.matrix.api.core.RoomId
import io.element.android.libraries.matrix.api.core.SessionId
import io.element.android.libraries.push.api.history.PushHistoryItem
import io.element.android.libraries.troubleshoot.impl.R
import io.element.android.libraries.ui.strings.CommonStrings
@ -60,7 +58,6 @@ import io.element.android.libraries.ui.strings.CommonStrings
fun PushHistoryView(
state: PushHistoryState,
onBackClick: () -> Unit,
onItemClick: (SessionId, RoomId, EventId) -> Unit,
modifier: Modifier = Modifier,
) {
var showMenu by remember { mutableStateOf(false) }
@ -123,7 +120,6 @@ fun PushHistoryView(
.padding(padding)
.consumeWindowInsets(padding),
state = state,
onItemClick = onItemClick,
)
}
@ -142,12 +138,18 @@ fun PushHistoryView(
},
onErrorDismiss = {},
)
if (state.showNotSameAccountError) {
ErrorDialog(
content = "Please switch account first to navigate to the event.",
onSubmit = { state.eventSink(PushHistoryEvents.ClearDialog) }
)
}
}
@Composable
private fun PushHistoryContent(
state: PushHistoryState,
onItemClick: (SessionId, RoomId, EventId) -> Unit,
modifier: Modifier = Modifier,
) {
Column(
@ -173,7 +175,7 @@ private fun PushHistoryContent(
val roomId = pushHistory.roomId
val eventId = pushHistory.eventId
if (sessionId != null && roomId != null && eventId != null) {
onItemClick(sessionId, roomId, eventId)
state.eventSink(PushHistoryEvents.NavigateTo(sessionId, roomId, eventId))
}
}
)
@ -271,6 +273,5 @@ internal fun PushHistoryViewPreview(
PushHistoryView(
state = state,
onBackClick = {},
onItemClick = { _, _, _ -> },
)
}

View file

@ -12,7 +12,7 @@ import com.bumble.appyx.core.modality.BuildContext
import com.google.common.truth.Truth.assertThat
import io.element.android.libraries.matrix.api.core.EventId
import io.element.android.libraries.matrix.api.core.RoomId
import io.element.android.libraries.matrix.api.core.SessionId
import io.element.android.libraries.matrix.test.FakeMatrixClient
import io.element.android.libraries.push.test.FakePushService
import io.element.android.libraries.troubleshoot.api.PushHistoryEntryPoint
import io.element.android.services.analytics.test.FakeScreenTracker
@ -32,15 +32,21 @@ class DefaultPushHistoryEntryPointTest {
PushHistoryNode(
buildContext = buildContext,
plugins = plugins,
presenter = PushHistoryPresenter(
pushService = FakePushService(),
),
presenterFactory = {
PushHistoryPresenter(
pushHistoryNavigator = object : PushHistoryNavigator {
override fun navigateTo(roomId: RoomId, eventId: EventId) = lambdaError()
},
pushService = FakePushService(),
matrixClient = FakeMatrixClient(),
)
},
screenTracker = FakeScreenTracker(),
)
}
val callback = object : PushHistoryEntryPoint.Callback {
override fun onDone() = lambdaError()
override fun onItemClick(sessionId: SessionId, roomId: RoomId, eventId: EventId) = lambdaError()
override fun navigateTo(roomId: RoomId, eventId: EventId) = lambdaError()
}
val result = entryPoint.nodeBuilder(parentNode, BuildContext.root(null))
.callback(callback)

View file

@ -11,9 +11,19 @@ package io.element.android.libraries.troubleshoot.impl.history
import com.google.common.truth.Truth.assertThat
import io.element.android.libraries.architecture.AsyncAction
import io.element.android.libraries.matrix.api.MatrixClient
import io.element.android.libraries.matrix.api.core.EventId
import io.element.android.libraries.matrix.api.core.RoomId
import io.element.android.libraries.matrix.test.AN_EVENT_ID
import io.element.android.libraries.matrix.test.A_ROOM_ID
import io.element.android.libraries.matrix.test.A_SESSION_ID
import io.element.android.libraries.matrix.test.A_SESSION_ID_2
import io.element.android.libraries.matrix.test.FakeMatrixClient
import io.element.android.libraries.push.api.PushService
import io.element.android.libraries.push.test.FakePushService
import io.element.android.tests.testutils.lambda.lambdaError
import io.element.android.tests.testutils.lambda.lambdaRecorder
import io.element.android.tests.testutils.lambda.value
import io.element.android.tests.testutils.test
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
@ -29,6 +39,7 @@ class PushHistoryPresenterTest {
assertThat(initialState.pushHistoryItems).isEmpty()
assertThat(initialState.showOnlyErrors).isFalse()
assertThat(initialState.resetAction).isEqualTo(AsyncAction.Uninitialized)
assertThat(initialState.showNotSameAccountError).isFalse()
}
}
@ -119,11 +130,57 @@ class PushHistoryPresenterTest {
}
}
@Test
fun `present - item click current account`() = runTest {
val pushHistoryNavigatorResult = lambdaRecorder<RoomId, EventId, Unit> { _, _ -> }
val presenter = createPushHistoryPresenter(
pushHistoryNavigator = { roomId, eventId ->
pushHistoryNavigatorResult(roomId, eventId)
}
)
presenter.test {
val initialState = awaitItem()
initialState.eventSink(
PushHistoryEvents.NavigateTo(
sessionId = A_SESSION_ID,
roomId = A_ROOM_ID,
eventId = AN_EVENT_ID,
)
)
pushHistoryNavigatorResult.assertions()
.isCalledOnce()
.with(value(A_ROOM_ID), value(AN_EVENT_ID))
}
}
@Test
fun `present - item click not current account`() = runTest {
val presenter = createPushHistoryPresenter()
presenter.test {
val initialState = awaitItem()
initialState.eventSink(
PushHistoryEvents.NavigateTo(
sessionId = A_SESSION_ID_2,
roomId = A_ROOM_ID,
eventId = AN_EVENT_ID,
)
)
assertThat(awaitItem().showNotSameAccountError).isTrue()
// Reset error
initialState.eventSink(PushHistoryEvents.ClearDialog)
assertThat(awaitItem().showNotSameAccountError).isFalse()
}
}
private fun createPushHistoryPresenter(
pushHistoryNavigator: PushHistoryNavigator = PushHistoryNavigator { _, _ -> lambdaError() },
pushService: PushService = FakePushService(),
matrixClient: MatrixClient = FakeMatrixClient(),
): PushHistoryPresenter {
return PushHistoryPresenter(
pushHistoryNavigator = pushHistoryNavigator,
pushService = pushService,
matrixClient = matrixClient,
)
}
}

View file

@ -14,20 +14,14 @@ import androidx.compose.ui.test.onNodeWithContentDescription
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.test.ext.junit.runners.AndroidJUnit4
import io.element.android.libraries.matrix.api.core.EventId
import io.element.android.libraries.matrix.api.core.RoomId
import io.element.android.libraries.matrix.api.core.SessionId
import io.element.android.libraries.matrix.test.AN_EVENT_ID
import io.element.android.libraries.matrix.test.A_FORMATTED_DATE
import io.element.android.libraries.matrix.test.A_ROOM_ID
import io.element.android.libraries.matrix.test.A_SESSION_ID
import io.element.android.libraries.ui.strings.CommonStrings
import io.element.android.tests.testutils.EnsureNeverCalled
import io.element.android.tests.testutils.EnsureNeverCalledWithThreeParams
import io.element.android.tests.testutils.EventsRecorder
import io.element.android.tests.testutils.clickOn
import io.element.android.tests.testutils.lambda.lambdaRecorder
import io.element.android.tests.testutils.lambda.value
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TestRule
@ -103,9 +97,8 @@ class PushHistoryViewTest {
}
@Test
fun `clicking on a valid event invokes the expected callback`() {
val eventsRecorder = EventsRecorder<PushHistoryEvents>(expectEvents = false)
val onItemClick = lambdaRecorder<SessionId, RoomId, EventId, Unit> { _, _, _ -> }
fun `clicking on a valid event emits the expected Event`() {
val eventsRecorder = EventsRecorder<PushHistoryEvents>()
rule.setPushHistoryView(
aPushHistoryState(
pushHistoryItems = listOf(
@ -118,25 +111,26 @@ class PushHistoryViewTest {
),
eventSink = eventsRecorder,
),
onItemClick = onItemClick,
)
rule.onNodeWithText(A_FORMATTED_DATE).performClick()
onItemClick.assertions()
.isCalledOnce()
.with(value(A_SESSION_ID), value(A_ROOM_ID), value(AN_EVENT_ID))
eventsRecorder.assertSingle(
PushHistoryEvents.NavigateTo(
sessionId = A_SESSION_ID,
roomId = A_ROOM_ID,
eventId = AN_EVENT_ID,
)
)
}
}
private fun <R : TestRule> AndroidComposeTestRule<R, ComponentActivity>.setPushHistoryView(
state: PushHistoryState,
onBackClick: () -> Unit = EnsureNeverCalled(),
onItemClick: (SessionId, RoomId, EventId) -> Unit = EnsureNeverCalledWithThreeParams(),
) {
setContent {
PushHistoryView(
state = state,
onBackClick = onBackClick,
onItemClick = onItemClick,
)
}
}