Skip to content

基于自定义规则的番剧采集APP,支持流媒体在线观看,支持弹幕,支持实时超分辨率。

feat: search history

Prediditb79483d921e0
diff --git a/lib/modules/search/search_history_module.dart b/lib/modules/search/search_history_module.dart
new file mode 100644
index 00000000..e58741ae
--- /dev/null
+++ b/lib/modules/search/search_history_module.dart
@@ -0,0 +1,21 @@
+import 'package:hive/hive.dart';
+
+part 'search_history_module.g.dart';
+
+@HiveType(typeId: 6)
+class SearchHistory {
+ @HiveField(0)
+ String keyword;
+
+ @HiveField(1)
+ int timestamp;
+
+ SearchHistory(this.keyword, this.timestamp);
+
+ String get key => timestamp.toString();
+
+ @override
+ String toString() {
+ return 'Search keyword: $keyword, search time: ${DateTime.fromMillisecondsSinceEpoch(timestamp)}';
+ }
+}
\ No newline at end of file
diff --git a/lib/modules/search/search_history_module.g.dart b/lib/modules/search/search_history_module.g.dart
new file mode 100644
index 00000000..71e36969
--- /dev/null
+++ b/lib/modules/search/search_history_module.g.dart
@@ -0,0 +1,44 @@
+// GENERATED CODE - DO NOT MODIFY BY HAND
+
+part of 'search_history_module.dart';
+
+// **************************************************************************
+// TypeAdapterGenerator
+// **************************************************************************
+
+class SearchHistoryAdapter extends TypeAdapter<SearchHistory> {
+ @override
+ final int typeId = 6;
+
+ @override
+ SearchHistory read(BinaryReader reader) {
+ final numOfFields = reader.readByte();
+ final fields = <int, dynamic>{
+ for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
+ };
+ return SearchHistory(
+ fields[0] as String,
+ fields[1] as int,
+ );
+ }
+
+ @override
+ void write(BinaryWriter writer, SearchHistory obj) {
+ writer
+ ..writeByte(2)
+ ..writeByte(0)
+ ..write(obj.keyword)
+ ..writeByte(1)
+ ..write(obj.timestamp);
+ }
+
+ @override
+ int get hashCode => typeId.hashCode;
+
+ @override
+ bool operator ==(Object other) =>
+ identical(this, other) ||
+ other is SearchHistoryAdapter &&
+ runtimeType == other.runtimeType &&
+ typeId == other.typeId;
+}
diff --git a/lib/pages/search/search_controller.dart b/lib/pages/search/search_controller.dart
index 612befe8..8057bb4d 100644
--- a/lib/pages/search/search_controller.dart
+++ b/lib/pages/search/search_controller.dart
@@ -2,12 +2,18 @@ import 'package:mobx/mobx.dart';
import 'package:kazumi/modules/bangumi/bangumi_item.dart';
import 'package:kazumi/request/bangumi.dart';
import 'package:kazumi/utils/search_parser.dart';
+import 'package:kazumi/utils/storage.dart';
+import 'package:kazumi/modules/search/search_history_module.dart';
+import 'package:hive/hive.dart';
part 'search_controller.g.dart';
class SearchPageController = _SearchPageController with _$SearchPageController;
abstract class _SearchPageController with Store {
+ final Box setting = GStorage.setting;
+ final Box searchHistoryBox = GStorage.searchHistory;
+
@observable
bool isLoading = false;
@@ -17,6 +23,19 @@ abstract class _SearchPageController with Store {
@observable
ObservableList<BangumiItem> bangumiList = ObservableList.of([]);
+ @observable
+ ObservableList<SearchHistory> searchHistories = ObservableList.of([]);
+
+ @action
+ void loadSearchHistories() {
+ var temp = searchHistoryBox.values.toList().cast<SearchHistory>();
+ temp.sort(
+ (a, b) => b.timestamp - a.timestamp,
+ );
+ searchHistories.clear();
+ searchHistories.addAll(temp);
+ }
+
/// Avaliable sort parameters:
/// 1. heat
/// 2. match
@@ -28,9 +47,28 @@ abstract class _SearchPageController with Store {
return newInput;
}
+ @action
Future<void> searchBangumi(String input, {String type = 'add'}) async {
if (type != 'add') {
bangumiList.clear();
+ bool privateMode =
+ await setting.get(SettingBoxKey.privateMode, defaultValue: false);
+ if (!privateMode) {
+ if (searchHistories.length >= 10) {
+ await searchHistoryBox.delete(searchHistories.last.key);
+ }
+ final historiesToDelete =
+ searchHistories.where((element) => element.keyword == input);
+ if (historiesToDelete.isNotEmpty) {
+ for (var history in historiesToDelete) {
+ await searchHistoryBox.delete(history.key);
+ }
+ }
+ await searchHistoryBox.put(
+ DateTime.now().millisecondsSinceEpoch.toString(),
+ SearchHistory(input, DateTime.now().millisecondsSinceEpoch));
+ loadSearchHistories();
+ }
}
isLoading = true;
isTimeOut = false;
@@ -49,10 +87,24 @@ abstract class _SearchPageController with Store {
return;
}
}
- var result =
- await BangumiHTTP.bangumiSearch(keywords, tags: [if (tag != null) tag], offset: bangumiList.length, sort: sort ?? 'heat');
+ var result = await BangumiHTTP.bangumiSearch(keywords,
+ tags: [if (tag != null) tag],
+ offset: bangumiList.length,
+ sort: sort ?? 'heat');
bangumiList.addAll(result);
isLoading = false;
isTimeOut = bangumiList.isEmpty;
}
+
+ @action
+ Future<void> deleteSearchHistory(SearchHistory history) async {
+ await searchHistoryBox.delete(history.key);
+ loadSearchHistories();
+ }
+
+ @action
+ Future<void> clearSearchHistory() async {
+ await searchHistoryBox.clear();
+ loadSearchHistories();
+ }
}
diff --git a/lib/pages/search/search_controller.g.dart b/lib/pages/search/search_controller.g.dart
index ad5c97f9..1e52028f 100644
--- a/lib/pages/search/search_controller.g.dart
+++ b/lib/pages/search/search_controller.g.dart
@@ -57,12 +57,71 @@ mixin _$SearchPageController on _SearchPageController, Store {
});
}
+ late final _$searchHistoriesAtom =
+ Atom(name: '_SearchPageController.searchHistories', context: context);
+
+ @override
+ ObservableList<SearchHistory> get searchHistories {
+ _$searchHistoriesAtom.reportRead();
+ return super.searchHistories;
+ }
+
+ @override
+ set searchHistories(ObservableList<SearchHistory> value) {
+ _$searchHistoriesAtom.reportWrite(value, super.searchHistories, () {
+ super.searchHistories = value;
+ });
+ }
+
+ late final _$searchBangumiAsyncAction =
+ AsyncAction('_SearchPageController.searchBangumi', context: context);
+
+ @override
+ Future<void> searchBangumi(String input, {String type = 'add'}) {
+ return _$searchBangumiAsyncAction
+ .run(() => super.searchBangumi(input, type: type));
+ }
+
+ late final _$deleteSearchHistoryAsyncAction = AsyncAction(
+ '_SearchPageController.deleteSearchHistory',
+ context: context);
+
+ @override
+ Future<void> deleteSearchHistory(SearchHistory history) {
+ return _$deleteSearchHistoryAsyncAction
+ .run(() => super.deleteSearchHistory(history));
+ }
+
+ late final _$clearSearchHistoryAsyncAction =
+ AsyncAction('_SearchPageController.clearSearchHistory', context: context);
+
+ @override
+ Future<void> clearSearchHistory() {
+ return _$clearSearchHistoryAsyncAction
+ .run(() => super.clearSearchHistory());
+ }
+
+ late final _$_SearchPageControllerActionController =
+ ActionController(name: '_SearchPageController', context: context);
+
+ @override
+ void loadSearchHistories() {
+ final _$actionInfo = _$_SearchPageControllerActionController.startAction(
+ name: '_SearchPageController.loadSearchHistories');
+ try {
+ return super.loadSearchHistories();
+ } finally {
+ _$_SearchPageControllerActionController.endAction(_$actionInfo);
+ }
+ }
+
@override
String toString() {
return '''
isLoading: ${isLoading},
isTimeOut: ${isTimeOut},
-bangumiList: ${bangumiList}
+bangumiList: ${bangumiList},
+searchHistories: ${searchHistories}
''';
}
}
diff --git a/lib/pages/search/search_page.dart b/lib/pages/search/search_page.dart
index ea524104..29f6609e 100644
--- a/lib/pages/search/search_page.dart
+++ b/lib/pages/search/search_page.dart
@@ -27,6 +27,7 @@ class _SearchPageState extends State<SearchPage> {
void initState() {
super.initState();
scrollController.addListener(scrollListener);
+ searchPageController.loadSearchHistories();
}
@override
@@ -136,11 +137,42 @@ class _SearchPageState extends State<SearchPage> {
),
isFullScreen: MediaQuery.sizeOf(context).width <
LayoutBreakpoint.compact['width']!,
- suggestionsBuilder: (context, controller) => <Widget>[
- Container(
- height: 400,
- alignment: Alignment.center,
- child: Text("无可用搜索建议,回车以直接检索"),
+ suggestionsBuilder: (context, controller) => [
+ Observer(
+ builder: (context) {
+ if (controller.text.isNotEmpty) {
+ return Container(
+ height: 400,
+ alignment: Alignment.center,
+ child: Text("无可用搜索建议,回车以直接检索"),
+ );
+ } else {
+ return Column(
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ for (var history
+ in searchPageController.searchHistories.take(10))
+ ListTile(
+ title: Text(history.keyword),
+ onTap: () {
+ controller.text = history.keyword;
+ searchPageController.searchBangumi(controller.text,
+ type: 'init');
+ if (searchController.isOpen) {
+ searchController.closeView(history.keyword);
+ }
+ },
+ trailing: IconButton(
+ icon: const Icon(Icons.close),
+ onPressed: () {
+ searchPageController.deleteSearchHistory(history);
+ },
+ ),
+ ),
+ ],
+ );
+ }
+ },
),
],
onSubmitted: (value) {
diff --git a/lib/utils/storage.dart b/lib/utils/storage.dart
index 09be9dc6..19dde609 100644
--- a/lib/utils/storage.dart
+++ b/lib/utils/storage.dart
@@ -7,15 +7,17 @@ import 'package:kazumi/modules/bangumi/bangumi_tag.dart';
import 'package:kazumi/modules/history/history_module.dart';
import 'package:kazumi/modules/collect/collect_module.dart';
import 'package:kazumi/modules/collect/collect_change_module.dart';
+import 'package:kazumi/modules/search/search_history_module.dart';
class GStorage {
- // Don't use favorites box, it's replaced by collectibles.
+ /// Don't use favorites box, it's replaced by collectibles.
static late Box<BangumiItem> favorites;
static late Box<CollectedBangumi> collectibles;
static late Box<History> histories;
static late Box<CollectedBangumiChange> collectChanges;
static late Box<String> shieldList;
static late final Box<dynamic> setting;
+ static late Box<SearchHistory> searchHistory;
static Future init() async {
Hive.registerAdapter(BangumiItemAdapter());
@@ -24,12 +26,14 @@ class GStorage {
Hive.registerAdapter(ProgressAdapter());
Hive.registerAdapter(HistoryAdapter());
Hive.registerAdapter(CollectedBangumiChangeAdapter());
+ Hive.registerAdapter(SearchHistoryAdapter());
favorites = await Hive.openBox('favorites');
collectibles = await Hive.openBox('collectibles');
histories = await Hive.openBox('histories');
setting = await Hive.openBox('setting');
collectChanges = await Hive.openBox('collectchanges');
shieldList = await Hive.openBox('shieldList');
+ searchHistory = await Hive.openBox('searchHistory');
}
static Future<void> backupBox(String boxName, String backupFilePath) async {