Use SAF for Android
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import 'package:file_picker/file_picker.dart';
|
import 'package:file_picker/file_picker.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:saf/saf.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
import 'package:sheetless/sheetview.dart';
|
import 'package:sheetless/sheetview.dart';
|
||||||
|
|
||||||
@@ -33,6 +34,11 @@ class MyHomePage extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _MyHomePageState extends State<MyHomePage> {
|
class _MyHomePageState extends State<MyHomePage> {
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
}
|
||||||
|
|
||||||
Future<String?> getSavedSheetsPath() async {
|
Future<String?> getSavedSheetsPath() async {
|
||||||
final prefs = await SharedPreferences.getInstance();
|
final prefs = await SharedPreferences.getInstance();
|
||||||
return prefs.getString("sheets-path");
|
return prefs.getString("sheets-path");
|
||||||
@@ -62,9 +68,31 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||||||
return "No path selected";
|
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 {
|
Future<List<Sheet>> acquireSheets() async {
|
||||||
var sheetsPath = await findAndSaveSheetsPath();
|
String? sheetsDirectory = await getSafPickedSheetsDirectory();
|
||||||
return loadSheetsSorted(sheetsPath);
|
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
|
@override
|
||||||
|
|||||||
@@ -11,27 +11,34 @@ class Sheet {
|
|||||||
Sheet(this.author, this.name, this.path);
|
Sheet(this.author, this.name, this.path);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<List<Sheet>> loadSheetsSorted(String path) async {
|
Future<List<Sheet>> loadSheetsSorted(List<String> sheetsDirectoryFiles) async {
|
||||||
var sheets = await _loadSheets(path);
|
var sheets = await _loadSheets(sheetsDirectoryFiles);
|
||||||
sheets.sort((left, right) => left.name.compareTo(right.name));
|
sheets.sort((left, right) => left.name.compareTo(right.name));
|
||||||
return sheets;
|
return sheets;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<List<Sheet>> _loadSheets(String path) async {
|
Future<List<Sheet>> _loadSheets(List<String> sheetsDirectoryFiles) async {
|
||||||
var dir = Directory(path);
|
|
||||||
if (!dir.existsSync()) {
|
|
||||||
throw Exception("Specified path '$path' does not exist");
|
|
||||||
}
|
|
||||||
final List<Sheet> sheets = List.empty(growable: true);
|
final List<Sheet> sheets = List.empty(growable: true);
|
||||||
await for (final FileSystemEntity x in dir.list()) {
|
var authorDirectories = sheetsDirectoryFiles
|
||||||
if (x is Directory) {
|
.map((e) => Directory(e))
|
||||||
var authorName = p.basename(x.path);
|
.where((element) => element.existsSync());
|
||||||
await for (final FileSystemEntity a in x.list()) {
|
|
||||||
if (a is File) {
|
for (Directory authorDirectory in authorDirectories) {
|
||||||
var sheetName = p.basenameWithoutExtension(a.path);
|
var authorName = p.basename(authorDirectory.path);
|
||||||
sheetName = sheetName.capitalize();
|
// Ignore hidden directories
|
||||||
sheets.add(Sheet(authorName, sheetName, a.path));
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
48
pubspec.lock
48
pubspec.lock
@@ -256,6 +256,46 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.3.0"
|
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:
|
photo_view:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@@ -288,6 +328,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "4.2.4"
|
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:
|
shared_preferences:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ dependencies:
|
|||||||
pdfx: ^2.3.0
|
pdfx: ^2.3.0
|
||||||
file_picker: ^5.2.5
|
file_picker: ^5.2.5
|
||||||
shared_preferences: ^2.0.17
|
shared_preferences: ^2.0.17
|
||||||
|
saf: ^1.0.3+4
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
|
|||||||
@@ -7,8 +7,11 @@
|
|||||||
#include "generated_plugin_registrant.h"
|
#include "generated_plugin_registrant.h"
|
||||||
|
|
||||||
#include <pdfx/pdfx_plugin.h>
|
#include <pdfx/pdfx_plugin.h>
|
||||||
|
#include <permission_handler_windows/permission_handler_windows_plugin.h>
|
||||||
|
|
||||||
void RegisterPlugins(flutter::PluginRegistry* registry) {
|
void RegisterPlugins(flutter::PluginRegistry* registry) {
|
||||||
PdfxPluginRegisterWithRegistrar(
|
PdfxPluginRegisterWithRegistrar(
|
||||||
registry->GetRegistrarForPlugin("PdfxPlugin"));
|
registry->GetRegistrarForPlugin("PdfxPlugin"));
|
||||||
|
PermissionHandlerWindowsPluginRegisterWithRegistrar(
|
||||||
|
registry->GetRegistrarForPlugin("PermissionHandlerWindowsPlugin"));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
list(APPEND FLUTTER_PLUGIN_LIST
|
list(APPEND FLUTTER_PLUGIN_LIST
|
||||||
pdfx
|
pdfx
|
||||||
|
permission_handler_windows
|
||||||
)
|
)
|
||||||
|
|
||||||
list(APPEND FLUTTER_FFI_PLUGIN_LIST
|
list(APPEND FLUTTER_FFI_PLUGIN_LIST
|
||||||
|
|||||||
Reference in New Issue
Block a user