Skip to content

Commit

Permalink
Implement google drive video preview
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-pratik-k committed Apr 11, 2024
1 parent 3be95d2 commit e1508c4
Show file tree
Hide file tree
Showing 13 changed files with 331 additions and 180 deletions.
8 changes: 7 additions & 1 deletion app/assets/locales/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,19 @@
"delete_media_from_google_drive_confirmation_message": "Are you sure you want to delete this media? It will be permanently deleted from your Google Drive.",

"waiting_in_queue_text": "Waiting in Queue...",
"waiting_in_download_queue_message": "Your video download is in queue. We appreciate your patience!",

"transfer_screen_title": "Transfer",

"empty_upload_title":"No Files Being Uploads",
"empty_upload_message": "No uploads are happening right now. If you have files to upload, go ahead and start uploading.",

"empty_download_title":"No Files Being Downloads",
"empty_download_message": "No downloads are happening right now. If you have files to download, go ahead and start downloading."
"empty_download_message": "No downloads are happening right now. If you have files to download, go ahead and start downloading.",

"download_in_progress_text": "Download in progress",

"download_require_text": "Download required",
"download_require_message": "To watch the video, simply download it first. Tap the download button to begin downloading the video."

}
6 changes: 3 additions & 3 deletions app/lib/components/error_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ class ErrorView extends StatelessWidget {
color: context.colorScheme.containerHighOnSurface,
size: 100,
),
const SizedBox(height: 20),
const SizedBox(height: 40),
Text(title,
style: AppTextStyles.subtitle2.copyWith(
style: AppTextStyles.subtitle1.copyWith(
color: foregroundColor?? context.colorScheme.textPrimary,
)),
const SizedBox(height: 20),
Text(
message,
style: AppTextStyles.body2.copyWith(
style: AppTextStyles.subtitle2.copyWith(
color: foregroundColor ??context.colorScheme.textSecondary,
),
textAlign: TextAlign.center,
Expand Down
30 changes: 13 additions & 17 deletions app/lib/domain/extensions/media_list_extension.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import 'package:data/extensions/iterable_extension.dart';
import 'package:data/models/app_process/app_process.dart';
import 'package:data/models/media/media.dart';
import 'package:data/models/media/media_extension.dart';

extension MediaListHelper on List<AppMedia> {
extension MediaListExtension on List<AppMedia> {
void removeGoogleDriveRefFromMedias({List<String>? removeFromIds}) {
for (int index = 0; index < length; index++) {
if (this[index].isGoogleDriveStored &&
Expand All @@ -20,40 +21,35 @@ extension MediaListHelper on List<AppMedia> {
}
}

void addGoogleDriveRefInMedia(
{required List<AppProcess> process, required List<String> processIds}) {
void addGoogleDriveRefInMedias(
{required List<AppProcess> process, List<String>? processIds}) {
processIds ??= process.map((e) => e.id).toList();
updateWhere(
where: (media) => processIds.contains(media.id),
where: (media) => processIds?.contains(media.id) ?? false,
update: (media) {
final res = process
.where((element) => element.id == media.id)
.first
.response as AppMedia?;
return media.copyWith(
thumbnailLink: res?.thumbnailLink,
driveMediaRefId: res?.id,
sources: media.sources.toList()..add(AppMediaSource.googleDrive),
);
if (res == null) return media;
return media.margeGoogleDriveMedia(res);
},
);
}

void addLocalRefInMedias(
{required List<AppProcess> process, required List<String> processIds}) {
void replaceMediaRefInMedias(
{required List<AppProcess> process, List<String>? processIds}) {
processIds ??= process.map((e) => e.id).toList();
updateWhere(
where: (media) => processIds.contains(media.id),
where: (media) => processIds?.contains(media.id) ?? false,
update: (media) {
final res = process
.where((element) => element.id == media.id)
.first
.response as AppMedia?;

if (res == null) return media;
return res.copyWith(
thumbnailLink: media.thumbnailLink,
driveMediaRefId: media.id,
sources: res.sources.toList()..add(AppMediaSource.googleDrive),
);
return res;
},
);
}
Expand Down
52 changes: 6 additions & 46 deletions app/lib/ui/flow/home/components/app_media_item.dart
Original file line number Diff line number Diff line change
@@ -1,42 +1,17 @@
import 'dart:async';
import 'dart:ui';
import 'dart:typed_data';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:cloud_gallery/domain/formatter/duration_formatter.dart';
import 'package:data/models/app_process/app_process.dart';
import 'package:data/models/media/media.dart';
import 'package:data/models/media/media_extension.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter_svg/svg.dart';
import 'package:style/extensions/context_extensions.dart';
import 'package:style/indicators/circular_progress_indicator.dart';
import 'package:style/text/app_text_style.dart';
import '../../../../domain/assets/assets_paths.dart';
import 'package:style/animations/item_selector.dart';
import 'package:photo_manager/photo_manager.dart'
show AssetEntity, ThumbnailFormat, ThumbnailSize;

class ThumbNailParameter {
final RootIsolateToken token;
final Size size;
final String id;
final AppMediaType type;

ThumbNailParameter(this.token,this.size, this.id, this.type);
}

FutureOr thumbnailDataWithSize(ThumbNailParameter thumbNailParameter) async {
BackgroundIsolateBinaryMessenger.ensureInitialized(thumbNailParameter.token);

return await AssetEntity(id: thumbNailParameter.id, typeInt: thumbNailParameter.type.index, width: 0, height: 0)
.thumbnailDataWithSize(
ThumbnailSize(thumbNailParameter.size.width.toInt(), thumbNailParameter.size.height.toInt()),
format: ThumbnailFormat.png,
quality: 70,
);
}



class AppMediaItem extends StatefulWidget {
final AppMedia media;
Expand All @@ -60,29 +35,16 @@ class AppMediaItem extends StatefulWidget {

class _AppMediaItemState extends State<AppMediaItem>
with AutomaticKeepAliveClientMixin {
late Future<dynamic> thumbnailByte;
late Future<Uint8List?> thumbnailByte;

@override
void initState() {
if (widget.media.sources.contains(AppMediaSource.local)) {
// _loadImage();
thumbnailByte = widget.media.loadThumbnail();
}
super.initState();
}

Future _loadImage() async {
var rootToken = RootIsolateToken.instance!;
final ThumbNailParameter thumbNailParameter= ThumbNailParameter(rootToken,const Size(300,300), widget.media.id, widget.media.type);
final bytes=await compute(
thumbnailDataWithSize, thumbNailParameter
);

return bytes;
//thumbnailByte = widget.media.thumbnailDataWithSize(const Size(300, 300));
}



@override
Widget build(BuildContext context) {
super.build(context);
Expand All @@ -106,8 +68,6 @@ class _AppMediaItemState extends State<AppMediaItem>
);
}



Widget _videoDuration(BuildContext context) => Align(
alignment: Alignment.bottomRight,
child: _BackgroundContainer(
Expand Down Expand Up @@ -135,7 +95,7 @@ class _AppMediaItemState extends State<AppMediaItem>
{required BuildContext context, required BoxConstraints constraints}) {
if (widget.media.sources.contains(AppMediaSource.local)) {
return FutureBuilder(
future: _loadImage(),
future: thumbnailByte,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done &&
snapshot.hasData) {
Expand All @@ -157,7 +117,7 @@ class _AppMediaItemState extends State<AppMediaItem>
return Hero(
tag: widget.media,
child: CachedNetworkImage(
imageUrl: widget.media.thumbnailLink!,
imageUrl: widget.media.thumbnailLink ?? '',
width: constraints.maxWidth,
height: constraints.maxHeight,
fit: BoxFit.cover,
Expand Down
17 changes: 9 additions & 8 deletions app/lib/ui/flow/home/home_screen_view_model.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'dart:async';
import 'package:cloud_gallery/domain/extensions/map_extensions.dart';
import 'package:cloud_gallery/domain/extensions/media_list_extension.dart';
import 'package:data/models/app_process/app_process.dart';
import 'package:data/models/media/media.dart';
import 'package:data/models/media/media_extension.dart';
import 'package:data/repositories/google_drive_process_repo.dart';
import 'package:data/services/auth_service.dart';
import 'package:data/services/google_drive_service.dart';
Expand Down Expand Up @@ -62,7 +64,7 @@ class HomeViewStateNotifier extends StateNotifier<HomeViewState>
_backUpFolderId = null;
_uploadedMedia.clear();
state = state.copyWith(
medias: removeGoogleDriveRefFromMedias(medias: state.medias),
medias: removeGoogleDriveRefFromMediaMap(medias: state.medias),
);
}
});
Expand All @@ -81,23 +83,23 @@ class HomeViewStateNotifier extends StateNotifier<HomeViewState>

if (successUploads.isNotEmpty) {
state = state.copyWith(
medias: addGoogleDriveMediaRef(
medias: addGoogleDriveRefInMediaMap(
medias: state.medias,
process: successUploads.toList(),
));
}

if (successDeletes.isNotEmpty) {
state = state.copyWith(
medias: removeGoogleDriveRefFromMedias(
medias: removeGoogleDriveRefFromMediaMap(
medias: state.medias,
removeFromIds: successDeletes.toList(),
));
}

if (successDownloads.isNotEmpty) {
state = state.copyWith(
medias: addLocalMediaRef(
medias: replaceMediaRefInMediaMap(
medias: state.medias,
process: successDownloads.toList(),
));
Expand Down Expand Up @@ -221,10 +223,9 @@ class HomeViewStateNotifier extends StateNotifier<HomeViewState>
state = state.copyWith(
medias: sortMedias(medias: [
...mergeCommonMedia(
localMedias: removeGoogleDriveRefFromMedias(medias: state.medias)
.values
.expand((element) => element)
.toList(),
localMedias:
state.medias.values.expand((element) => element).toList()
..removeGoogleDriveRefFromMedias(),
googleDriveMedias: uploadedMedia,
),
...googleDriveMedia
Expand Down
73 changes: 13 additions & 60 deletions app/lib/ui/flow/home/home_view_model_helper_mixin.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import 'package:cloud_gallery/domain/extensions/media_list_extension.dart';
import 'package:cloud_gallery/domain/formatter/date_formatter.dart';
import 'package:collection/collection.dart';
import 'package:data/extensions/iterable_extension.dart';
import 'package:data/models/app_process/app_process.dart';
import 'package:data/models/media/media.dart';
import 'package:data/models/media/media_extension.dart';

mixin HomeViewModelHelperMixin {
List<AppMedia> mergeCommonMedia({
Expand All @@ -26,11 +27,7 @@ mixin HomeViewModelHelperMixin {
.forEach((googleDriveMedia) {
localMedias.removeWhere((media) => media.id == localMedia.id);

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

Expand All @@ -47,29 +44,16 @@ mixin HomeViewModelHelperMixin {
);
}

Map<DateTime, List<AppMedia>> removeGoogleDriveRefFromMedias(
Map<DateTime, List<AppMedia>> removeGoogleDriveRefFromMediaMap(
{required Map<DateTime, List<AppMedia>> medias,
List<String>? removeFromIds}) {
return medias.map((key, mediaList) {
for (int index = 0; index < mediaList.length; index++) {
if (mediaList[index].isGoogleDriveStored &&
(removeFromIds?.contains(mediaList[index].id) ?? true)) {
mediaList.removeAt(index);
} else if (mediaList[index].isCommonStored &&
(removeFromIds?.contains(mediaList[index].id) ?? true)) {
mediaList[index] = mediaList[index].copyWith(
sources: mediaList[index].sources.toList()
..remove(AppMediaSource.googleDrive),
thumbnailLink: null,
driveMediaRefId: null,
);
}
}
return MapEntry(key, mediaList);
return medias.map((key, value) {
return MapEntry(key,
value..removeGoogleDriveRefFromMedias(removeFromIds: removeFromIds));
});
}

Map<DateTime, List<AppMedia>> addGoogleDriveMediaRef({
Map<DateTime, List<AppMedia>> addGoogleDriveRefInMediaMap({
required Map<DateTime, List<AppMedia>> medias,
required List<AppProcess> process,
}) {
Expand All @@ -78,50 +62,19 @@ mixin HomeViewModelHelperMixin {
return MapEntry(
key,
value
..updateWhere(
where: (media) => processIds.contains(media.id),
update: (media) {
final res = process
.where((element) => element.id == media.id)
.first
.response as AppMedia?;
return media.copyWith(
thumbnailLink: res?.thumbnailLink,
driveMediaRefId: res?.id,
sources: media.sources.toList()
..add(AppMediaSource.googleDrive),
);
},
));
..addGoogleDriveRefInMedias(
process: process, processIds: processIds));
});
}

Map<DateTime, List<AppMedia>> addLocalMediaRef({
Map<DateTime, List<AppMedia>> replaceMediaRefInMediaMap({
required Map<DateTime, List<AppMedia>> medias,
required List<AppProcess> process,
}) {
final processIds = process.map((e) => e.id).toList();
return medias.map((key, value) {
return MapEntry(
key,
value
..updateWhere(
where: (media) => processIds.contains(media.id),
update: (media) {
final res = process
.where((element) => element.id == media.id)
.first
.response as AppMedia?;

if (res == null) return media;
return res.copyWith(
thumbnailLink: media.thumbnailLink,
driveMediaRefId: media.id,
sources: res.sources.toList()
..add(AppMediaSource.googleDrive),
);
},
));
return MapEntry(key,
value..replaceMediaRefInMedias(process: process, processIds: processIds));
});
}
}
Loading

0 comments on commit e1508c4

Please sign in to comment.