Split Settings into StorageConfig

This commit is contained in:
Vishesh Handa
2021-07-26 21:53:44 +02:00
parent b2e08550bb
commit 7fa67497bb
11 changed files with 156 additions and 97 deletions

View File

@ -8,7 +8,6 @@ import 'package:device_info_plus/device_info_plus.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:easy_localization_loader/easy_localization_loader.dart';
import 'package:flutter_runtime_env/flutter_runtime_env.dart';
import 'package:gitjournal/settings/git_config.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:path_provider/path_provider.dart';
import 'package:provider/provider.dart';
@ -24,8 +23,10 @@ import 'package:gitjournal/iap/iap.dart';
import 'package:gitjournal/repository.dart';
import 'package:gitjournal/repository_manager.dart';
import 'package:gitjournal/settings/app_settings.dart';
import 'package:gitjournal/settings/git_config.dart';
import 'package:gitjournal/settings/markdown_renderer_config.dart';
import 'package:gitjournal/settings/settings.dart';
import 'package:gitjournal/settings/storage_config.dart';
import 'package:gitjournal/themes.dart';
import 'package:gitjournal/utils/logger.dart';
@ -296,7 +297,12 @@ class _JournalAppState extends State<JournalApp> {
var stateContainer = Provider.of<GitJournalRepo>(context);
var settings = Provider.of<Settings>(context);
var appSettings = Provider.of<AppSettings>(context);
var router = AppRouter(settings: settings, appSettings: appSettings);
var storageConfig = Provider.of<StorageConfig>(context);
var router = AppRouter(
settings: settings,
appSettings: appSettings,
storageConfig: storageConfig,
);
/*
const FlexSchemeData customFlexScheme = FlexSchemeData(
@ -375,6 +381,8 @@ class GitJournalChangeNotifiers extends StatelessWidget {
child: Consumer<GitJournalRepo>(
builder: (_, repo, __) => ChangeNotifierProvider<GitConfig>.value(
value: repo.gitConfig,
child: ChangeNotifierProvider<StorageConfig>.value(
value: repo.storageConfig,
child: ChangeNotifierProvider<Settings>.value(
value: repo.settings,
child: Consumer<GitJournalRepo>(
@ -390,6 +398,7 @@ class GitJournalChangeNotifiers extends StatelessWidget {
),
),
),
),
);
return ChangeNotifierProvider.value(

View File

@ -16,6 +16,7 @@ import 'package:gitjournal/screens/tag_listing.dart';
import 'package:gitjournal/settings/app_settings.dart';
import 'package:gitjournal/settings/settings.dart';
import 'package:gitjournal/settings/settings_screen.dart';
import 'package:gitjournal/settings/storage_config.dart';
import 'package:gitjournal/setup/screens.dart';
import 'package:gitjournal/utils/logger.dart';
import 'package:gitjournal/utils/utils.dart';
@ -51,8 +52,13 @@ class AppRoute {
class AppRouter {
final AppSettings appSettings;
final Settings settings;
final StorageConfig storageConfig;
AppRouter({required this.appSettings, required this.settings});
AppRouter({
required this.appSettings,
required this.settings,
required this.storageConfig,
});
String initialRoute() {
var route = '/';
@ -81,7 +87,7 @@ class AppRouter {
pageBuilder: (_, __, ___) => screenForRoute(
route,
repository,
settings,
storageConfig,
sharedText,
sharedImages,
callbackIfUsedShared,
@ -97,7 +103,7 @@ class AppRouter {
builder: (context) => screenForRoute(
route,
repository,
settings,
storageConfig,
sharedText,
sharedImages,
callbackIfUsedShared,
@ -108,7 +114,7 @@ class AppRouter {
Widget? screenForRoute(
String route,
GitJournalRepo repository,
Settings settings,
StorageConfig storageConfig,
String sharedText,
List<String> sharedImages,
Function callbackIfUsedShared,
@ -132,7 +138,7 @@ class AppRouter {
return SignUpScreen();
case AppRoute.SetupRemoteGit:
return GitHostSetupScreen(
repoFolderName: settings.folderName,
repoFolderName: storageConfig.folderName,
remoteName: "origin",
onCompletedFunction: repository.completeGitHostSetup,
);

View File

@ -38,13 +38,17 @@ void main() async {
await repoManager.buildActiveRepository();
var repo = repoManager.currentRepo;
var settings = repo.settings;
var appRouter = AppRouter(settings: settings, appSettings: appSettings);
var storageConfig = repo.storageConfig;
var appRouter = AppRouter(
settings: settings,
appSettings: appSettings,
storageConfig: storageConfig);
for (var routeName in AppRoute.all) {
dashbook.storiesOf(routeName).decorator(CenterDecorator()).add('all',
(context) {
return appRouter.screenForRoute(
routeName, repo, settings, "", [], () {})!;
routeName, repo, storageConfig, "", [], () {})!;
});
}

View File

@ -23,6 +23,7 @@ import 'package:gitjournal/error_reporting.dart';
import 'package:gitjournal/settings/git_config.dart';
import 'package:gitjournal/settings/settings.dart';
import 'package:gitjournal/settings/settings_migrations.dart';
import 'package:gitjournal/settings/storage_config.dart';
import 'package:gitjournal/utils/logger.dart';
enum SyncStatus {
@ -34,8 +35,9 @@ enum SyncStatus {
}
class GitJournalRepo with ChangeNotifier {
final Settings settings;
final StorageConfig storageConfig;
final GitConfig gitConfig;
final Settings settings;
final _opLock = Lock();
final _loadLock = Lock();
@ -71,26 +73,31 @@ class GitJournalRepo with ChangeNotifier {
}) async {
await migrateSettings(id, pref, gitBaseDir);
var storageConfig = StorageConfig(id);
storageConfig.load(pref);
var settings = Settings(id);
settings.load(pref);
logEvent(Event.Settings, parameters: settings.toLoggableMap());
Log.i("Setting ${settings.toLoggableMap()}");
var gitConfig = GitConfig(id);
gitConfig.load(pref);
var repoPath = await settings.buildRepoPath(gitBaseDir);
// logEvent(Event.Settings, parameters: settings.toLoggableMap());
Log.i("StorageConfig ${storageConfig.toLoggableMap()}");
Log.i("GitConfig ${gitConfig.toLoggableMap()}");
Log.i("Settings ${settings.toLoggableMap()}");
var repoPath = await storageConfig.buildRepoPath(gitBaseDir);
Log.i("Loading Repo at path $repoPath");
var repoDir = Directory(repoPath);
if (!repoDir.existsSync()) {
Log.i("Calling GitInit for ${settings.folderName} at: $repoPath");
Log.i("Calling GitInit for ${storageConfig.folderName} at: $repoPath");
await GitRepository.init(repoPath);
settings.save();
storageConfig.save();
}
var valid = await GitRepository.isValidRepo(repoPath);
@ -111,6 +118,7 @@ class GitJournalRepo with ChangeNotifier {
gitBaseDirectory: gitBaseDir,
cacheDir: cacheDir,
remoteGitRepoConfigured: remoteConfigured,
storageConfig: storageConfig,
settings: settings,
gitConfig: gitConfig,
id: id,
@ -123,6 +131,7 @@ class GitJournalRepo with ChangeNotifier {
required this.repoPath,
required this.gitBaseDirectory,
required this.cacheDir,
required this.storageConfig,
required this.settings,
required this.gitConfig,
required this.remoteGitRepoConfigured,
@ -423,8 +432,8 @@ class GitJournalRepo with ChangeNotifier {
remoteGitRepoConfigured = true;
notesFolder.reset(repoPath);
settings.folderName = repoFolderName;
settings.save();
storageConfig.folderName = repoFolderName;
storageConfig.save();
await _persistConfig();
_loadNotes();
@ -435,10 +444,12 @@ class GitJournalRepo with ChangeNotifier {
Future _persistConfig() async {
await settings.save();
await storageConfig.save();
await gitConfig.save();
}
Future<void> moveRepoToPath() async {
var newRepoPath = await settings.buildRepoPath(gitBaseDirectory);
var newRepoPath = await storageConfig.buildRepoPath(gitBaseDirectory);
if (newRepoPath != repoPath) {
Log.i("Old Path: $repoPath");

View File

@ -5,6 +5,7 @@ import 'package:shared_preferences/shared_preferences.dart';
import 'package:gitjournal/repository.dart';
import 'package:gitjournal/settings/settings.dart';
import 'package:gitjournal/settings/storage_config.dart';
import 'package:gitjournal/utils/logger.dart';
class RepositoryManager with ChangeNotifier {

View File

@ -15,13 +15,9 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
import 'dart:io' show Platform;
import 'package:flutter/material.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:icloud_documents_path/icloud_documents_path.dart';
import 'package:path/path.dart' as p;
import 'package:shared_preferences/shared_preferences.dart';
import 'package:uuid/uuid.dart';
@ -31,7 +27,6 @@ import 'package:gitjournal/folder_views/common_types.dart';
import 'settings_sharedpref.dart';
const DEFAULT_ID = "0";
const FOLDER_NAME_KEY = "remoteGitRepoPath";
const SETTINGS_VERSION = 3;
class Settings extends ChangeNotifier with SettingsSharedPref {
@ -40,8 +35,6 @@ class Settings extends ChangeNotifier with SettingsSharedPref {
@override
final String id;
String folderName = "journal";
NoteFileNameFormat noteFileNameFormat = NoteFileNameFormat.Default;
NoteFileNameFormat journalNoteFileNameFormat = NoteFileNameFormat.Default;
@ -86,9 +79,6 @@ class Settings extends ChangeNotifier with SettingsSharedPref {
bool confirmDelete = true;
bool hardWrap = false;
bool storeInternally = true;
String storageLocation = "";
void load(SharedPreferences pref) {
noteFileNameFormat = NoteFileNameFormat.fromInternalString(
getString(pref, "noteFileNameFormat"));
@ -152,12 +142,8 @@ class Settings extends ChangeNotifier with SettingsSharedPref {
getStringSet(pref, "inlineTagPrefixes") ?? inlineTagPrefixes;
// From AppState
folderName = getString(pref, FOLDER_NAME_KEY) ?? folderName;
bottomMenuBar = getBool(pref, "bottomMenuBar") ?? bottomMenuBar;
confirmDelete = getBool(pref, "confirmDelete") ?? confirmDelete;
storeInternally = getBool(pref, "storeInternally") ?? storeInternally;
storageLocation = getString(pref, "storageLocation") ?? "";
hardWrap = getBool(pref, "hardWrap") ?? hardWrap;
}
@ -240,15 +226,9 @@ class Settings extends ChangeNotifier with SettingsSharedPref {
pref, "bottomMenuBar", bottomMenuBar, defaultSet.bottomMenuBar);
await setBool(
pref, "confirmDelete", confirmDelete, defaultSet.confirmDelete);
await setBool(
pref, "storeInternally", storeInternally, defaultSet.storeInternally);
await setString(
pref, "storageLocation", storageLocation, defaultSet.storageLocation);
await setInt(pref, "settingsVersion", version, defaultSet.version);
await setString(pref, FOLDER_NAME_KEY, folderName, defaultSet.folderName);
await setBool(pref, "hardWrap", hardWrap, defaultSet.hardWrap);
notifyListeners();
@ -286,36 +266,10 @@ class Settings extends ChangeNotifier with SettingsSharedPref {
'swipeToDelete': swipeToDelete.toString(),
'inlineTagPrefixes': inlineTagPrefixes.join(' '),
'emojiParser': emojiParser.toString(),
'folderName': folderName.toString(),
'bottomMenuBar': bottomMenuBar.toString(),
'confirmDelete': confirmDelete.toString(),
'storeInternally': storeInternally.toString(),
'storageLocation': storageLocation,
};
}
Future<String> buildRepoPath(String internalDir) async {
if (storeInternally) {
return p.join(internalDir, folderName);
}
if (Platform.isIOS) {
//
// iOS is strange as fuck and it seems if you don't call this function
// asking for the path, you won't be able to access the path
// So even though we have it stored in the settings, this method
// must be called
//
var basePath = await ICloudDocumentsPath.documentsPath;
if (basePath == null) {
// Go back to the normal path
return p.join(storageLocation, folderName);
}
assert(basePath == storageLocation);
return p.join(basePath, folderName);
}
return p.join(storageLocation, folderName);
}
}
class NoteFileNameFormat {

View File

@ -12,6 +12,7 @@ import 'package:gitjournal/repository.dart';
import 'package:gitjournal/settings/git_config.dart';
import 'package:gitjournal/settings/settings.dart';
import 'package:gitjournal/settings/settings_widgets.dart';
import 'package:gitjournal/settings/storage_config.dart';
import 'package:gitjournal/setup/screens.dart';
import 'package:gitjournal/setup/sshkey.dart';
import 'package:gitjournal/ssh/keygen.dart';
@ -227,10 +228,10 @@ class _GitRemoteSettingsScreenState extends State<GitRemoteSettingsScreen> {
}
repoFolderName = repoFolderName + num.toString();
var settings = Provider.of<Settings>(context, listen: false);
settings.folderName = repoFolderName;
settings.storeInternally = true;
await settings.save();
var storageConfig = Provider.of<StorageConfig>(context, listen: false);
storageConfig.folderName = repoFolderName;
storageConfig.storeInternally = true;
await storageConfig.save();
var route = MaterialPageRoute(
builder: (context) => GitHostSetupScreen(

View File

@ -50,6 +50,7 @@ import 'package:gitjournal/settings/settings_misc.dart';
import 'package:gitjournal/settings/settings_note_metadata.dart';
import 'package:gitjournal/settings/settings_tags.dart';
import 'package:gitjournal/settings/settings_widgets.dart';
import 'package:gitjournal/settings/storage_config.dart';
import 'package:gitjournal/utils/logger.dart';
import 'package:gitjournal/utils/utils.dart';
import 'package:gitjournal/widgets/folder_selection_dialog.dart';
@ -89,6 +90,7 @@ class SettingsListState extends State<SettingsList> {
Widget build(BuildContext context) {
var settings = Provider.of<Settings>(context);
var gitConfig = Provider.of<GitConfig>(context);
var storageConfig = Provider.of<StorageConfig>(context);
var appSettings = Provider.of<AppSettings>(context);
final repo = Provider.of<GitJournalRepo>(context);
var repoManager = Provider.of<RepositoryManager>(context);
@ -343,13 +345,13 @@ class SettingsListState extends State<SettingsList> {
if (Platform.isAndroid)
SwitchListTile(
title: Text(tr('settings.storage.external')),
value: !settings.storeInternally,
value: !storageConfig.storeInternally,
onChanged: (bool newVal) async {
Future<void> moveBackToInternal(bool showError) async {
settings.storeInternally = true;
settings.storageLocation = "";
storageConfig.storeInternally = true;
storageConfig.storageLocation = "";
settings.save();
storageConfig.save();
setState(() {});
await repo.moveRepoToPath();
@ -372,10 +374,9 @@ class SettingsListState extends State<SettingsList> {
Log.i("Moving repo to $path");
settings.storeInternally = false;
settings.storageLocation = p.join(path, "GitJournal");
settings.save();
storageConfig.storeInternally = false;
storageConfig.storageLocation = p.join(path, "GitJournal");
storageConfig.save();
setState(() {});
try {
@ -393,22 +394,23 @@ class SettingsListState extends State<SettingsList> {
if (Platform.isAndroid)
ListTile(
title: Text(tr('settings.storage.repoLocation')),
subtitle: Text(p.join(settings.storageLocation, settings.folderName)),
enabled: !settings.storeInternally,
subtitle: Text(
p.join(storageConfig.storageLocation, storageConfig.folderName)),
enabled: !storageConfig.storeInternally,
),
if (Platform.isIOS)
SwitchListTile(
title: Text(tr('settings.storage.icloud')),
value: !settings.storeInternally,
value: !storageConfig.storeInternally,
onChanged: (bool newVal) async {
if (newVal == false) {
settings.storeInternally = true;
settings.storageLocation = "";
storageConfig.storeInternally = true;
storageConfig.storageLocation = "";
} else {
settings.storageLocation =
storageConfig.storageLocation =
(await ICloudDocumentsPath.documentsPath)!;
if (settings.storageLocation.isNotEmpty) {
settings.storeInternally = false;
if (storageConfig.storageLocation.isNotEmpty) {
storageConfig.storeInternally = false;
}
}
settings.save();

View File

@ -0,0 +1,70 @@
import 'dart:io' show Platform;
import 'package:flutter/foundation.dart';
import 'package:icloud_documents_path/icloud_documents_path.dart';
import 'package:path/path.dart' as p;
import 'package:shared_preferences/shared_preferences.dart';
import 'package:gitjournal/settings/settings_sharedpref.dart';
const FOLDER_NAME_KEY = "remoteGitRepoPath";
class StorageConfig extends ChangeNotifier with SettingsSharedPref {
StorageConfig(this.id);
@override
final String id;
var folderName = "journal";
var storeInternally = true;
var storageLocation = "";
void load(SharedPreferences pref) {
folderName = getString(pref, FOLDER_NAME_KEY) ?? folderName;
storeInternally = getBool(pref, "storeInternally") ?? storeInternally;
storageLocation = getString(pref, "storageLocation") ?? "";
}
Future<void> save() async {
var pref = await SharedPreferences.getInstance();
var defaultSet = StorageConfig(id);
await setString(pref, FOLDER_NAME_KEY, folderName, defaultSet.folderName);
await setBool(
pref, "storeInternally", storeInternally, defaultSet.storeInternally);
await setString(
pref, "storageLocation", storageLocation, defaultSet.storageLocation);
}
Map<String, String> toLoggableMap() {
return <String, String>{
'folderName': folderName.toString(),
'storeInternally': storeInternally.toString(),
'storageLocation': storageLocation,
};
}
Future<String> buildRepoPath(String internalDir) async {
if (storeInternally) {
return p.join(internalDir, folderName);
}
if (Platform.isIOS) {
//
// iOS is strange as fuck and it seems if you don't call this function
// asking for the path, you won't be able to access the path
// So even though we have it stored in the settings, this method
// must be called
//
var basePath = await ICloudDocumentsPath.documentsPath;
if (basePath == null) {
// Go back to the normal path
return p.join(storageLocation, folderName);
}
assert(basePath == storageLocation);
return p.join(basePath, folderName);
}
return p.join(storageLocation, folderName);
}
}

View File

@ -17,7 +17,7 @@ import 'package:gitjournal/apis/githost_factory.dart';
import 'package:gitjournal/error_reporting.dart';
import 'package:gitjournal/repository.dart';
import 'package:gitjournal/settings/git_config.dart';
import 'package:gitjournal/settings/settings.dart';
import 'package:gitjournal/settings/storage_config.dart';
import 'package:gitjournal/setup/autoconfigure.dart';
import 'package:gitjournal/setup/button.dart';
import 'package:gitjournal/setup/clone.dart';
@ -594,7 +594,7 @@ class GitHostSetupScreenState extends State<GitHostSetupScreen> {
parameters: _buildOnboardingAnalytics(),
);
var settings = Provider.of<Settings>(context, listen: false);
var storageConfig = Provider.of<StorageConfig>(context, listen: false);
var folderName = folderNameFromCloneUrl(_gitCloneUrl);
if (folderName != widget.repoFolderName) {
var newRepoPath = p.join(basePath, folderName);
@ -608,7 +608,7 @@ class GitHostSetupScreenState extends State<GitHostSetupScreen> {
var repoPath = p.join(basePath, widget.repoFolderName);
Log.i("Renaming $repoPath --> $newRepoPath");
await Directory(repoPath).rename(newRepoPath);
settings.folderName = p.basename(newRepoPath);
storageConfig.folderName = p.basename(newRepoPath);
}
Log.i("calling onComplete $folderName ${widget.remoteName}");

View File

@ -169,7 +169,8 @@ class __CurrentRepoState extends State<_CurrentRepo>
}
var repo = context.watch<GitJournalRepo>();
var repoPath = await repo.settings.buildRepoPath(repo.gitBaseDirectory);
var repoPath =
await repo.storageConfig.buildRepoPath(repo.gitBaseDirectory);
_repoFolderName = p.basename(repoPath);
var remoteConfigs = await repo.remoteConfigs();