From b451aa95c35bc0067fb68f7b81a391c6f49a6ae6 Mon Sep 17 00:00:00 2001 From: HuyNguyen Date: Wed, 3 Jan 2024 20:34:18 +0700 Subject: [PATCH] fixup! fixup! fixup! fixup! fixup! fixup! fixup! TW-1193: Store/get/delete active account in persistent storage --- lib/pages/chat_list/chat_list.dart | 18 +++++++----- .../multiple_accounts_picker.dart | 18 +++++++----- lib/widgets/matrix.dart | 29 ++++++++++++------- 3 files changed, 38 insertions(+), 27 deletions(-) diff --git a/lib/pages/chat_list/chat_list.dart b/lib/pages/chat_list/chat_list.dart index 92a0421d4f..bdc11beb7a 100644 --- a/lib/pages/chat_list/chat_list.dart +++ b/lib/pages/chat_list/chat_list.dart @@ -452,12 +452,14 @@ class ChatListController extends State void setActiveClient(Client client) { context.go('/rooms'); - activeFilter = AppConfig.separateChatTypes - ? ActiveFilter.messages - : ActiveFilter.allChats; - activeSpaceId = null; - conversationSelectionNotifier.value.clear(); - Matrix.of(context).setActiveClient(client); + setState(() { + activeFilter = AppConfig.separateChatTypes + ? ActiveFilter.messages + : ActiveFilter.allChats; + activeSpaceId = null; + conversationSelectionNotifier.value.clear(); + Matrix.of(context).setActiveClient(client); + }); _clientStream.add(client); } @@ -779,14 +781,14 @@ class ChatListController extends State scrollController.addListener(_onScroll); _waitForFirstSync(); _hackyWebRTCFixForWeb(); - _getCurrentProfile(activeClient); CallKeepManager().initialize(); WidgetsBinding.instance.addPostFrameCallback((_) async { if (mounted) { Matrix.of(context).backgroundPush?.setupPush(); + await matrixState.retrievePersistedActiveClient(); + _getCurrentProfile(activeClient); } }); - _checkTorBrowser(); super.initState(); } diff --git a/lib/pages/multiple_accounts/multiple_accounts_picker.dart b/lib/pages/multiple_accounts/multiple_accounts_picker.dart index 4ebe36e1c8..22660d081d 100644 --- a/lib/pages/multiple_accounts/multiple_accounts_picker.dart +++ b/lib/pages/multiple_accounts/multiple_accounts_picker.dart @@ -138,13 +138,15 @@ class MultipleAccountsPickerController { ); } - void _setActiveClient(Client newClient) { - context.go( - '/rooms', - extra: SwitchActiveAccountBodyArgs( - newActiveClient: newClient, - ), - ); - Matrix.of(context).setActiveClient(newClient); + void _setActiveClient(Client newClient) async { + final result = await _matrixState.setActiveClient(newClient); + if (result.isSuccess) { + context.go( + '/rooms', + extra: SwitchActiveAccountBodyArgs( + newActiveClient: newClient, + ), + ); + } } } diff --git a/lib/widgets/matrix.dart b/lib/widgets/matrix.dart index 0981ebc25c..44a2f465ae 100644 --- a/lib/widgets/matrix.dart +++ b/lib/widgets/matrix.dart @@ -49,6 +49,13 @@ import '../utils/background_push.dart'; import '../utils/famedlysdk_store.dart'; import 'local_notifications_extension.dart'; +enum SetActiveClientResult { + success, + unknownClient; + + bool get isSuccess => this == SetActiveClientResult.success; +} + class Matrix extends StatefulWidget { final Widget? child; @@ -120,16 +127,18 @@ class MatrixState extends State late String currentClientSecret; RequestTokenResponse? currentThreepidCreds; - void setActiveClient(Client? newClient) { - _checkHomeserverExists(newClient); + Future setActiveClient(Client? newClient) async { final index = widget.clients.indexWhere((client) => client == newClient); if (index != -1) { _activeClient = index; // TODO: Multi-client VoiP support createVoipPlugin(); - _storePersistActiveAccount(newClient!); + _setTwakeSupport(newClient); + await _storePersistActiveAccount(newClient!); + return SetActiveClientResult.success; } else { Logs().w('Tried to set an unknown client ${newClient!.userID} as active'); + return SetActiveClientResult.unknownClient; } } @@ -188,7 +197,7 @@ class MatrixState extends State .stream .where((l) => l == LoginState.loggedIn) .first - .then((_) { + .then((_) async { Logs().d( 'MatrixState::getLoginClient() Login successful - Client ${_loginClientCandidate!.clientName}', ); @@ -302,7 +311,6 @@ class MatrixState extends State void initState() { super.initState(); WidgetsBinding.instance.addPostFrameCallback((_) async { - await _retrievePersistedActiveClient(); WidgetsBinding.instance.addObserver(this); initMatrix(); initReceiveSharingIntent(); @@ -387,8 +395,8 @@ class MatrixState extends State TwakeApp.routerKey.currentContext!, L10n.of(context)!.oneClientLoggedOut, ); - setActiveClient(widget.clients.first); - if (state != LoginState.loggedIn) { + final result = await setActiveClient(widget.clients.first); + if (state != LoginState.loggedIn && result.isSuccess) { TwakeApp.router.go( '/rooms', extra: LogoutBodyArgs( @@ -666,7 +674,7 @@ class MatrixState extends State } } - void _checkHomeserverExists(Client? client) async { + void _setTwakeSupport(Client? client) async { Logs().d( 'Matrix::_checkHomeserverExists: Old twakeSupported - $twakeSupported', ); @@ -690,7 +698,7 @@ class MatrixState extends State ); } - Future _retrievePersistedActiveClient() async { + Future retrievePersistedActiveClient() async { try { final multipleAccountRepository = getIt.get(); final persistActiveAccount = @@ -701,8 +709,7 @@ class MatrixState extends State } else { final newActiveClient = getClientByUserId(persistActiveAccount); if (newActiveClient != null) { - _activeClient = - widget.clients.indexWhere((client) => client == newActiveClient); + setActiveClient(newActiveClient); } } } catch (e) {