From 5825f88e78303debe1e041c67d275bf463fcc028 Mon Sep 17 00:00:00 2001 From: pkuislm <69719051+pkuislm@users.noreply.github.com> Date: Tue, 12 Nov 2024 19:50:53 +0800 Subject: [PATCH] Allow custom creation time of favorite items, add LocalFavoritesManager.existsFolder function. --- lib/foundation/favorites.dart | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/lib/foundation/favorites.dart b/lib/foundation/favorites.dart index a2aff14..3482601 100644 --- a/lib/foundation/favorites.dart +++ b/lib/foundation/favorites.dart @@ -11,8 +11,8 @@ import 'app.dart'; import 'comic_source/comic_source.dart'; import 'comic_type.dart'; -String _getCurTime() { - return DateTime.now() +String _getTimeString(DateTime time) { + return time .toIso8601String() .replaceFirst("T", " ") .substring(0, 19); @@ -27,7 +27,7 @@ class FavoriteItem implements Comic { @override String id; String coverPath; - String time = _getCurTime(); + late String time; FavoriteItem({ required this.id, @@ -36,7 +36,11 @@ class FavoriteItem implements Comic { required this.author, required this.type, required this.tags, - }); + DateTime? favoriteTime + }) { + var t = favoriteTime ?? DateTime.now(); + time = _getTimeString(t); + } FavoriteItem.fromRow(Row row) : name = row["name"], @@ -296,12 +300,16 @@ class LocalFavoritesManager with ChangeNotifier { return res; } + bool existsFolder(String name) { + return folderNames.contains(name); + } + /// create a folder String createFolder(String name, [bool renameWhenInvalidName = false]) { if (name.isEmpty) { if (renameWhenInvalidName) { int i = 0; - while (folderNames.contains(i.toString())) { + while (existsFolder(i.toString())) { i++; } name = i.toString(); @@ -309,11 +317,11 @@ class LocalFavoritesManager with ChangeNotifier { throw "name is empty!"; } } - if (folderNames.contains(name)) { + if (existsFolder(name)) { if (renameWhenInvalidName) { var prevName = name; int i = 0; - while (folderNames.contains(i.toString())) { + while (existsFolder(i.toString())) { i++; } name = prevName + i.toString(); @@ -362,7 +370,7 @@ class LocalFavoritesManager with ChangeNotifier { /// This method will download cover to local, to avoid problems like changing url void addComic(String folder, FavoriteItem comic, [int? order]) async { _modifiedAfterLastCache = true; - if (!folderNames.contains(folder)) { + if (!existsFolder(folder)) { throw Exception("Folder does not exists"); } var res = _db.select(""" @@ -431,7 +439,7 @@ class LocalFavoritesManager with ChangeNotifier { } void reorder(List newFolder, String folder) async { - if (!folderNames.contains(folder)) { + if (!existsFolder(folder)) { throw Exception("Failed to reorder: folder not found"); } deleteFolder(folder); @@ -443,7 +451,7 @@ class LocalFavoritesManager with ChangeNotifier { } void rename(String before, String after) { - if (folderNames.contains(after)) { + if (existsFolder(after)) { throw "Name already exists!"; } if (after.contains('"')) { @@ -598,9 +606,9 @@ class LocalFavoritesManager with ChangeNotifier { if (folder == null || folder is! String) { throw "Invalid data"; } - if (folderNames.contains(folder)) { + if (existsFolder(folder)) { int i = 0; - while (folderNames.contains("$folder($i)")) { + while (existsFolder("$folder($i)")) { i++; } folder = "$folder($i)";