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 11, 2024
1 parent 899591d commit f49b066
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 All @@ -146,6 +152,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 @@ -193,6 +200,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 @@ -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,
Expand Down Expand Up @@ -316,5 +324,10 @@ mixin class ContactsViewControllerMixin {
textEditingController.clear();
searchFocusNode.dispose();
textEditingController.dispose();
warningBannerNotifier.dispose();
isSearchModeNotifier.dispose();
presentationRecentContactNotifier.dispose();
presentationContactNotifier.dispose();
presentationPhonebookContactNotifier.dispose();
}
}
9 changes: 8 additions & 1 deletion lib/widgets/matrix.dart
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,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 @@ -419,6 +420,7 @@ class MatrixState extends State<Matrix>
Future<void> _handleFirstLoggedIn(Client newActiveClient) async {
await setUpToMServicesInLogin(newActiveClient);
await _storePersistActiveAccount(newActiveClient);
matrixState.reSyncContacts();
TwakeApp.router.go(
'/rooms',
extra: LoggedInBodyArgs(
Expand Down Expand Up @@ -630,6 +632,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 @@ -695,8 +700,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 @@ -794,6 +800,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 f49b066

Please sign in to comment.