Skip to content

Commit 2f10fa8

Browse files
committed
Multi accounts - handle permalink
1 parent ca21861 commit 2f10fa8

File tree

2 files changed

+60
-23
lines changed
  • appnav/src/main/kotlin/io/element/android/appnav

2 files changed

+60
-23
lines changed

appnav/src/main/kotlin/io/element/android/appnav/RootFlowNode.kt

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ class RootFlowNode(
193193
@Parcelize
194194
data class AccountSelect(
195195
val currentSessionId: SessionId,
196-
val intent: Intent,
196+
val intent: Intent?,
197+
val permalinkData: PermalinkData?,
197198
) : NavTarget
198199

199200
@Parcelize
@@ -299,8 +300,13 @@ class RootFlowNode(
299300
// Do not pop when the account is changed to avoid a UI flicker.
300301
backstack.pop()
301302
}
302-
attachSession(sessionId)
303-
.attachIncomingShare(navTarget.intent)
303+
attachSession(sessionId).apply {
304+
if (navTarget.intent != null) {
305+
attachIncomingShare(navTarget.intent)
306+
} else if (navTarget.permalinkData != null) {
307+
attachPermalinkData(navTarget.permalinkData)
308+
}
309+
}
304310
}
305311
}
306312

@@ -375,6 +381,7 @@ class RootFlowNode(
375381
NavTarget.AccountSelect(
376382
currentSessionId = latestSessionId,
377383
intent = intent,
384+
permalinkData = null,
378385
)
379386
)
380387
} else {
@@ -386,25 +393,53 @@ class RootFlowNode(
386393

387394
private suspend fun navigateTo(permalinkData: PermalinkData) {
388395
Timber.d("Navigating to $permalinkData")
389-
attachSession(null)
390-
.apply {
391-
when (permalinkData) {
392-
is PermalinkData.FallbackLink -> Unit
393-
is PermalinkData.RoomEmailInviteLink -> Unit
394-
is PermalinkData.RoomLink -> {
395-
attachRoom(
396-
roomIdOrAlias = permalinkData.roomIdOrAlias,
397-
trigger = JoinedRoom.Trigger.MobilePermalink,
398-
serverNames = permalinkData.viaParameters,
399-
eventId = permalinkData.eventId,
400-
clearBackstack = true
396+
// Is there a session already?
397+
val latestSessionId = authenticationService.getLatestSessionId()
398+
if (latestSessionId == null) {
399+
// No session, open login
400+
switchToNotLoggedInFlow(null)
401+
} else {
402+
// wait for the current session to be restored
403+
val loggedInFlowNode = attachSession(latestSessionId)
404+
when (permalinkData) {
405+
is PermalinkData.FallbackLink -> Unit
406+
is PermalinkData.RoomEmailInviteLink -> Unit
407+
else -> {
408+
if (sessionStore.getAllSessions().size > 1) {
409+
// Several accounts, let the user choose which one to use
410+
backstack.push(
411+
NavTarget.AccountSelect(
412+
currentSessionId = latestSessionId,
413+
intent = null,
414+
permalinkData = permalinkData,
415+
)
401416
)
402-
}
403-
is PermalinkData.UserLink -> {
404-
attachUser(permalinkData.userId)
417+
} else {
418+
// Only one account, directly attach the room or the user node.
419+
loggedInFlowNode.attachPermalinkData(permalinkData)
405420
}
406421
}
407422
}
423+
}
424+
}
425+
426+
private suspend fun LoggedInFlowNode.attachPermalinkData(permalinkData: PermalinkData) {
427+
when (permalinkData) {
428+
is PermalinkData.FallbackLink -> Unit
429+
is PermalinkData.RoomEmailInviteLink -> Unit
430+
is PermalinkData.RoomLink -> {
431+
attachRoom(
432+
roomIdOrAlias = permalinkData.roomIdOrAlias,
433+
trigger = JoinedRoom.Trigger.MobilePermalink,
434+
serverNames = permalinkData.viaParameters,
435+
eventId = permalinkData.eventId,
436+
clearBackstack = true
437+
)
438+
}
439+
is PermalinkData.UserLink -> {
440+
attachUser(permalinkData.userId)
441+
}
442+
}
408443
}
409444

410445
private suspend fun navigateTo(deeplinkData: DeeplinkData) {
@@ -422,12 +457,11 @@ class RootFlowNode(
422457
oidcActionFlow.post(oidcAction)
423458
}
424459

425-
// [sessionId] will be null for permalink.
426-
private suspend fun attachSession(sessionId: SessionId?): LoggedInFlowNode {
460+
private suspend fun attachSession(sessionId: SessionId): LoggedInFlowNode {
427461
// Ensure that the session is the latest one
428-
sessionId?.let { sessionStore.setLatestSession(it.value) }
462+
sessionStore.setLatestSession(sessionId.value)
429463
return waitForChildAttached<LoggedInAppScopeFlowNode, NavTarget> { navTarget ->
430-
navTarget is NavTarget.LoggedInFlow && (sessionId == null || navTarget.sessionId == sessionId)
464+
navTarget is NavTarget.LoggedInFlow && navTarget.sessionId == sessionId
431465
}
432466
.attachSession()
433467
}

libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/permalink/PermalinkData.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,23 @@
88
package io.element.android.libraries.matrix.api.permalink
99

1010
import android.net.Uri
11+
import android.os.Parcelable
1112
import androidx.compose.runtime.Immutable
1213
import io.element.android.libraries.matrix.api.core.EventId
1314
import io.element.android.libraries.matrix.api.core.RoomId
1415
import io.element.android.libraries.matrix.api.core.RoomIdOrAlias
1516
import io.element.android.libraries.matrix.api.core.UserId
1617
import kotlinx.collections.immutable.ImmutableList
1718
import kotlinx.collections.immutable.persistentListOf
19+
import kotlinx.parcelize.Parcelize
1820

1921
/**
2022
* This sealed class represents all the permalink cases.
2123
* You don't have to instantiate yourself but should use [PermalinkParser] instead.
2224
*/
2325
@Immutable
24-
sealed interface PermalinkData {
26+
@Parcelize
27+
sealed interface PermalinkData : Parcelable {
2528
data class RoomLink(
2629
val roomIdOrAlias: RoomIdOrAlias,
2730
val eventId: EventId? = null,

0 commit comments

Comments
 (0)