diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index be66168..cb09871 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -1,6 +1,7 @@ + { ..showSnackBar(SnackBar(content: Text(message))); } + Future _downloadMedia() async { + final mediaState = ref.read(mediaProvider); + final currentItem = mediaState.mediaItems[_currentIndex]; + + if (Platform.isAndroid || Platform.isIOS) { + var status = await Permission.storage.status; + if (!status.isGranted) { + status = await Permission.storage.request(); + if (!status.isGranted) { + _showError("Speicherberechtigung wurde nicht erteilt."); + return; + } + } + } + + String localPath; + if (Platform.isAndroid) { + final directory = await getExternalStorageDirectory(); + localPath = "${directory!.path}/Download/fApp"; + } else if (Platform.isIOS) { + final directory = await getApplicationDocumentsDirectory(); + localPath = directory.path; + } else { + final directory = await getTemporaryDirectory(); + localPath = directory.path; + } + + final savedDir = Directory(localPath); + if (!await savedDir.exists()) { + await savedDir.create(recursive: true); + } + + try { + await FlutterDownloader.enqueue( + url: currentItem.mediaUrl, + savedDir: localPath, + fileName: currentItem.mediaUrl.split('/').last, + showNotification: true, + openFileFromNotification: true, + ); + + if (mounted) { + _showError('Download gestartet: ${currentItem.mediaUrl}'); + } + } catch (e) { + _showError('Download fehlgeschlagen: $e'); + } + } + @override void dispose() { _pageController?.dispose(); @@ -78,6 +130,7 @@ class _DetailViewState extends ConsumerState { final int itemIndex = mediaState.mediaItems.indexWhere( (item) => item.id == widget.initialItemId, ); + print('itemIndex: ${itemIndex}; initial: ${widget.initialItemId}'); if (itemIndex == -1) { Future.microtask(() { @@ -115,14 +168,12 @@ class _DetailViewState extends ConsumerState { IconButton( icon: const Icon(Icons.fullscreen), onPressed: () { - _showError('fullscreen ist wip'); + _showError('download ist wip'); }, ), IconButton( icon: const Icon(Icons.download), - onPressed: () { - _showError('download ist wip'); - }, + onPressed: _downloadMedia, ), PopupMenuButton( onSelected: (value) async { diff --git a/pubspec.lock b/pubspec.lock index d86de63..6c942c4 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -150,6 +150,14 @@ packages: url: "https://pub.dev" source: hosted version: "3.4.1" + flutter_downloader: + dependency: "direct main" + description: + name: flutter_downloader + sha256: "93a9ddbd561f8a3f5483b4189453fba145a0a1014a88143c96a966296b78a118" + url: "https://pub.dev" + source: hosted + version: "1.12.0" flutter_lints: dependency: "direct dev" description: @@ -385,7 +393,7 @@ packages: source: hosted version: "1.9.1" path_provider: - dependency: transitive + dependency: "direct main" description: name: path_provider sha256: "50c5dd5b6e1aaf6fb3a78b33f6aa3afca52bf903a8a5298f53101fdaee55bbcd" @@ -432,6 +440,54 @@ packages: url: "https://pub.dev" source: hosted version: "2.3.0" + permission_handler: + dependency: "direct main" + description: + name: permission_handler + sha256: "2d070d8684b68efb580a5997eb62f675e8a885ef0be6e754fb9ef489c177470f" + url: "https://pub.dev" + source: hosted + version: "12.0.0+1" + permission_handler_android: + dependency: transitive + description: + name: permission_handler_android + sha256: "1e3bc410ca1bf84662104b100eb126e066cb55791b7451307f9708d4007350e6" + url: "https://pub.dev" + source: hosted + version: "13.0.1" + permission_handler_apple: + dependency: transitive + description: + name: permission_handler_apple + sha256: f000131e755c54cf4d84a5d8bd6e4149e262cc31c5a8b1d698de1ac85fa41023 + url: "https://pub.dev" + source: hosted + version: "9.4.7" + permission_handler_html: + dependency: transitive + description: + name: permission_handler_html + sha256: "38f000e83355abb3392140f6bc3030660cfaef189e1f87824facb76300b4ff24" + url: "https://pub.dev" + source: hosted + version: "0.1.3+5" + permission_handler_platform_interface: + dependency: transitive + description: + name: permission_handler_platform_interface + sha256: eb99b295153abce5d683cac8c02e22faab63e50679b937fa1bf67d58bb282878 + url: "https://pub.dev" + source: hosted + version: "4.3.0" + permission_handler_windows: + dependency: transitive + description: + name: permission_handler_windows + sha256: "1a790728016f79a41216d88672dbc5df30e686e811ad4e698bfc51f76ad91f1e" + url: "https://pub.dev" + source: hosted + version: "0.2.1" platform: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 64d61c8..d7106e1 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # In Windows, build-name is used as the major, minor, and patch parts # of the product and file versions while build-number is used as the build suffix. -version: 1.1.9+39 +version: 1.1.10+40 environment: sdk: ^3.9.0-100.2.beta @@ -42,6 +42,9 @@ dependencies: flutter_secure_storage: ^9.2.4 flutter_riverpod: ^2.6.1 go_router: ^15.1.3 + flutter_downloader: ^1.12.0 + permission_handler: ^12.0.0+1 + path_provider: ^2.1.5 dev_dependencies: flutter_test: