Improve init

This commit is contained in:
2024-12-22 11:31:57 +08:00
parent 0e22574002
commit 6023e462d7
2 changed files with 65 additions and 36 deletions

View File

@@ -76,15 +76,16 @@ class LocalComic with HistoryMixin implements Comic {
cover, cover,
)); ));
String get baseDir => (directory.contains('/') || directory.contains('\\')) ? directory : FilePath.join(LocalManager().path, directory); String get baseDir => (directory.contains('/') || directory.contains('\\'))
? directory
: FilePath.join(LocalManager().path, directory);
@override @override
String get description => ""; String get description => "";
@override @override
String get sourceKey => comicType == ComicType.local String get sourceKey =>
? "local" comicType == ComicType.local ? "local" : comicType.sourceKey;
: comicType.sourceKey;
@override @override
Map<String, dynamic> toJson() { Map<String, dynamic> toJson() {
@@ -112,11 +113,12 @@ class LocalComic with HistoryMixin implements Comic {
chapters: chapters, chapters: chapters,
initialChapter: history?.ep, initialChapter: history?.ep,
initialPage: history?.page, initialPage: history?.page,
history: history ?? History.fromModel( history: history ??
model: this, History.fromModel(
ep: 0, model: this,
page: 0, ep: 0,
), page: 0,
),
), ),
); );
} }
@@ -176,7 +178,8 @@ class LocalManager with ChangeNotifier {
directory, directory,
newDir, newDir,
); );
await File(FilePath.join(App.dataPath, 'local_path')).writeAsString(newPath); await File(FilePath.join(App.dataPath, 'local_path'))
.writeAsString(newPath);
} catch (e, s) { } catch (e, s) {
Log.error("IO", e, s); Log.error("IO", e, s);
return e.toString(); return e.toString();
@@ -197,7 +200,8 @@ class LocalManager with ChangeNotifier {
} }
} else if (App.isIOS) { } else if (App.isIOS) {
var oldPath = FilePath.join(App.dataPath, 'local'); var oldPath = FilePath.join(App.dataPath, 'local');
if (Directory(oldPath).existsSync() && Directory(oldPath).listSync().isNotEmpty) { if (Directory(oldPath).existsSync() &&
Directory(oldPath).listSync().isNotEmpty) {
return oldPath; return oldPath;
} else { } else {
var directory = await getApplicationDocumentsDirectory(); var directory = await getApplicationDocumentsDirectory();
@@ -208,6 +212,18 @@ class LocalManager with ChangeNotifier {
} }
} }
Future<void> _checkPathValidation() async {
var testFile = File(FilePath.join(path, 'venera_test'));
try {
testFile.createSync();
testFile.deleteSync();
} catch (e) {
Log.error("IO",
"Failed to create test file in local path: $e\nUsing default path instead.");
path = await findDefaultPath();
}
}
Future<void> init() async { Future<void> init() async {
_db = sqlite3.open( _db = sqlite3.open(
'${App.dataPath}/local.db', '${App.dataPath}/local.db',
@@ -239,10 +255,10 @@ class LocalManager with ChangeNotifier {
if (!directory.existsSync()) { if (!directory.existsSync()) {
await directory.create(); await directory.create();
} }
} } catch (e, s) {
catch(e, s) {
Log.error("IO", "Failed to create local folder: $e", s); Log.error("IO", "Failed to create local folder: $e", s);
} }
_checkPathValidation();
_checkNoMedia(); _checkNoMedia();
restoreDownloadingTasks(); restoreDownloadingTasks();
} }
@@ -253,7 +269,8 @@ class LocalManager with ChangeNotifier {
SELECT id FROM comics WHERE comic_type = ? SELECT id FROM comics WHERE comic_type = ?
ORDER BY CAST(id AS INTEGER) DESC ORDER BY CAST(id AS INTEGER) DESC
LIMIT 1; LIMIT 1;
''', [type.value], ''',
[type.value],
); );
if (res.isEmpty) { if (res.isEmpty) {
return '1'; return '1';
@@ -363,15 +380,14 @@ class LocalManager with ChangeNotifier {
} }
Future<List<String>> getImages(String id, ComicType type, Object ep) async { Future<List<String>> getImages(String id, ComicType type, Object ep) async {
if(ep is! String && ep is! int) { if (ep is! String && ep is! int) {
throw "Invalid ep"; throw "Invalid ep";
} }
var comic = find(id, type) ?? (throw "Comic Not Found"); var comic = find(id, type) ?? (throw "Comic Not Found");
var directory = Directory(comic.baseDir); var directory = Directory(comic.baseDir);
if (comic.chapters != null) { if (comic.chapters != null) {
var cid = ep is int var cid =
? comic.chapters!.keys.elementAt(ep - 1) ep is int ? comic.chapters!.keys.elementAt(ep - 1) : (ep as String);
: (ep as String);
directory = Directory(FilePath.join(directory.path, cid)); directory = Directory(FilePath.join(directory.path, cid));
} }
var files = <File>[]; var files = <File>[];
@@ -383,7 +399,7 @@ class LocalManager with ChangeNotifier {
continue; continue;
} }
//Hidden file in some file system //Hidden file in some file system
if(entity.name.startsWith('.')) { if (entity.name.startsWith('.')) {
continue; continue;
} }
files.add(entity); files.add(entity);
@@ -405,7 +421,7 @@ class LocalManager with ChangeNotifier {
if (comic == null) return false; if (comic == null) return false;
if (comic.chapters == null || ep == null) return true; if (comic.chapters == null || ep == null) return true;
return comic.downloadedChapters return comic.downloadedChapters
.contains(comic.chapters!.keys.elementAt(ep-1)); .contains(comic.chapters!.keys.elementAt(ep - 1));
} }
List<DownloadTask> downloadingTasks = []; List<DownloadTask> downloadingTasks = [];
@@ -470,8 +486,7 @@ class LocalManager with ChangeNotifier {
downloadingTasks.add(task); downloadingTasks.add(task);
} }
} }
} } catch (e) {
catch(e) {
file.delete(); file.delete();
Log.error("LocalManager", "Failed to restore downloading tasks: $e"); Log.error("LocalManager", "Failed to restore downloading tasks: $e");
} }
@@ -486,13 +501,13 @@ class LocalManager with ChangeNotifier {
} }
void deleteComic(LocalComic c, [bool removeFileOnDisk = true]) { void deleteComic(LocalComic c, [bool removeFileOnDisk = true]) {
if(removeFileOnDisk) { if (removeFileOnDisk) {
var dir = Directory(FilePath.join(path, c.directory)); var dir = Directory(FilePath.join(path, c.directory));
dir.deleteIgnoreError(recursive: true); dir.deleteIgnoreError(recursive: true);
} }
// Deleting a local comic means that it's nolonger available, thus both favorite and history should be deleted. // Deleting a local comic means that it's nolonger available, thus both favorite and history should be deleted.
if(c.comicType == ComicType.local) { if (c.comicType == ComicType.local) {
if(HistoryManager().findSync(c.id, c.comicType) != null) { if (HistoryManager().findSync(c.id, c.comicType) != null) {
HistoryManager().remove(c.id, c.comicType); HistoryManager().remove(c.id, c.comicType);
} }
var folders = LocalFavoritesManager().find(c.id, c.comicType); var folders = LocalFavoritesManager().find(c.id, c.comicType);
@@ -522,4 +537,4 @@ enum LocalSortType {
} }
return name; return name;
} }
} }

