From 7affd0e49da96ac04ad8462a489347482d07a497 Mon Sep 17 00:00:00 2001 From: Julian Mutter Date: Wed, 22 Feb 2023 19:36:23 +0100 Subject: [PATCH] Add folder selection --- lib/main.dart | 38 +++++- lib/sheet.dart | 11 +- macos/Flutter/GeneratedPluginRegistrant.swift | 2 + pubspec.lock | 120 ++++++++++++++++++ pubspec.yaml | 2 + 5 files changed, 166 insertions(+), 7 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 870588b..f2a944f 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,4 +1,6 @@ +import 'package:file_picker/file_picker.dart'; import 'package:flutter/material.dart'; +import 'package:shared_preferences/shared_preferences.dart'; import 'package:sheetless/sheetview.dart'; import 'sheet.dart'; @@ -31,6 +33,40 @@ class MyHomePage extends StatefulWidget { } class _MyHomePageState extends State { + Future getSavedSheetsPath() async { + final prefs = await SharedPreferences.getInstance(); + return prefs.getString("sheets-path"); + } + + Future saveSheetsPath(String path) async { + final prefs = await SharedPreferences.getInstance(); + prefs.setString("sheets-path", path); + } + + Future askForSheetsPath() async { + return await FilePicker.platform.getDirectoryPath(); + } + + Future findAndSaveSheetsPath() async { + String? savedSheetsPath = await getSavedSheetsPath(); + if (savedSheetsPath != null) { + return savedSheetsPath; + } + + String? selectedSheetsPath = await askForSheetsPath(); + if (selectedSheetsPath != null) { + await saveSheetsPath(selectedSheetsPath); + return selectedSheetsPath; + } + + return "No path selected"; + } + + Future> acquireSheets() async { + var sheetsPath = await findAndSaveSheetsPath(); + return loadSheetsSorted(sheetsPath); + } + @override Widget build(BuildContext context) { return Scaffold( @@ -38,7 +74,7 @@ class _MyHomePageState extends State { title: const Text("My Sheets"), ), body: FutureBuilder( - future: loadSheetsSorted(), + future: acquireSheets(), builder: (BuildContext context, AsyncSnapshot> snapshot) { if (snapshot.hasData) { return SheetsWidget( diff --git a/lib/sheet.dart b/lib/sheet.dart index 977280b..f4f4a6f 100644 --- a/lib/sheet.dart +++ b/lib/sheet.dart @@ -11,17 +11,16 @@ class Sheet { Sheet(this.author, this.name, this.path); } -Future> loadSheetsSorted() async { - var sheets = await _loadSheets(); +Future> loadSheetsSorted(String path) async { + var sheets = await _loadSheets(path); sheets.sort((left, right) => left.name.compareTo(right.name)); return sheets; } -Future> _loadSheets() async { - // TODO: Handle directory not found - var dir = Directory("/home/julian/Nextcloud/jhome/Klavier-Noten/"); +Future> _loadSheets(String path) async { + var dir = Directory(path); if (!dir.existsSync()) { - throw Exception("Folder does not exist"); + throw Exception("Specified path '$path' does not exist"); } final List sheets = List.empty(growable: true); await for (final FileSystemEntity x in dir.list()) { diff --git a/macos/Flutter/GeneratedPluginRegistrant.swift b/macos/Flutter/GeneratedPluginRegistrant.swift index 0e67812..9a01812 100644 --- a/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/macos/Flutter/GeneratedPluginRegistrant.swift @@ -7,8 +7,10 @@ import Foundation import device_info_plus_macos import pdfx +import shared_preferences_foundation func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { DeviceInfoPlusMacosPlugin.register(with: registry.registrar(forPlugin: "DeviceInfoPlusMacosPlugin")) PdfxPlugin.register(with: registry.registrar(forPlugin: "PdfxPlugin")) + SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin")) } diff --git a/pubspec.lock b/pubspec.lock index d4ade6a..bb17fb7 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -137,6 +137,14 @@ packages: url: "https://pub.dev" source: hosted version: "6.1.4" + file_picker: + dependency: "direct main" + description: + name: file_picker + sha256: d090ae03df98b0247b82e5928f44d1b959867049d18d73635e2e0bc3f49542b9 + url: "https://pub.dev" + source: hosted + version: "5.2.5" flutter: dependency: "direct main" description: flutter @@ -150,6 +158,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.0.1" + flutter_plugin_android_lifecycle: + dependency: transitive + description: + name: flutter_plugin_android_lifecycle + sha256: "60fc7b78455b94e6de2333d2f95196d32cf5c22f4b0b0520a628804cb463503b" + url: "https://pub.dev" + source: hosted + version: "2.0.7" flutter_test: dependency: "direct dev" description: flutter @@ -208,6 +224,30 @@ packages: url: "https://pub.dev" source: hosted version: "1.8.2" + path_provider_linux: + dependency: transitive + description: + name: path_provider_linux + sha256: "2e32f1640f07caef0d3cb993680f181c79e54a3827b997d5ee221490d131fbd9" + url: "https://pub.dev" + source: hosted + version: "2.1.8" + path_provider_platform_interface: + dependency: transitive + description: + name: path_provider_platform_interface + sha256: f0abc8ebd7253741f05488b4813d936b4d07c6bae3e86148a09e342ee4b08e76 + url: "https://pub.dev" + source: hosted + version: "2.0.5" + path_provider_windows: + dependency: transitive + description: + name: path_provider_windows + sha256: bcabbe399d4042b8ee687e17548d5d3f527255253b4a639f5f8d2094a9c2b45c + url: "https://pub.dev" + source: hosted + version: "2.1.3" pdfx: dependency: "direct main" description: @@ -224,6 +264,14 @@ packages: url: "https://pub.dev" source: hosted version: "0.14.0" + platform: + dependency: transitive + description: + name: platform + sha256: "4a451831508d7d6ca779f7ac6e212b4023dd5a7d08a27a63da33756410e32b76" + url: "https://pub.dev" + source: hosted + version: "3.1.0" plugin_platform_interface: dependency: transitive description: @@ -232,6 +280,70 @@ packages: url: "https://pub.dev" source: hosted version: "2.1.3" + process: + dependency: transitive + description: + name: process + sha256: "53fd8db9cec1d37b0574e12f07520d582019cb6c44abf5479a01505099a34a09" + url: "https://pub.dev" + source: hosted + version: "4.2.4" + shared_preferences: + dependency: "direct main" + description: + name: shared_preferences + sha256: "5949029e70abe87f75cfe59d17bf5c397619c4b74a099b10116baeb34786fad9" + url: "https://pub.dev" + source: hosted + version: "2.0.17" + shared_preferences_android: + dependency: transitive + description: + name: shared_preferences_android + sha256: "955e9736a12ba776bdd261cf030232b30eadfcd9c79b32a3250dd4a494e8c8f7" + url: "https://pub.dev" + source: hosted + version: "2.0.15" + shared_preferences_foundation: + dependency: transitive + description: + name: shared_preferences_foundation + sha256: "2b55c18636a4edc529fa5cd44c03d3f3100c00513f518c5127c951978efcccd0" + url: "https://pub.dev" + source: hosted + version: "2.1.3" + shared_preferences_linux: + dependency: transitive + description: + name: shared_preferences_linux + sha256: f8ea038aa6da37090093974ebdcf4397010605fd2ff65c37a66f9d28394cb874 + url: "https://pub.dev" + source: hosted + version: "2.1.3" + shared_preferences_platform_interface: + dependency: transitive + description: + name: shared_preferences_platform_interface + sha256: da9431745ede5ece47bc26d5d73a9d3c6936ef6945c101a5aca46f62e52c1cf3 + url: "https://pub.dev" + source: hosted + version: "2.1.0" + shared_preferences_web: + dependency: transitive + description: + name: shared_preferences_web + sha256: a4b5bc37fe1b368bbc81f953197d55e12f49d0296e7e412dfe2d2d77d6929958 + url: "https://pub.dev" + source: hosted + version: "2.0.4" + shared_preferences_windows: + dependency: transitive + description: + name: shared_preferences_windows + sha256: "5eaf05ae77658d3521d0e993ede1af962d4b326cd2153d312df716dc250f00c9" + url: "https://pub.dev" + source: hosted + version: "2.1.3" sky_engine: dependency: transitive description: flutter @@ -333,6 +445,14 @@ packages: url: "https://pub.dev" source: hosted version: "3.1.3" + xdg_directories: + dependency: transitive + description: + name: xdg_directories + sha256: ee1505df1426458f7f60aac270645098d318a8b4766d85fde75f76f2e21807d1 + url: "https://pub.dev" + source: hosted + version: "1.0.0" sdks: dart: ">=2.19.2 <3.0.0" flutter: ">=3.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index 4babbff..dac84ea 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -38,6 +38,8 @@ dependencies: path: ^1.8.2 pdfx: ^2.3.0 + file_picker: ^5.2.5 + shared_preferences: ^2.0.17 dev_dependencies: flutter_test: