From cdfa7d690afec5d2b7e45469d03c37a956b73458 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Fri, 9 Oct 2020 10:43:16 +0200 Subject: [PATCH] Add a setting to store the repo in the external storage This is Android only, and for now it is disabled as I'm not sure why I cannot access the "general" root directory. --- android/app/src/main/AndroidManifest.xml | 4 ++ assets/langs/en.yaml | 2 + lib/features.dart | 1 + lib/screens/settings_screen.dart | 61 ++++++++++++++++++++++++ lib/settings.dart | 11 +++++ pubspec.lock | 21 ++++++++ pubspec.yaml | 2 + 7 files changed, 102 insertions(+) diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 549ea852..5949b137 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -5,6 +5,10 @@ + + + + diff --git a/assets/langs/en.yaml b/assets/langs/en.yaml index 0ecb3f82..23f50770 100644 --- a/assets/langs/en.yaml +++ b/assets/langs/en.yaml @@ -134,6 +134,8 @@ settings: storage: title: Storage fileName: Filename + useLocal: Store Repo Internally + repoLocation: Repo Location drawer: title: Sidebar Settings bottomMenuBar: diff --git a/lib/features.dart b/lib/features.dart index b6d93630..f06f608c 100644 --- a/lib/features.dart +++ b/lib/features.dart @@ -3,6 +3,7 @@ import 'package:easy_localization/easy_localization.dart'; class Features { static bool alwaysPro = false; static bool perFolderConfig = false; + static bool externalStorage = false; static final all = [ Feature.basicSearch, diff --git a/lib/screens/settings_screen.dart b/lib/screens/settings_screen.dart index e6d8eb44..c920bb1e 100644 --- a/lib/screens/settings_screen.dart +++ b/lib/screens/settings_screen.dart @@ -1,7 +1,13 @@ +import 'dart:io'; + import 'package:flutter/material.dart'; import 'package:dynamic_theme/dynamic_theme.dart'; import 'package:easy_localization/easy_localization.dart'; +import 'package:filesystem_picker/filesystem_picker.dart'; +import 'package:path/path.dart' as p; +import 'package:path_provider/path_provider.dart'; +import 'package:permission_handler/permission_handler.dart'; import 'package:provider/provider.dart'; import 'package:url_launcher/url_launcher.dart'; @@ -282,6 +288,61 @@ class SettingsListState extends State { Navigator.of(context).push(route); }, ), + if (Platform.isAndroid && Features.externalStorage) + SwitchListTile( + title: Text(tr('settings.storage.useLocal')), + value: settings.storeInternally, + onChanged: (bool newVal) async { + if (newVal == true) { + settings.storeInternally = true; + settings.storageLocation = ""; + } else { + var req = await Permission.storage.request(); + if (req.isDenied) { + settings.storeInternally = false; + settings.storageLocation = ""; + + settings.save(); + setState(() {}); + return; + } + settings.storeInternally = true; + + var root = await getExternalStorageDirectory(); + String path = await FilesystemPicker.open( + title: tr('settings.storage.repoLocation'), + context: context, + rootDirectory: root, + fsType: FilesystemType.folder, + folderIconColor: Colors.green[500], + ); + + if (path == null) { + settings.storeInternally = false; + settings.storageLocation = ""; + settings.save(); + setState(() {}); + return; + } + + settings.storageLocation = p.join(path, "GitJournal"); + settings.storeInternally = false; + settings.save(); + setState(() {}); + return; + } + settings.save(); + setState(() {}); + + // FIXME: Move the folder to be stored locally! + }, + ), + if (Platform.isAndroid && Features.externalStorage) + ListTile( + title: Text(tr('settings.storage.repoLocation')), + subtitle: Text(settings.storageLocation), + enabled: !settings.storeInternally, + ), ListTile( title: Text(tr('settings.misc.title')), onTap: () { diff --git a/lib/settings.dart b/lib/settings.dart index effcaca9..d9c3789c 100644 --- a/lib/settings.dart +++ b/lib/settings.dart @@ -61,6 +61,9 @@ class Settings extends ChangeNotifier { String remoteGitRepoFolderName = ""; bool remoteGitRepoConfigured = false; + bool storeInternally = true; + String storageLocation = ""; + void load(SharedPreferences pref) { gitAuthor = pref.getString("gitAuthor") ?? gitAuthor; gitAuthorEmail = pref.getString("gitAuthorEmail") ?? gitAuthorEmail; @@ -129,6 +132,8 @@ class Settings extends ChangeNotifier { remoteGitRepoFolderName = pref.getString("remoteGitRepoPath") ?? ""; bottomMenuBar = pref.getBool("bottomMenuBar") ?? bottomMenuBar; + storeInternally = pref.getBool("storeInternally") ?? storeInternally; + storageLocation = pref.getString("storageLocation") ?? ""; } Future save() async { @@ -199,6 +204,10 @@ class Settings extends ChangeNotifier { _setStringSet(pref, "inlineTagPrefixes", inlineTagPrefixes, defaultSet.inlineTagPrefixes); _setBool(pref, "bottomMenuBar", bottomMenuBar, defaultSet.bottomMenuBar); + _setBool( + pref, "storeInternally", storeInternally, defaultSet.storeInternally); + _setString( + pref, "storageLocation", storageLocation, defaultSet.storageLocation); pref.setInt("settingsVersion", version); @@ -287,6 +296,8 @@ class Settings extends ChangeNotifier { 'localGitRepoFolderName': localGitRepoFolderName.toString(), 'remoteGitRepoFolderName': remoteGitRepoFolderName.toString(), 'bottomMenuBar': bottomMenuBar.toString(), + 'storeInternally': storeInternally.toString(), + 'storageLocation': storageLocation, }; } diff --git a/pubspec.lock b/pubspec.lock index 04645bb5..35e4d9b9 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -248,6 +248,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.0.7" + filesystem_picker: + dependency: "direct main" + description: + name: filesystem_picker + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.3" fimber: dependency: "direct main" description: @@ -686,6 +693,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.10.0-nullsafety.1" + permission_handler: + dependency: "direct main" + description: + name: permission_handler + url: "https://pub.dartlang.org" + source: hosted + version: "5.0.1+1" + permission_handler_platform_interface: + dependency: transitive + description: + name: permission_handler_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.1" petitparser: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 28c192c0..aeaed48a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -59,6 +59,8 @@ dependencies: multicast_dns: ^0.2.2 bonsoir: ^0.1.2+2 file_picker: ^2.0.7 + filesystem_picker: ^1.0.3 # for directories + permission_handler: ^5.0.1+1 dev_dependencies: flutter_launcher_icons: "^0.7.2"