Skip to content

Commit

Permalink
fix(mobile): download asset to Camera folder on Android (immich-app#1…
Browse files Browse the repository at this point in the history
…2355)

* fix(mobile): download asset to Camera folder on Android

* remove unused import

* better message

* linting
  • Loading branch information
alextran1502 authored Sep 5, 2024
1 parent 0148005 commit 77904a5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
9 changes: 5 additions & 4 deletions mobile/assets/i18n/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,10 @@
"home_page_share_err_local": "Can not share local assets via link, skipping",
"home_page_upload_err_limit": "Can only upload a maximum of 30 assets at a time, skipping",
"image_saved_successfully": "Image saved",
"image_viewer_page_state_provider_download_error": "Download Error",
"image_viewer_page_state_provider_download_started": "Download Started",
"image_viewer_page_state_provider_download_success": "Download Success",
"download_error": "Download Error",
"download_started": "Download started",
"download_sucess": "Download success",
"download_sucess_android": "The media has been downloaded to DCIM/Immich",
"image_viewer_page_state_provider_share_error": "Share Error",
"invalid_date": "Invalid date",
"invalid_date_format": "Invalid date format",
Expand Down Expand Up @@ -585,4 +586,4 @@
"viewer_remove_from_stack": "Remove from Stack",
"viewer_stack_use_as_main_asset": "Use as Main Asset",
"viewer_unstack": "Un-Stack"
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:io';

import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
Expand Down Expand Up @@ -31,19 +33,21 @@ class ImageViewerStateNotifier extends StateNotifier<AssetViewerPageState> {

ImmichToast.show(
context: context,
msg: 'image_viewer_page_state_provider_download_started'.tr(),
msg: 'download_started'.tr(),
toastType: ToastType.info,
gravity: ToastGravity.BOTTOM,
);

bool isSuccess = await _imageViewerService.downloadAssetToDevice(asset);
bool isSuccess = await _imageViewerService.downloadAsset(asset);

if (isSuccess) {
state = state.copyWith(downloadAssetStatus: DownloadAssetStatus.success);

ImmichToast.show(
context: context,
msg: 'image_viewer_page_state_provider_download_success'.tr(),
msg: Platform.isAndroid
? 'download_sucess_android'.tr()
: 'download_sucess'.tr(),
toastType: ToastType.success,
gravity: ToastGravity.BOTTOM,
);
Expand All @@ -52,7 +56,7 @@ class ImageViewerStateNotifier extends StateNotifier<AssetViewerPageState> {
state = state.copyWith(downloadAssetStatus: DownloadAssetStatus.error);
ImmichToast.show(
context: context,
msg: 'image_viewer_page_state_provider_download_error'.tr(),
msg: 'download_error'.tr(),
toastType: ToastType.error,
gravity: ToastGravity.BOTTOM,
);
Expand Down
11 changes: 8 additions & 3 deletions mobile/lib/services/image_viewer.service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ImageViewerService {

ImageViewerService(this._apiService);

Future<bool> downloadAssetToDevice(Asset asset) async {
Future<bool> downloadAsset(Asset asset) async {
File? imageFile;
File? videoFile;
try {
Expand Down Expand Up @@ -82,18 +82,23 @@ class ImageViewerService {
}

final AssetEntity? entity;
final relativePath = Platform.isAndroid ? 'DCIM/Immich' : null;

if (asset.isImage) {
entity = await PhotoManager.editor.saveImage(
res.bodyBytes,
title: asset.fileName,
relativePath: relativePath,
);
} else {
final tempDir = await getTemporaryDirectory();
videoFile = await File('${tempDir.path}/${asset.fileName}').create();
videoFile.writeAsBytesSync(res.bodyBytes);
entity = await PhotoManager.editor
.saveVideo(videoFile, title: asset.fileName);
entity = await PhotoManager.editor.saveVideo(
videoFile,
title: asset.fileName,
relativePath: relativePath,
);
}
return entity != null;
}
Expand Down
12 changes: 5 additions & 7 deletions mobile/lib/widgets/asset_viewer/gallery_app_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ class GalleryAppBar extends ConsumerWidget {
);
}

handleDownloadAsset() {
ref.read(imageViewerStateProvider.notifier).downloadAsset(asset, context);
}

return IgnorePointer(
ignoring: !ref.watch(showControlsProvider),
child: AnimatedOpacity(
Expand All @@ -109,13 +113,7 @@ class GalleryAppBar extends ConsumerWidget {
onFavorite: toggleFavorite,
onRestorePressed: () => handleRestore(asset),
onUploadPressed: asset.isLocal ? () => handleUpload(asset) : null,
onDownloadPressed: asset.isLocal
? null
: () =>
ref.read(imageViewerStateProvider.notifier).downloadAsset(
asset,
context,
),
onDownloadPressed: asset.isLocal ? null : handleDownloadAsset,
onToggleMotionVideo: onToggleMotionVideo,
onAddToAlbumPressed: () => addToAlbum(asset),
onActivitiesPressed: handleActivities,
Expand Down

0 comments on commit 77904a5

Please sign in to comment.