io utils; single favorite folder exporting and importing

This commit is contained in:
nyne
2024-10-28 15:59:17 +08:00
parent 174b7d4135
commit 69befb9a84
20 changed files with 276 additions and 46 deletions

View File

@@ -1,5 +1,8 @@
import 'dart:convert';
import 'package:sqlite3/sqlite3.dart';
import 'package:venera/foundation/appdata.dart';
import 'package:venera/foundation/log.dart';
import 'dart:io';
import 'app.dart';
@@ -92,7 +95,39 @@ class FavoriteItem implements Comic {
@override
Map<String, dynamic> toJson() {
throw UnimplementedError();
return {
"name": name,
"author": author,
"type": type.value,
"tags": tags,
"id": id,
"coverPath": coverPath,
};
}
static FavoriteItem fromJson(Map<String, dynamic> json) {
var type = json["type"] as int;
if(type == 0 && json['coverPath'].toString().startsWith('http')) {
type = 'picacg'.hashCode;
} else if(type == 1) {
type = 'ehentai'.hashCode;
} else if(type == 2) {
type = 'jm'.hashCode;
} else if(type == 3) {
type = 'hitomi'.hashCode;
} else if(type == 4) {
type = 'wnacg'.hashCode;
} else if(type == 6) {
type = 'nhentai'.hashCode;
}
return FavoriteItem(
id: json["id"] ?? json['target'],
name: json["name"],
author: json["author"],
coverPath: json["coverPath"],
type: ComicType(type),
tags: List<String>.from(json["tags"] ?? []),
);
}
}
@@ -525,4 +560,39 @@ class LocalFavoritesManager {
comic.type.value
]);
}
String folderToJson(String folder) {
var res = _db.select("""
select * from "$folder";
""");
return jsonEncode({
"info": "Generated by Venera",
"name": folder,
"comics": res.map((e) => FavoriteItem.fromRow(e).toJson()).toList(),
});
}
void fromJson(String json) {
var data = jsonDecode(json);
var folder = data["name"];
if(folder == null || folder is! String) {
throw "Invalid data";
}
if (folderNames.contains(folder)) {
int i = 0;
while (folderNames.contains("$folder($i)")) {
i++;
}
folder = "$folder($i)";
}
createFolder(folder);
for (var comic in data["comics"]) {
try {
addComic(folder, FavoriteItem.fromJson(comic));
}
catch(e) {
Log.error("Import Data", e.toString());
}
}
}
}