settings page

This commit is contained in:
nyne
2024-10-11 21:47:50 +08:00
parent f228c7ee17
commit a26e5e20de
21 changed files with 1515 additions and 26 deletions

View File

@@ -1,5 +1,6 @@
import 'dart:convert';
import 'package:path_provider/path_provider.dart';
import 'package:venera/foundation/app.dart';
import 'package:venera/utils/io.dart';
@@ -9,9 +10,9 @@ class _Appdata {
var searchHistory = <String>[];
bool _isSavingData = false;
Future<void> saveData() async {
if(_isSavingData) {
if (_isSavingData) {
await Future.doWhile(() async {
await Future.delayed(const Duration(milliseconds: 20));
return _isSavingData;
@@ -25,11 +26,11 @@ class _Appdata {
}
void addSearchHistory(String keyword) {
if(searchHistory.contains(keyword)) {
if (searchHistory.contains(keyword)) {
searchHistory.remove(keyword);
}
searchHistory.insert(0, keyword);
if(searchHistory.length > 50) {
if (searchHistory.length > 50) {
searchHistory.removeLast();
}
saveData();
@@ -46,13 +47,16 @@ class _Appdata {
}
Future<void> init() async {
var file = File(FilePath.join(App.dataPath, 'appdata.json'));
if(!await file.exists()) {
var file = File(FilePath.join(
(await getApplicationSupportDirectory()).path,
'appdata.json',
));
if (!await file.exists()) {
return;
}
var json = jsonDecode(await file.readAsString());
for(var key in (json['settings'] as Map<String, dynamic>).keys) {
if(json['settings'][key] != null) {
for (var key in (json['settings'] as Map<String, dynamic>).keys) {
if (json['settings'][key] != null) {
settings[key] = json['settings'][key];
}
}
@@ -74,7 +78,7 @@ class _Settings {
final _data = <String, dynamic>{
'comicDisplayMode': 'detailed', // detailed, brief
'comicTileScale': 1.0, // 0.8-1.2
'comicTileScale': 1.00, // 0.75-1.25
'color': 'blue', // red, pink, purple, green, orange, blue
'theme_mode': 'system', // light, dark, system
'newFavoriteAddTo': 'end', // start, end
@@ -91,13 +95,14 @@ class _Settings {
'readerMode': 'galleryLeftToRight', // values of [ReaderMode]
'enableTapToTurnPages': true,
'enablePageAnimation': true,
'language': 'system', // system, zh-CN, zh-TW, en-US
};
operator[](String key) {
operator [](String key) {
return _data[key];
}
operator[]=(String key, dynamic value) {
operator []=(String key, dynamic value) {
_data[key] = value;
}
@@ -105,4 +110,4 @@ class _Settings {
String toString() {
return _data.toString();
}
}
}

View File

@@ -100,6 +100,29 @@ class LocalManager with ChangeNotifier {
late String path;
// return error message if failed
Future<String?> setNewPath(String newPath) async {
var newDir = Directory(newPath);
if(!await newDir.exists()) {
return "Directory does not exist";
}
if(!await newDir.list().isEmpty) {
return "Directory is not empty";
}
try {
await copyDirectory(
Directory(path),
newDir,
);
await File(FilePath.join(App.dataPath, 'local_path')).writeAsString(path);
} catch (e) {
return e.toString();
}
await Directory(path).deleteIgnoreError();
path = newPath;
return null;
}
Future<void> init() async {
_db = sqlite3.open(
'${App.dataPath}/local.db',
@@ -118,18 +141,18 @@ class LocalManager with ChangeNotifier {
PRIMARY KEY (id, comic_type)
);
''');
if (File('${App.dataPath}/local_path').existsSync()) {
path = File('${App.dataPath}/local_path').readAsStringSync();
if (File(FilePath.join(App.dataPath, 'local_path')).existsSync()) {
path = File(FilePath.join(App.dataPath, 'local_path')).readAsStringSync();
} else {
if (App.isAndroid) {
var external = await getExternalStorageDirectories();
if (external != null && external.isNotEmpty) {
path = '${external.first.path}/local';
path = FilePath.join(external.first.path, 'local_path');
} else {
path = '${App.dataPath}/local';
path = FilePath.join(App.dataPath, 'local_path');
}
} else {
path = '${App.dataPath}/local';
path = FilePath.join(App.dataPath, 'local_path');
}
}
if (!Directory(path).existsSync()) {