Skip to content

Commit

Permalink
LA-1352 sync deleted recieved shares with local db
Browse files Browse the repository at this point in the history
  • Loading branch information
KhaledNjim authored and hoangdat committed Nov 15, 2024
1 parent 45aba5b commit 49ca084
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 8 deletions.
1 change: 1 addition & 0 deletions domain/lib/domain.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Original file line number Diff line number Diff line change
Expand Up @@ -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<Either<Failure, Success>> execute(String recipient) async {
try {
Expand All @@ -51,6 +56,9 @@ class GetAllReceivedSharesInteractor {
.getAllReceivedShareOfflineByRecipient(recipient));
final combinedReceivedShares = List<ReceivedShare>.empty(growable: true);

_removeDeletedReceivedShareFromLocalDatabase.execute(
receivedShares, recipient);

if (receivedShares.isNotEmpty) {
for (final received in receivedShares) {
final localReceivedShare = await _receivedShareRepository
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:domain/domain.dart';
import 'package:collection/collection.dart';

class RemoveDeletedReceivedShareFromLocalDatabaseInteractor {
final ReceivedShareRepository _receivedShareRepository;

RemoveDeletedReceivedShareFromLocalDatabaseInteractor(
this._receivedShareRepository);

Future<void> execute(
List<ReceivedShare> 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 ?? '');
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -46,19 +45,30 @@ 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 {
when(receivedShareRepository.getAllReceivedShares()).thenAnswer((_) async => [receivedShare1, receivedShare2]);
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(
Expand All @@ -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<GetAllReceivedShareFailure>()),
Expand Down
7 changes: 6 additions & 1 deletion lib/presentation/di/module/app_module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,12 @@ class AppModule {
getIt.registerFactory(() => GetSharedSpacesRootNodeInfoInteractor(getIt<SharedSpaceDocumentRepository>()));
getIt.registerFactory(() => DownloadMultipleFileIOSInteractor(getIt<DownloadFileIOSInteractor>()));
getIt.registerFactory(() => GetAuthorizedInteractor(getIt<AuthenticationRepository>(), getIt<CredentialRepository>()));
getIt.registerFactory(() => GetAllReceivedSharesInteractor(getIt<ReceivedShareRepository>()));
getIt.registerFactory(() =>
RemoveDeletedReceivedShareFromLocalDatabaseInteractor(
getIt<ReceivedShareRepository>()));
getIt.registerFactory(() => GetAllReceivedSharesInteractor(
getIt<ReceivedShareRepository>(),
getIt<RemoveDeletedReceivedShareFromLocalDatabaseInteractor>()));
getIt.registerFactory(() => CopyToMySpaceInteractor(getIt<DocumentRepository>()));
getIt.registerFactory(() => CopyMultipleFilesToMySpaceInteractor(getIt<CopyToMySpaceInteractor>()));
getIt.registerFactory(() => SearchDocumentInteractor());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -619,8 +619,7 @@ class ReceivedShareViewModel extends BaseViewModel {
OnlineThunkAction _makeAvailableOfflineAction(ReceivedShare receivedShare, int position) {
return OnlineThunkAction((Store<AppState> 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);
Expand Down

0 comments on commit 49ca084

Please sign in to comment.