From bad1a03812f7c028ad243982455af2dc1ef91d28 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Sat, 17 Oct 2020 12:16:35 +0200 Subject: [PATCH] Implement moving the Git Repo to a folder accessible by other apps This doesn't seem to move it to the SD card, but it does move it to a public location. This is an Android specific feature. Not sure if this is allowed with Android 11 Related to #99 Fixes #154 --- lib/features.dart | 1 - lib/screens/settings_screen.dart | 20 ++++++++++------ lib/state_container.dart | 40 +++++++++++++++++++++++++++++++- 3 files changed, 52 insertions(+), 9 deletions(-) diff --git a/lib/features.dart b/lib/features.dart index 7d8dabef..5d1ca5a4 100644 --- a/lib/features.dart +++ b/lib/features.dart @@ -3,7 +3,6 @@ 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 463189a8..faddcece 100644 --- a/lib/screens/settings_screen.dart +++ b/lib/screens/settings_screen.dart @@ -65,7 +65,8 @@ class SettingsListState extends State { Widget build(BuildContext context) { var settings = Provider.of(context); var appSettings = Provider.of(context); - final appState = Provider.of(context).appState; + final stateContainer = Provider.of(context); + final appState = stateContainer.appState; var saveGitAuthor = (String gitAuthor) { settings.gitAuthor = gitAuthor; @@ -289,7 +290,7 @@ class SettingsListState extends State { Navigator.of(context).push(route); }, ), - if (Platform.isAndroid && Features.externalStorage) + if (Platform.isAndroid) SwitchListTile( title: Text(tr('settings.storage.useLocal')), value: settings.storeInternally, @@ -297,6 +298,10 @@ class SettingsListState extends State { if (newVal == true) { settings.storeInternally = true; settings.storageLocation = ""; + + settings.save(); + setState(() {}); + stateContainer.moveRepoToPath(); } else { var req = await Permission.storage.request(); if (req.isDenied) { @@ -305,6 +310,7 @@ class SettingsListState extends State { settings.save(); setState(() {}); + stateContainer.moveRepoToPath(); return; } settings.storeInternally = true; @@ -321,24 +327,24 @@ class SettingsListState extends State { if (path == null) { settings.storeInternally = false; settings.storageLocation = ""; + settings.save(); setState(() {}); + stateContainer.moveRepoToPath(); return; } settings.storageLocation = p.join(path, "GitJournal"); settings.storeInternally = false; + settings.save(); setState(() {}); + stateContainer.moveRepoToPath(); return; } - settings.save(); - setState(() {}); - - // FIXME: Move the folder to be stored locally! }, ), - if (Platform.isAndroid && Features.externalStorage) + if (Platform.isAndroid) ListTile( title: Text(tr('settings.storage.repoLocation')), subtitle: Text(settings.storageLocation), diff --git a/lib/state_container.dart b/lib/state_container.dart index 73786e53..a8ebe6c4 100644 --- a/lib/state_container.dart +++ b/lib/state_container.dart @@ -35,13 +35,18 @@ class StateContainer with ChangeNotifier { GitNoteRepository _gitRepo; NotesCache _notesCache; + String repoPath; + StateContainer({ @required this.appState, @required this.settings, @required this.gitBaseDirectory, @required this.cacheDirectory, }) { - var repoPath = p.join(gitBaseDirectory, settings.internalRepoFolderName); + var folderName = settings.internalRepoFolderName; + repoPath = settings.storeInternally + ? p.join(gitBaseDirectory, folderName) + : p.join(settings.storageLocation, folderName); _gitRepo = GitNoteRepository(gitDirPath: repoPath, settings: settings); appState.notesFolder = NotesFolderFS(null, _gitRepo.gitDirPath, settings); @@ -351,4 +356,37 @@ class StateContainer with ChangeNotifier { Future _persistConfig() async { await settings.save(); } + + Future moveRepoToPath() async { + var folderName = settings.internalRepoFolderName; + var newRepoPath = settings.storeInternally + ? p.join(gitBaseDirectory, folderName) + : p.join(settings.storageLocation, folderName); + + if (newRepoPath != repoPath) { + Log.i("Old Path: $repoPath"); + Log.i("New Path: $newRepoPath"); + + await Directory(newRepoPath).create(recursive: true); + await _copyDirectory(repoPath, newRepoPath); + await Directory(repoPath).delete(recursive: true); + + repoPath = newRepoPath; + appState.notesFolder.reset(repoPath); + notifyListeners(); + } + } +} + +Future _copyDirectory(String source, String destination) async { + await for (var entity in Directory(source).list(recursive: false)) { + if (entity is Directory) { + var newDirectory = Directory(p.join( + Directory(destination).absolute.path, p.basename(entity.path))); + await newDirectory.create(); + await _copyDirectory(entity.absolute.path, newDirectory.path); + } else if (entity is File) { + await entity.copy(p.join(destination, p.basename(entity.path))); + } + } }