Skip to content

Commit

Permalink
TW-1193: Store/get/delete active account in persistent storage
Browse files Browse the repository at this point in the history
  • Loading branch information
nqhhdev committed Dec 29, 2023
1 parent 6435ad8 commit cec6cbb
Show file tree
Hide file tree
Showing 14 changed files with 332 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
abstract class MultipleAccountDatasource {
Future<void> storePersistActiveAccount(String userId);

Future<String?> getPersistActiveAccount();

Future<void> deletePersistActiveAccount();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'package:fluffychat/data/datasource/multiple_account/multiple_account_datasource.dart';
import 'package:fluffychat/data/local/multiple_account/multiple_account_cache_manager.dart';
import 'package:fluffychat/di/global/get_it_initializer.dart';

class MultipleAccountDatasourceImpl implements MultipleAccountDatasource {
final MultipleAccountCacheManager _multipleAccountCacheManager =
getIt.get<MultipleAccountCacheManager>();

@override
Future<String?> getPersistActiveAccount() {
return _multipleAccountCacheManager.getPersistActiveAccount();
}

@override
Future<void> storePersistActiveAccount(String userId) {
return _multipleAccountCacheManager.storePersistActiveAccount(userId);
}

@override
Future<void> deletePersistActiveAccount() {
return _multipleAccountCacheManager.deletePersistActiveAccount();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:fluffychat/di/global/get_it_initializer.dart';
import 'package:fluffychat/utils/famedlysdk_store.dart';

class MultipleAccountCacheManager {
final pres = getIt.get<Store>();

static const String persistActiveAccountKey = 'persist_active_account_key';

Future<void> storePersistActiveAccount(String userId) async {
await pres.setItem(persistActiveAccountKey, userId);
}

Future<String?> getPersistActiveAccount() async {
return await pres.getItem(persistActiveAccountKey);
}

Future<void> deletePersistActiveAccount() async {
await pres.deleteItem(persistActiveAccountKey);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'package:fluffychat/data/datasource/multiple_account/multiple_account_datasource.dart';
import 'package:fluffychat/di/global/get_it_initializer.dart';
import 'package:fluffychat/domain/repository/multiple_account/multiple_account_repository.dart';

class MultipleAccountRepositoryImpl extends MultipleAccountRepository {
final MultipleAccountDatasource _multipleAccountDatasource =
getIt.get<MultipleAccountDatasource>();

@override
Future<String?> getPersistActiveAccount() {
return _multipleAccountDatasource.getPersistActiveAccount();
}

@override
Future<void> storePersistActiveAccount(String userId) {
return _multipleAccountDatasource.storePersistActiveAccount(userId);
}

@override
Future<void> deletePersistActiveAccount() {
return _multipleAccountDatasource.deletePersistActiveAccount();
}
}
23 changes: 23 additions & 0 deletions lib/di/global/get_it_initializer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:collection';
import 'package:fluffychat/data/datasource/localizations/localizations_datasource.dart';
import 'package:fluffychat/data/datasource/lookup_datasource.dart';
import 'package:fluffychat/data/datasource/media/media_data_source.dart';
import 'package:fluffychat/data/datasource/multiple_account/multiple_account_datasource.dart';
import 'package:fluffychat/data/datasource/phonebook_datasouce.dart';
import 'package:fluffychat/data/datasource/recovery_words_data_source.dart';
import 'package:fluffychat/data/datasource/server_search_datasource.dart';
Expand All @@ -13,10 +14,12 @@ import 'package:fluffychat/data/datasource_impl/contact/phonebook_contact_dataso
import 'package:fluffychat/data/datasource_impl/contact/tom_contacts_datasource_impl.dart';
import 'package:fluffychat/data/datasource_impl/localizations/localizations_datasource_impl.dart';
import 'package:fluffychat/data/datasource_impl/media/media_data_source_impl.dart';
import 'package:fluffychat/data/datasource_impl/multiple_account/multiple_account_datasource_impl.dart';
import 'package:fluffychat/data/datasource_impl/recovery_words_data_source_impl.dart';
import 'package:fluffychat/data/datasource_impl/server_search_datasource_impl.dart';
import 'package:fluffychat/data/datasource_impl/tom_configurations_datasource_impl.dart';
import 'package:fluffychat/data/local/localizations/language_cache_manager.dart';
import 'package:fluffychat/data/local/multiple_account/multiple_account_cache_manager.dart';
import 'package:fluffychat/data/network/contact/lookup_api.dart';
import 'package:fluffychat/data/network/contact/tom_contact_api.dart';
import 'package:fluffychat/data/network/dio_cache_option.dart';
Expand All @@ -28,6 +31,7 @@ import 'package:fluffychat/data/repository/contact/phonebook_contact_repository_
import 'package:fluffychat/data/repository/contact/tom_contact_repository_impl.dart';
import 'package:fluffychat/data/repository/localizations/localizations_repository_impl.dart';
import 'package:fluffychat/data/repository/media/media_repository_impl.dart';
import 'package:fluffychat/data/repository/multiple_account/multiple_account_repository_impl.dart';
import 'package:fluffychat/data/repository/recovery_words_repository_impl.dart';
import 'package:fluffychat/data/repository/server_search_repository_impl.dart';
import 'package:fluffychat/data/repository/tom_configurations_repository_impl.dart';
Expand All @@ -38,6 +42,7 @@ import 'package:fluffychat/domain/contact_manager/contacts_manager.dart';
import 'package:fluffychat/domain/repository/contact_repository.dart';
import 'package:fluffychat/domain/repository/localizations/localizations_repository.dart';
import 'package:fluffychat/domain/repository/lookup_repository.dart';
import 'package:fluffychat/domain/repository/multiple_account/multiple_account_repository.dart';
import 'package:fluffychat/domain/repository/phonebook_contact_repository.dart';
import 'package:fluffychat/domain/repository/recovery_words_repository.dart';
import 'package:fluffychat/domain/repository/server_search_repository.dart';
Expand All @@ -48,6 +53,9 @@ import 'package:fluffychat/domain/usecase/download_file_for_preview_interactor.d
import 'package:fluffychat/domain/usecase/forward/forward_message_interactor.dart';
import 'package:fluffychat/domain/usecase/contacts/get_tom_contacts_interactor.dart';
import 'package:fluffychat/domain/usecase/contacts/phonebook_contact_interactor.dart';
import 'package:fluffychat/domain/usecase/multiple_account/delete_persist_active_account_interactor.dart';
import 'package:fluffychat/domain/usecase/multiple_account/get_persist_active_account_interactor.dart';
import 'package:fluffychat/domain/usecase/multiple_account/store_persist_active_account_interactor.dart';
import 'package:fluffychat/domain/usecase/preview_url/get_preview_url_interactor.dart';
import 'package:fluffychat/domain/usecase/recovery/delete_recovery_words_interactor.dart';
import 'package:fluffychat/domain/usecase/recovery/get_recovery_words_interactor.dart';
Expand Down Expand Up @@ -150,6 +158,9 @@ class GetItInitializer {
getIt.registerFactory<ServerSearchDatasource>(
() => ServerSearchDatasourceImpl(),
);
getIt.registerFactory<MultipleAccountDatasource>(
() => MultipleAccountDatasourceImpl(),
);
}

void bindingDatasourceImpl() {
Expand Down Expand Up @@ -202,6 +213,9 @@ class GetItInitializer {
getIt.registerFactory<ServerSearchRepository>(
() => ServerSearchRepositoryImpl(),
);
getIt.registerFactory<MultipleAccountRepository>(
() => MultipleAccountRepositoryImpl(),
);
}

void bindingInteractor() {
Expand Down Expand Up @@ -284,5 +298,14 @@ class GetItInitializer {
getIt.registerFactory<SendFilesOnWebWithCaptionInteractor>(
() => SendFilesOnWebWithCaptionInteractor(),
);
getIt.registerFactory<GetPersistActiveAccountInteractor>(
() => GetPersistActiveAccountInteractor(),
);
getIt.registerFactory<StorePersistActiveAccountInteractor>(
() => StorePersistActiveAccountInteractor(),
);
getIt.registerFactory<DeletePersistActiveAccountInteractor>(
() => DeletePersistActiveAccountInteractor(),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:fluffychat/presentation/state/failure.dart';
import 'package:fluffychat/presentation/state/success.dart';

class DeletePersistActiveAccountSuccess extends UIState {
DeletePersistActiveAccountSuccess();

@override
List<Object?> get props => [];
}

class DeletePersistActiveAccountFailure extends FeatureFailure {
const DeletePersistActiveAccountFailure(dynamic exception)
: super(exception: exception);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:fluffychat/presentation/state/failure.dart';
import 'package:fluffychat/presentation/state/success.dart';

class GetPersistActiveAccountSuccess extends UIState {
final String userId;

GetPersistActiveAccountSuccess(this.userId);

@override
List<Object?> get props => [userId];
}

class GetPersistActiveAccountFailure extends FeatureFailure {
const GetPersistActiveAccountFailure(dynamic exception)
: super(exception: exception);
}

class PersistedActiveAccountIsEmpty extends FeatureFailure {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:fluffychat/presentation/state/failure.dart';
import 'package:fluffychat/presentation/state/success.dart';

class StorePersistActiveAccountSuccess extends UIState {
StorePersistActiveAccountSuccess();

@override
List<Object?> get props => [];
}

class StorePersistActiveAccountFailure extends FeatureFailure {
const StorePersistActiveAccountFailure(dynamic exception)
: super(exception: exception);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
abstract class MultipleAccountRepository {
Future<void> storePersistActiveAccount(String userId);

Future<String?> getPersistActiveAccount();

Future<void> deletePersistActiveAccount();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:dartz/dartz.dart';
import 'package:fluffychat/app_state/failure.dart';
import 'package:fluffychat/app_state/success.dart';
import 'package:fluffychat/di/global/get_it_initializer.dart';
import 'package:fluffychat/domain/app_state/multiple_account/delete_persist_active_account_state.dart';
import 'package:fluffychat/domain/repository/multiple_account/multiple_account_repository.dart';
import 'package:matrix/matrix.dart';

class DeletePersistActiveAccountInteractor {
final MultipleAccountRepository _multipleAccountRepository =
getIt.get<MultipleAccountRepository>();

Future<Either<Failure, Success>> execute() async {
try {
await _multipleAccountRepository.deletePersistActiveAccount();
return Right(DeletePersistActiveAccountSuccess());
} catch (e) {
Logs().e('DeletePersistActiveAccountInteractor::execute(): Error $e');
return Left(DeletePersistActiveAccountFailure(e));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'package:dartz/dartz.dart';
import 'package:fluffychat/app_state/failure.dart';
import 'package:fluffychat/app_state/success.dart';
import 'package:fluffychat/di/global/get_it_initializer.dart';
import 'package:fluffychat/domain/app_state/multiple_account/get_persist_active_account_state.dart';
import 'package:fluffychat/domain/repository/multiple_account/multiple_account_repository.dart';
import 'package:matrix/matrix.dart';

class GetPersistActiveAccountInteractor {
final MultipleAccountRepository _multipleAccountRepository =
getIt.get<MultipleAccountRepository>();

Future<Either<Failure, Success>> execute() async {
try {
final persistActiveAccount =
await _multipleAccountRepository.getPersistActiveAccount();
if (persistActiveAccount == null) {
return Left(PersistedActiveAccountIsEmpty());
} else {
return Right(GetPersistActiveAccountSuccess(persistActiveAccount));
}
} catch (e) {
Logs().e('GetPersistActiveAccountInteractor::execute(): Error $e');
return Left(GetPersistActiveAccountFailure(e));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'package:dartz/dartz.dart';
import 'package:fluffychat/app_state/failure.dart';
import 'package:fluffychat/app_state/success.dart';
import 'package:fluffychat/di/global/get_it_initializer.dart';
import 'package:fluffychat/domain/app_state/multiple_account/store_persist_active_account_state.dart';
import 'package:fluffychat/domain/repository/multiple_account/multiple_account_repository.dart';
import 'package:matrix/matrix.dart';

class StorePersistActiveAccountInteractor {
final MultipleAccountRepository _multipleAccountRepository =
getIt.get<MultipleAccountRepository>();

Future<Either<Failure, Success>> execute(String userId) async {
try {
await _multipleAccountRepository.storePersistActiveAccount(userId);
return Right(StorePersistActiveAccountSuccess());
} catch (e) {
Logs().e('GetPersistActiveAccountInteractor::execute(): Error $e');
return Left(StorePersistActiveAccountFailure(e));
}
}
}
Loading

0 comments on commit cec6cbb

Please sign in to comment.