mirror of
https://github.com/venera-app/venera.git
synced 2025-09-27 15:57:25 +00:00
search suggestions
This commit is contained in:
@@ -3,9 +3,15 @@ import 'package:venera/components/components.dart';
|
||||
import 'package:venera/foundation/app.dart';
|
||||
import 'package:venera/foundation/appdata.dart';
|
||||
import 'package:venera/foundation/comic_source/comic_source.dart';
|
||||
import 'package:venera/foundation/state_controller.dart';
|
||||
import 'package:venera/pages/search_result_page.dart';
|
||||
import 'package:venera/utils/app_links.dart';
|
||||
import 'package:venera/utils/ext.dart';
|
||||
import 'package:venera/utils/tags_translation.dart';
|
||||
import 'package:venera/utils/translations.dart';
|
||||
|
||||
import 'comic_page.dart';
|
||||
|
||||
class SearchPage extends StatefulWidget {
|
||||
const SearchPage({super.key});
|
||||
|
||||
@@ -25,13 +31,95 @@ class _SearchPageState extends State<SearchPage> {
|
||||
}
|
||||
|
||||
void search([String? text]) {
|
||||
context.to(
|
||||
() => SearchResultPage(
|
||||
text: text ?? controller.text,
|
||||
sourceKey: searchTarget,
|
||||
options: options,
|
||||
),
|
||||
);
|
||||
context
|
||||
.to(
|
||||
() => SearchResultPage(
|
||||
text: text ?? controller.text,
|
||||
sourceKey: searchTarget,
|
||||
options: options,
|
||||
),
|
||||
)
|
||||
.then((_) => update());
|
||||
}
|
||||
|
||||
var suggestions = <Pair<String, TranslationType>>[];
|
||||
|
||||
bool canHandleUrl(String text) {
|
||||
if (!text.isURL) return false;
|
||||
for (var source in ComicSource.all()) {
|
||||
if (source.linkHandler != null) {
|
||||
var uri = Uri.parse(text);
|
||||
if (source.linkHandler!.domains.contains(uri.host)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void findSuggestions() {
|
||||
var text = controller.text.split(" ").last;
|
||||
var suggestions = this.suggestions;
|
||||
|
||||
suggestions.clear();
|
||||
|
||||
if (canHandleUrl(controller.text)) {
|
||||
suggestions.add(Pair("**URL**", TranslationType.other));
|
||||
} else {
|
||||
var text = controller.text;
|
||||
|
||||
for (var comicSource in ComicSource.all()) {
|
||||
if (comicSource.idMatcher?.hasMatch(text) ?? false) {
|
||||
suggestions.add(Pair(
|
||||
"**${comicSource.key}**",
|
||||
TranslationType.other,
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!ComicSource.find(searchTarget)!.enableTagsSuggestions) {
|
||||
update();
|
||||
return;
|
||||
}
|
||||
|
||||
bool check(String text, String key, String value) {
|
||||
if (text.removeAllBlank == "") {
|
||||
return false;
|
||||
}
|
||||
if (key.length >= text.length && key.substring(0, text.length) == text ||
|
||||
(key.contains(" ") &&
|
||||
key.split(" ").last.length >= text.length &&
|
||||
key.split(" ").last.substring(0, text.length) == text)) {
|
||||
return true;
|
||||
} else if (value.length >= text.length && value.contains(text)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void find(Map<String, String> map, TranslationType type) {
|
||||
for (var element in map.entries) {
|
||||
if (suggestions.length > 100) {
|
||||
break;
|
||||
}
|
||||
if (check(text, element.key, element.value)) {
|
||||
suggestions.add(Pair(element.key, type));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
find(TagsTranslation.femaleTags, TranslationType.female);
|
||||
find(TagsTranslation.maleTags, TranslationType.male);
|
||||
find(TagsTranslation.parodyTags, TranslationType.parody);
|
||||
find(TagsTranslation.characterTranslations, TranslationType.character);
|
||||
find(TagsTranslation.otherTags, TranslationType.other);
|
||||
find(TagsTranslation.mixedTags, TranslationType.mixed);
|
||||
find(TagsTranslation.languageTranslations, TranslationType.language);
|
||||
find(TagsTranslation.artistTags, TranslationType.artist);
|
||||
find(TagsTranslation.groupTags, TranslationType.group);
|
||||
find(TagsTranslation.cosplayerTags, TranslationType.cosplayer);
|
||||
update();
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -53,15 +141,27 @@ class _SearchPageState extends State<SearchPage> {
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
body: SmoothCustomScrollView(
|
||||
slivers: [
|
||||
SliverSearchBar(controller: controller),
|
||||
buildSearchTarget(),
|
||||
buildSearchOptions(),
|
||||
],
|
||||
slivers: buildSlivers().toList(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Iterable<Widget> buildSlivers() sync* {
|
||||
yield SliverSearchBar(
|
||||
controller: controller,
|
||||
onChanged: (s) {
|
||||
findSuggestions();
|
||||
},
|
||||
);
|
||||
if (suggestions.isNotEmpty) {
|
||||
yield buildSuggestions(context);
|
||||
} else {
|
||||
yield buildSearchTarget();
|
||||
yield buildSearchOptions();
|
||||
yield buildSearchHistory();
|
||||
}
|
||||
}
|
||||
|
||||
Widget buildSearchTarget() {
|
||||
var sources =
|
||||
ComicSource.all().where((e) => e.searchPageData != null).toList();
|
||||
@@ -148,21 +248,199 @@ class _SearchPageState extends State<SearchPage> {
|
||||
delegate: SliverChildBuilderDelegate(
|
||||
(context, index) {
|
||||
if (index == 0) {
|
||||
return const Divider(
|
||||
thickness: 0.6,
|
||||
).paddingTop(16);
|
||||
}
|
||||
if (index == 1) {
|
||||
return ListTile(
|
||||
leading: const Icon(Icons.history),
|
||||
contentPadding: EdgeInsets.zero,
|
||||
title: Text("Search History".tl),
|
||||
trailing: Flyout(
|
||||
flyoutBuilder: (context) {
|
||||
return FlyoutContent(
|
||||
title: "Clear Search History".tl,
|
||||
actions: [
|
||||
FilledButton(
|
||||
child: Text("Clear".tl),
|
||||
onPressed: () {
|
||||
appdata.clearSearchHistory();
|
||||
context.pop();
|
||||
update();
|
||||
},
|
||||
)
|
||||
],
|
||||
);
|
||||
},
|
||||
child: Builder(
|
||||
builder: (context) {
|
||||
return Tooltip(
|
||||
message: "Clear".tl,
|
||||
child: IconButton(
|
||||
icon: const Icon(Icons.clear_all),
|
||||
onPressed: () {
|
||||
context
|
||||
.findAncestorStateOfType<FlyoutState>()!
|
||||
.show();
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
return ListTile(
|
||||
contentPadding: EdgeInsets.zero,
|
||||
title: Text(appdata.searchHistory[index - 1]),
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 12),
|
||||
title: Text(appdata.searchHistory[index - 2]),
|
||||
onTap: () {
|
||||
search(appdata.searchHistory[index - 1]);
|
||||
search(appdata.searchHistory[index - 2]);
|
||||
},
|
||||
);
|
||||
},
|
||||
childCount: 1 + appdata.searchHistory.length,
|
||||
childCount: 2 + appdata.searchHistory.length,
|
||||
),
|
||||
).sliverPaddingHorizontal(16);
|
||||
}
|
||||
|
||||
Widget buildSuggestions(BuildContext context) {
|
||||
bool check(String text, String key, String value) {
|
||||
if (text.removeAllBlank == "") {
|
||||
return false;
|
||||
}
|
||||
if (key.length >= text.length && key.substring(0, text.length) == text ||
|
||||
(key.contains(" ") &&
|
||||
key.split(" ").last.length >= text.length &&
|
||||
key.split(" ").last.substring(0, text.length) == text)) {
|
||||
return true;
|
||||
} else if (value.length >= text.length && value.contains(text)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void onSelected(String text, TranslationType? type) {
|
||||
var words = controller.text.split(" ");
|
||||
if (words.length >= 2 &&
|
||||
check("${words[words.length - 2]} ${words[words.length - 1]}", text,
|
||||
text.translateTagsToCN)) {
|
||||
controller.text = controller.text.replaceLast(
|
||||
"${words[words.length - 2]} ${words[words.length - 1]}", "");
|
||||
} else {
|
||||
controller.text =
|
||||
controller.text.replaceLast(words[words.length - 1], "");
|
||||
}
|
||||
if (type != null) {
|
||||
controller.text += "${type.name}:$text ";
|
||||
} else {
|
||||
controller.text += "$text ";
|
||||
}
|
||||
update();
|
||||
}
|
||||
|
||||
bool showMethod = MediaQuery.of(context).size.width < 600;
|
||||
bool showTranslation = App.locale.languageCode == "zh";
|
||||
Widget buildItem(Pair<String, TranslationType> value) {
|
||||
if (value.left == "**URL**") {
|
||||
return ListTile(
|
||||
leading: const Icon(Icons.link),
|
||||
title: Text("Open link".tl),
|
||||
subtitle: Text(
|
||||
controller.text,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.fade,
|
||||
),
|
||||
trailing: const Icon(Icons.arrow_right),
|
||||
onTap: () {
|
||||
handleAppLink(Uri.parse(controller.text));
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
if (RegExp(r"^\*\*.*\*\*$").hasMatch(value.left)) {
|
||||
var key = value.left.substring(2, value.left.length - 2);
|
||||
var comicSource = ComicSource.find(key);
|
||||
if (comicSource == null) {
|
||||
return const SizedBox();
|
||||
}
|
||||
return ListTile(
|
||||
leading: const Icon(Icons.link),
|
||||
title: Text("${"Open comic".tl}: ${comicSource.name}"),
|
||||
subtitle: Text(
|
||||
controller.text,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.fade,
|
||||
),
|
||||
trailing: const Icon(Icons.arrow_right),
|
||||
onTap: () {
|
||||
context.to(
|
||||
() => ComicPage(
|
||||
sourceKey: key,
|
||||
id: controller.text,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
var subTitle = TagsTranslation.translationTagWithNamespace(
|
||||
value.left, value.right.name);
|
||||
return ListTile(
|
||||
title: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(value.left),
|
||||
),
|
||||
if (!showMethod)
|
||||
const SizedBox(
|
||||
width: 12,
|
||||
),
|
||||
if (!showMethod && showTranslation)
|
||||
Text(
|
||||
subTitle,
|
||||
style: TextStyle(
|
||||
fontSize: 14, color: Theme.of(context).colorScheme.outline),
|
||||
)
|
||||
],
|
||||
),
|
||||
subtitle: (showMethod && showTranslation) ? Text(subTitle) : null,
|
||||
trailing: Text(
|
||||
value.right.name,
|
||||
style: const TextStyle(fontSize: 13),
|
||||
),
|
||||
onTap: () => onSelected(value.left, value.right),
|
||||
);
|
||||
}
|
||||
|
||||
return SliverMainAxisGroup(
|
||||
slivers: [
|
||||
SliverToBoxAdapter(
|
||||
child: ListTile(
|
||||
leading: const Icon(Icons.hub_outlined),
|
||||
title: Text("Suggestions".tl),
|
||||
trailing: Tooltip(
|
||||
message: "Clear".tl,
|
||||
child: IconButton(
|
||||
icon: const Icon(Icons.clear_all),
|
||||
onPressed: () {
|
||||
suggestions.clear();
|
||||
update();
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
SliverList(
|
||||
delegate: SliverChildBuilderDelegate(
|
||||
(context, index) {
|
||||
return buildItem(suggestions[index]);
|
||||
},
|
||||
childCount: suggestions.length,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user