Skip to content

Commit 6e08ed0

Browse files
committed
TW-2313 Handle login from URI when app is logged with same home server and multiple clients exist
1 parent fb6cdd1 commit 6e08ed0

File tree

3 files changed

+134
-38
lines changed

3 files changed

+134
-38
lines changed

lib/presentation/mixins/connect_page_mixin.dart

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'dart:convert';
22

3+
import 'package:collection/collection.dart';
34
import 'package:fluffychat/config/app_config.dart';
45
import 'package:fluffychat/di/global/get_it_initializer.dart';
56
import 'package:fluffychat/domain/repository/federation_configurations_repository.dart';
@@ -301,32 +302,29 @@ mixin ConnectPageMixin {
301302
);
302303
}
303304

304-
Future<bool> validateHomeServerExisted({required String homeServer}) async {
305+
Future<Client?> getClientExisted({required String homeServer}) async {
305306
try {
306307
final clients = await ClientManager.getClients();
307308
Logs().d(
308-
'ConnectPageMixin::validateHomeServerExisted:Clients = ${clients.map((client) => client.homeserver).toString()}',
309+
'ConnectPageMixin::getClientExisted:Clients = ${clients.map((client) => client.homeserver).toString()}',
309310
);
310-
311-
final loggedInHomeServers = clients
312-
.map((client) => client.homeserver?.toString())
313-
.whereType<String>()
314-
.toSet();
315-
316-
Logs().d(
317-
'ConnectPageMixin::validateHomeServerExisted: All HomeServers: $loggedInHomeServers',
311+
final clientExisted = clients.firstWhereOrNull(
312+
(client) => client.homeserver?.toString().contains(homeServer) == true,
318313
);
319-
320-
final exists = loggedInHomeServers.any(
321-
(existingServer) => existingServer.contains(homeServer),
314+
Logs().d(
315+
'ConnectPageMixin::getClientExisted: $clientExisted',
322316
);
323-
324-
return exists && !AppConfig.supportMultipleAccountsInTheSameHomeserver;
317+
if (clientExisted != null &&
318+
!AppConfig.supportMultipleAccountsInTheSameHomeserver) {
319+
return clientExisted;
320+
} else {
321+
return null;
322+
}
325323
} catch (e) {
326324
Logs().e(
327-
'ConnectPageMixin::validateHomeServerExisted: Exception: $e',
325+
'ConnectPageMixin::getClientExisted: Exception: $e',
328326
);
329-
return false;
327+
return null;
330328
}
331329
}
332330

lib/presentation/mixins/deep_link_intent_mixin.dart

Lines changed: 118 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'package:fluffychat/utils/deep_link/deep_link_utils.dart';
88
import 'package:fluffychat/utils/dialog/twake_dialog.dart';
99
import 'package:fluffychat/utils/platform_infos.dart';
1010
import 'package:fluffychat/utils/responsive/responsive_utils.dart';
11+
import 'package:fluffychat/widgets/layouts/agruments/logout_body_args.dart';
1112
import 'package:fluffychat/widgets/matrix.dart';
1213
import 'package:fluffychat/widgets/twake_app.dart';
1314
import 'package:flutter/material.dart';
@@ -58,13 +59,16 @@ mixin DeepLinkIntentMixin<T extends StatefulWidget> on State<T> {
5859
try {
5960
TwakeDialog.showLoadingTwakeWelcomeDialog(context);
6061

61-
final homeServerExisted = await matrixState.validateHomeServerExisted(
62+
final clientExisted = await matrixState.getClientExisted(
6263
homeServer: openAppDeepLink.homeServer,
6364
);
64-
Logs().d("DeepLinkIntentMixin::homeServerExisted: $homeServerExisted");
65-
if (homeServerExisted) {
65+
Logs().d(
66+
"DeepLinkIntentMixin::clientExisted: UserID =${clientExisted?.userID}",
67+
);
68+
if (clientExisted != null) {
6669
await _handleSignInWithSameHomeServer(
6770
openAppDeepLink: openAppDeepLink,
71+
clientExisted: clientExisted,
6872
);
6973
return;
7074
}
@@ -114,32 +118,113 @@ mixin DeepLinkIntentMixin<T extends StatefulWidget> on State<T> {
114118

115119
Future<void> _handleSignInWithSameHomeServer({
116120
required OpenAppDeepLink openAppDeepLink,
121+
required Client clientExisted,
117122
}) async {
118-
final isSameAccount =
119-
matrixState.activatedUserId == openAppDeepLink.qualifiedUserId;
123+
if (clientExisted.userID == null) {
124+
TwakeDialog.hideLoadingDialog(context);
125+
return;
126+
}
127+
128+
final isSameClient =
129+
clientExisted.userID == openAppDeepLink.qualifiedUserId;
130+
final isActiveClient = matrixState.activatedUserId == clientExisted.userID;
120131
Logs().d(
121-
'DeepLinkIntentMixin::_handleSignInWithSameHomeServer():activatedUserId = ${matrixState.activatedUserId}, userId = ${openAppDeepLink.qualifiedUserId}, loginToken = ${openAppDeepLink.loginToken}, isSameAccount = $isSameAccount',
132+
'DeepLinkIntentMixin::_handleSignInWithSameHomeServer(): isSameClient = $isSameClient, isActiveClient = $isActiveClient',
122133
);
123-
if (!isSameAccount) {
124-
final confirmResult = await _showConfirmSwitchAccountDialog(
125-
activeUserId: matrixState.activatedUserId,
126-
userId: openAppDeepLink.qualifiedUserId,
134+
if (isSameClient && isActiveClient) {
135+
TwakeDialog.hideLoadingDialog(context);
136+
} else if (isSameClient && !isActiveClient) {
137+
await _handleSignInWithSameInactiveClient(
138+
openAppDeepLink: openAppDeepLink,
139+
clientExisted: clientExisted,
140+
);
141+
} else if (!isSameClient && isActiveClient) {
142+
await _handleSignInWithDifferentActiveClient(
143+
openAppDeepLink: openAppDeepLink,
144+
userIdExisted: clientExisted.userID!,
127145
);
146+
} else {
147+
await _handleSignInWithDifferentInactiveClient(
148+
openAppDeepLink: openAppDeepLink,
149+
clientExisted: clientExisted,
150+
);
151+
}
152+
}
128153

129-
if (confirmResult == ConfirmResult.cancel) {
130-
TwakeDialog.hideLoadingDialog(context);
131-
return;
132-
}
154+
Future<void> _switchActiveClient({required Client clientExisted}) async {
155+
final activeClientState = await matrixState.setActiveClient(clientExisted);
156+
if (activeClientState.isSuccess) {
157+
TwakeApp.router.go(
158+
'/rooms',
159+
extra: LogoutBodyArgs(newActiveClient: clientExisted),
160+
);
161+
}
162+
}
133163

134-
processingDeepLink = openAppDeepLink;
135-
await _autoLogoutActiveAccount();
136-
} else {
164+
Future<void> _handleSignInWithSameInactiveClient({
165+
required OpenAppDeepLink openAppDeepLink,
166+
required Client clientExisted,
167+
}) async {
168+
Logs().d(
169+
'DeepLinkIntentMixin::_handleSignInWithSameInactiveClient():',
170+
);
171+
final confirmResult = await _showConfirmSwitchAccountDialog(
172+
userIdExisted: clientExisted.userID!,
173+
userId: openAppDeepLink.qualifiedUserId,
174+
);
175+
176+
if (confirmResult == ConfirmResult.cancel) {
137177
TwakeDialog.hideLoadingDialog(context);
178+
return;
138179
}
180+
181+
await _switchActiveClient(clientExisted: clientExisted);
182+
}
183+
184+
Future<void> _handleSignInWithDifferentActiveClient({
185+
required OpenAppDeepLink openAppDeepLink,
186+
required String userIdExisted,
187+
}) async {
188+
Logs().d(
189+
'DeepLinkIntentMixin::_handleSignInWithDifferentActiveClient(): userIdExisted = $userIdExisted',
190+
);
191+
final confirmResult = await _showConfirmSwitchAccountDialog(
192+
userIdExisted: userIdExisted,
193+
userId: openAppDeepLink.qualifiedUserId,
194+
);
195+
196+
if (confirmResult == ConfirmResult.cancel) {
197+
TwakeDialog.hideLoadingDialog(context);
198+
return;
199+
}
200+
201+
processingDeepLink = openAppDeepLink;
202+
await _autoLogoutActiveClient();
203+
}
204+
205+
Future<void> _handleSignInWithDifferentInactiveClient({
206+
required OpenAppDeepLink openAppDeepLink,
207+
required Client clientExisted,
208+
}) async {
209+
Logs().d(
210+
'DeepLinkIntentMixin::_handleSignInWithDifferentInactiveClient(): UserID = ${clientExisted.userID}',
211+
);
212+
final confirmResult = await _showConfirmSwitchAccountDialog(
213+
userIdExisted: clientExisted.userID!,
214+
userId: openAppDeepLink.qualifiedUserId,
215+
);
216+
217+
if (confirmResult == ConfirmResult.cancel) {
218+
TwakeDialog.hideLoadingDialog(context);
219+
return;
220+
}
221+
222+
processingDeepLink = openAppDeepLink;
223+
await _autoLogoutInactiveClient(client: clientExisted);
139224
}
140225

141226
Future<ConfirmResult> _showConfirmSwitchAccountDialog({
142-
required String activeUserId,
227+
required String userIdExisted,
143228
required String userId,
144229
}) async {
145230
final l10n = L10n.of(context);
@@ -152,7 +237,7 @@ mixin DeepLinkIntentMixin<T extends StatefulWidget> on State<T> {
152237
textSpanMessages: [
153238
TextSpan(text: l10n?.youAreCurrentlyLoggedInWith),
154239
TextSpan(
155-
text: ' $activeUserId',
240+
text: ' $userIdExisted',
156241
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
157242
color: Colors.black,
158243
fontWeight: FontWeight.bold,
@@ -175,10 +260,23 @@ mixin DeepLinkIntentMixin<T extends StatefulWidget> on State<T> {
175260
);
176261
}
177262

178-
Future<void> _autoLogoutActiveAccount() async {
263+
Future<void> _autoLogoutActiveClient() async {
264+
Logs().d('DeepLinkIntentMixin::_autoLogoutActiveClient');
179265
await matrixState.logoutAction(matrix: matrixState);
180266
}
181267

268+
Future<void> _autoLogoutInactiveClient({required Client client}) async {
269+
Logs().d('DeepLinkIntentMixin::_autoLogoutInactiveClient');
270+
final activeClientState = await matrixState.setActiveClient(client);
271+
272+
if (activeClientState.isSuccess) {
273+
await matrixState.logoutAction(matrix: matrixState);
274+
} else {
275+
processingDeepLink = null;
276+
TwakeDialog.hideLoadingDialog(context);
277+
}
278+
}
279+
182280
void disposeDeepLink() {
183281
Logs().d('DeepLinkIntentMixin::disposeDeepLink');
184282
intentUriStreamSubscription?.cancel();

lib/widgets/matrix.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ class MatrixState extends State<Matrix>
453453
Logs().v(
454454
'[MATRIX]:_handleLogoutWithMultipleAccount:: Log out Client ${client.clientName} successful',
455455
);
456-
if (state != LoginState.loggedIn && result.isSuccess) {
456+
if (result.isSuccess) {
457457
TwakeApp.router.go(
458458
'/rooms',
459459
extra: LogoutBodyArgs(

0 commit comments

Comments
 (0)