mirror of
https://github.com/venera-app/venera.git
synced 2025-09-27 07:47:24 +00:00
Improve init. Close #236
This commit is contained in:
@@ -77,10 +77,15 @@ class _App {
|
||||
Future<void> init() async {
|
||||
cachePath = (await getApplicationCacheDirectory()).path;
|
||||
dataPath = (await getApplicationSupportDirectory()).path;
|
||||
await data.init();
|
||||
await history.init();
|
||||
await favorites.init();
|
||||
await local.init();
|
||||
}
|
||||
|
||||
Future<void> initComponents() async {
|
||||
await Future.wait([
|
||||
data.init(),
|
||||
history.init(),
|
||||
favorites.init(),
|
||||
local.init(),
|
||||
]);
|
||||
}
|
||||
|
||||
Function? _forceRebuildHandler;
|
||||
|
@@ -145,7 +145,7 @@ class RandomCategoryPartWithRuntimeData extends BaseCategoryPart {
|
||||
}
|
||||
|
||||
CategoryData getCategoryDataWithKey(String key) {
|
||||
for (var source in ComicSource._sources) {
|
||||
for (var source in ComicSource.all()) {
|
||||
if (source.categoryData?.key == key) {
|
||||
return source.categoryData!;
|
||||
}
|
||||
|
@@ -13,6 +13,7 @@ import 'package:venera/foundation/history.dart';
|
||||
import 'package:venera/foundation/res.dart';
|
||||
import 'package:venera/utils/data_sync.dart';
|
||||
import 'package:venera/utils/ext.dart';
|
||||
import 'package:venera/utils/init.dart';
|
||||
import 'package:venera/utils/io.dart';
|
||||
import 'package:venera/utils/translations.dart';
|
||||
|
||||
@@ -27,81 +28,29 @@ part 'parser.dart';
|
||||
|
||||
part 'models.dart';
|
||||
|
||||
/// build comic list, [Res.subData] should be maxPage or null if there is no limit.
|
||||
typedef ComicListBuilder = Future<Res<List<Comic>>> Function(int page);
|
||||
part 'types.dart';
|
||||
|
||||
/// build comic list with next param, [Res.subData] should be next page param or null if there is no next page.
|
||||
typedef ComicListBuilderWithNext = Future<Res<List<Comic>>> Function(
|
||||
String? next);
|
||||
class ComicSourceManager with ChangeNotifier, Init {
|
||||
final List<ComicSource> _sources = [];
|
||||
|
||||
typedef LoginFunction = Future<Res<bool>> Function(String, String);
|
||||
static ComicSourceManager? _instance;
|
||||
|
||||
typedef LoadComicFunc = Future<Res<ComicDetails>> Function(String id);
|
||||
ComicSourceManager._create();
|
||||
|
||||
typedef LoadComicPagesFunc = Future<Res<List<String>>> Function(
|
||||
String id, String? ep);
|
||||
factory ComicSourceManager() => _instance ??= ComicSourceManager._create();
|
||||
|
||||
typedef CommentsLoader = Future<Res<List<Comment>>> Function(
|
||||
String id, String? subId, int page, String? replyTo);
|
||||
List<ComicSource> all() => List.from(_sources);
|
||||
|
||||
typedef SendCommentFunc = Future<Res<bool>> Function(
|
||||
String id, String? subId, String content, String? replyTo);
|
||||
|
||||
typedef GetImageLoadingConfigFunc = Future<Map<String, dynamic>> Function(
|
||||
String imageKey, String comicId, String epId)?;
|
||||
typedef GetThumbnailLoadingConfigFunc = Map<String, dynamic> Function(
|
||||
String imageKey)?;
|
||||
|
||||
typedef ComicThumbnailLoader = Future<Res<List<String>>> Function(
|
||||
String comicId, String? next);
|
||||
|
||||
typedef LikeOrUnlikeComicFunc = Future<Res<bool>> Function(
|
||||
String comicId, bool isLiking);
|
||||
|
||||
/// [isLiking] is true if the user is liking the comment, false if unliking.
|
||||
/// return the new likes count or null.
|
||||
typedef LikeCommentFunc = Future<Res<int?>> Function(
|
||||
String comicId, String? subId, String commentId, bool isLiking);
|
||||
|
||||
/// [isUp] is true if the user is upvoting the comment, false if downvoting.
|
||||
/// return the new vote count or null.
|
||||
typedef VoteCommentFunc = Future<Res<int?>> Function(
|
||||
String comicId, String? subId, String commentId, bool isUp, bool isCancel);
|
||||
|
||||
typedef HandleClickTagEvent = Map<String, String> Function(
|
||||
String namespace, String tag);
|
||||
|
||||
/// [rating] is the rating value, 0-10. 1 represents 0.5 star.
|
||||
typedef StarRatingFunc = Future<Res<bool>> Function(String comicId, int rating);
|
||||
|
||||
class ComicSource {
|
||||
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) =>
|
||||
ComicSource? find(String key) =>
|
||||
_sources.firstWhereOrNull((element) => element.key == key);
|
||||
|
||||
static ComicSource? fromIntKey(int key) =>
|
||||
ComicSource? fromIntKey(int key) =>
|
||||
_sources.firstWhereOrNull((element) => element.key.hashCode == key);
|
||||
|
||||
static Future<void> init() async {
|
||||
@override
|
||||
@protected
|
||||
Future<void> doInit() async {
|
||||
await JsEngine().ensureInit();
|
||||
final path = "${App.dataPath}/comic_source";
|
||||
if (!(await Directory(path).exists())) {
|
||||
Directory(path).create();
|
||||
@@ -120,26 +69,49 @@ class ComicSource {
|
||||
}
|
||||
}
|
||||
|
||||
static Future reload() async {
|
||||
Future reload() async {
|
||||
_sources.clear();
|
||||
JsEngine().runCode("ComicSource.sources = {};");
|
||||
await init();
|
||||
await doInit();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
static void add(ComicSource source) {
|
||||
void add(ComicSource source) {
|
||||
_sources.add(source);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
static void remove(String key) {
|
||||
void remove(String key) {
|
||||
_sources.removeWhere((element) => element.key == key);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
static final availableUpdates = <String, String>{};
|
||||
bool get isEmpty => _sources.isEmpty;
|
||||
|
||||
static bool get isEmpty => _sources.isEmpty;
|
||||
/// Key is the source key, value is the version.
|
||||
final _availableUpdates = <String, String>{};
|
||||
|
||||
void updateAvailableUpdates(Map<String, String> updates) {
|
||||
_availableUpdates.addAll(updates);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Map<String, String> get availableUpdates => Map.from(_availableUpdates);
|
||||
|
||||
void notifyStateChange() {
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
class ComicSource {
|
||||
static List<ComicSource> all() => ComicSourceManager().all();
|
||||
|
||||
static ComicSource? find(String key) => ComicSourceManager().find(key);
|
||||
|
||||
static ComicSource? fromIntKey(int key) =>
|
||||
ComicSourceManager().fromIntKey(key);
|
||||
|
||||
static bool get isEmpty => ComicSourceManager().isEmpty;
|
||||
|
||||
/// Name of this source.
|
||||
final String name;
|
||||
@@ -321,7 +293,7 @@ class AccountConfig {
|
||||
this.onLoginWithWebviewSuccess,
|
||||
this.cookieFields,
|
||||
this.validateCookies,
|
||||
) : infoItems = const [];
|
||||
) : infoItems = const [];
|
||||
}
|
||||
|
||||
class AccountInfoItem {
|
||||
@@ -478,4 +450,4 @@ class ArchiveDownloader {
|
||||
final Future<Res<String>> Function(String cid, String aid) getDownloadUrl;
|
||||
|
||||
const ArchiveDownloader(this.getArchives, this.getDownloadUrl);
|
||||
}
|
||||
}
|
||||
|
48
lib/foundation/comic_source/types.dart
Normal file
48
lib/foundation/comic_source/types.dart
Normal file
@@ -0,0 +1,48 @@
|
||||
part of 'comic_source.dart';
|
||||
|
||||
/// build comic list, [Res.subData] should be maxPage or null if there is no limit.
|
||||
typedef ComicListBuilder = Future<Res<List<Comic>>> Function(int page);
|
||||
|
||||
/// build comic list with next param, [Res.subData] should be next page param or null if there is no next page.
|
||||
typedef ComicListBuilderWithNext = Future<Res<List<Comic>>> Function(
|
||||
String? next);
|
||||
|
||||
typedef LoginFunction = Future<Res<bool>> Function(String, String);
|
||||
|
||||
typedef LoadComicFunc = Future<Res<ComicDetails>> Function(String id);
|
||||
|
||||
typedef LoadComicPagesFunc = Future<Res<List<String>>> Function(
|
||||
String id, String? ep);
|
||||
|
||||
typedef CommentsLoader = Future<Res<List<Comment>>> Function(
|
||||
String id, String? subId, int page, String? replyTo);
|
||||
|
||||
typedef SendCommentFunc = Future<Res<bool>> Function(
|
||||
String id, String? subId, String content, String? replyTo);
|
||||
|
||||
typedef GetImageLoadingConfigFunc = Future<Map<String, dynamic>> Function(
|
||||
String imageKey, String comicId, String epId)?;
|
||||
typedef GetThumbnailLoadingConfigFunc = Map<String, dynamic> Function(
|
||||
String imageKey)?;
|
||||
|
||||
typedef ComicThumbnailLoader = Future<Res<List<String>>> Function(
|
||||
String comicId, String? next);
|
||||
|
||||
typedef LikeOrUnlikeComicFunc = Future<Res<bool>> Function(
|
||||
String comicId, bool isLiking);
|
||||
|
||||
/// [isLiking] is true if the user is liking the comment, false if unliking.
|
||||
/// return the new likes count or null.
|
||||
typedef LikeCommentFunc = Future<Res<int?>> Function(
|
||||
String comicId, String? subId, String commentId, bool isLiking);
|
||||
|
||||
/// [isUp] is true if the user is upvoting the comment, false if downvoting.
|
||||
/// return the new vote count or null.
|
||||
typedef VoteCommentFunc = Future<Res<int?>> Function(
|
||||
String comicId, String? subId, String commentId, bool isUp, bool isCancel);
|
||||
|
||||
typedef HandleClickTagEvent = Map<String, String> Function(
|
||||
String namespace, String tag);
|
||||
|
||||
/// [rating] is the rating value, 0-10. 1 represents 0.5 star.
|
||||
typedef StarRatingFunc = Future<Res<bool>> Function(String comicId, int rating);
|
@@ -3,6 +3,7 @@ import 'dart:io';
|
||||
import 'dart:math' as math;
|
||||
import 'package:crypto/crypto.dart';
|
||||
import 'package:dio/io.dart';
|
||||
import 'package:flutter/foundation.dart' show protected;
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:html/parser.dart' as html;
|
||||
import 'package:html/dom.dart' as dom;
|
||||
@@ -24,6 +25,7 @@ import 'package:venera/components/js_ui.dart';
|
||||
import 'package:venera/foundation/app.dart';
|
||||
import 'package:venera/network/app_dio.dart';
|
||||
import 'package:venera/network/cookie_jar.dart';
|
||||
import 'package:venera/utils/init.dart';
|
||||
|
||||
import 'comic_source/comic_source.dart';
|
||||
import 'consts.dart';
|
||||
@@ -40,7 +42,7 @@ class JavaScriptRuntimeException implements Exception {
|
||||
}
|
||||
}
|
||||
|
||||
class JsEngine with _JSEngineApi, JsUiApi {
|
||||
class JsEngine with _JSEngineApi, JsUiApi, Init {
|
||||
factory JsEngine() => _cache ?? (_cache = JsEngine._create());
|
||||
|
||||
static JsEngine? _cache;
|
||||
@@ -64,7 +66,9 @@ class JsEngine with _JSEngineApi, JsUiApi {
|
||||
responseType: ResponseType.plain, validateStatus: (status) => true));
|
||||
}
|
||||
|
||||
Future<void> init() async {
|
||||
@override
|
||||
@protected
|
||||
Future<void> doInit() async {
|
||||
if (!_closed) {
|
||||
return;
|
||||
}
|
||||
|
@@ -265,6 +265,7 @@ class LocalManager with ChangeNotifier {
|
||||
}
|
||||
_checkPathValidation();
|
||||
_checkNoMedia();
|
||||
await ComicSourceManager().ensureInit();
|
||||
restoreDownloadingTasks();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user