mod: history: full type

Closes #473

Signed-off-by: bggRGjQaUbCoE <githubaccount56556@proton.me>
This commit is contained in:
bggRGjQaUbCoE
2025-03-24 18:52:10 +08:00
parent 34f63612a4
commit 90c8aeb05d
7 changed files with 396 additions and 211 deletions

View File

@@ -96,9 +96,9 @@ class Request {
//请求基地址,可以包含子路径 //请求基地址,可以包含子路径
baseUrl: HttpString.apiBaseUrl, baseUrl: HttpString.apiBaseUrl,
//连接服务器超时时间,单位是毫秒. //连接服务器超时时间,单位是毫秒.
connectTimeout: const Duration(milliseconds: 4000), connectTimeout: const Duration(milliseconds: 10000),
//响应流上前后两次接受到数据的间隔,单位为毫秒。 //响应流上前后两次接受到数据的间隔,单位为毫秒。
receiveTimeout: const Duration(milliseconds: 4000), receiveTimeout: const Duration(milliseconds: 10000),
//Http请求头. //Http请求头.
headers: { headers: {
'connection': 'keep-alive', 'connection': 'keep-alive',

View File

@@ -196,11 +196,12 @@ class UserHttp {
// 观看历史 // 观看历史
static Future<LoadingState> historyList({ static Future<LoadingState> historyList({
required String type,
int? max, int? max,
int? viewAt, int? viewAt,
}) async { }) async {
var res = await Request().get(Api.historyList, queryParameters: { var res = await Request().get(Api.historyList, queryParameters: {
'type': 'all', 'type': type,
'ps': 20, 'ps': 20,
'max': max ?? 0, 'max': max ?? 0,
'view_at': viewAt ?? 0, 'view_at': viewAt ?? 0,

View File

@@ -4,8 +4,8 @@ import 'package:get/get.dart';
import 'package:PiliPlus/utils/extension.dart'; import 'package:PiliPlus/utils/extension.dart';
abstract class MultiSelectController extends CommonController { abstract class MultiSelectController extends CommonController {
RxBool enableMultiSelect = false.obs; late final RxBool enableMultiSelect = false.obs;
RxInt checkedCount = 0.obs; late final RxInt checkedCount = 0.obs;
onSelect(int index) { onSelect(int index) {
List list = (loadingState.value as Success).response; List list = (loadingState.value as Success).response;

View File

@@ -0,0 +1,74 @@
import 'package:PiliPlus/http/user.dart';
import 'package:PiliPlus/utils/storage.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
class HistoryBaseController extends GetxController {
RxBool pauseStatus = false.obs;
RxBool enableMultiSelect = false.obs;
RxInt checkedCount = 0.obs;
// 清空观看历史
Future onClearHistory(BuildContext context, VoidCallback onSuccess) async {
await showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('提示'),
content: const Text('啊叻?你要清空历史记录功能吗?'),
actions: [
TextButton(onPressed: Get.back, child: const Text('取消')),
TextButton(
onPressed: () async {
Get.back();
SmartDialog.showLoading(msg: '请求中');
var res = await UserHttp.clearHistory();
SmartDialog.dismiss();
if (res.data['code'] == 0) {
SmartDialog.showToast('清空观看历史');
onSuccess();
}
},
child: const Text('确认清空'),
)
],
);
},
);
}
// 暂停观看历史
Future onPauseHistory(BuildContext context) async {
await showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('提示'),
content:
Text(!pauseStatus.value ? '啊叻?你要暂停历史记录功能吗?' : '啊叻?要恢复历史记录功能吗?'),
actions: [
TextButton(onPressed: Get.back, child: const Text('取消')),
TextButton(
onPressed: () async {
SmartDialog.showLoading(msg: '请求中');
var res = await UserHttp.pauseHistory(!pauseStatus.value);
SmartDialog.dismiss();
if (res.data['code'] == 0) {
SmartDialog.showToast(
!pauseStatus.value ? '暂停观看历史' : '恢复观看历史');
pauseStatus.value = !pauseStatus.value;
GStorage.localCache
.put(LocalCacheKey.historyPause, pauseStatus.value);
}
Get.back();
},
child: Text(!pauseStatus.value ? '确认暂停' : '确认恢复'),
)
],
);
},
);
}
}

View File

@@ -1,5 +1,6 @@
import 'package:PiliPlus/http/loading_state.dart'; import 'package:PiliPlus/http/loading_state.dart';
import 'package:PiliPlus/pages/common/multi_select_controller.dart'; import 'package:PiliPlus/pages/common/multi_select_controller.dart';
import 'package:PiliPlus/pages/history/base_controller.dart';
import 'package:PiliPlus/utils/extension.dart'; import 'package:PiliPlus/utils/extension.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
@@ -8,8 +9,15 @@ import 'package:PiliPlus/http/user.dart';
import 'package:PiliPlus/models/user/history.dart'; import 'package:PiliPlus/models/user/history.dart';
import 'package:PiliPlus/utils/storage.dart'; import 'package:PiliPlus/utils/storage.dart';
class HistoryController extends MultiSelectController { class HistoryController extends MultiSelectController
RxBool pauseStatus = false.obs; with GetTickerProviderStateMixin {
HistoryController(this.type);
late final baseCtr = Get.put(HistoryBaseController());
final String? type;
TabController? tabController;
late RxList<HisTabItem> tabs = <HisTabItem>[].obs;
int? max; int? max;
int? viewAt; int? viewAt;
@@ -28,13 +36,46 @@ class HistoryController extends MultiSelectController {
return super.onRefresh(); return super.onRefresh();
} }
@override
onSelect(int index) {
List list = (loadingState.value as Success).response;
list[index].checked = !(list[index]?.checked ?? false);
baseCtr.checkedCount.value =
list.where((item) => item.checked == true).length;
loadingState.value = LoadingState.success(list);
if (baseCtr.checkedCount.value == 0) {
baseCtr.enableMultiSelect.value = false;
}
}
@override
void handleSelect([bool checked = false]) {
if (loadingState.value is Success) {
List list = (loadingState.value as Success).response;
if (list.isNotEmpty) {
loadingState.value = LoadingState.success(
list.map((item) => item..checked = checked).toList());
baseCtr.checkedCount.value = checked ? list.length : 0;
}
}
if (checked.not) {
baseCtr.enableMultiSelect.value = false;
}
}
@override @override
bool customHandleResponse(Success response) { bool customHandleResponse(Success response) {
HistoryData data = response.response; HistoryData data = response.response;
isEnd = data.list.isNullOrEmpty || data.list!.length < 20; isEnd = data.list.isNullOrEmpty || data.list!.length < 20;
max = data.list?.lastOrNull?.history?.oid; max = data.list?.lastOrNull?.history?.oid;
viewAt = data.list?.lastOrNull?.viewAt; viewAt = data.list?.lastOrNull?.viewAt;
if (currentPage != 1 && loadingState.value is Success) { if (currentPage == 1) {
if (type == null && tabs.isEmpty && data.tab?.isNotEmpty == true) {
tabs.value = data.tab!;
tabController =
TabController(length: data.tab!.length + 1, vsync: this);
}
} else if (loadingState.value is Success) {
data.list ??= <HisListItem>[]; data.list ??= <HisListItem>[];
data.list!.insertAll( data.list!.insertAll(
0, 0,
@@ -45,79 +86,17 @@ class HistoryController extends MultiSelectController {
return true; return true;
} }
// 暂停观看历史
Future onPauseHistory(BuildContext context) async {
await showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('提示'),
content:
Text(!pauseStatus.value ? '啊叻?你要暂停历史记录功能吗?' : '啊叻?要恢复历史记录功能吗?'),
actions: [
TextButton(onPressed: Get.back, child: const Text('取消')),
TextButton(
onPressed: () async {
SmartDialog.showLoading(msg: '请求中');
var res = await UserHttp.pauseHistory(!pauseStatus.value);
SmartDialog.dismiss();
if (res.data['code'] == 0) {
SmartDialog.showToast(
!pauseStatus.value ? '暂停观看历史' : '恢复观看历史');
pauseStatus.value = !pauseStatus.value;
GStorage.localCache
.put(LocalCacheKey.historyPause, pauseStatus.value);
}
Get.back();
},
child: Text(!pauseStatus.value ? '确认暂停' : '确认恢复'),
)
],
);
},
);
}
// 观看历史暂停状态 // 观看历史暂停状态
Future historyStatus() async { Future historyStatus() async {
var res = await UserHttp.historyStatus(); var res = await UserHttp.historyStatus();
if (res['status']) { if (res['status']) {
pauseStatus.value = res['data']; baseCtr.pauseStatus.value = res['data'];
GStorage.localCache.put(LocalCacheKey.historyPause, res['data']); GStorage.localCache.put(LocalCacheKey.historyPause, res['data']);
} else { } else {
SmartDialog.showToast(res['msg']); SmartDialog.showToast(res['msg']);
} }
} }
// 清空观看历史
Future onClearHistory(BuildContext context) async {
await showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('提示'),
content: const Text('啊叻?你要清空历史记录功能吗?'),
actions: [
TextButton(onPressed: Get.back, child: const Text('取消')),
TextButton(
onPressed: () async {
SmartDialog.showLoading(msg: '请求中');
var res = await UserHttp.clearHistory();
SmartDialog.dismiss();
if (res.data['code'] == 0) {
SmartDialog.showToast('清空观看历史');
}
Get.back();
loadingState.value = LoadingState.success([]);
},
child: const Text('确认清空'),
)
],
);
},
);
}
// 删除某条历史记录 // 删除某条历史记录
Future delHistory(kid, business) async { Future delHistory(kid, business) async {
_onDelete(((loadingState.value as Success).response as List) _onDelete(((loadingState.value as Success).response as List)
@@ -155,9 +134,9 @@ class HistoryController extends MultiSelectController {
} else { } else {
onReload(); onReload();
} }
if (enableMultiSelect.value) { if (baseCtr.enableMultiSelect.value) {
checkedCount.value = 0; baseCtr.checkedCount.value = 0;
enableMultiSelect.value = false; baseCtr.enableMultiSelect.value = false;
} }
} }
SmartDialog.dismiss(); SmartDialog.dismiss();
@@ -201,5 +180,11 @@ class HistoryController extends MultiSelectController {
@override @override
Future<LoadingState> customGetData() => Future<LoadingState> customGetData() =>
UserHttp.historyList(max: max, viewAt: viewAt); UserHttp.historyList(type: type ?? 'all', max: max, viewAt: viewAt);
@override
void onClose() {
tabController?.dispose();
super.onClose();
}
} }

View File

@@ -2,6 +2,7 @@ import 'package:PiliPlus/common/widgets/http_error.dart';
import 'package:PiliPlus/common/widgets/refresh_indicator.dart'; import 'package:PiliPlus/common/widgets/refresh_indicator.dart';
import 'package:PiliPlus/http/loading_state.dart'; import 'package:PiliPlus/http/loading_state.dart';
import 'package:PiliPlus/pages/fav_search/view.dart' show SearchType; import 'package:PiliPlus/pages/fav_search/view.dart' show SearchType;
import 'package:PiliPlus/pages/history/base_controller.dart';
import 'package:PiliPlus/utils/extension.dart'; import 'package:PiliPlus/utils/extension.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
@@ -13,139 +14,252 @@ import '../../utils/grid.dart';
import 'widgets/item.dart'; import 'widgets/item.dart';
class HistoryPage extends StatefulWidget { class HistoryPage extends StatefulWidget {
const HistoryPage({super.key}); const HistoryPage({super.key, this.type});
final String? type;
@override @override
State<HistoryPage> createState() => _HistoryPageState(); State<HistoryPage> createState() => _HistoryPageState();
} }
class _HistoryPageState extends State<HistoryPage> { class _HistoryPageState extends State<HistoryPage>
final _historyController = Get.put(HistoryController()); with AutomaticKeepAliveClientMixin {
late final _historyController = Get.put(
HistoryController(widget.type),
tag: widget.type ?? 'all',
);
HistoryController get currCtr {
try {
if (_historyController.tabController != null &&
_historyController.tabController!.index != 0) {
return Get.find<HistoryController>(
tag: _historyController
.tabs[_historyController.tabController!.index - 1].type,
);
}
} catch (_) {
return _historyController;
}
return _historyController;
}
bool get enableMultiSelect =>
_historyController.baseCtr.enableMultiSelect.value;
@override
void dispose() {
Get.delete<HistoryBaseController>();
super.dispose();
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Obx( super.build(context);
() => PopScope( return widget.type != null
canPop: _historyController.enableMultiSelect.value.not, ? _buildPage
onPopInvokedWithResult: (didPop, result) { : Obx(
if (_historyController.enableMultiSelect.value) { () => PopScope(
_historyController.handleSelect(); canPop: enableMultiSelect.not,
} onPopInvokedWithResult: (didPop, result) {
}, if (enableMultiSelect) {
child: Scaffold( currCtr.handleSelect();
appBar: AppBarWidget( }
visible: _historyController.enableMultiSelect.value, },
child1: AppBar( child: Scaffold(
title: const Text('观看记录'), appBar: widget.type != null
actions: [ ? null
IconButton( : AppBarWidget(
tooltip: '搜索', visible: enableMultiSelect,
onPressed: () => Get.toNamed( child1: AppBar(
'/favSearch', title: const Text('观看记录'),
arguments: { actions: [
'searchType': SearchType.history, IconButton(
}, tooltip: '搜索',
), onPressed: () => Get.toNamed(
icon: const Icon(Icons.search_outlined), '/favSearch',
), arguments: {
PopupMenuButton<String>( 'searchType': SearchType.history,
onSelected: (String type) { },
// 处理菜单项选择的逻辑 ),
switch (type) { icon: const Icon(Icons.search_outlined),
case 'pause': ),
_historyController.onPauseHistory(context); PopupMenuButton<String>(
break; onSelected: (String type) {
case 'clear': // 处理菜单项选择的逻辑
_historyController.onClearHistory(context); switch (type) {
break; case 'pause':
case 'del': _historyController.baseCtr
_historyController.onDelHistory(); .onPauseHistory(context);
break; break;
case 'multiple': case 'clear':
_historyController.enableMultiSelect.value = true; _historyController.baseCtr
break; .onClearHistory(context, () {
} _historyController.loadingState.value =
}, LoadingState.success(null);
itemBuilder: (BuildContext context) => if (_historyController.tabController !=
<PopupMenuEntry<String>>[ null) {
PopupMenuItem<String>( for (final item
value: 'pause', in _historyController.tabs) {
child: Obx( try {
() => Text( Get.find<HistoryController>(
!_historyController.pauseStatus.value tag: item.type)
? '暂停观看记录' .loadingState
: '恢复观看记录', .value =
LoadingState.success(null);
} catch (_) {}
}
}
});
break;
case 'del':
currCtr.onDelHistory();
break;
case 'multiple':
_historyController
.baseCtr.enableMultiSelect.value = true;
break;
}
},
itemBuilder: (BuildContext context) =>
<PopupMenuEntry<String>>[
PopupMenuItem<String>(
value: 'pause',
child: Obx(
() => Text(
!_historyController
.baseCtr.pauseStatus.value
? '暂停观看记录'
: '恢复观看记录',
),
),
),
const PopupMenuItem<String>(
value: 'clear',
child: Text('清空观看记录'),
),
const PopupMenuItem<String>(
value: 'del',
child: Text('删除已看记录'),
),
const PopupMenuItem<String>(
value: 'multiple',
child: Text('多选删除'),
),
],
),
const SizedBox(width: 6),
],
),
child2: AppBar(
leading: IconButton(
tooltip: '取消',
onPressed: () {
currCtr.handleSelect();
},
icon: const Icon(Icons.close_outlined),
),
title: Obx(
() => Text(
'已选: ${_historyController.baseCtr.checkedCount.value}',
),
),
actions: [
TextButton(
onPressed: () => currCtr.handleSelect(true),
child: const Text('全选'),
),
TextButton(
onPressed: () =>
currCtr.onDelCheckedHistory(context),
child: Text(
'删除',
style: TextStyle(
color: Theme.of(context).colorScheme.error),
),
),
const SizedBox(width: 6),
],
), ),
), ),
), body: Obx(
const PopupMenuItem<String>( () => _historyController.tabs.isNotEmpty
value: 'clear', ? Column(
child: Text('清空观看记录'), crossAxisAlignment: CrossAxisAlignment.start,
), children: [
const PopupMenuItem<String>( TabBar(
value: 'del', controller: _historyController.tabController,
child: Text('删除已看记录'), onTap: (value) {
), if (_historyController
const PopupMenuItem<String>( .tabController!.indexIsChanging.not) {
value: 'multiple', currCtr.scrollController.animToTop();
child: Text('多选删除'), } else {
), if (enableMultiSelect) {
], if (_historyController
), .tabController!.previousIndex ==
const SizedBox(width: 6), 0) {
], _historyController.handleSelect();
), } else {
child2: AppBar( try {
leading: IconButton( Get.find<HistoryController>(
tooltip: '取消', tag: _historyController
onPressed: _historyController.handleSelect, .tabs[_historyController
icon: const Icon(Icons.close_outlined), .tabController!
), .previousIndex -
title: Obx( 1]
() => Text( .type)
'已选: ${_historyController.checkedCount.value}', .handleSelect();
} catch (_) {}
}
}
}
},
tabs: [
Tab(text: '全部'),
..._historyController.tabs.map(
(item) => Tab(text: item.name),
),
],
),
Expanded(
child: Material(
color: Colors.transparent,
child: TabBarView(
physics: enableMultiSelect
? const NeverScrollableScrollPhysics()
: null,
controller: _historyController.tabController,
children: [
_buildPage,
..._historyController.tabs.map(
(item) => HistoryPage(type: item.type),
),
],
),
),
),
],
)
: _buildPage,
), ),
), ),
actions: [
TextButton(
onPressed: () => _historyController.handleSelect(true),
child: const Text('全选'),
),
TextButton(
onPressed: () =>
_historyController.onDelCheckedHistory(context),
child: Text(
'删除',
style:
TextStyle(color: Theme.of(context).colorScheme.error),
),
),
const SizedBox(width: 6),
],
), ),
), );
body: refreshIndicator(
onRefresh: () async {
await _historyController.onRefresh();
},
child: CustomScrollView(
physics: const AlwaysScrollableScrollPhysics(),
controller: _historyController.scrollController,
slivers: [
Obx(() => _buildBody(_historyController.loadingState.value)),
SliverToBoxAdapter(
child: SizedBox(
height: MediaQuery.of(context).padding.bottom + 80,
),
),
],
),
),
),
),
);
} }
Widget get _buildPage => refreshIndicator(
onRefresh: () async {
await _historyController.onRefresh();
},
child: CustomScrollView(
physics: const AlwaysScrollableScrollPhysics(),
controller: _historyController.scrollController,
slivers: [
Obx(() => _buildBody(_historyController.loadingState.value)),
],
),
);
Widget _buildBody(LoadingState loadingState) { Widget _buildBody(LoadingState loadingState) {
return switch (loadingState) { return switch (loadingState) {
Loading() => SliverGrid( Loading() => SliverGrid(
@@ -162,24 +276,30 @@ class _HistoryPageState extends State<HistoryPage> {
), ),
), ),
Success() => (loadingState.response as List?)?.isNotEmpty == true Success() => (loadingState.response as List?)?.isNotEmpty == true
? SliverGrid( ? SliverPadding(
gridDelegate: SliverGridDelegateWithExtentAndRatio( padding: EdgeInsets.only(
mainAxisSpacing: 2, top: StyleString.safeSpace - 5,
maxCrossAxisExtent: Grid.mediumCardWidth * 2, bottom: MediaQuery.of(context).padding.bottom + 80,
childAspectRatio: StyleString.aspectRatio * 2.2,
), ),
delegate: SliverChildBuilderDelegate( sliver: SliverGrid(
(context, index) { gridDelegate: SliverGridDelegateWithExtentAndRatio(
if (index == loadingState.response.length - 1) { mainAxisSpacing: 2,
_historyController.onLoadMore(); maxCrossAxisExtent: Grid.mediumCardWidth * 2,
} childAspectRatio: StyleString.aspectRatio * 2.2,
return HistoryItem( ),
videoItem: loadingState.response[index], delegate: SliverChildBuilderDelegate(
ctr: _historyController, (context, index) {
onChoose: () => _historyController.onSelect(index), if (index == loadingState.response.length - 1) {
); _historyController.onLoadMore();
}, }
childCount: loadingState.response.length, return HistoryItem(
videoItem: loadingState.response[index],
ctr: _historyController.baseCtr,
onChoose: () => _historyController.onSelect(index),
);
},
childCount: loadingState.response.length,
),
), ),
) )
: HttpError( : HttpError(
@@ -192,6 +312,9 @@ class _HistoryPageState extends State<HistoryPage> {
LoadingState() => throw UnimplementedError(), LoadingState() => throw UnimplementedError(),
}; };
} }
@override
bool get wantKeepAlive => true;
} }
class AppBarWidget extends StatelessWidget implements PreferredSizeWidget { class AppBarWidget extends StatelessWidget implements PreferredSizeWidget {

View File

@@ -3,6 +3,7 @@ import 'package:PiliPlus/common/widgets/video_progress_indicator.dart';
import 'package:PiliPlus/models/user/history.dart'; import 'package:PiliPlus/models/user/history.dart';
import 'package:PiliPlus/pages/common/multi_select_controller.dart'; import 'package:PiliPlus/pages/common/multi_select_controller.dart';
import 'package:PiliPlus/pages/fav_search/controller.dart'; import 'package:PiliPlus/pages/fav_search/controller.dart';
import 'package:PiliPlus/pages/history/base_controller.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
@@ -35,7 +36,8 @@ class HistoryItem extends StatelessWidget {
String bvid = videoItem.history.bvid ?? IdUtils.av2bv(aid); String bvid = videoItem.history.bvid ?? IdUtils.av2bv(aid);
return InkWell( return InkWell(
onTap: () async { onTap: () async {
if (ctr is MultiSelectController && ctr!.enableMultiSelect.value) { if ((ctr is MultiSelectController || ctr is HistoryBaseController) &&
ctr!.enableMultiSelect.value) {
feedBack(); feedBack();
onChoose?.call(); onChoose?.call();
return; return;