diff --git a/lib/main.dart b/lib/main.dart index f2a944f..e763a12 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,5 +1,6 @@ import 'package:file_picker/file_picker.dart'; import 'package:flutter/material.dart'; +import 'package:saf/saf.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:sheetless/sheetview.dart'; @@ -33,6 +34,11 @@ class MyHomePage extends StatefulWidget { } class _MyHomePageState extends State { + @override + void initState() { + super.initState(); + } + Future getSavedSheetsPath() async { final prefs = await SharedPreferences.getInstance(); return prefs.getString("sheets-path"); @@ -62,9 +68,31 @@ class _MyHomePageState extends State { return "No path selected"; } + Future getSafPickedSheetsDirectory() async { + var pickedDirectories = await Saf.getPersistedPermissionDirectories(); + if (pickedDirectories == null || pickedDirectories.isEmpty) { + return null; + } + return pickedDirectories.last; + } + Future> acquireSheets() async { - var sheetsPath = await findAndSaveSheetsPath(); - return loadSheetsSorted(sheetsPath); + String? sheetsDirectory = await getSafPickedSheetsDirectory(); + if (sheetsDirectory == null || sheetsDirectory.isEmpty) { + Saf.getDynamicDirectoryPermission(grantWritePermission: false); + sheetsDirectory = await getSafPickedSheetsDirectory(); + if (sheetsDirectory == null || sheetsDirectory.isEmpty) { + throw Exception("No Directory selected"); + } + } + + var sheetsDirectoryFiles = await Saf.getFilesPathFor(sheetsDirectory); + if (sheetsDirectoryFiles == null) { + Saf.releasePersistedPermissionFor(sheetsDirectory); + throw Exception( + "Permissions for directory no longer valid or Directory deleted. Please restart app."); + } + return loadSheetsSorted(sheetsDirectoryFiles); } @override diff --git a/lib/sheet.dart b/lib/sheet.dart index f4f4a6f..19ca237 100644 --- a/lib/sheet.dart +++ b/lib/sheet.dart @@ -11,27 +11,34 @@ class Sheet { Sheet(this.author, this.name, this.path); } -Future> loadSheetsSorted(String path) async { - var sheets = await _loadSheets(path); +Future> loadSheetsSorted(List sheetsDirectoryFiles) async { + var sheets = await _loadSheets(sheetsDirectoryFiles); sheets.sort((left, right) => left.name.compareTo(right.name)); return sheets; } -Future> _loadSheets(String path) async { - var dir = Directory(path); - if (!dir.existsSync()) { - throw Exception("Specified path '$path' does not exist"); - } +Future> _loadSheets(List sheetsDirectoryFiles) async { final List sheets = List.empty(growable: true); - await for (final FileSystemEntity x in dir.list()) { - if (x is Directory) { - var authorName = p.basename(x.path); - await for (final FileSystemEntity a in x.list()) { - if (a is File) { - var sheetName = p.basenameWithoutExtension(a.path); - sheetName = sheetName.capitalize(); - sheets.add(Sheet(authorName, sheetName, a.path)); + var authorDirectories = sheetsDirectoryFiles + .map((e) => Directory(e)) + .where((element) => element.existsSync()); + + for (Directory authorDirectory in authorDirectories) { + var authorName = p.basename(authorDirectory.path); + // Ignore hidden directories + if (authorName.startsWith(".")) { + continue; + } + + await for (final FileSystemEntity sheetFile in authorDirectory.list()) { + if (sheetFile is File) { + var sheetName = p.basenameWithoutExtension(sheetFile.path); + // Ignore hidden files + if (sheetName.startsWith(".")) { + continue; } + sheetName = sheetName.capitalize(); + sheets.add(Sheet(authorName, sheetName, sheetFile.path)); } } } diff --git a/pubspec.lock b/pubspec.lock index bb17fb7..a16381b 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -256,6 +256,46 @@ packages: url: "https://pub.dev" source: hosted version: "2.3.0" + permission_handler: + dependency: transitive + description: + name: permission_handler + sha256: "33c6a1253d1f95fd06fa74b65b7ba907ae9811f9d5c1d3150e51417d04b8d6a8" + url: "https://pub.dev" + source: hosted + version: "10.2.0" + permission_handler_android: + dependency: transitive + description: + name: permission_handler_android + sha256: "8028362b40c4a45298f1cbfccd227c8dd6caf0e27088a69f2ba2ab15464159e2" + url: "https://pub.dev" + source: hosted + version: "10.2.0" + permission_handler_apple: + dependency: transitive + description: + name: permission_handler_apple + sha256: "9c370ef6a18b1c4b2f7f35944d644a56aa23576f23abee654cf73968de93f163" + url: "https://pub.dev" + source: hosted + version: "9.0.7" + permission_handler_platform_interface: + dependency: transitive + description: + name: permission_handler_platform_interface + sha256: "68abbc472002b5e6dfce47fe9898c6b7d8328d58b5d2524f75e277c07a97eb84" + url: "https://pub.dev" + source: hosted + version: "3.9.0" + permission_handler_windows: + dependency: transitive + description: + name: permission_handler_windows + sha256: f67cab14b4328574938ecea2db3475dad7af7ead6afab6338772c5f88963e38b + url: "https://pub.dev" + source: hosted + version: "0.1.2" photo_view: dependency: transitive description: @@ -288,6 +328,14 @@ packages: url: "https://pub.dev" source: hosted version: "4.2.4" + saf: + dependency: "direct main" + description: + name: saf + sha256: "3b7565638bc155801dc2b1b948ee0545f8cf9310da6e0f4ded45f62ee83f1321" + url: "https://pub.dev" + source: hosted + version: "1.0.3+4" shared_preferences: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index dac84ea..260a53c 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -40,6 +40,7 @@ dependencies: pdfx: ^2.3.0 file_picker: ^5.2.5 shared_preferences: ^2.0.17 + saf: ^1.0.3+4 dev_dependencies: flutter_test: diff --git a/windows/flutter/generated_plugin_registrant.cc b/windows/flutter/generated_plugin_registrant.cc index 338e020..aeef807 100644 --- a/windows/flutter/generated_plugin_registrant.cc +++ b/windows/flutter/generated_plugin_registrant.cc @@ -7,8 +7,11 @@ #include "generated_plugin_registrant.h" #include +#include void RegisterPlugins(flutter::PluginRegistry* registry) { PdfxPluginRegisterWithRegistrar( registry->GetRegistrarForPlugin("PdfxPlugin")); + PermissionHandlerWindowsPluginRegisterWithRegistrar( + registry->GetRegistrarForPlugin("PermissionHandlerWindowsPlugin")); } diff --git a/windows/flutter/generated_plugins.cmake b/windows/flutter/generated_plugins.cmake index 73e221f..49667b4 100644 --- a/windows/flutter/generated_plugins.cmake +++ b/windows/flutter/generated_plugins.cmake @@ -4,6 +4,7 @@ list(APPEND FLUTTER_PLUGIN_LIST pdfx + permission_handler_windows ) list(APPEND FLUTTER_FFI_PLUGIN_LIST