mirror of
https://github.com/bggRGjQaUbCoE/PiliPlus.git
synced 2026-05-14 21:24:02 +08:00
@@ -4,14 +4,12 @@ import 'package:PiliPlus/common/widgets/dialog/dialog.dart';
|
||||
import 'package:PiliPlus/http/loading_state.dart';
|
||||
import 'package:PiliPlus/http/search.dart';
|
||||
import 'package:PiliPlus/models/search/suggest.dart';
|
||||
import 'package:PiliPlus/models_new/search/search_rcmd/data.dart';
|
||||
import 'package:PiliPlus/models_new/search/search_trending/data.dart';
|
||||
import 'package:PiliPlus/utils/extension/get_ext.dart';
|
||||
import 'package:PiliPlus/utils/extension/string_ext.dart';
|
||||
import 'package:PiliPlus/utils/id_utils.dart';
|
||||
import 'package:PiliPlus/utils/platform_utils.dart';
|
||||
import 'package:PiliPlus/utils/storage.dart';
|
||||
import 'package:PiliPlus/utils/storage_pref.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/scheduler.dart';
|
||||
import 'package:get/get.dart';
|
||||
@@ -59,19 +57,12 @@ class BaseSearchController extends GetxController {
|
||||
|
||||
late final Rx<LoadingState<SearchTrendingData>> trendingState;
|
||||
|
||||
final recordSearchHistory = Pref.recordSearchHistory.obs;
|
||||
final searchSuggestion = Pref.searchSuggestion;
|
||||
final enableTrending = Pref.enableTrending;
|
||||
final enableSearchRcmd = Pref.enableSearchRcmd;
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
|
||||
if (enableTrending) {
|
||||
trendingState = LoadingState<SearchTrendingData>.loading().obs;
|
||||
queryTrendingList();
|
||||
}
|
||||
trendingState = LoadingState<SearchTrendingData>.loading().obs;
|
||||
queryTrendingList();
|
||||
}
|
||||
|
||||
// 获取热搜关键词
|
||||
@@ -97,22 +88,15 @@ class SSearchController extends GetxController
|
||||
final RxBool showUidBtn = false.obs;
|
||||
|
||||
// history
|
||||
RxBool get recordSearchHistory => _baseCtr.recordSearchHistory;
|
||||
RxList<String> get historyList => _baseCtr.historyList;
|
||||
|
||||
// suggestion
|
||||
bool get searchSuggestion => _baseCtr.searchSuggestion;
|
||||
late final RxList<SearchSuggestItem> searchSuggestList;
|
||||
|
||||
// trending
|
||||
bool get enableTrending => _baseCtr.enableTrending;
|
||||
Rx<LoadingState<SearchTrendingData>> get trendingState =>
|
||||
_baseCtr.trendingState;
|
||||
|
||||
// rcmd
|
||||
bool get enableSearchRcmd => _baseCtr.enableSearchRcmd;
|
||||
late final Rx<LoadingState<SearchRcmdData>> recommendData;
|
||||
|
||||
Future<void> Function() get queryTrendingList => _baseCtr.queryTrendingList;
|
||||
|
||||
@override
|
||||
@@ -125,15 +109,8 @@ class SSearchController extends GetxController
|
||||
controller.text = text;
|
||||
}
|
||||
|
||||
if (searchSuggestion) {
|
||||
subInit();
|
||||
searchSuggestList = <SearchSuggestItem>[].obs;
|
||||
}
|
||||
|
||||
if (enableSearchRcmd) {
|
||||
recommendData = LoadingState<SearchRcmdData>.loading().obs;
|
||||
queryRecommendList();
|
||||
}
|
||||
subInit();
|
||||
searchSuggestList = <SearchSuggestItem>[].obs;
|
||||
}
|
||||
|
||||
void validateUid() {
|
||||
@@ -142,19 +119,17 @@ class SSearchController extends GetxController
|
||||
|
||||
void onChange(String value) {
|
||||
validateUid();
|
||||
if (searchSuggestion) {
|
||||
if (value.isEmpty) {
|
||||
searchSuggestList.clear();
|
||||
} else {
|
||||
ctr!.add(value);
|
||||
}
|
||||
if (value.isEmpty) {
|
||||
searchSuggestList.clear();
|
||||
} else {
|
||||
ctr!.add(value);
|
||||
}
|
||||
}
|
||||
|
||||
void onClear() {
|
||||
if (controller.value.text != '') {
|
||||
controller.clear();
|
||||
if (searchSuggestion) searchSuggestList.clear();
|
||||
searchSuggestList.clear();
|
||||
searchFocusNode.requestFocus();
|
||||
showUidBtn.value = false;
|
||||
} else {
|
||||
@@ -172,12 +147,10 @@ class SSearchController extends GetxController
|
||||
validateUid();
|
||||
}
|
||||
|
||||
if (recordSearchHistory.value) {
|
||||
historyList
|
||||
..remove(controller.text)
|
||||
..insert(0, controller.text);
|
||||
GStorage.historyWord.put('cacheList', historyList);
|
||||
}
|
||||
historyList
|
||||
..remove(controller.text)
|
||||
..insert(0, controller.text);
|
||||
GStorage.historyWord.put('cacheList', historyList);
|
||||
|
||||
searchFocusNode.unfocus();
|
||||
await Get.toNamed(
|
||||
@@ -201,15 +174,11 @@ class SSearchController extends GetxController
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> queryRecommendList() async {
|
||||
recommendData.value = await SearchHttp.searchRecommend();
|
||||
}
|
||||
|
||||
void onClickKeyword(String keyword) {
|
||||
controller.text = keyword;
|
||||
validateUid();
|
||||
|
||||
if (searchSuggestion) searchSuggestList.clear();
|
||||
searchSuggestList.clear();
|
||||
submit();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,5 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:PiliPlus/common/widgets/dialog/export_import.dart';
|
||||
import 'package:PiliPlus/common/widgets/disabled_icon.dart';
|
||||
import 'package:PiliPlus/common/widgets/loading_widget/http_error.dart';
|
||||
import 'package:PiliPlus/common/widgets/scaffold.dart';
|
||||
import 'package:PiliPlus/common/widgets/sliver_wrap.dart';
|
||||
import 'package:PiliPlus/http/loading_state.dart';
|
||||
import 'package:PiliPlus/models_new/search/search_rcmd/data.dart';
|
||||
@@ -11,8 +8,6 @@ import 'package:PiliPlus/pages/search/widgets/hot_keyword.dart';
|
||||
import 'package:PiliPlus/pages/search/widgets/search_text.dart';
|
||||
import 'package:PiliPlus/utils/em.dart' show Em;
|
||||
import 'package:PiliPlus/utils/extension/size_ext.dart';
|
||||
import 'package:PiliPlus/utils/storage.dart';
|
||||
import 'package:PiliPlus/utils/storage_key.dart';
|
||||
import 'package:PiliPlus/utils/utils.dart';
|
||||
import 'package:flutter/material.dart' hide LayoutBuilder;
|
||||
import 'package:get/get.dart';
|
||||
@@ -50,34 +45,21 @@ class _SearchPageState extends State<SearchPage> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final trending = _searchController.enableTrending
|
||||
? _buildHotSearch()
|
||||
: null;
|
||||
final rcmd = _searchController.enableSearchRcmd
|
||||
? _buildHotSearch(isTrending: false)
|
||||
: null;
|
||||
|
||||
return Scaffold(
|
||||
final trending = _buildHotSearch();
|
||||
return scaffold(
|
||||
appBar: _buildAppBar,
|
||||
body: Padding(
|
||||
padding: .only(left: padding.left, right: padding.right),
|
||||
child: CustomScrollView(
|
||||
slivers: [
|
||||
if (_searchController.searchSuggestion) _buildSearchSuggest(),
|
||||
_buildSearchSuggest(),
|
||||
if (isPortrait) ...[
|
||||
?trending,
|
||||
trending,
|
||||
_buildHistory,
|
||||
?rcmd,
|
||||
] else if (_searchController.enableTrending ||
|
||||
_searchController.enableSearchRcmd)
|
||||
] else
|
||||
SliverCrossAxisGroup(
|
||||
slivers: [
|
||||
SliverMainAxisGroup(slivers: [?trending, ?rcmd]),
|
||||
_buildHistory,
|
||||
],
|
||||
)
|
||||
else
|
||||
_buildHistory,
|
||||
slivers: [trending, _buildHistory],
|
||||
),
|
||||
SliverPadding(padding: .only(bottom: padding.bottom)),
|
||||
],
|
||||
),
|
||||
@@ -173,11 +155,9 @@ class _SearchPageState extends State<SearchPage> {
|
||||
});
|
||||
}
|
||||
|
||||
Widget _buildHotSearch({
|
||||
bool isTrending = true,
|
||||
}) {
|
||||
Widget _buildHotSearch() {
|
||||
final text = Text(
|
||||
isTrending ? '大家都在搜' : '搜索发现',
|
||||
'大家都在搜',
|
||||
strutStyle: const StrutStyle(leading: 0, height: 1),
|
||||
style: theme.textTheme.titleMedium!.copyWith(
|
||||
height: 1,
|
||||
@@ -194,9 +174,7 @@ class _SearchPageState extends State<SearchPage> {
|
||||
return SliverPadding(
|
||||
padding: EdgeInsets.fromLTRB(
|
||||
10,
|
||||
!isTrending && (isPortrait || _searchController.enableTrending)
|
||||
? 4
|
||||
: 25,
|
||||
isPortrait ? 4 : 25,
|
||||
4,
|
||||
25,
|
||||
),
|
||||
@@ -208,42 +186,40 @@ class _SearchPageState extends State<SearchPage> {
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
isTrending
|
||||
? Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
text,
|
||||
const SizedBox(width: 14),
|
||||
TextButton(
|
||||
style: const ButtonStyle(
|
||||
visualDensity: .compact,
|
||||
tapTargetSize: .shrinkWrap,
|
||||
padding: WidgetStatePropertyAll(
|
||||
.symmetric(horizontal: 10),
|
||||
),
|
||||
),
|
||||
onPressed: () => Get.toNamed('/searchTrending'),
|
||||
child: Row(
|
||||
children: [
|
||||
text,
|
||||
const SizedBox(width: 14),
|
||||
TextButton(
|
||||
style: const ButtonStyle(
|
||||
visualDensity: .compact,
|
||||
tapTargetSize: .shrinkWrap,
|
||||
padding: WidgetStatePropertyAll(
|
||||
.symmetric(horizontal: 10),
|
||||
),
|
||||
),
|
||||
onPressed: () => Get.toNamed('/searchTrending'),
|
||||
child: Row(
|
||||
children: [
|
||||
Text(
|
||||
'完整榜单',
|
||||
strutStyle: const StrutStyle(
|
||||
leading: 0,
|
||||
height: 1,
|
||||
),
|
||||
style: style,
|
||||
),
|
||||
Icon(
|
||||
size: 18,
|
||||
Icons.keyboard_arrow_right,
|
||||
color: outline,
|
||||
),
|
||||
],
|
||||
Text(
|
||||
'完整榜单',
|
||||
strutStyle: const StrutStyle(
|
||||
leading: 0,
|
||||
height: 1,
|
||||
),
|
||||
style: style,
|
||||
),
|
||||
Icon(
|
||||
size: 18,
|
||||
Icons.keyboard_arrow_right,
|
||||
color: outline,
|
||||
),
|
||||
],
|
||||
)
|
||||
: text,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
TextButton.icon(
|
||||
style: const ButtonStyle(
|
||||
visualDensity: .compact,
|
||||
@@ -252,9 +228,7 @@ class _SearchPageState extends State<SearchPage> {
|
||||
.symmetric(horizontal: 10),
|
||||
),
|
||||
),
|
||||
onPressed: isTrending
|
||||
? _searchController.queryTrendingList
|
||||
: _searchController.queryRecommendList,
|
||||
onPressed: _searchController.queryTrendingList,
|
||||
icon: Icon(
|
||||
Icons.refresh_outlined,
|
||||
size: 18,
|
||||
@@ -274,12 +248,7 @@ class _SearchPageState extends State<SearchPage> {
|
||||
),
|
||||
),
|
||||
Obx(
|
||||
() => _buildHotKey(
|
||||
isTrending
|
||||
? _searchController.trendingState.value
|
||||
: _searchController.recommendData.value,
|
||||
isTrending,
|
||||
),
|
||||
() => _buildHotKey(_searchController.trendingState.value),
|
||||
),
|
||||
],
|
||||
),
|
||||
@@ -298,11 +267,7 @@ class _SearchPageState extends State<SearchPage> {
|
||||
return SliverPadding(
|
||||
padding: EdgeInsets.fromLTRB(
|
||||
10,
|
||||
!isPortrait
|
||||
? 25
|
||||
: _searchController.enableTrending
|
||||
? 0
|
||||
: 6,
|
||||
!isPortrait ? 25 : 0,
|
||||
6,
|
||||
25,
|
||||
),
|
||||
@@ -312,6 +277,7 @@ class _SearchPageState extends State<SearchPage> {
|
||||
padding: const EdgeInsets.fromLTRB(6, 0, 6, 6),
|
||||
sliver: SliverToBoxAdapter(
|
||||
child: Row(
|
||||
mainAxisAlignment: .spaceBetween,
|
||||
children: [
|
||||
Text(
|
||||
'搜索历史',
|
||||
@@ -321,10 +287,6 @@ class _SearchPageState extends State<SearchPage> {
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
_recordBtn,
|
||||
_exportBtn,
|
||||
const Spacer(),
|
||||
TextButton.icon(
|
||||
style: const ButtonStyle(
|
||||
visualDensity: .compact,
|
||||
@@ -379,64 +341,8 @@ class _SearchPageState extends State<SearchPage> {
|
||||
);
|
||||
}
|
||||
|
||||
Widget get _recordBtn => Obx(
|
||||
() {
|
||||
bool enable = _searchController.recordSearchHistory.value;
|
||||
return IconButton(
|
||||
iconSize: 22,
|
||||
tooltip: enable ? '记录搜索' : '无痕搜索',
|
||||
icon: DisabledIcon(
|
||||
disable: !enable,
|
||||
child: Icon(
|
||||
Icons.history,
|
||||
color: theme.colorScheme.onSurfaceVariant.withValues(alpha: 0.8),
|
||||
),
|
||||
),
|
||||
style: const ButtonStyle(
|
||||
visualDensity: .comfortable,
|
||||
tapTargetSize: .shrinkWrap,
|
||||
padding: WidgetStatePropertyAll(.zero),
|
||||
),
|
||||
onPressed: () {
|
||||
enable = !enable;
|
||||
_searchController.recordSearchHistory.value = enable;
|
||||
GStorage.setting.put(
|
||||
SettingBoxKey.recordSearchHistory,
|
||||
enable,
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
Widget get _exportBtn => IconButton(
|
||||
iconSize: 22,
|
||||
tooltip: '导入/导出历史记录',
|
||||
icon: Icon(
|
||||
Icons.import_export_outlined,
|
||||
color: theme.colorScheme.onSurfaceVariant.withValues(alpha: 0.8),
|
||||
),
|
||||
style: const ButtonStyle(
|
||||
visualDensity: .comfortable,
|
||||
tapTargetSize: .shrinkWrap,
|
||||
padding: WidgetStatePropertyAll(.zero),
|
||||
),
|
||||
onPressed: () => showImportExportDialog<List>(
|
||||
context,
|
||||
title: '历史记录',
|
||||
localFileName: () => 'search',
|
||||
onExport: () => jsonEncode(_searchController.historyList),
|
||||
onImport: (json) {
|
||||
final list = List<String>.from(json);
|
||||
_searchController.historyList.value = list;
|
||||
GStorage.historyWord.put('cacheList', list);
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
Widget _buildHotKey(
|
||||
LoadingState<SearchRcmdData> loadingState,
|
||||
bool isTrending,
|
||||
) {
|
||||
return switch (loadingState) {
|
||||
Success(:final response) when (response.list?.isNotEmpty ?? false) =>
|
||||
@@ -447,9 +353,7 @@ class _SearchPageState extends State<SearchPage> {
|
||||
Error(:final errMsg) => HttpError(
|
||||
safeArea: false,
|
||||
errMsg: errMsg,
|
||||
onReload: isTrending
|
||||
? _searchController.queryTrendingList
|
||||
: _searchController.queryRecommendList,
|
||||
onReload: _searchController.queryTrendingList,
|
||||
),
|
||||
_ => const SliverToBoxAdapter(),
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user