home page

This commit is contained in:
nyne
2024-09-29 21:49:57 +08:00
parent e8276f243c
commit 932e0c3896
6 changed files with 301 additions and 27 deletions

View File

@@ -144,7 +144,7 @@ class RandomCategoryPartWithRuntimeData extends BaseCategoryPart {
}
CategoryData getCategoryDataWithKey(String key) {
for (var source in ComicSource.sources) {
for (var source in ComicSource._sources) {
if (source.categoryData?.key == key) {
return source.categoryData!;
}

View File

@@ -43,13 +43,31 @@ typedef GetThumbnailLoadingConfigFunc = Map<String, dynamic> Function(
String imageKey)?;
class ComicSource {
static List<ComicSource> sources = [];
static final List<ComicSource> _sources = [];
static final List<Function> _listeners = [];
static void addListener(Function listener) {
_listeners.add(listener);
}
static void removeListener(Function listener) {
_listeners.remove(listener);
}
static void notifyListeners() {
for (var listener in _listeners) {
listener();
}
}
static List<ComicSource> all() => List.from(_sources);
static ComicSource? find(String key) =>
sources.firstWhereOrNull((element) => element.key == key);
_sources.firstWhereOrNull((element) => element.key == key);
static ComicSource? fromIntKey(int key) =>
sources.firstWhereOrNull((element) => element.key.hashCode == key);
_sources.firstWhereOrNull((element) => element.key.hashCode == key);
static Future<void> init() async {
final path = "${App.dataPath}/comic_source";
@@ -62,7 +80,7 @@ class ComicSource {
try {
var source = await ComicSourceParser()
.parse(await entity.readAsString(), entity.absolute.path);
sources.add(source);
_sources.add(source);
} catch (e, s) {
Log.error("ComicSource", "$e\n$s");
}
@@ -71,9 +89,20 @@ class ComicSource {
}
static Future reload() async {
sources.clear();
_sources.clear();
JsEngine().runCode("ComicSource.sources = {};");
await init();
notifyListeners();
}
static void add(ComicSource source) {
_sources.add(source);
notifyListeners();
}
static void remove(String key) {
_sources.removeWhere((element) => element.key == key);
notifyListeners();
}
/// Name of this source.
@@ -123,7 +152,7 @@ class ComicSource {
var data = <String, dynamic>{};
bool get isLogin => data["account"] != null;
bool get isLogged => data["account"] != null;
final String filePath;

View File

@@ -105,7 +105,7 @@ class ComicSourceParser {
throw ComicSourceParseException("minAppVersion $minAppVersion is required");
}
}
for(var source in ComicSource.sources){
for(var source in ComicSource.all()){
if(source.key == key){
throw ComicSourceParseException("key($key) already exists");
}
@@ -188,8 +188,7 @@ class ComicSourceParser {
ComicSource.sources.$_key.account.login(${jsonEncode(account)},
${jsonEncode(pwd)})
""");
var source = ComicSource.sources
.firstWhere((element) => element.key == _key);
var source = ComicSource.find(_key!)!;
source.data["account"] = <String>[account, pwd];
source.saveData();
return const Res(true);
@@ -461,7 +460,7 @@ class ComicSourceParser {
final bool multiFolder = _getValue("favorites.multiFolder");
Future<Res<T>> retryZone<T>(Future<Res<T>> Function() func) async{
if(!ComicSource.find(_key!)!.isLogin){
if(!ComicSource.find(_key!)!.isLogged){
return const Res.error("Not login");
}
var res = await func();

View File

@@ -15,7 +15,7 @@ class ComicType {
if(this == local) {
return null;
} else {
return ComicSource.sources.firstWhere((element) => element.intKey == value);
return ComicSource.fromIntKey(value);
}
}

View File

@@ -22,7 +22,6 @@ import 'package:pointycastle/block/modes/ecb.dart';
import 'package:pointycastle/block/modes/ofb.dart';
import 'package:venera/network/app_dio.dart';
import 'package:venera/network/cookie_jar.dart';
import 'package:venera/utils/ext.dart';
import 'comic_source/comic_source.dart';
import 'consts.dart';
@@ -107,8 +106,7 @@ class JsEngine with _JSEngineApi{
{
String key = message["key"];
String dataKey = message["data_key"];
return ComicSource.sources
.firstWhereOrNull((element) => element.key == key)
return ComicSource.find(key)
?.data[dataKey];
}
case 'save_data':
@@ -116,8 +114,7 @@ class JsEngine with _JSEngineApi{
String key = message["key"];
String dataKey = message["data_key"];
var data = message["data"];
var source = ComicSource.sources
.firstWhere((element) => element.key == key);
var source = ComicSource.find(key)!;
source.data[dataKey] = data;
source.saveData();
}
@@ -125,8 +122,7 @@ class JsEngine with _JSEngineApi{
{
String key = message["key"];
String dataKey = message["data_key"];
var source = ComicSource.sources
.firstWhereOrNull((element) => element.key == key);
var source = ComicSource.find(key);
source?.data.remove(dataKey);
source?.saveData();
}