Skip to content

Commit

Permalink
TW-1791: Refresh contacts when switch another account or login new ac…
Browse files Browse the repository at this point in the history
…count
  • Loading branch information
nqhhdev committed Jun 6, 2024
1 parent 32901cc commit 3ad79a6
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
6 changes: 6 additions & 0 deletions lib/app_state/success.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:dartz/dartz.dart';
import 'package:equatable/equatable.dart';
import 'package:fluffychat/app_state/failure.dart';

abstract class Success extends Equatable {
const Success();
Expand All @@ -10,4 +11,9 @@ extension SuccessExtension on Either {
(failure) => fallbackValue,
(success) => success is T ? success : fallbackValue,
);

T? getFailureOrNull<T extends Failure>({T? fallbackValue}) => fold(
(failure) => failure is T ? failure : fallbackValue,
(success) => fallbackValue,
);
}
9 changes: 7 additions & 2 deletions lib/domain/contact_manager/contacts_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ContactsManager {
ValueNotifier<Either<Failure, Success>> getPhonebookContactsNotifier() =>
_phonebookContactsNotifier;

bool get _isInitial =>
bool get _isSynchronizedTomContacts =>
_contactsNotifier.value.getSuccessOrNull<ContactsInitial>() != null;

bool get isDoNotShowWarningContactsBannerAgain =>
Expand All @@ -44,10 +44,14 @@ class ContactsManager {
_doNotShowWarningContactsBannerAgain = value;
}

Future<void> reSyncContacts() async {
_contactsNotifier.value = const Right(ContactsInitial());
}

void initialSynchronizeContacts({
bool isAvailableSupportPhonebookContacts = false,
}) async {
if (!_isInitial) {
if (!_isSynchronizedTomContacts) {
return;
}
_getAllContacts(
Expand Down Expand Up @@ -76,6 +80,7 @@ class ContactsManager {
if (!isAvailableSupportPhonebookContacts) {
return;
}

phonebookContactInteractor
.execute(lookupChunkSize: _lookupChunkSize)
.listen(
Expand Down
1 change: 1 addition & 0 deletions lib/pages/multiple_accounts/multiple_accounts_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class MultipleAccountsPickerController {
Future<void> _setActiveClient(Client newClient) async {
final result = await _matrixState.setActiveClient(newClient);
if (result.isSuccess) {
_matrixState.reSyncContacts();
context.go(
'/rooms',
extra: SwitchActiveAccountBodyArgs(
Expand Down
3 changes: 0 additions & 3 deletions lib/presentation/mixins/contacts_view_controller_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,5 @@ mixin class ContactsViewControllerMixin {
textEditingController.clear();
searchFocusNode.dispose();
textEditingController.dispose();
presentationContactNotifier.dispose();
presentationPhonebookContactNotifier.dispose();
presentationRecentContactNotifier.dispose();
}
}
8 changes: 8 additions & 0 deletions lib/widgets/matrix.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';
import 'dart:io';
import 'package:fluffychat/domain/contact_manager/contacts_manager.dart';
import 'package:fluffychat/presentation/mixins/init_config_mixin.dart';
import 'package:universal_html/html.dart' as html hide File;

Expand Down Expand Up @@ -77,6 +78,8 @@ class MatrixState extends State<Matrix>
with WidgetsBindingObserver, ReceiveSharingIntentMixin, InitConfigMixin {
final tomConfigurationRepository = getIt.get<ToMConfigurationsRepository>();

final _contactsManager = getIt.get<ContactsManager>();

int _activeClient = -1;
String? activeBundle;
Store store = Store();
Expand Down Expand Up @@ -443,6 +446,7 @@ class MatrixState extends State<Matrix>
if (activeClient == null) return;
await setUpToMServicesInLogin(activeClient);
final result = await setActiveClient(activeClient);
matrixState.reSyncContacts();
if (result.isSuccess) {
TwakeApp.router.go(
'/rooms',
Expand Down Expand Up @@ -799,6 +803,10 @@ class MatrixState extends State<Matrix>
await _deleteAllTomConfigurations();
}

Future<void> reSyncContacts() async {
_contactsManager.reSyncContacts();
}

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
Logs().i('didChangeAppLifecycleState: AppLifecycleState = $state');
Expand Down

0 comments on commit 3ad79a6

Please sign in to comment.