Files
GitJournal/lib/settings/storage_config.dart
Vishesh Handa 2505d00dfb Replace dart:io with universal_io
Platform from dart:io cannot be used on the web for reasons. And this
way we get a fake File/Directory class which we can use for atleast
running the web version, even if it won't work.
2021-07-30 11:39:26 +02:00

72 lines
2.1 KiB
Dart

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:universal_io/io.dart' show Platform;
import 'package:gitjournal/settings/settings_sharedpref.dart';
const FOLDER_NAME_KEY = "remoteGitRepoPath";
class StorageConfig extends ChangeNotifier with SettingsSharedPref {
StorageConfig(this.id, this.pref);
@override
final String id;
@override
final SharedPreferences pref;
var folderName = "journal";
var storeInternally = true;
var storageLocation = "";
void load() {
folderName = getString(FOLDER_NAME_KEY) ?? folderName;
storeInternally = getBool("storeInternally") ?? storeInternally;
storageLocation = getString("storageLocation") ?? "";
}
Future<void> save() async {
var def = StorageConfig(id, pref);
await setString(FOLDER_NAME_KEY, folderName, def.folderName);
await setBool("storeInternally", storeInternally, def.storeInternally);
await setString("storageLocation", storageLocation, def.storageLocation);
notifyListeners();
}
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);
}
}