@@ -8,6 +8,7 @@ import 'package:fluffychat/utils/deep_link/deep_link_utils.dart';
88import  'package:fluffychat/utils/dialog/twake_dialog.dart' ;
99import  'package:fluffychat/utils/platform_infos.dart' ;
1010import  'package:fluffychat/utils/responsive/responsive_utils.dart' ;
11+ import  'package:fluffychat/widgets/layouts/agruments/logout_body_args.dart' ;
1112import  'package:fluffychat/widgets/matrix.dart' ;
1213import  'package:fluffychat/widgets/twake_app.dart' ;
1314import  '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 ();
0 commit comments