Skip to content

Commit

Permalink
Merge pull request #408 from poppingmoon/image-dialog-save-file
Browse files Browse the repository at this point in the history
ImageDialogでファイルをそのまま保存できるように
  • Loading branch information
shiosyakeyakini-info authored Jul 14, 2024
2 parents 8233d61 + 1c318b3 commit d070fc6
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 55 deletions.
51 changes: 51 additions & 0 deletions lib/state_notifier/common/download_file_notifier.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import "package:device_info_plus/device_info_plus.dart";
import "package:dio/dio.dart";
import "package:flutter/foundation.dart";
import "package:image_gallery_saver/image_gallery_saver.dart";
import "package:miria/providers.dart";
import "package:misskey_dart/misskey_dart.dart" hide Permission;
import "package:permission_handler/permission_handler.dart";
import "package:riverpod_annotation/riverpod_annotation.dart";

part "download_file_notifier.g.dart";

@Riverpod(keepAlive: true)
class DownloadFileNotifier extends _$DownloadFileNotifier {
@override
void build() {
return;
}

Future<void> downloadFile(DriveFile driveFile) async {
if (defaultTargetPlatform == TargetPlatform.android) {
final androidInfo = await DeviceInfoPlugin().androidInfo;
if (androidInfo.version.sdkInt <= 32) {
final permissionStatus = await Permission.storage.status;
if (permissionStatus.isDenied) {
await Permission.storage.request();
}
} else {
final permissionStatus = await Permission.photos.status;
if (permissionStatus.isDenied) {
await Permission.photos.request();
}
}
}

final tempDir = ref.read(fileSystemProvider).systemTempDirectory;
final savePath = "${tempDir.path}/${driveFile.name}";

await ref.read(dioProvider).download(
driveFile.url,
savePath,
options: Options(
responseType: ResponseType.bytes,
),
);

await ImageGallerySaver.saveFile(
savePath,
name: driveFile.name,
);
}
}
27 changes: 27 additions & 0 deletions lib/state_notifier/common/download_file_notifier.g.dart

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

49 changes: 11 additions & 38 deletions lib/view/common/image_dialog.dart
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
import "dart:math";

import "package:device_info_plus/device_info_plus.dart";
import "package:dio/dio.dart";
import "package:flutter/foundation.dart";
import "package:flutter/material.dart";
import "package:flutter/services.dart";
import "package:flutter_gen/gen_l10n/app_localizations.dart";
import "package:hooks_riverpod/hooks_riverpod.dart";
import "package:image_gallery_saver/image_gallery_saver.dart";
import "package:miria/providers.dart";
import "package:miria/state_notifier/common/download_file_notifier.dart";
import "package:miria/view/common/interactive_viewer.dart" as iv;
import "package:miria/view/common/misskey_notes/network_image.dart";
import "package:permission_handler/permission_handler.dart";
import "package:misskey_dart/misskey_dart.dart";

