Skip to content

Commit

Permalink
Fix delete media issue
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-pratik-k committed Apr 23, 2024
1 parent 941d5cd commit eef4881
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
16 changes: 16 additions & 0 deletions app/lib/domain/extensions/media_list_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ extension MediaListExtension on List<AppMedia> {
}
}

void removeLocalRefFromMedias({List<String>? 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<AppProcess> process, List<String>? processIds}) {
processIds ??= process.map((e) => e.id).toList();
Expand Down
15 changes: 11 additions & 4 deletions app/lib/ui/flow/home/home_screen_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -271,13 +271,20 @@ class HomeViewStateNotifier extends StateNotifier<HomeViewState>

Future<void> 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);
}
Expand Down
9 changes: 9 additions & 0 deletions app/lib/ui/flow/home/home_view_model_helper_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ mixin HomeViewModelHelperMixin {
);
}

Map<DateTime, List<AppMedia>> removeLocalRefFromMediaMap(
{required Map<DateTime, List<AppMedia>> medias,
List<String>? removeFromIds}) {
return sortMedias(
medias: medias.values.expand((element) => element).toList()
..removeLocalRefFromMedias(removeFromIds: removeFromIds),
);
}

Map<DateTime, List<AppMedia>> addGoogleDriveRefInMediaMap({
required Map<DateTime, List<AppMedia>> medias,
required List<AppProcess> process,
Expand Down

0 comments on commit eef4881

Please sign in to comment.