diff --git a/lib/data/datasource/tom_configurations_datasource.dart b/lib/data/datasource/tom_configurations_datasource.dart index a39ae18400..6cfac3820c 100644 --- a/lib/data/datasource/tom_configurations_datasource.dart +++ b/lib/data/datasource/tom_configurations_datasource.dart @@ -7,4 +7,6 @@ abstract class ToMConfigurationsDatasource { String userId, ToMConfigurations toMConfigurations, ); + + Future deleteTomConfigurations(String userId); } diff --git a/lib/data/datasource_impl/tom_configurations_datasource_impl.dart b/lib/data/datasource_impl/tom_configurations_datasource_impl.dart index b6914654ed..2a51dc247a 100644 --- a/lib/data/datasource_impl/tom_configurations_datasource_impl.dart +++ b/lib/data/datasource_impl/tom_configurations_datasource_impl.dart @@ -46,4 +46,10 @@ class HiveToMConfigurationDatasource implements ToMConfigurationsDatasource { .toJson(), ); } + + @override + Future deleteTomConfigurations(String userId) { + final hiveCollectionToMDatabase = getIt.get(); + return hiveCollectionToMDatabase.tomConfigurationsBox.delete(userId); + } } diff --git a/lib/data/repository/tom_configurations_repository_impl.dart b/lib/data/repository/tom_configurations_repository_impl.dart index 4a8bcdc115..9ddecd06f0 100644 --- a/lib/data/repository/tom_configurations_repository_impl.dart +++ b/lib/data/repository/tom_configurations_repository_impl.dart @@ -22,4 +22,9 @@ class ToMConfigurationsRepositoryImpl implements ToMConfigurationsRepository { toMConfigurations, ); } + + @override + Future deleteTomConfigurations(String userId) { + return tomConfigurationsDatasource.deleteTomConfigurations(userId); + } } diff --git a/lib/domain/repository/tom_configurations_repository.dart b/lib/domain/repository/tom_configurations_repository.dart index 673f1ee6ce..de71cb74bc 100644 --- a/lib/domain/repository/tom_configurations_repository.dart +++ b/lib/domain/repository/tom_configurations_repository.dart @@ -7,4 +7,6 @@ abstract class ToMConfigurationsRepository { String userId, ToMConfigurations toMConfigurations, ); + + Future deleteTomConfigurations(String userId); } diff --git a/lib/pages/settings_dashboard/settings/settings.dart b/lib/pages/settings_dashboard/settings/settings.dart index 4a56c4edfd..bd33e7b0aa 100644 --- a/lib/pages/settings_dashboard/settings/settings.dart +++ b/lib/pages/settings_dashboard/settings/settings.dart @@ -1,8 +1,8 @@ import 'dart:async'; import 'package:fluffychat/config/app_config.dart'; -import 'package:fluffychat/data/hive/hive_collection_tom_database.dart'; import 'package:fluffychat/di/global/get_it_initializer.dart'; +import 'package:fluffychat/domain/repository/tom_configurations_repository.dart'; import 'package:fluffychat/event/twake_inapp_event_types.dart'; import 'package:fluffychat/pages/bootstrap/bootstrap_dialog.dart'; import 'package:fluffychat/presentation/mixins/connect_page_mixin.dart'; @@ -39,6 +39,8 @@ class SettingsController extends State with ConnectPageMixin { final ValueNotifier avatarUriNotifier = ValueNotifier(Uri()); final ValueNotifier displayNameNotifier = ValueNotifier(''); + final tomConfigurationRepository = getIt.get(); + StreamSubscription? onAccountDataSubscription; final List getListSettingItem = [ @@ -86,13 +88,19 @@ class SettingsController extends State with ConnectPageMixin { OkCancelResult.cancel) { return; } - final matrix = Matrix.of(context); if (PlatformInfos.isMobile) { - await tryLogoutSso(context); + await _logoutActionsOnMobile(); + } else { + await _logoutActions(); } - if (matrix.twakeSupported == true) { - final hiveCollectionToMDatabase = getIt.get(); - await hiveCollectionToMDatabase.clear(); + } + + Future _logoutActionsOnMobile() async { + try { + await tryLogoutSso(context); + } catch (e) { + Logs().e('SettingsController()::_logoutActionsOnMobile - error: $e'); + return; } await TwakeDialog.showFutureLoadingDialogFullScreen( future: () async { @@ -100,12 +108,36 @@ class SettingsController extends State with ConnectPageMixin { if (matrix.backgroundPush != null) { await matrix.backgroundPush!.removeCurrentPusher(); } - await matrix.client.logout(); + await Future.wait([ + matrix.client.logout(), + _deleteTomConfigurations(matrix.client), + ]); + } catch (e) { + Logs().e('SettingsController()::_logoutActionsOnMobile - error: $e'); + } + }, + ); + } + + Future _logoutActions() async { + await TwakeDialog.showFutureLoadingDialogFullScreen( + future: () async { + try { + if (matrix.backgroundPush != null) { + await matrix.backgroundPush!.removeCurrentPusher(); + } + await Future.wait([ + matrix.client.logout(), + _deleteTomConfigurations(matrix.client), + ]); } catch (e) { - Logs().e('SettingsController()::logoutAction - error: $e'); + Logs().e('SettingsController()::_logoutActions - error: $e'); } finally { - if (PlatformInfos.isWeb) { + try { await tryLogoutSso(context); + } catch (e) { + Logs().e('SettingsController()::_logoutActions - error: $e'); + return; } } }, @@ -244,6 +276,25 @@ class SettingsController extends State with ConnectPageMixin { }); } + Future _deleteTomConfigurations(Client currentClient) async { + try { + Logs().d( + 'SettingsController::_deleteTomConfigurations - Client ID: ${currentClient.userID}', + ); + if (matrix.twakeSupported) { + await tomConfigurationRepository + .deleteTomConfigurations(currentClient.userID!); + } + Logs().d( + 'SettingsController::_deleteTomConfigurations - Success', + ); + } catch (e) { + Logs().e( + 'SettingsController::_deleteTomConfigurations - error: $e', + ); + } + } + @override void initState() { _getCurrentProfile(client); diff --git a/lib/presentation/mixins/connect_page_mixin.dart b/lib/presentation/mixins/connect_page_mixin.dart index b16f423b65..67114b1849 100644 --- a/lib/presentation/mixins/connect_page_mixin.dart +++ b/lib/presentation/mixins/connect_page_mixin.dart @@ -177,6 +177,7 @@ mixin ConnectPageMixin { Logs().d('tryLogoutSso::result: $result'); } catch (e) { Logs().d('tryLogoutSso::error: $e'); + rethrow; } } diff --git a/lib/widgets/matrix.dart b/lib/widgets/matrix.dart index 887bcee2cf..2904728166 100644 --- a/lib/widgets/matrix.dart +++ b/lib/widgets/matrix.dart @@ -81,7 +81,7 @@ class MatrixState extends State String? activeBundle; Store store = Store(); HomeserverSummary? loginHomeserverSummary; - String? authUrl; + String? _authUrl; XFile? loginAvatar; String? loginUsername; LoginType? loginType; @@ -96,6 +96,8 @@ class MatrixState extends State BackgroundPush? backgroundPush; + String? get authUrl => _authUrl; + Client get client { if (widget.clients.isEmpty) { widget.clients.add(getLoginClient()); @@ -125,7 +127,7 @@ class MatrixState extends State _activeClient = index; // TODO: Multi-client VoiP support createVoipPlugin(); - _setUpToMServicesWhenChangingActiveClient(newClient); + await _setUpToMServicesWhenChangingActiveClient(newClient); await _storePersistActiveAccount(newClient!); return SetActiveClientState.success; } else { @@ -375,13 +377,8 @@ class MatrixState extends State Logs().v('[MATRIX]:_listenLoginStateChanged:: First Log in successful'); _handleFirstLoggedIn(client); } else { - Logs().v('[MATRIX]:_listenLoginStateChanged:: Log out successful'); - if (PlatformInfos.isMobile) { - _deletePersistActiveAccount(state); - TwakeApp.router.go('/home/twakeWelcome'); - } else { - TwakeApp.router.go('/home', extra: true); - } + Logs().v('[MATRIX]:_listenLoginStateChanged:: Last Log out successful'); + await _handleLastLogout(); } } } @@ -392,7 +389,7 @@ class MatrixState extends State ) async { await _cancelSubs(currentClient.clientName); widget.clients.remove(currentClient); - ClientManager.removeClientNameFromStore(currentClient.clientName); + await ClientManager.removeClientNameFromStore(currentClient.clientName); TwakeSnackBar.show( TwakeApp.routerKey.currentContext!, L10n.of(context)!.oneClientLoggedOut, @@ -411,9 +408,9 @@ class MatrixState extends State } } - void _handleFirstLoggedIn(Client newActiveClient) { - setUpToMServicesInLogin(newActiveClient); - _storePersistActiveAccount(newActiveClient); + Future _handleFirstLoggedIn(Client newActiveClient) async { + await setUpToMServicesInLogin(newActiveClient); + await _storePersistActiveAccount(newActiveClient); TwakeApp.router.go( '/rooms', extra: LoggedInBodyArgs( @@ -439,7 +436,7 @@ class MatrixState extends State _loginClientCandidate!.clientName, ); if (activeClient == null) return; - setUpToMServicesInLogin(activeClient); + await setUpToMServicesInLogin(activeClient); final result = await setActiveClient(activeClient); if (result.isSuccess) { TwakeApp.router.go( @@ -452,7 +449,7 @@ class MatrixState extends State } } - void _deletePersistActiveAccount(LoginState state) async { + Future _deletePersistActiveAccount() async { try { final multipleAccountRepository = getIt.get(); await multipleAccountRepository.deletePersistActiveAccount(); @@ -566,7 +563,7 @@ class MatrixState extends State toMConfigurations.tomServerInformation, toMConfigurations.identityServerInformation, ); - authUrl = toMConfigurations.authUrl; + _setupAuthUrl(url: toMConfigurations.authUrl); loginType = toMConfigurations.loginType; } catch (e) { Logs().e('MatrixState::_retrieveToMConfiguration: $e'); @@ -588,7 +585,7 @@ class MatrixState extends State } } - void setUpToMServicesInLogin(Client client) { + Future setUpToMServicesInLogin(Client client) async { final tomServer = loginHomeserverSummary?.tomServer; Logs().d('MatrixState::setUpToMServicesInLogin: $tomServer'); if (tomServer != null) { @@ -598,9 +595,7 @@ class MatrixState extends State loginHomeserverSummary?.discoveryInformation?.mIdentityServer; final homeServer = loginHomeserverSummary?.discoveryInformation?.mHomeserver; - final newAuthUrl = loginHomeserverSummary?.discoveryInformation - ?.additionalProperties["m.authentication"]?["issuer"]; - authUrl = newAuthUrl is String ? newAuthUrl : null; + _setupAuthUrl(); if (identityServer != null) { _setUpIdentityServer(identityServer); } @@ -608,7 +603,7 @@ class MatrixState extends State _setUpHomeServer(homeServer.baseUrl); } if (tomServer != null) { - _storeToMConfiguration( + await _storeToMConfiguration( client, ToMConfigurations( tomServerInformation: tomServer, @@ -657,10 +652,10 @@ class MatrixState extends State .changeBaseUrl(identityServer.baseUrl.toString()); } - void _storeToMConfiguration( + Future _storeToMConfiguration( Client client, ToMConfigurations config, - ) { + ) async { try { Logs().e( 'Matrix::_storeToMConfiguration: clientName - ${client.clientName}', @@ -671,7 +666,7 @@ class MatrixState extends State if (client.userID == null) return; final ToMConfigurationsRepository configurationRepository = getIt.get(); - configurationRepository.saveTomConfigurations( + await configurationRepository.saveTomConfigurations( client.userID!, config, ); @@ -683,7 +678,7 @@ class MatrixState extends State } } - void _setUpToMServicesWhenChangingActiveClient(Client? client) async { + Future _setUpToMServicesWhenChangingActiveClient(Client? client) async { Logs().d( 'Matrix::_checkHomeserverExists: Old twakeSupported - $twakeSupported', ); @@ -697,6 +692,7 @@ class MatrixState extends State _setUpToMServer(null); } else { _setUpToMServer(toMConfigurations.tomServerInformation); + _setupAuthUrl(url: toMConfigurations.authUrl); } } catch (e) { _setUpToMServer(null); @@ -713,12 +709,12 @@ class MatrixState extends State final persistActiveAccount = await multipleAccountRepository.getPersistActiveAccount(); if (persistActiveAccount == null) { - _storePersistActiveAccount(client); + await _storePersistActiveAccount(client); return; } else { final newActiveClient = getClientByUserId(persistActiveAccount); if (newActiveClient != null) { - setActiveClient(newActiveClient); + await setActiveClient(newActiveClient); } } } catch (e) { @@ -731,10 +727,10 @@ class MatrixState extends State Future _storePersistActiveAccount(Client newClient) async { if (newClient.userID == null) return; try { - Logs().e( + Logs().d( 'Matrix::_storePersistActiveAccount: clientName - ${newClient.clientName}', ); - Logs().e( + Logs().d( 'Matrix::_storePersistActiveAccount: userId - ${newClient.userID}', ); final MultipleAccountRepository multipleAccountRepository = @@ -757,6 +753,42 @@ class MatrixState extends State didChangeAppLifecycleState(AppLifecycleState.paused); } + Future _setupAuthUrl({ + String? url, + }) async { + if (url != null) { + Logs().e( + 'Matrix::_setupAuthUrl: newAuthUrl - $url', + ); + _authUrl = url; + } else { + final newAuthUrl = loginHomeserverSummary?.discoveryInformation + ?.additionalProperties["m.authentication"]?["issuer"]; + Logs().e( + 'Matrix::_setupAuthUrl: newAuthUrl - $newAuthUrl', + ); + _authUrl = newAuthUrl is String ? newAuthUrl : url; + } + } + + Future _deleteAllTomConfigurations() async { + final hiveCollectionToMDatabase = getIt.get(); + await hiveCollectionToMDatabase.clear(); + Logs().d( + 'MatrixState::_deleteAllTomConfigurations: Delete ToM database success', + ); + } + + Future _handleLastLogout() async { + if (PlatformInfos.isMobile) { + await _deletePersistActiveAccount(); + TwakeApp.router.go('/home/twakeWelcome'); + } else { + TwakeApp.router.go('/home', extra: true); + } + await _deleteAllTomConfigurations(); + } + @override void didChangeAppLifecycleState(AppLifecycleState state) { Logs().i('didChangeAppLifecycleState: AppLifecycleState = $state');