Skip to content

Commit

Permalink
Implement google drive preview
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-pratik-k committed Apr 11, 2024
1 parent e1508c4 commit 9616dd2
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class DownloadRequireView extends StatelessWidget {
],
),
title:
"${downloadProcess?.progress?.chunk.formatBytes ?? "0.0 B"} - ${downloadProcess?.progress?.total.formatBytes ?? "0.0 B"} ${downloadProcess?.progress?.percentage ?? "0.0"}%",
"${downloadProcess?.progress?.chunk.formatBytes ?? "0.0 B"} - ${downloadProcess?.progress?.total.formatBytes ?? "0.0 B"} ${downloadProcess?.progress?.percentage.toStringAsFixed(0) ?? "0.0"}%",
message: context.l10n.download_in_progress_text),
],
if (downloadProcess?.status.isWaiting ?? false) ...[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class _ProcessItemState extends State<ProcessItem> {
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'${widget.process.progress?.chunk.formatBytes} ${widget.process.progress?.percentage.toStringAsFixed(2)}%',
'${widget.process.progress?.chunk.formatBytes} ${widget.process.progress?.percentage.toStringAsFixed(0)}%',
style: AppTextStyles.body2.copyWith(
color: context.colorScheme.textSecondary,
),
Expand Down
41 changes: 13 additions & 28 deletions data/lib/repositories/google_drive_process_repo.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:async';
import 'dart:typed_data';
import 'package:collection/collection.dart';
import 'package:data/extensions/iterable_extension.dart';
import 'package:data/models/app_process/app_process.dart';
Expand Down Expand Up @@ -164,7 +163,6 @@ class GoogleDriveProcessRepo extends ChangeNotifier {
}

Future<void> _downloadFromGoogleDrive(AppProcess process) async {
StreamSubscription? subscription;
try {
_downloadQueue.updateWhere(
where: (element) => element.id == process.id,
Expand All @@ -176,30 +174,19 @@ class GoogleDriveProcessRepo extends ChangeNotifier {
final mediaContent = await _googleDriveService
.fetchMediaBytes(process.media.driveMediaRefId!);

List<int> bytes = [];

subscription = mediaContent.stream.listen((chunk) {
bytes.addAll(chunk);
_downloadQueue.updateWhere(
where: (element) => element.id == process.id,
update: (element) => element.copyWith(
progress: AppProcessProgress(
total: mediaContent.length ?? 0, chunk: bytes.length)),
);
notifyListeners();
}, onError: (error) {
_downloadQueue.updateWhere(
where: (element) => element.id == process.id,
update: (element) =>
element.copyWith(status: AppProcessStatus.failed),
);
notifyListeners();
subscription?.cancel();
});
await subscription.asFuture();

final localMedia = await _localMediaService.saveMedia(
process.media, Uint8List.fromList(bytes));
content: mediaContent,
onProgress: (total, chunk) {
_downloadQueue.updateWhere(
where: (element) => element.id == process.id,
update: (element) => element.copyWith(
progress: AppProcessProgress(total: total, chunk: chunk)),
);
notifyListeners();
},
mimeType: process.media.mimeType,
type: process.media.type,
);

final updatedMedia = await _googleDriveService.updateMediaDescription(
process.media.id, localMedia?.path ?? "");
Expand All @@ -210,14 +197,12 @@ class GoogleDriveProcessRepo extends ChangeNotifier {
status: AppProcessStatus.success,
response: localMedia?.margeGoogleDriveMedia(updatedMedia)),
);

notifyListeners();
subscription.cancel();
} catch (error) {
_downloadQueue.updateWhere(
where: (element) => element.id == process.id,
update: (element) => element.copyWith(status: AppProcessStatus.failed),
);
} finally {
notifyListeners();
}
}
Expand Down
66 changes: 47 additions & 19 deletions data/lib/services/local_media_service.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
import 'package:collection/collection.dart';
import 'package:data/models/media/media.dart';
import 'package:data/models/media_content/media_content.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:path_provider/path_provider.dart';
import 'package:photo_manager/photo_manager.dart';
Expand Down Expand Up @@ -56,25 +57,52 @@ class LocalMediaService {
}
}

Future<AppMedia?> saveMedia(AppMedia media, Uint8List bytes) async {
final extension = media.mimeType?.trim().isNotEmpty ?? false
? media.mimeType!.split('/').last
: media.type.isVideo
? 'mp4'
: 'jpg';
AssetEntity? asset;
if (media.type.isVideo) {
Future<AppMedia?> saveMedia({
required AppMediaType type,
required String? mimeType,
required AppMediaContent content,
required void Function(int total, int chunk) onProgress,
}) async {
try {
final extension = mimeType?.trim().isNotEmpty ?? false
? mimeType!.split('/').last
: type.isVideo
? 'mp4'
: 'jpg';

AssetEntity? asset;

final tempDir = await getTemporaryDirectory();
final tempVideoFile = File('${tempDir.path}/temp_video');
await tempVideoFile.writeAsBytes(bytes);
asset = await PhotoManager.editor.saveVideo(
tempVideoFile,
title: "${media.name ?? DateTime.now()}_gd_cloud_gallery.$extension",
);
} else if (media.type.isImage) {
asset = await PhotoManager.editor.saveImage(bytes,
title: "${media.name ?? DateTime.now()}_gd_cloud_gallery.$extension");
final tempFile = File(
'${tempDir.path}${DateTime.now()}_gd_cloud_gallery_temp.$extension');
await tempFile.create();

int chunkLength = 0;

StreamSubscription<List<int>> subscription =
content.stream.listen((chunk) {
chunkLength += chunk.length;
onProgress(content.length ?? 0, chunkLength);
tempFile.writeAsBytesSync(chunk, mode: FileMode.append);
});
await subscription.asFuture();
subscription.cancel();

if (type.isVideo) {
asset = await PhotoManager.editor.saveVideo(
tempFile,
title: "${DateTime.now()}_gd_cloud_gallery.$extension",
);
} else if (type.isImage) {
asset = await PhotoManager.editor.saveImageWithPath(
tempFile.path,
title: "${DateTime.now()}_gd_cloud_gallery.$extension",
);
}
await tempFile.delete();
return asset != null ? AppMedia.fromAssetEntity(asset) : null;
} catch (e) {
throw AppError.fromError(e);
}
return asset != null ? AppMedia.fromAssetEntity(asset) : null;
}
}

0 comments on commit 9616dd2

Please sign in to comment.