diff --git a/lib/domain/contact_manager/contacts_manager.dart b/lib/domain/contact_manager/contacts_manager.dart index 72a631a1fb..afeb26b603 100644 --- a/lib/domain/contact_manager/contacts_manager.dart +++ b/lib/domain/contact_manager/contacts_manager.dart @@ -6,7 +6,7 @@ import 'package:fluffychat/domain/app_state/contact/get_contacts_state.dart'; import 'package:fluffychat/domain/app_state/contact/get_phonebook_contacts_state.dart'; import 'package:fluffychat/domain/usecase/contacts/get_tom_contacts_interactor.dart'; import 'package:fluffychat/domain/usecase/contacts/phonebook_contact_interactor.dart'; -import 'package:flutter/foundation.dart'; +import 'package:fluffychat/presentation/extensions/value_notifier_custom.dart'; class ContactsManager { static const int _lookupChunkSize = 50; @@ -17,22 +17,23 @@ class ContactsManager { bool _doNotShowWarningContactsBannerAgain = false; - final ValueNotifier> _contactsNotifier = - ValueNotifier(const Right(ContactsInitial())); + final ValueNotifierCustom> _contactsNotifier = + ValueNotifierCustom(const Right(ContactsInitial())); - final ValueNotifier> _phonebookContactsNotifier = - ValueNotifier(const Right(GetPhonebookContactsInitial())); + final ValueNotifierCustom> + _phonebookContactsNotifier = + ValueNotifierCustom(const Right(GetPhonebookContactsInitial())); ContactsManager({ required this.getTomContactsInteractor, required this.phonebookContactInteractor, }); - ValueNotifier> getContactsNotifier() => + ValueNotifierCustom> getContactsNotifier() => _contactsNotifier; - ValueNotifier> getPhonebookContactsNotifier() => - _phonebookContactsNotifier; + ValueNotifierCustom> + getPhonebookContactsNotifier() => _phonebookContactsNotifier; bool get _isSynchronizedTomContacts => _contactsNotifier.value.getSuccessOrNull() != null; diff --git a/lib/presentation/extensions/value_notifier_custom.dart b/lib/presentation/extensions/value_notifier_custom.dart new file mode 100644 index 0000000000..835b41c681 --- /dev/null +++ b/lib/presentation/extensions/value_notifier_custom.dart @@ -0,0 +1,15 @@ +import 'package:flutter/foundation.dart'; + +class ValueNotifierCustom extends ValueNotifier { + bool _isDisposed = false; + + ValueNotifierCustom(super.value); + + bool get isDisposed => _isDisposed; + + @override + void dispose() { + _isDisposed = true; + super.dispose(); + } +} diff --git a/lib/presentation/mixins/contacts_view_controller_mixin.dart b/lib/presentation/mixins/contacts_view_controller_mixin.dart index 4a872b494b..5640523d5e 100644 --- a/lib/presentation/mixins/contacts_view_controller_mixin.dart +++ b/lib/presentation/mixins/contacts_view_controller_mixin.dart @@ -12,6 +12,7 @@ import 'package:fluffychat/domain/model/extensions/contact/contacts_extension.da import 'package:fluffychat/domain/usecase/search/search_recent_chat_interactor.dart'; import 'package:fluffychat/presentation/enum/contacts/warning_contacts_banner_enum.dart'; import 'package:fluffychat/presentation/extensions/contact/presentation_contact_extension.dart'; +import 'package:fluffychat/presentation/extensions/value_notifier_custom.dart'; import 'package:fluffychat/presentation/model/contact/get_presentation_contacts_empty.dart'; import 'package:fluffychat/presentation/model/contact/get_presentation_contacts_failure.dart'; import 'package:fluffychat/presentation/model/contact/get_presentation_contacts_success.dart'; @@ -45,14 +46,15 @@ mixin class ContactsViewControllerMixin { final ValueNotifier isSearchModeNotifier = ValueNotifier(false); final presentationRecentContactNotifier = - ValueNotifier>([]); + ValueNotifierCustom>([]); - final presentationContactNotifier = ValueNotifier>( + final presentationContactNotifier = + ValueNotifierCustom>( const Right(ContactsInitial()), ); final presentationPhonebookContactNotifier = - ValueNotifier>( + ValueNotifierCustom>( const Right(GetPhonebookContactsInitial()), ); @@ -123,6 +125,10 @@ mixin class ContactsViewControllerMixin { }) { final keyword = _debouncer.value; if (keyword.isValidMatrixId && keyword.startsWith("@")) { + if (presentationContactNotifier.isDisposed && + presentationPhonebookContactNotifier.isDisposed) { + return; + } presentationContactNotifier.value = Right( PresentationExternalContactSuccess( contact: PresentationContact( @@ -146,6 +152,7 @@ mixin class ContactsViewControllerMixin { } Future _refreshContacts(String keyword) async { + if (presentationContactNotifier.isDisposed) return; presentationContactNotifier.value = contactsManager.getContactsNotifier().value.fold( (failure) { @@ -193,6 +200,7 @@ mixin class ContactsViewControllerMixin { } Future _refreshPhoneBookContacts(String keyword) async { + if (presentationPhonebookContactNotifier.isDisposed) return; presentationPhonebookContactNotifier.value = contactsManager.getPhonebookContactsNotifier().value.fold( (failure) { @@ -259,7 +267,7 @@ mixin class ContactsViewControllerMixin { .contacts .where((contact) => contact.directChatMatrixID != null) .toList(); - + if (presentationRecentContactNotifier.isDisposed) return; presentationRecentContactNotifier.value = recent .take( keyword == null ? _defaultLimitRecentContacts : recent.length, @@ -316,5 +324,10 @@ mixin class ContactsViewControllerMixin { textEditingController.clear(); searchFocusNode.dispose(); textEditingController.dispose(); + warningBannerNotifier.dispose(); + isSearchModeNotifier.dispose(); + presentationRecentContactNotifier.dispose(); + presentationContactNotifier.dispose(); + presentationPhonebookContactNotifier.dispose(); } } diff --git a/lib/widgets/matrix.dart b/lib/widgets/matrix.dart index b8ae3758a1..6d7debfe11 100644 --- a/lib/widgets/matrix.dart +++ b/lib/widgets/matrix.dart @@ -398,6 +398,7 @@ class MatrixState extends State await _cancelSubs(currentClient.clientName); widget.clients.remove(currentClient); await ClientManager.removeClientNameFromStore(currentClient.clientName); + matrixState.reSyncContacts(); TwakeSnackBar.show( TwakeApp.routerKey.currentContext!, L10n.of(context)!.oneClientLoggedOut, @@ -419,6 +420,7 @@ class MatrixState extends State Future _handleFirstLoggedIn(Client newActiveClient) async { await setUpToMServicesInLogin(newActiveClient); await _storePersistActiveAccount(newActiveClient); + matrixState.reSyncContacts(); TwakeApp.router.go( '/rooms', extra: LoggedInBodyArgs( @@ -630,6 +632,9 @@ class MatrixState extends State void setUpAuthorization(Client client) { final authorizationInterceptor = getIt.get(); + Logs().d( + 'MatrixState::setUpAuthorization: accessToken ${client.accessToken}', + ); authorizationInterceptor.accessToken = client.accessToken; } @@ -695,8 +700,9 @@ class MatrixState extends State 'Matrix::_checkHomeserverExists: Old twakeSupported - $twakeSupported', ); if (client == null && client?.userID == null) return; + setUpAuthorization(client!); try { - final toMConfigurations = await getTomConfigurations(client!.userID!); + final toMConfigurations = await getTomConfigurations(client.userID!); Logs().d( 'Matrix::_checkHomeserverExists: toMConfigurations - $toMConfigurations', ); @@ -794,6 +800,7 @@ class MatrixState extends State } Future _handleLastLogout() async { + matrixState.reSyncContacts(); if (PlatformInfos.isMobile) { await _deletePersistActiveAccount(); TwakeApp.router.go('/home/twakeWelcome');