Skip to content

Commit

Permalink
Use mixin for helper class
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-pratik-k committed Apr 1, 2024
1 parent 88e4e1f commit 97c07e3
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 1 deletion.
Empty file.
71 changes: 71 additions & 0 deletions app/lib/ui/flow/home/home_view_model_helper_mixin.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import 'package:cloud_gallery/domain/formatter/date_formatter.dart';
import 'package:collection/collection.dart';
import 'package:data/models/media/media.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

final homeViewModelHelperProvider = Provider<HomeViewModelHelper>((ref) {
return const HomeViewModelHelper();
});

class HomeViewModelHelper {

List<AppMedia> mergeCommonMedia({
required List<AppMedia> localMedias,
required List<AppMedia> googleDriveMedias,
}) {
// If one of the lists is empty, return the other list.
if (googleDriveMedias.isEmpty) return localMedias;
if (localMedias.isEmpty) return [];

// Convert the lists to mutable lists.
localMedias = localMedias.toList();
googleDriveMedias = googleDriveMedias.toList();

final mergedMedias = <AppMedia>[];

// Add common media to mergedMedias and remove them from the lists.
for (AppMedia localMedia in localMedias.toList()) {
googleDriveMedias
.toList()
.where((googleDriveMedia) => googleDriveMedia.path == localMedia.path)
.forEach((googleDriveMedia) {
localMedias.removeWhere((media) => media.id == localMedia.id);

mergedMedias.add(localMedia.copyWith(
sources: [AppMediaSource.local, AppMediaSource.googleDrive],
thumbnailLink: googleDriveMedia.thumbnailLink,
driveRefId: googleDriveMedia.id,
));
});
}

return [...mergedMedias, ...localMedias];
}

Map<DateTime, List<AppMedia>> sortMedias({required List<AppMedia> medias}) {
medias.sort((a, b) => (b.createdTime ?? DateTime.now())
.compareTo(a.createdTime ?? DateTime.now()));
return groupBy<AppMedia, DateTime>(
medias,
(AppMedia media) =>
media.createdTime?.dateOnly ?? DateTime.now().dateOnly,
);
}

List<AppMedia> removeGoogleDriveRefFromMedias(
Map<DateTime, List<AppMedia>> medias) {
final allMedias = medias.values.expand((element) => element).toList();
for (int index = 0; index < allMedias.length; index++) {
if (allMedias[index].sources.length > 1) {
allMedias[index] = allMedias[index].copyWith(
sources: allMedias[index].sources.toList()
..remove(AppMediaSource.googleDrive),
thumbnailLink: null,
);
} else if (allMedias.contains(AppMediaSource.googleDrive)) {
allMedias.removeAt(index);
}
}
return allMedias;
}
}
9 changes: 9 additions & 0 deletions data/lib/models/media/media.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ enum AppMediaSource {
class AppMedia with _$AppMedia {
const factory AppMedia({
required String id,
String? driveMediaRefId,
String? name,
required String path,
String? thumbnailLink,
Expand Down Expand Up @@ -204,4 +205,12 @@ extension AppMediaExtension on AppMedia {
quality: 70,
);
}

bool get isGoogleDriveStored =>
sources.contains(AppMediaSource.googleDrive) && sources.length == 1;

bool get isLocalStored =>
sources.contains(AppMediaSource.local) && sources.length == 1;

bool get isCommonStored => sources.length > 1;
}
24 changes: 23 additions & 1 deletion data/lib/models/media/media.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ AppMedia _$AppMediaFromJson(Map<String, dynamic> json) {
/// @nodoc
mixin _$AppMedia {
String get id => throw _privateConstructorUsedError;
String? get driveMediaRefId => throw _privateConstructorUsedError;
String? get name => throw _privateConstructorUsedError;
String get path => throw _privateConstructorUsedError;
String? get thumbnailLink => throw _privateConstructorUsedError;
Expand Down Expand Up @@ -50,6 +51,7 @@ abstract class $AppMediaCopyWith<$Res> {
@useResult
$Res call(
{String id,
String? driveMediaRefId,
String? name,
String path,
String? thumbnailLink,
Expand Down Expand Up @@ -81,6 +83,7 @@ class _$AppMediaCopyWithImpl<$Res, $Val extends AppMedia>
@override
$Res call({
Object? id = null,
Object? driveMediaRefId = freezed,
Object? name = freezed,
Object? path = null,
Object? thumbnailLink = freezed,
Expand All @@ -102,6 +105,10 @@ class _$AppMediaCopyWithImpl<$Res, $Val extends AppMedia>
? _value.id
: id // ignore: cast_nullable_to_non_nullable
as String,
driveMediaRefId: freezed == driveMediaRefId
? _value.driveMediaRefId
: driveMediaRefId // ignore: cast_nullable_to_non_nullable
as String?,
name: freezed == name
? _value.name
: name // ignore: cast_nullable_to_non_nullable
Expand Down Expand Up @@ -176,6 +183,7 @@ abstract class _$$AppMediaImplCopyWith<$Res>
@useResult
$Res call(
{String id,
String? driveMediaRefId,
String? name,
String path,
String? thumbnailLink,
Expand Down Expand Up @@ -205,6 +213,7 @@ class __$$AppMediaImplCopyWithImpl<$Res>
@override
$Res call({
Object? id = null,
Object? driveMediaRefId = freezed,
Object? name = freezed,
Object? path = null,
Object? thumbnailLink = freezed,
Expand All @@ -226,6 +235,10 @@ class __$$AppMediaImplCopyWithImpl<$Res>
? _value.id
: id // ignore: cast_nullable_to_non_nullable
as String,
driveMediaRefId: freezed == driveMediaRefId
? _value.driveMediaRefId
: driveMediaRefId // ignore: cast_nullable_to_non_nullable
as String?,
name: freezed == name
? _value.name
: name // ignore: cast_nullable_to_non_nullable
Expand Down Expand Up @@ -295,6 +308,7 @@ class __$$AppMediaImplCopyWithImpl<$Res>
class _$AppMediaImpl implements _AppMedia {
const _$AppMediaImpl(
{required this.id,
this.driveMediaRefId,
this.name,
required this.path,
this.thumbnailLink,
Expand All @@ -318,6 +332,8 @@ class _$AppMediaImpl implements _AppMedia {
@override
final String id;
@override
final String? driveMediaRefId;
@override
final String? name;
@override
final String path;
Expand Down Expand Up @@ -356,7 +372,7 @@ class _$AppMediaImpl implements _AppMedia {

@override
String toString() {
return 'AppMedia(id: $id, name: $name, path: $path, thumbnailLink: $thumbnailLink, displayHeight: $displayHeight, displayWidth: $displayWidth, type: $type, mimeType: $mimeType, createdTime: $createdTime, modifiedTime: $modifiedTime, orientation: $orientation, size: $size, videoDuration: $videoDuration, latitude: $latitude, longitude: $longitude, sources: $sources)';
return 'AppMedia(id: $id, driveMediaRefId: $driveMediaRefId, name: $name, path: $path, thumbnailLink: $thumbnailLink, displayHeight: $displayHeight, displayWidth: $displayWidth, type: $type, mimeType: $mimeType, createdTime: $createdTime, modifiedTime: $modifiedTime, orientation: $orientation, size: $size, videoDuration: $videoDuration, latitude: $latitude, longitude: $longitude, sources: $sources)';
}

@override
Expand All @@ -365,6 +381,8 @@ class _$AppMediaImpl implements _AppMedia {
(other.runtimeType == runtimeType &&
other is _$AppMediaImpl &&
(identical(other.id, id) || other.id == id) &&
(identical(other.driveMediaRefId, driveMediaRefId) ||
other.driveMediaRefId == driveMediaRefId) &&
(identical(other.name, name) || other.name == name) &&
(identical(other.path, path) || other.path == path) &&
(identical(other.thumbnailLink, thumbnailLink) ||
Expand Down Expand Up @@ -397,6 +415,7 @@ class _$AppMediaImpl implements _AppMedia {
int get hashCode => Object.hash(
runtimeType,
id,
driveMediaRefId,
name,
path,
thumbnailLink,
Expand Down Expand Up @@ -430,6 +449,7 @@ class _$AppMediaImpl implements _AppMedia {
abstract class _AppMedia implements AppMedia {
const factory _AppMedia(
{required final String id,
final String? driveMediaRefId,
final String? name,
required final String path,
final String? thumbnailLink,
Expand All @@ -452,6 +472,8 @@ abstract class _AppMedia implements AppMedia {
@override
String get id;
@override
String? get driveMediaRefId;
@override
String? get name;
@override
String get path;
Expand Down
2 changes: 2 additions & 0 deletions data/lib/models/media/media.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions data/lib/services/google_drive_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ class GoogleDriveService {
}
}

Future<void> deleteMedia(String id) async {
try {
final driveApi = await _getGoogleDriveAPI();
await driveApi.files.delete(id);
} catch (e) {
throw AppError.fromError(e);
}
}

Future<void> uploadInGoogleDrive(
{required String folderID, required AppMedia media}) async {
final localFile = File(media.path);
Expand Down
8 changes: 8 additions & 0 deletions data/lib/services/local_media_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,12 @@ class LocalMediaService {
throw AppError.fromError(e);
}
}

Future<List<String>> deleteMedias(List<String> medias) async {
try {
return await PhotoManager.editor.deleteWithIds(medias);
} catch (e) {
throw AppError.fromError(e);
}
}
}

0 comments on commit 97c07e3

Please sign in to comment.