Skip to content

Commit

Permalink
TW-1791: Refresh contacts when login or logout
Browse files Browse the repository at this point in the history
  • Loading branch information
nqhhdev committed Jun 17, 2024
1 parent 7b2514f commit 52c6b0c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 13 deletions.
17 changes: 9 additions & 8 deletions lib/domain/contact_manager/contacts_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,22 +17,23 @@ class ContactsManager {

bool _doNotShowWarningContactsBannerAgain = false;

final ValueNotifier<Either<Failure, Success>> _contactsNotifier =
ValueNotifier(const Right(ContactsInitial()));
final ValueNotifierCustom<Either<Failure, Success>> _contactsNotifier =
ValueNotifierCustom(const Right(ContactsInitial()));

final ValueNotifier<Either<Failure, Success>> _phonebookContactsNotifier =
ValueNotifier(const Right(GetPhonebookContactsInitial()));
final ValueNotifierCustom<Either<Failure, Success>>
_phonebookContactsNotifier =
ValueNotifierCustom(const Right(GetPhonebookContactsInitial()));

ContactsManager({
required this.getTomContactsInteractor,
required this.phonebookContactInteractor,
});

ValueNotifier<Either<Failure, Success>> getContactsNotifier() =>
ValueNotifierCustom<Either<Failure, Success>> getContactsNotifier() =>
_contactsNotifier;

ValueNotifier<Either<Failure, Success>> getPhonebookContactsNotifier() =>
_phonebookContactsNotifier;
ValueNotifierCustom<Either<Failure, Success>>
getPhonebookContactsNotifier() => _phonebookContactsNotifier;

bool get _isSynchronizedTomContacts =>
_contactsNotifier.value.getSuccessOrNull<ContactsInitial>() != null;
Expand Down
15 changes: 15 additions & 0 deletions lib/presentation/extensions/value_notifier_custom.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:flutter/foundation.dart';

class ValueNotifierCustom<T> extends ValueNotifier<T> {
bool _isDisposed = false;

ValueNotifierCustom(super.value);

bool get isDisposed => _isDisposed;

@override
void dispose() {
_isDisposed = true;
super.dispose();
}
}
21 changes: 17 additions & 4 deletions lib/presentation/mixins/contacts_view_controller_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -45,14 +46,15 @@ mixin class ContactsViewControllerMixin {
final ValueNotifier<bool> isSearchModeNotifier = ValueNotifier(false);

final presentationRecentContactNotifier =
ValueNotifier<List<PresentationSearch>>([]);
ValueNotifierCustom<List<PresentationSearch>>([]);

final presentationContactNotifier = ValueNotifier<Either<Failure, Success>>(
final presentationContactNotifier =
ValueNotifierCustom<Either<Failure, Success>>(
const Right(ContactsInitial()),
);

final presentationPhonebookContactNotifier =
ValueNotifier<Either<Failure, Success>>(
ValueNotifierCustom<Either<Failure, Success>>(
const Right(GetPhonebookContactsInitial()),
);

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -151,6 +157,7 @@ mixin class ContactsViewControllerMixin {
}

Future<void> _refreshContacts(String keyword) async {
if (presentationContactNotifier.isDisposed) return;
presentationContactNotifier.value =
contactsManager.getContactsNotifier().value.fold(
(failure) {
Expand Down Expand Up @@ -198,6 +205,7 @@ mixin class ContactsViewControllerMixin {
}

Future<void> _refreshPhoneBookContacts(String keyword) async {
if (presentationPhonebookContactNotifier.isDisposed) return;
presentationPhonebookContactNotifier.value =
contactsManager.getPhonebookContactsNotifier().value.fold(
(failure) {
Expand Down Expand Up @@ -264,7 +272,7 @@ mixin class ContactsViewControllerMixin {
.contacts
.where((contact) => contact.directChatMatrixID != null)
.toList();

if (presentationRecentContactNotifier.isDisposed) return;
presentationRecentContactNotifier.value = recent
.take(
keyword == null ? _defaultLimitRecentContacts : recent.length,
Expand Down Expand Up @@ -321,6 +329,11 @@ mixin class ContactsViewControllerMixin {
textEditingController.clear();
searchFocusNode.dispose();
textEditingController.dispose();
warningBannerNotifier.dispose();
isSearchModeNotifier.dispose();
presentationRecentContactNotifier.dispose();
presentationContactNotifier.dispose();
presentationPhonebookContactNotifier.dispose();
}

@visibleForTesting
Expand Down
9 changes: 8 additions & 1 deletion lib/widgets/matrix.dart
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ class MatrixState extends State<Matrix>
await _cancelSubs(currentClient.clientName);
widget.clients.remove(currentClient);
await ClientManager.removeClientNameFromStore(currentClient.clientName);
matrixState.reSyncContacts();
TwakeSnackBar.show(
TwakeApp.routerKey.currentContext!,
L10n.of(context)!.oneClientLoggedOut,
Expand All @@ -426,6 +427,7 @@ class MatrixState extends State<Matrix>
waitForFirstSync = false;
await setUpToMServicesInLogin(newActiveClient);
await _storePersistActiveAccount(newActiveClient);
matrixState.reSyncContacts();
onClientLoginStateChanged.add(
ClientLoginStateEvent(
client: client,
Expand Down Expand Up @@ -641,6 +643,9 @@ class MatrixState extends State<Matrix>

void setUpAuthorization(Client client) {
final authorizationInterceptor = getIt.get<AuthorizationInterceptor>();
Logs().d(
'MatrixState::setUpAuthorization: accessToken ${client.accessToken}',
);
authorizationInterceptor.accessToken = client.accessToken;
}

Expand Down Expand Up @@ -706,8 +711,9 @@ class MatrixState extends State<Matrix>
'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',
);
Expand Down Expand Up @@ -805,6 +811,7 @@ class MatrixState extends State<Matrix>
}

Future<void> _handleLastLogout() async {
matrixState.reSyncContacts();
if (PlatformInfos.isMobile) {
await _deletePersistActiveAccount();
TwakeApp.router.go('/home/twakeWelcome');
Expand Down

0 comments on commit 52c6b0c

Please sign in to comment.