Use SAF for Android

This commit is contained in:
2023-02-22 23:42:03 +01:00
parent 7affd0e49d
commit 0098f84517
6 changed files with 105 additions and 17 deletions

View File

@@ -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<MyHomePage> {
@override
void initState() {
super.initState();
}
Future<String?> getSavedSheetsPath() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getString("sheets-path");
@@ -62,9 +68,31 @@ class _MyHomePageState extends State<MyHomePage> {
return "No path selected";
}
Future<String?> getSafPickedSheetsDirectory() async {
var pickedDirectories = await Saf.getPersistedPermissionDirectories();
if (pickedDirectories == null || pickedDirectories.isEmpty) {
return null;
}
return pickedDirectories.last;
}
Future<List<Sheet>> 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

View File

@@ -11,27 +11,34 @@ class Sheet {
Sheet(this.author, this.name, this.path);
}
Future<List<Sheet>> loadSheetsSorted(String path) async {
var sheets = await _loadSheets(path);
Future<List<Sheet>> loadSheetsSorted(List<String> sheetsDirectoryFiles) async {
var sheets = await _loadSheets(sheetsDirectoryFiles);
sheets.sort((left, right) => left.name.compareTo(right.name));
return sheets;
}
Future<List<Sheet>> _loadSheets(String path) async {
var dir = Directory(path);
if (!dir.existsSync()) {
throw Exception("Specified path '$path' does not exist");
}
Future<List<Sheet>> _loadSheets(List<String> sheetsDirectoryFiles) async {
final List<Sheet> 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));
}
}
}

View File

@@ -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:

View File

@@ -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:

View File

@@ -7,8 +7,11 @@
#include "generated_plugin_registrant.h"
#include <pdfx/pdfx_plugin.h>
#include <permission_handler_windows/permission_handler_windows_plugin.h>
void RegisterPlugins(flutter::PluginRegistry* registry) {
PdfxPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("PdfxPlugin"));
PermissionHandlerWindowsPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("PermissionHandlerWindowsPlugin"));
}

View File

@@ -4,6 +4,7 @@
list(APPEND FLUTTER_PLUGIN_LIST
pdfx
permission_handler_windows
)
list(APPEND FLUTTER_FFI_PLUGIN_LIST