@@ -193,7 +193,8 @@ class RootFlowNode(
193
193
@Parcelize
194
194
data class AccountSelect (
195
195
val currentSessionId : SessionId ,
196
- val intent : Intent ,
196
+ val intent : Intent ? ,
197
+ val permalinkData : PermalinkData ? ,
197
198
) : NavTarget
198
199
199
200
@Parcelize
@@ -299,8 +300,13 @@ class RootFlowNode(
299
300
// Do not pop when the account is changed to avoid a UI flicker.
300
301
backstack.pop()
301
302
}
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
+ }
304
310
}
305
311
}
306
312
@@ -375,6 +381,7 @@ class RootFlowNode(
375
381
NavTarget .AccountSelect (
376
382
currentSessionId = latestSessionId,
377
383
intent = intent,
384
+ permalinkData = null ,
378
385
)
379
386
)
380
387
} else {
@@ -386,25 +393,53 @@ class RootFlowNode(
386
393
387
394
private suspend fun navigateTo (permalinkData : PermalinkData ) {
388
395
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
+ )
401
416
)
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)
405
420
}
406
421
}
407
422
}
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
+ }
408
443
}
409
444
410
445
private suspend fun navigateTo (deeplinkData : DeeplinkData ) {
@@ -422,12 +457,11 @@ class RootFlowNode(
422
457
oidcActionFlow.post(oidcAction)
423
458
}
424
459
425
- // [sessionId] will be null for permalink.
426
- private suspend fun attachSession (sessionId : SessionId ? ): LoggedInFlowNode {
460
+ private suspend fun attachSession (sessionId : SessionId ): LoggedInFlowNode {
427
461
// Ensure that the session is the latest one
428
- sessionId?. let { sessionStore.setLatestSession(it .value) }
462
+ sessionStore.setLatestSession(sessionId .value)
429
463
return waitForChildAttached<LoggedInAppScopeFlowNode , NavTarget > { navTarget ->
430
- navTarget is NavTarget .LoggedInFlow && (sessionId == null || navTarget.sessionId == sessionId)
464
+ navTarget is NavTarget .LoggedInFlow && navTarget.sessionId == sessionId
431
465
}
432
466
.attachSession()
433
467
}
0 commit comments