add loadNext to search

This commit is contained in:
nyne
2024-10-26 09:41:30 +08:00
parent 897f92f4c9
commit 2a672f9715
3 changed files with 52 additions and 25 deletions

View File

@@ -384,24 +384,18 @@ enum ExplorePageType {
typedef SearchFunction = Future<Res<List<Comic>>> Function(
String keyword, int page, List<String> searchOption);
typedef SearchNextFunction = Future<Res<List<Comic>>> Function(
String keyword, String? next, List<String> searchOption);
class SearchPageData {
/// If this is not null, the default value of search options will be first element.
final List<SearchOptions>? searchOptions;
final Widget Function(BuildContext, List<String> initialValues,
void Function(List<String>))? customOptionsBuilder;
final Widget Function(String keyword, List<String> options)?
overrideSearchResultBuilder;
final SearchFunction? loadPage;
final bool enableLanguageFilter;
final SearchNextFunction? loadNext;
const SearchPageData(this.searchOptions, this.loadPage)
: enableLanguageFilter = false,
customOptionsBuilder = null,
overrideSearchResultBuilder = null;
const SearchPageData(this.searchOptions, this.loadPage, this.loadNext);
}
class SearchOptions {

View File

@@ -536,7 +536,13 @@ class ComicSourceParser {
element['default'] == null ? null : jsonEncode(element['default']),
));
}
return SearchPageData(options, (keyword, page, searchOption) async {
SearchFunction? loadPage;
SearchNextFunction? loadNext;
if (_checkExists('search.load')) {
loadPage = (keyword, page, searchOption) async {
try {
var res = await JsEngine().runCode("""
ComicSource.sources.$_key.search.load(
@@ -550,7 +556,27 @@ class ComicSourceParser {
Log.error("Network", "$e\n$s");
return Res.error(e.toString());
}
});
};
} else {
loadNext = (keyword, next, searchOption) async {
try {
var res = await JsEngine().runCode("""
ComicSource.sources.$_key.search.loadNext(
${jsonEncode(keyword)}, ${jsonEncode(searchOption)}, ${jsonEncode(next)})
""");
return Res(
List.generate(res["comics"].length,
(index) => Comic.fromJson(res["comics"][index], _key!)),
subData: res["next"],
);
} catch (e, s) {
Log.error("Network", "$e\n$s");
return Res.error(e.toString());
}
};
}
return SearchPageData(options, loadPage, loadNext);
}
LoadComicFunc? _parseLoadComicFunc() {

View File

@@ -111,6 +111,7 @@ class _SearchResultPageState extends State<SearchResultPage> {
@override
Widget build(BuildContext context) {
var source = ComicSource.find(sourceKey);
return ComicList(
key: Key(text + options.toString() + sourceKey),
errorLeading: AppSearchBar(
@@ -122,9 +123,15 @@ class _SearchResultPageState extends State<SearchResultPage> {
onChanged: onChanged,
action: buildAction(),
),
loadPage: (i) {
var source = ComicSource.find(sourceKey);
return source!.searchPageData!.loadPage!(
loadPage: source!.searchPageData!.loadPage == null ? null : (i) {
return source.searchPageData!.loadPage!(
text,
i,
options,
);
},
loadNext: source.searchPageData!.loadNext == null ? null : (i) {
return source.searchPageData!.loadNext!(
text,
i,
options,