code refactor
This commit is contained in:
parent
cbd01a3c17
commit
9df0fe73e1
10 changed files with 200 additions and 203 deletions
|
|
@ -4,13 +4,13 @@ import 'package:go_router/go_router.dart';
|
|||
import 'package:isar/isar.dart';
|
||||
import 'package:mangayomi/main.dart';
|
||||
import 'package:mangayomi/models/source.dart';
|
||||
import 'package:mangayomi/services/fetch_manga_sources.dart';
|
||||
import 'package:mangayomi/modules/more/settings/browse/providers/browse_state_provider.dart';
|
||||
import 'package:mangayomi/providers/l10n_providers.dart';
|
||||
import 'package:mangayomi/providers/storage_provider.dart';
|
||||
import 'package:mangayomi/modules/browse/extension/extension_screen.dart';
|
||||
import 'package:mangayomi/modules/browse/sources/sources_screen.dart';
|
||||
import 'package:mangayomi/modules/library/widgets/search_text_form_field.dart';
|
||||
import 'package:mangayomi/services/fetch_sources_list.dart';
|
||||
|
||||
class BrowseScreen extends ConsumerStatefulWidget {
|
||||
const BrowseScreen({super.key});
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import 'package:mangayomi/services/fetch_anime_sources.dart';
|
|||
import 'package:mangayomi/services/fetch_manga_sources.dart';
|
||||
import 'package:mangayomi/modules/widgets/progress_center.dart';
|
||||
import 'package:mangayomi/providers/l10n_providers.dart';
|
||||
import 'package:mangayomi/services/fetch_sources_list.dart';
|
||||
import 'package:mangayomi/sources/source_test.dart';
|
||||
import 'package:mangayomi/utils/language.dart';
|
||||
import 'package:mangayomi/modules/browse/extension/widgets/extension_list_tile_widget.dart';
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import 'package:mangayomi/models/source.dart';
|
|||
import 'package:mangayomi/services/fetch_anime_sources.dart';
|
||||
import 'package:mangayomi/services/fetch_manga_sources.dart';
|
||||
import 'package:mangayomi/providers/l10n_providers.dart';
|
||||
import 'package:mangayomi/services/fetch_sources_list.dart';
|
||||
import 'package:mangayomi/utils/cached_network.dart';
|
||||
import 'package:mangayomi/utils/extensions/build_context_extensions.dart';
|
||||
import 'package:mangayomi/utils/language.dart';
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ import 'package:mangayomi/modules/more/backup_and_restore/providers/auto_backup.
|
|||
import 'package:mangayomi/modules/more/settings/browse/providers/browse_state_provider.dart';
|
||||
import 'package:mangayomi/providers/l10n_providers.dart';
|
||||
import 'package:mangayomi/router/router.dart';
|
||||
import 'package:mangayomi/services/fetch_sources_list.dart';
|
||||
import 'package:mangayomi/utils/extensions/build_context_extensions.dart';
|
||||
import 'package:mangayomi/modules/library/providers/library_state_provider.dart';
|
||||
import 'package:mangayomi/modules/more/providers/incognito_mode_state_provider.dart';
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@ import 'dart:developer';
|
|||
import 'package:bot_toast/bot_toast.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:mangayomi/services/fetch_manga_sources.dart';
|
||||
import 'package:mangayomi/providers/l10n_providers.dart';
|
||||
import 'package:mangayomi/services/fetch_sources_list.dart';
|
||||
import 'package:mangayomi/services/http/m_client.dart';
|
||||
import 'package:mangayomi/utils/extensions/string_extensions.dart';
|
||||
import 'package:package_info_plus/package_info_plus.dart';
|
||||
|
|
|
|||
|
|
@ -47,12 +47,4 @@ class OkruExtractor {
|
|||
|
||||
return [];
|
||||
}
|
||||
|
||||
String resolveUrl(Uri uri, String url) {
|
||||
return "${Uri(
|
||||
scheme: uri.scheme,
|
||||
host: uri.host,
|
||||
pathSegments: uri.pathSegments.sublist(0, uri.pathSegments.length - 1),
|
||||
).toString()}/$url";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import 'package:mangayomi/services/fetch_manga_sources.dart';
|
||||
import 'package:mangayomi/modules/more/settings/browse/providers/browse_state_provider.dart';
|
||||
import 'package:mangayomi/services/fetch_sources_list.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
part 'fetch_anime_sources.g.dart';
|
||||
|
||||
|
|
|
|||
|
|
@ -1,13 +1,5 @@
|
|||
import 'dart:convert';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:isar/isar.dart';
|
||||
import 'package:mangayomi/eval/dart/service.dart';
|
||||
import 'package:mangayomi/eval/javascript/service.dart';
|
||||
import 'package:mangayomi/main.dart';
|
||||
import 'package:mangayomi/models/source.dart';
|
||||
import 'package:mangayomi/modules/more/settings/browse/providers/browse_state_provider.dart';
|
||||
import 'package:mangayomi/services/http/m_client.dart';
|
||||
import 'package:package_info_plus/package_info_plus.dart';
|
||||
import 'package:mangayomi/services/fetch_sources_list.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
part 'fetch_manga_sources.g.dart';
|
||||
|
||||
|
|
@ -24,185 +16,3 @@ Future fetchMangaSourcesList(FetchMangaSourcesListRef ref,
|
|||
isManga: true);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> fetchSourcesList(
|
||||
{int? id,
|
||||
required bool refresh,
|
||||
required String sourcesIndexUrl,
|
||||
required AutoDisposeRef ref,
|
||||
required bool isManga}) async {
|
||||
final http = MClient.init();
|
||||
final req = await http.get(Uri.parse(sourcesIndexUrl));
|
||||
|
||||
final sourceList =
|
||||
(jsonDecode(req.body) as List).map((e) => Source.fromJson(e)).toList();
|
||||
|
||||
final info = await PackageInfo.fromPlatform();
|
||||
isar.writeTxnSync(() async {
|
||||
for (var source in sourceList) {
|
||||
if (source.appMinVerReq != null) {
|
||||
if (compareVersions(info.version, source.appMinVerReq!) > -1) {
|
||||
if ((source.isManga ?? true) == isManga) {
|
||||
if (id != null) {
|
||||
if (id == source.id) {
|
||||
final sourc = isar.sources.getSync(id)!;
|
||||
final req = await http.get(Uri.parse(source.sourceCodeUrl!));
|
||||
final headers = getSourceHeaders(source..sourceCode = req.body);
|
||||
isar.writeTxnSync(() {
|
||||
isar.sources.putSync(sourc
|
||||
..headers = jsonEncode(headers)
|
||||
..isAdded = true
|
||||
..sourceCode = req.body
|
||||
..sourceCodeUrl = source.sourceCodeUrl
|
||||
..id = id
|
||||
..apiUrl = source.apiUrl
|
||||
..baseUrl = source.baseUrl
|
||||
..dateFormat = source.dateFormat
|
||||
..dateFormatLocale = source.dateFormatLocale
|
||||
..hasCloudflare = source.hasCloudflare
|
||||
..iconUrl = source.iconUrl
|
||||
..typeSource = source.typeSource
|
||||
..lang = source.lang
|
||||
..isNsfw = source.isNsfw
|
||||
..name = source.name
|
||||
..version = source.version
|
||||
..versionLast = source.version
|
||||
..isManga = source.isManga
|
||||
..isFullData = source.isFullData ?? false
|
||||
..appMinVerReq = source.appMinVerReq
|
||||
..sourceCodeLanguage = source.sourceCodeLanguage
|
||||
..additionalParams = source.additionalParams ?? "");
|
||||
});
|
||||
// log("successfully installed or updated");
|
||||
}
|
||||
} else if (isar.sources.getSync(source.id!) != null) {
|
||||
// log("exist");
|
||||
final sourc = isar.sources.getSync(source.id!)!;
|
||||
if (sourc.isAdded!) {
|
||||
if (compareVersions(sourc.version!, source.version!) < 0) {
|
||||
// log("update aivalable auto update");
|
||||
if (ref.watch(autoUpdateExtensionsStateProvider)) {
|
||||
final req =
|
||||
await http.get(Uri.parse(source.sourceCodeUrl!));
|
||||
final headers =
|
||||
getSourceHeaders(source..sourceCode = req.body);
|
||||
isar.writeTxnSync(() {
|
||||
isar.sources.putSync(sourc
|
||||
..headers = jsonEncode(headers)
|
||||
..isAdded = true
|
||||
..sourceCode = req.body
|
||||
..sourceCodeUrl = source.sourceCodeUrl
|
||||
..id = source.id
|
||||
..apiUrl = source.apiUrl
|
||||
..baseUrl = source.baseUrl
|
||||
..dateFormat = source.dateFormat
|
||||
..dateFormatLocale = source.dateFormatLocale
|
||||
..hasCloudflare = source.hasCloudflare
|
||||
..iconUrl = source.iconUrl
|
||||
..typeSource = source.typeSource
|
||||
..lang = source.lang
|
||||
..isNsfw = source.isNsfw
|
||||
..name = source.name
|
||||
..version = source.version
|
||||
..versionLast = source.version
|
||||
..isManga = source.isManga
|
||||
..isFullData = source.isFullData ?? false
|
||||
..appMinVerReq = source.appMinVerReq
|
||||
..sourceCodeLanguage = source.sourceCodeLanguage
|
||||
..additionalParams = source.additionalParams ?? "");
|
||||
});
|
||||
} else {
|
||||
// log("update aivalable");
|
||||
isar.sources.putSync(sourc..versionLast = source.version);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
isar.sources.putSync(Source()
|
||||
..sourceCodeUrl = source.sourceCodeUrl
|
||||
..id = source.id
|
||||
..sourceCode = source.sourceCode
|
||||
..apiUrl = source.apiUrl
|
||||
..baseUrl = source.baseUrl
|
||||
..dateFormat = source.dateFormat
|
||||
..dateFormatLocale = source.dateFormatLocale
|
||||
..hasCloudflare = source.hasCloudflare
|
||||
..iconUrl = source.iconUrl
|
||||
..typeSource = source.typeSource
|
||||
..lang = source.lang
|
||||
..isNsfw = source.isNsfw
|
||||
..name = source.name
|
||||
..version = source.version
|
||||
..versionLast = source.version
|
||||
..isManga = source.isManga
|
||||
..sourceCodeLanguage = source.sourceCodeLanguage
|
||||
..isFullData = source.isFullData ?? false
|
||||
..appMinVerReq = source.appMinVerReq);
|
||||
// log("new source");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
checkIfSourceIsObsolete(sourceList, isManga);
|
||||
}
|
||||
|
||||
void checkIfSourceIsObsolete(List<Source> sourceList, bool isManga) {
|
||||
for (var source in isar.sources
|
||||
.filter()
|
||||
.idIsNotNull()
|
||||
.isMangaEqualTo(isManga)
|
||||
.findAllSync()) {
|
||||
if (sourceList.isNotEmpty && !(source.isLocal ?? false)) {
|
||||
final ids =
|
||||
sourceList.where((e) => e.id != null).map((e) => e.id).toList();
|
||||
if (ids.isNotEmpty) {
|
||||
if (!ids.contains(source.id)) {
|
||||
isar.writeTxnSync(
|
||||
() => isar.sources.putSync(source..isObsolete = true));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int compareVersions(String version1, String version2) {
|
||||
List<String> v1Components = version1.split('.');
|
||||
List<String> v2Components = version2.split('.');
|
||||
|
||||
for (int i = 0; i < v1Components.length && i < v2Components.length; i++) {
|
||||
int v1Value = int.parse(
|
||||
v1Components.length == i + 1 && v1Components[i].length == 1
|
||||
? "${v1Components[i]}0"
|
||||
: v1Components[i]);
|
||||
int v2Value = int.parse(
|
||||
v2Components.length == i + 1 && v2Components[i].length == 1
|
||||
? "${v2Components[i]}0"
|
||||
: v2Components[i]);
|
||||
|
||||
if (v1Value < v2Value) {
|
||||
return -1;
|
||||
} else if (v1Value > v2Value) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (v1Components.length < v2Components.length) {
|
||||
return -1;
|
||||
} else if (v1Components.length > v2Components.length) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Map<String, String> getSourceHeaders(Source source) {
|
||||
Map<String, String> headers = {};
|
||||
if (source.sourceCodeLanguage == SourceCodeLanguage.javascript) {
|
||||
headers = JsExtensionService(source).getHeaders(source.baseUrl ?? "");
|
||||
} else {
|
||||
headers = DartExtensionService(source).getHeaders();
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
|
|
|
|||
192
lib/services/fetch_sources_list.dart
Normal file
192
lib/services/fetch_sources_list.dart
Normal file
|
|
@ -0,0 +1,192 @@
|
|||
import 'dart:convert';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:isar/isar.dart';
|
||||
import 'package:mangayomi/eval/dart/service.dart';
|
||||
import 'package:mangayomi/eval/javascript/service.dart';
|
||||
import 'package:mangayomi/main.dart';
|
||||
import 'package:mangayomi/models/source.dart';
|
||||
import 'package:mangayomi/modules/more/settings/browse/providers/browse_state_provider.dart';
|
||||
import 'package:mangayomi/services/http/m_client.dart';
|
||||
import 'package:package_info_plus/package_info_plus.dart';
|
||||
|
||||
Future<void> fetchSourcesList(
|
||||
{int? id,
|
||||
required bool refresh,
|
||||
required String sourcesIndexUrl,
|
||||
required AutoDisposeRef ref,
|
||||
required bool isManga}) async {
|
||||
final http = MClient.init();
|
||||
final req = await http.get(Uri.parse(sourcesIndexUrl));
|
||||
|
||||
final sourceList =
|
||||
(jsonDecode(req.body) as List).map((e) => Source.fromJson(e)).toList();
|
||||
|
||||
final info = await PackageInfo.fromPlatform();
|
||||
isar.writeTxnSync(() async {
|
||||
for (var source in sourceList) {
|
||||
if (source.appMinVerReq != null) {
|
||||
if (compareVersions(info.version, source.appMinVerReq!) > -1) {
|
||||
if ((source.isManga ?? true) == isManga) {
|
||||
if (id != null) {
|
||||
if (id == source.id) {
|
||||
final sourc = isar.sources.getSync(id)!;
|
||||
final req = await http.get(Uri.parse(source.sourceCodeUrl!));
|
||||
final headers = getSourceHeaders(source..sourceCode = req.body);
|
||||
isar.writeTxnSync(() {
|
||||
isar.sources.putSync(sourc
|
||||
..headers = jsonEncode(headers)
|
||||
..isAdded = true
|
||||
..sourceCode = req.body
|
||||
..sourceCodeUrl = source.sourceCodeUrl
|
||||
..id = id
|
||||
..apiUrl = source.apiUrl
|
||||
..baseUrl = source.baseUrl
|
||||
..dateFormat = source.dateFormat
|
||||
..dateFormatLocale = source.dateFormatLocale
|
||||
..hasCloudflare = source.hasCloudflare
|
||||
..iconUrl = source.iconUrl
|
||||
..typeSource = source.typeSource
|
||||
..lang = source.lang
|
||||
..isNsfw = source.isNsfw
|
||||
..name = source.name
|
||||
..version = source.version
|
||||
..versionLast = source.version
|
||||
..isManga = source.isManga
|
||||
..isFullData = source.isFullData ?? false
|
||||
..appMinVerReq = source.appMinVerReq
|
||||
..sourceCodeLanguage = source.sourceCodeLanguage
|
||||
..additionalParams = source.additionalParams ?? "");
|
||||
});
|
||||
// log("successfully installed or updated");
|
||||
}
|
||||
} else if (isar.sources.getSync(source.id!) != null) {
|
||||
// log("exist");
|
||||
final sourc = isar.sources.getSync(source.id!)!;
|
||||
if (sourc.isAdded!) {
|
||||
if (compareVersions(sourc.version!, source.version!) < 0) {
|
||||
// log("update aivalable auto update");
|
||||
if (ref.watch(autoUpdateExtensionsStateProvider)) {
|
||||
final req =
|
||||
await http.get(Uri.parse(source.sourceCodeUrl!));
|
||||
final headers =
|
||||
getSourceHeaders(source..sourceCode = req.body);
|
||||
isar.writeTxnSync(() {
|
||||
isar.sources.putSync(sourc
|
||||
..headers = jsonEncode(headers)
|
||||
..isAdded = true
|
||||
..sourceCode = req.body
|
||||
..sourceCodeUrl = source.sourceCodeUrl
|
||||
..id = source.id
|
||||
..apiUrl = source.apiUrl
|
||||
..baseUrl = source.baseUrl
|
||||
..dateFormat = source.dateFormat
|
||||
..dateFormatLocale = source.dateFormatLocale
|
||||
..hasCloudflare = source.hasCloudflare
|
||||
..iconUrl = source.iconUrl
|
||||
..typeSource = source.typeSource
|
||||
..lang = source.lang
|
||||
..isNsfw = source.isNsfw
|
||||
..name = source.name
|
||||
..version = source.version
|
||||
..versionLast = source.version
|
||||
..isManga = source.isManga
|
||||
..isFullData = source.isFullData ?? false
|
||||
..appMinVerReq = source.appMinVerReq
|
||||
..sourceCodeLanguage = source.sourceCodeLanguage
|
||||
..additionalParams = source.additionalParams ?? "");
|
||||
});
|
||||
} else {
|
||||
// log("update aivalable");
|
||||
isar.sources.putSync(sourc..versionLast = source.version);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
isar.sources.putSync(Source()
|
||||
..sourceCodeUrl = source.sourceCodeUrl
|
||||
..id = source.id
|
||||
..sourceCode = source.sourceCode
|
||||
..apiUrl = source.apiUrl
|
||||
..baseUrl = source.baseUrl
|
||||
..dateFormat = source.dateFormat
|
||||
..dateFormatLocale = source.dateFormatLocale
|
||||
..hasCloudflare = source.hasCloudflare
|
||||
..iconUrl = source.iconUrl
|
||||
..typeSource = source.typeSource
|
||||
..lang = source.lang
|
||||
..isNsfw = source.isNsfw
|
||||
..name = source.name
|
||||
..version = source.version
|
||||
..versionLast = source.version
|
||||
..isManga = source.isManga
|
||||
..sourceCodeLanguage = source.sourceCodeLanguage
|
||||
..isFullData = source.isFullData ?? false
|
||||
..appMinVerReq = source.appMinVerReq);
|
||||
// log("new source");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
checkIfSourceIsObsolete(sourceList, isManga);
|
||||
}
|
||||
|
||||
void checkIfSourceIsObsolete(List<Source> sourceList, bool isManga) {
|
||||
for (var source in isar.sources
|
||||
.filter()
|
||||
.idIsNotNull()
|
||||
.isMangaEqualTo(isManga)
|
||||
.findAllSync()) {
|
||||
if (sourceList.isNotEmpty && !(source.isLocal ?? false)) {
|
||||
final ids =
|
||||
sourceList.where((e) => e.id != null).map((e) => e.id).toList();
|
||||
if (ids.isNotEmpty) {
|
||||
if (!ids.contains(source.id)) {
|
||||
isar.writeTxnSync(
|
||||
() => isar.sources.putSync(source..isObsolete = true));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int compareVersions(String version1, String version2) {
|
||||
List<String> v1Components = version1.split('.');
|
||||
List<String> v2Components = version2.split('.');
|
||||
|
||||
for (int i = 0; i < v1Components.length && i < v2Components.length; i++) {
|
||||
int v1Value = int.parse(
|
||||
v1Components.length == i + 1 && v1Components[i].length == 1
|
||||
? "${v1Components[i]}0"
|
||||
: v1Components[i]);
|
||||
int v2Value = int.parse(
|
||||
v2Components.length == i + 1 && v2Components[i].length == 1
|
||||
? "${v2Components[i]}0"
|
||||
: v2Components[i]);
|
||||
|
||||
if (v1Value < v2Value) {
|
||||
return -1;
|
||||
} else if (v1Value > v2Value) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (v1Components.length < v2Components.length) {
|
||||
return -1;
|
||||
} else if (v1Components.length > v2Components.length) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Map<String, String> getSourceHeaders(Source source) {
|
||||
Map<String, String> headers = {};
|
||||
if (source.sourceCodeLanguage == SourceCodeLanguage.javascript) {
|
||||
headers = JsExtensionService(source).getHeaders(source.baseUrl ?? "");
|
||||
} else {
|
||||
headers = DartExtensionService(source).getHeaders();
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
import 'dart:convert';
|
||||
import 'package:mangayomi/eval/javascript/http.dart';
|
||||
import 'package:mangayomi/services/fetch_manga_sources.dart';
|
||||
import 'package:mangayomi/services/fetch_sources_list.dart';
|
||||
import 'package:mangayomi/services/http/m_client.dart';
|
||||
import 'package:mangayomi/sources/utils/utils.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
|
|
|
|||
Loading…
Reference in a new issue