Allow custom creation time of favorite items, add LocalFavoritesManager.existsFolder function.

This commit is contained in:
pkuislm
2024-11-12 19:50:53 +08:00
parent 389403c11d
commit 5825f88e78

View File

@@ -11,8 +11,8 @@ import 'app.dart';
import 'comic_source/comic_source.dart'; import 'comic_source/comic_source.dart';
import 'comic_type.dart'; import 'comic_type.dart';
String _getCurTime() { String _getTimeString(DateTime time) {
return DateTime.now() return time
.toIso8601String() .toIso8601String()
.replaceFirst("T", " ") .replaceFirst("T", " ")
.substring(0, 19); .substring(0, 19);
@@ -27,7 +27,7 @@ class FavoriteItem implements Comic {
@override @override
String id; String id;
String coverPath; String coverPath;
String time = _getCurTime(); late String time;
FavoriteItem({ FavoriteItem({
required this.id, required this.id,
@@ -36,7 +36,11 @@ class FavoriteItem implements Comic {
required this.author, required this.author,
required this.type, required this.type,
required this.tags, required this.tags,
}); DateTime? favoriteTime
}) {
var t = favoriteTime ?? DateTime.now();
time = _getTimeString(t);
}
FavoriteItem.fromRow(Row row) FavoriteItem.fromRow(Row row)
: name = row["name"], : name = row["name"],
@@ -296,12 +300,16 @@ class LocalFavoritesManager with ChangeNotifier {
return res; return res;
} }
bool existsFolder(String name) {
return folderNames.contains(name);
}
/// create a folder /// create a folder
String createFolder(String name, [bool renameWhenInvalidName = false]) { String createFolder(String name, [bool renameWhenInvalidName = false]) {
if (name.isEmpty) { if (name.isEmpty) {
if (renameWhenInvalidName) { if (renameWhenInvalidName) {
int i = 0; int i = 0;
while (folderNames.contains(i.toString())) { while (existsFolder(i.toString())) {
i++; i++;
} }
name = i.toString(); name = i.toString();
@@ -309,11 +317,11 @@ class LocalFavoritesManager with ChangeNotifier {
throw "name is empty!"; throw "name is empty!";
} }
} }
if (folderNames.contains(name)) { if (existsFolder(name)) {
if (renameWhenInvalidName) { if (renameWhenInvalidName) {
var prevName = name; var prevName = name;
int i = 0; int i = 0;
while (folderNames.contains(i.toString())) { while (existsFolder(i.toString())) {
i++; i++;
} }
name = prevName + i.toString(); 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 /// This method will download cover to local, to avoid problems like changing url
void addComic(String folder, FavoriteItem comic, [int? order]) async { void addComic(String folder, FavoriteItem comic, [int? order]) async {
_modifiedAfterLastCache = true; _modifiedAfterLastCache = true;
if (!folderNames.contains(folder)) { if (!existsFolder(folder)) {
throw Exception("Folder does not exists"); throw Exception("Folder does not exists");
} }
var res = _db.select(""" var res = _db.select("""
@@ -431,7 +439,7 @@ class LocalFavoritesManager with ChangeNotifier {
} }
void reorder(List<FavoriteItem> newFolder, String folder) async { void reorder(List<FavoriteItem> newFolder, String folder) async {
if (!folderNames.contains(folder)) { if (!existsFolder(folder)) {
throw Exception("Failed to reorder: folder not found"); throw Exception("Failed to reorder: folder not found");
} }
deleteFolder(folder); deleteFolder(folder);
@@ -443,7 +451,7 @@ class LocalFavoritesManager with ChangeNotifier {
} }
void rename(String before, String after) { void rename(String before, String after) {
if (folderNames.contains(after)) { if (existsFolder(after)) {
throw "Name already exists!"; throw "Name already exists!";
} }
if (after.contains('"')) { if (after.contains('"')) {
@@ -598,9 +606,9 @@ class LocalFavoritesManager with ChangeNotifier {
if (folder == null || folder is! String) { if (folder == null || folder is! String) {
throw "Invalid data"; throw "Invalid data";
} }
if (folderNames.contains(folder)) { if (existsFolder(folder)) {
int i = 0; int i = 0;
while (folderNames.contains("$folder($i)")) { while (existsFolder("$folder($i)")) {
i++; i++;
} }
folder = "$folder($i)"; folder = "$folder($i)";