View File

@@ -6,23 +6,37 @@ import 'package:venera/foundation/favorites.dart';
import 'package:venera/foundation/history.dart'; import 'package:venera/foundation/history.dart';
import 'package:venera/foundation/js_engine.dart'; import 'package:venera/foundation/js_engine.dart';
import 'package:venera/foundation/local.dart'; import 'package:venera/foundation/local.dart';
import 'package:venera/foundation/log.dart';
import 'package:venera/network/cookie_jar.dart'; import 'package:venera/network/cookie_jar.dart';
import 'package:venera/utils/tags_translation.dart'; import 'package:venera/utils/tags_translation.dart';
import 'package:venera/utils/translations.dart'; import 'package:venera/utils/translations.dart';
import 'foundation/appdata.dart'; import 'foundation/appdata.dart';
extension FutureInit<T> on Future<T> {
/// Prevent unhandled exception
///
/// A unhandled exception occurred in init() will cause the app to crash.
Future<void> wait() async {
try {
await this;
} catch (e, s) {
Log.error("init", "$e\n$s");
}
}
}
Future<void> init() async { Future<void> init() async {
await SAFTaskWorker().init(); await SAFTaskWorker().init().wait();
await AppTranslation.init(); await AppTranslation.init().wait();
await appdata.init(); await appdata.init().wait();
await App.init(); await App.init().wait();
await HistoryManager().init(); await HistoryManager().init().wait();
await TagsTranslation.readData(); await TagsTranslation.readData().wait();
await LocalFavoritesManager().init(); await LocalFavoritesManager().init().wait();
SingleInstanceCookieJar("${App.dataPath}/cookie.db"); SingleInstanceCookieJar("${App.dataPath}/cookie.db");
await JsEngine().init(); await JsEngine().init().wait();
await ComicSource.init(); await ComicSource.init().wait();
await LocalManager().init(); await LocalManager().init().wait();
CacheManager().setLimitSize(appdata.settings['cacheSize']); CacheManager().setLimitSize(appdata.settings['cacheSize']);
} }