Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(mobile): check and request permissions when saving files to prevent hard crash #14919

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mobile/lib/providers/asset_viewer/download.provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class DownloadStateNotifier extends StateNotifier<DownloadState> {
}

void downloadAsset(Asset asset, BuildContext context) async {
await _downloadService.download(asset);
await _downloadService.download(asset, context: context);
}

void cancelDownload(String id) async {
Expand Down
62 changes: 61 additions & 1 deletion mobile/lib/services/download.service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import 'package:immich_mobile/repositories/file_media.repository.dart';
import 'package:immich_mobile/services/api.service.dart';
import 'package:immich_mobile/utils/download.dart';
import 'package:logging/logging.dart';
import 'package:permission_handler/permission_handler.dart' as ph;
import 'package:flutter/material.dart';
import 'package:easy_localization/easy_localization.dart';

final downloadServiceProvider = Provider(
(ref) => DownloadService(
Expand Down Expand Up @@ -158,7 +161,11 @@ class DownloadService {
return await FileDownloader().cancelTaskWithId(id);
}

Future<void> download(Asset asset) async {
Future<void> download(Asset asset, {BuildContext? context}) async {
if (!await _handlePermission(context)) {
return;
}

if (asset.isImage && asset.livePhotoVideoId != null && Platform.isIOS) {
await _downloadRepository.download(
_buildDownloadTask(
Expand Down Expand Up @@ -196,6 +203,59 @@ class DownloadService {
}
}

Future<bool> _handlePermission(BuildContext? context) async {
final permission = await ph.Permission.photos.status;
if (permission.isDenied || permission.isPermanentlyDenied) {
if (context == null) return false;

final bool? shouldRequest = await showDialog<bool>(
context: context,
builder: (context) {
return AlertDialog(
title: const Text(
'permission_onboarding_request',
textAlign: TextAlign.center,
).tr(),
content: const Text(
'permission_onboarding_permission_denied',
textAlign: TextAlign.center,
).tr(),
actions: [
TextButton(
onPressed: () => Navigator.pop(context, false),
child: const Text('cancel').tr(),
),
TextButton(
onPressed: () => Navigator.pop(context, true),
child: Text(
permission.isPermanentlyDenied
? 'permission_onboarding_go_to_settings'
: 'permission_onboarding_grant_permission',
).tr(),
),
],
);
},
);

if (shouldRequest == true) {
if (permission.isPermanentlyDenied) {
await ph.openAppSettings();
return false;
}

final result = await ph.Permission.photos.request();
if (!result.isGranted) {
return false;
}
} else {
return false;
}
}

return true;
}

DownloadTask _buildDownloadTask(
String id,
String filename, {
Expand Down
Loading