class ImageDialog extends ConsumerStatefulWidget {
final List<String> imageUrlList;
final List<DriveFile> driveFiles;
final int initialPage;

const ImageDialog({
required this.imageUrlList,
required this.driveFiles,
required this.initialPage,
super.key,
});
Expand Down Expand Up @@ -166,9 +163,9 @@ class ImageDialogState extends ConsumerState<ImageDialog> {
? const ScrollPhysics()
: const NeverScrollableScrollPhysics(),
children: [
for (final url in widget.imageUrlList)
for (final file in widget.driveFiles)
ScaleNotifierInteractiveViewer(
imageUrl: url,
imageUrl: file.url,
controller: _transformationController,
onScaleChanged: (scaleUpdated) => setState(() {
scale = scaleUpdated;
Expand Down Expand Up @@ -219,37 +216,13 @@ class ImageDialogState extends ConsumerState<ImageDialog> {
onPressed: () async {
final page = pageController.page?.toInt();
if (page == null) return;
final response = await ref.read(dioProvider).get(
widget.imageUrlList[page],
options: Options(
responseType: ResponseType.bytes,
),
);

if (defaultTargetPlatform == TargetPlatform.android) {
final androidInfo =
await DeviceInfoPlugin().androidInfo;
if (androidInfo.version.sdkInt <= 32) {
final permissionStatus =
await Permission.storage.status;
if (permissionStatus.isDenied) {
await Permission.storage.request();
}
} else {
final permissionStatus =
await Permission.photos.status;
if (permissionStatus.isDenied) {
await Permission.photos.request();
}
}
}

await ImageGallerySaver.saveImage(response.data);
final driveFile = widget.driveFiles[page];
await ref
.read(downloadFileNotifierProvider.notifier)
.downloadFile(driveFile);
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(S.of(context).savedImage),
),
SnackBar(content: Text(S.of(context).savedImage)),
);
},
constraints:
Expand Down
16 changes: 8 additions & 8 deletions lib/view/common/misskey_notes/misskey_file_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class MisskeyFileView extends HookConsumerWidget {
child: MisskeyImage(
isSensitive: targetFile.isSensitive,
thumbnailUrl: targetFile.thumbnailUrl,
targetFiles: [targetFile.url.toString()],
targetFiles: [targetFile],
fileType: targetFile.type,
name: targetFile.name,
position: 0,
Expand Down Expand Up @@ -70,7 +70,7 @@ class MisskeyFileView extends HookConsumerWidget {
child: MisskeyImage(
isSensitive: targetFile.element.isSensitive,
thumbnailUrl: targetFile.element.thumbnailUrl,
targetFiles: targetFiles.map((e) => e.url).toList(),
targetFiles: targetFiles,
fileType: targetFile.element.type,
name: targetFile.element.name,
position: targetFile.index,
Expand All @@ -92,7 +92,7 @@ class MisskeyFileView extends HookConsumerWidget {
class MisskeyImage extends HookConsumerWidget {
final bool isSensitive;
final String? thumbnailUrl;
final List<String> targetFiles;
final List<DriveFile> targetFiles;
final int position;
final String fileType;
final String name;
Expand Down Expand Up @@ -158,21 +158,21 @@ class MisskeyImage extends HookConsumerWidget {
await showDialog(
context: context,
builder: (context) => ImageDialog(
imageUrlList: targetFiles,
driveFiles: targetFiles,
initialPage: position,
),
);
} else if (fileType.startsWith(RegExp("video|audio"))) {
await showDialog(
context: context,
builder: (context) => VideoDialog(
url: targetFiles[position],
url: targetFiles[position].url,
fileType: fileType,
),
);
} else {
await launchUrl(
Uri.parse(targetFiles[position]),
Uri.parse(targetFiles[position].url),
mode: LaunchMode.externalApplication,
);
}
Expand Down Expand Up @@ -225,7 +225,7 @@ class MisskeyImage extends HookConsumerWidget {
return SizedBox(
height: 200,
child: NetworkImageView(
url: thumbnailUrl ?? targetFiles[position],
url: thumbnailUrl ?? targetFiles[position].url,
type: ImageType.imageThumbnail,
loadingBuilder: (context, widget, chunkEvent) => SizedBox(
width: double.infinity,
Expand Down Expand Up @@ -264,7 +264,7 @@ class MisskeyImage extends HookConsumerWidget {
} else {
return TextButton.icon(
onPressed: () async => launchUrl(
Uri.parse(targetFiles[position]),
Uri.parse(targetFiles[position].url),
mode: LaunchMode.externalApplication,
),
icon: const Icon(Icons.file_download_outlined),
Expand Down
14 changes: 6 additions & 8 deletions lib/view/misskey_page_page/misskey_page_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -163,18 +163,16 @@ class PageContent extends ConsumerWidget {
);
}
if (content is misskey.PageImage) {
final url = page.attachedFiles
.firstWhereOrNull((e) => e.id == content.fileId)
?.url;
final thumbnailUrl = page.attachedFiles
.firstWhereOrNull((e) => e.id == content.fileId)
?.thumbnailUrl;
if (url != null) {
final file =
page.attachedFiles.firstWhereOrNull((e) => e.id == content.fileId);
if (file != null) {
final url = file.url;
final thumbnailUrl = file.thumbnailUrl;
return GestureDetector(
onTap: () async => showDialog(
context: context,
builder: (context) =>
ImageDialog(imageUrlList: [url], initialPage: 0),
ImageDialog(driveFiles: [file], initialPage: 0),
),
child: NetworkImageView(
url: thumbnailUrl ?? url,
Expand Down
2 changes: 1 addition & 1 deletion lib/view/user_page/user_info_notifier.g.dart

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

0 comments on commit d070fc6

Please sign in to comment.