From eef488123aef1e9e3cc9a9791ec116fea74fb166 Mon Sep 17 00:00:00 2001 From: Pratik-canopas Date: Tue, 23 Apr 2024 11:28:34 +0530 Subject: [PATCH] Fix delete media issue --- .../domain/extensions/media_list_extension.dart | 16 ++++++++++++++++ app/lib/ui/flow/home/home_screen_view_model.dart | 15 +++++++++++---- .../flow/home/home_view_model_helper_mixin.dart | 9 +++++++++ 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/app/lib/domain/extensions/media_list_extension.dart b/app/lib/domain/extensions/media_list_extension.dart index 49900ce..5124183 100644 --- a/app/lib/domain/extensions/media_list_extension.dart +++ b/app/lib/domain/extensions/media_list_extension.dart @@ -21,6 +21,22 @@ extension MediaListExtension on List { } } + void removeLocalRefFromMedias({List? removeFromIds}) { + for (int index = 0; index < length; index++) { + if (this[index].isLocalStored && + (removeFromIds?.contains(this[index].id) ?? true)) { + removeAt(index); + } else if (this[index].isCommonStored && + (removeFromIds?.contains(this[index].id) ?? true)) { + this[index] = this[index].copyWith( + id: this[index].driveMediaRefId ?? this[index].id, + sources: this[index].sources.toList() + ..remove(AppMediaSource.local), + ); + } + } + } + void addGoogleDriveRefInMedias( {required List process, List? processIds}) { processIds ??= process.map((e) => e.id).toList(); diff --git a/app/lib/ui/flow/home/home_screen_view_model.dart b/app/lib/ui/flow/home/home_screen_view_model.dart index 9ba17aa..709a384 100644 --- a/app/lib/ui/flow/home/home_screen_view_model.dart +++ b/app/lib/ui/flow/home/home_screen_view_model.dart @@ -271,13 +271,20 @@ class HomeViewStateNotifier extends StateNotifier Future deleteMediasFromLocal() async { try { - final medias = state.selectedMedias + final ids = state.selectedMedias .where((element) => element.sources.contains(AppMediaSource.local)) .map((e) => e.id) .toList(); - await _localMediaService.deleteMedias(medias); - state = state.copyWith(selectedMedias: []); - await loadLocalMedia(); + + _uploadedMedia.removeWhere((element) => ids.contains(element.id)); + + await _localMediaService.deleteMedias(ids); + state = state.copyWith( + selectedMedias: [], + medias: removeLocalRefFromMediaMap( + medias: state.medias, + removeFromIds: ids, + )); } catch (e) { state = state.copyWith(error: e); } diff --git a/app/lib/ui/flow/home/home_view_model_helper_mixin.dart b/app/lib/ui/flow/home/home_view_model_helper_mixin.dart index 5aa73c7..4d0482e 100644 --- a/app/lib/ui/flow/home/home_view_model_helper_mixin.dart +++ b/app/lib/ui/flow/home/home_view_model_helper_mixin.dart @@ -53,6 +53,15 @@ mixin HomeViewModelHelperMixin { ); } + Map> removeLocalRefFromMediaMap( + {required Map> medias, + List? removeFromIds}) { + return sortMedias( + medias: medias.values.expand((element) => element).toList() + ..removeLocalRefFromMedias(removeFromIds: removeFromIds), + ); + } + Map> addGoogleDriveRefInMediaMap({ required Map> medias, required List process,