From 49ca084e695e880445e8d415add19ef824bfe140 Mon Sep 17 00:00:00 2001 From: KhaledNjim Date: Wed, 4 Sep 2024 00:10:27 +0100 Subject: [PATCH] LA-1352 sync deleted recieved shares with local db --- domain/lib/domain.dart | 1 + .../get_all_received_shares_interactor.dart | 10 ++++++++- ...ed_received_share_from_local_database.dart | 21 +++++++++++++++++++ .../get_all_received_interactor_test.dart | 19 +++++++++++++---- lib/presentation/di/module/app_module.dart | 7 ++++++- .../received/received_share_viewmodel.dart | 3 +-- 6 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 domain/lib/src/usecases/received/remove_deleted_received_share_from_local_database.dart diff --git a/domain/lib/domain.dart b/domain/lib/domain.dart index cc57b3343..0b35ea86a 100644 --- a/domain/lib/domain.dart +++ b/domain/lib/domain.dart @@ -512,3 +512,4 @@ export 'src/usecases/workgroup/get_all_workgroups_interactor.dart'; export 'src/usecases/workgroup/get_all_workgroups_offline_interactor.dart'; export 'src/usecases/workgroup/workgroup_exception.dart'; export 'src/usecases/workgroup/workgroup_view_state.dart'; +export 'src/usecases/received/remove_deleted_received_share_from_local_database.dart'; \ No newline at end of file diff --git a/domain/lib/src/usecases/received/get_all_received_shares_interactor.dart b/domain/lib/src/usecases/received/get_all_received_shares_interactor.dart index 8d15e2d5d..49140f6ec 100644 --- a/domain/lib/src/usecases/received/get_all_received_shares_interactor.dart +++ b/domain/lib/src/usecases/received/get_all_received_shares_interactor.dart @@ -38,11 +38,16 @@ import 'package:domain/src/repository/received/received_share_repository.dart'; import 'package:domain/src/state/failure.dart'; import 'package:domain/src/state/success.dart'; import 'package:domain/src/usecases/received/received_share_view_state.dart'; +import 'package:collection/collection.dart'; +import 'package:domain/src/usecases/received/remove_deleted_received_share_from_local_database.dart'; class GetAllReceivedSharesInteractor { final ReceivedShareRepository _receivedShareRepository; + final RemoveDeletedReceivedShareFromLocalDatabaseInteractor + _removeDeletedReceivedShareFromLocalDatabase; - GetAllReceivedSharesInteractor(this._receivedShareRepository); + GetAllReceivedSharesInteractor(this._receivedShareRepository, + this._removeDeletedReceivedShareFromLocalDatabase); Future> execute(String recipient) async { try { @@ -51,6 +56,9 @@ class GetAllReceivedSharesInteractor { .getAllReceivedShareOfflineByRecipient(recipient)); final combinedReceivedShares = List.empty(growable: true); + _removeDeletedReceivedShareFromLocalDatabase.execute( + receivedShares, recipient); + if (receivedShares.isNotEmpty) { for (final received in receivedShares) { final localReceivedShare = await _receivedShareRepository diff --git a/domain/lib/src/usecases/received/remove_deleted_received_share_from_local_database.dart b/domain/lib/src/usecases/received/remove_deleted_received_share_from_local_database.dart new file mode 100644 index 000000000..ebef0caa3 --- /dev/null +++ b/domain/lib/src/usecases/received/remove_deleted_received_share_from_local_database.dart @@ -0,0 +1,21 @@ +import 'package:domain/domain.dart'; +import 'package:collection/collection.dart'; + +class RemoveDeletedReceivedShareFromLocalDatabaseInteractor { + final ReceivedShareRepository _receivedShareRepository; + + RemoveDeletedReceivedShareFromLocalDatabaseInteractor( + this._receivedShareRepository); + + Future execute( + List receivedShares, String recipient) async { + var localReceivedShares = await _receivedShareRepository + .getAllReceivedShareOfflineByRecipient(recipient); + final receivedShareIds = receivedShares.map((received) => received.shareId).toSet(); + for (final local in localReceivedShares) { + if (!receivedShareIds.contains(local.shareId)) { + await _receivedShareRepository.disableOffline(local.shareId, local.localPath ?? ''); + } + } + } +} diff --git a/domain/test/usecases/received/get_all_received_interactor_test.dart b/domain/test/usecases/received/get_all_received_interactor_test.dart index 4a09a514b..64825d407 100644 --- a/domain/test/usecases/received/get_all_received_interactor_test.dart +++ b/domain/test/usecases/received/get_all_received_interactor_test.dart @@ -33,7 +33,6 @@ import 'package:dartz/dartz.dart'; import 'package:domain/domain.dart'; -import 'package:domain/src/usecases/received/received_share_view_state.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mockito/annotations.dart'; import 'package:mockito/mockito.dart'; @@ -46,10 +45,18 @@ void main() { group('get_all_received_interactor', () { late MockReceivedShareRepository receivedShareRepository; late GetAllReceivedSharesInteractor getAllReceivedSharesInteractor; + late RemoveDeletedReceivedShareFromLocalDatabaseInteractor + removeDeletedReceivedShareFromLocalDatabaseInteractor; + setUp(() { receivedShareRepository = MockReceivedShareRepository(); - getAllReceivedSharesInteractor = GetAllReceivedSharesInteractor(receivedShareRepository); + removeDeletedReceivedShareFromLocalDatabaseInteractor = + RemoveDeletedReceivedShareFromLocalDatabaseInteractor( + receivedShareRepository); + getAllReceivedSharesInteractor = GetAllReceivedSharesInteractor( + receivedShareRepository, + removeDeletedReceivedShareFromLocalDatabaseInteractor); }); test('get all receives interactor should return success with receive list', () async { @@ -57,8 +64,11 @@ void main() { when(receivedShareRepository.getAllReceivedShareOffline()).thenAnswer((_) async => []); when(receivedShareRepository.getReceivedShareOffline(receivedShare1.shareId)).thenAnswer((_) async => null); when(receivedShareRepository.getReceivedShareOffline(receivedShare2.shareId)).thenAnswer((_) async => null); + when(receivedShareRepository + .getAllReceivedShareOfflineByRecipient(RECIPIENT_1.mail)) + .thenAnswer((_) async => [receivedShare1]); - final result = await getAllReceivedSharesInteractor.execute(); + final result = await getAllReceivedSharesInteractor.execute(RECIPIENT_1.mail); result.map((success) => (success as GetAllReceivedShareSuccess).receivedShares) .fold( @@ -71,7 +81,8 @@ void main() { final exception = Exception(); when(receivedShareRepository.getAllReceivedShares()).thenThrow(exception); - final result = await getAllReceivedSharesInteractor.execute(); + final result = + await getAllReceivedSharesInteractor.execute(RECIPIENT_1.mail); result.fold( (failure) => expect(failure, isA()), diff --git a/lib/presentation/di/module/app_module.dart b/lib/presentation/di/module/app_module.dart index c7e41ede8..35ee40bea 100644 --- a/lib/presentation/di/module/app_module.dart +++ b/lib/presentation/di/module/app_module.dart @@ -356,7 +356,12 @@ class AppModule { getIt.registerFactory(() => GetSharedSpacesRootNodeInfoInteractor(getIt())); getIt.registerFactory(() => DownloadMultipleFileIOSInteractor(getIt())); getIt.registerFactory(() => GetAuthorizedInteractor(getIt(), getIt())); - getIt.registerFactory(() => GetAllReceivedSharesInteractor(getIt())); + getIt.registerFactory(() => + RemoveDeletedReceivedShareFromLocalDatabaseInteractor( + getIt())); + getIt.registerFactory(() => GetAllReceivedSharesInteractor( + getIt(), + getIt())); getIt.registerFactory(() => CopyToMySpaceInteractor(getIt())); getIt.registerFactory(() => CopyMultipleFilesToMySpaceInteractor(getIt())); getIt.registerFactory(() => SearchDocumentInteractor()); diff --git a/lib/presentation/widget/received/received_share_viewmodel.dart b/lib/presentation/widget/received/received_share_viewmodel.dart index 95ef55937..8da38be06 100644 --- a/lib/presentation/widget/received/received_share_viewmodel.dart +++ b/lib/presentation/widget/received/received_share_viewmodel.dart @@ -619,8 +619,7 @@ class ReceivedShareViewModel extends BaseViewModel { OnlineThunkAction _makeAvailableOfflineAction(ReceivedShare receivedShare, int position) { return OnlineThunkAction((Store store) async { final recipient = GenericUser(store.state.account.user?.mail ?? ''); - await _makeReceivedShareOfflineInteractor - .execute(receivedShare, recipient) + await _makeReceivedShareOfflineInteractor.execute(receivedShare, recipient) .then((result) => result.fold( (failure) { _receivedSharesList[position] = receivedShare.toSyncOffline(syncOfflineState: SyncOfflineState.none);