mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-15 04:43:41 +00:00
feat: migrate shared_preferences to hive (#343)
This commit is contained in:
15 files changed
+247
-143
No files matched your search
@@ -1,33 +1,29 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:macos_secure_bookmarks/macos_secure_bookmarks.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import '../database/database.dart';
|
||||
import 'util.dart';
|
||||
|
||||
class MacSecureUtil {
|
||||
static const _bookmarkKey = "bookmark:";
|
||||
|
||||
static Future<void> saveBookmark(String dir) async {
|
||||
if (!Util.isMacos()) {
|
||||
return;
|
||||
}
|
||||
final secureBookmarks = SecureBookmarks();
|
||||
final bookmark = await secureBookmarks.bookmark(Directory(dir));
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString(_bookmarkKey + dir, bookmark);
|
||||
Database.instance.saveBookmark(MapEntry(dir, bookmark));
|
||||
}
|
||||
|
||||
static Future<void> loadBookmark() async {
|
||||
if (!Util.isMacos()) {
|
||||
return;
|
||||
}
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final keys = prefs.getKeys();
|
||||
final secureBookmarks = SecureBookmarks();
|
||||
keys.where((k) => k.startsWith(_bookmarkKey)).forEach((k) async {
|
||||
final resolvedFile = await secureBookmarks
|
||||
.resolveBookmark(prefs.getString(k)!, isDirectory: true);
|
||||
final bookmark = Database.instance.getBookmark() ?? {};
|
||||
bookmark.forEach((_, v) async {
|
||||
final resolvedFile =
|
||||
await secureBookmarks.resolveBookmark(v, isDirectory: true);
|
||||
await secureBookmarks.startAccessingSecurityScopedResource(resolvedFile);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -50,19 +50,21 @@ class Util {
|
||||
}
|
||||
|
||||
static Future<void> initStorageDir() async {
|
||||
var storageDir = "./";
|
||||
if (Platform.isAndroid) {
|
||||
storageDir = (await getExternalStorageDirectory())?.path ?? storageDir;
|
||||
} else if (Platform.isIOS) {
|
||||
storageDir = (await getLibraryDirectory()).path;
|
||||
} else if (Util.isLinux()) {
|
||||
storageDir = File(Platform.resolvedExecutable).parent.path;
|
||||
// check has write permission, if not, fallback to application support dir
|
||||
try {
|
||||
final testFile = File(path.join(storageDir, ".test"));
|
||||
await testFile.writeAsString("test");
|
||||
await testFile.delete();
|
||||
} catch (e) {
|
||||
var storageDir = "";
|
||||
if (Util.isWindows()) {
|
||||
storageDir = "./";
|
||||
} else if (!Util.isWeb()) {
|
||||
if (Util.isLinux()) {
|
||||
storageDir = File(Platform.resolvedExecutable).parent.path;
|
||||
// check has write permission, if not, fallback to application support dir
|
||||
try {
|
||||
final testFile = File(path.join(storageDir, ".test"));
|
||||
await testFile.writeAsString("test");
|
||||
await testFile.delete();
|
||||
} catch (e) {
|
||||
storageDir = (await getApplicationSupportDirectory()).path;
|
||||
}
|
||||
} else {
|
||||
storageDir = (await getApplicationSupportDirectory()).path;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class WindowState {
|
||||
late bool isMaximized;
|
||||
late double width;
|
||||
late double height;
|
||||
|
||||
WindowState(this.isMaximized, this.width, this.height);
|
||||
}
|
||||
|
||||
const String _windowStateIsMaximized = "window.isMaximized";
|
||||
const String _windowStateWidth = "window.width";
|
||||
const String _windowStateHeight = "window.height";
|
||||
|
||||
/// Save window state to storage.
|
||||
void saveState({bool? isMaximized, double? width, double? height}) async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
if (isMaximized != null) {
|
||||
await prefs.setBool(_windowStateIsMaximized, isMaximized);
|
||||
}
|
||||
if (width != null) {
|
||||
await prefs.setDouble(_windowStateWidth, width);
|
||||
}
|
||||
if (height != null) {
|
||||
await prefs.setDouble(_windowStateHeight, height);
|
||||
}
|
||||
}
|
||||
|
||||
/// Load window state from storage.
|
||||
Future<WindowState> loadState() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
return WindowState(
|
||||
prefs.getBool(_windowStateIsMaximized) ?? false,
|
||||
prefs.getDouble(_windowStateWidth) ?? 800,
|
||||
prefs.getDouble(_windowStateHeight) ?? 600,
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user