refactor: history page

Signed-off-by: bggRGjQaUbCoE <githubaccount56556@proton.me>
This commit is contained in:
bggRGjQaUbCoE
2024-11-28 20:49:07 +08:00
parent f4866258d2
commit 665dd8b92a
15 changed files with 327 additions and 374 deletions

View File

@@ -166,7 +166,10 @@ class UserHttp {
}
// 观看历史
static Future historyList(int? max, int? viewAt) async {
static Future<LoadingState> historyList({
int? max,
int? viewAt,
}) async {
var res = await Request().get(Api.historyList, data: {
'type': 'all',
'ps': 20,
@@ -174,9 +177,9 @@ class UserHttp {
'view_at': viewAt ?? 0,
});
if (res.data['code'] == 0) {
return {'status': true, 'data': HistoryData.fromJson(res.data['data'])};
return LoadingState.success(HistoryData.fromJson(res.data['data']));
} else {
return {'status': false, 'data': [], 'msg': res.data['message']};
return LoadingState.error(res.data['message']);
}
}

View File

@@ -85,7 +85,7 @@ class HisListItem {
this.kid,
this.tagName,
this.liveStatus,
this.checked,
required this.checked,
});
String? title;
@@ -112,7 +112,7 @@ class HisListItem {
int? kid;
String? tagName;
int? liveStatus;
bool? checked;
late bool checked;
void isFullScreen;
HisListItem.fromJson(Map<String, dynamic> json) {

View File

@@ -125,13 +125,11 @@ class BlackListController extends CommonController {
@override
bool customHandleResponse(Success response) {
total.value = response.response.total;
List currentList = loadingState.value is Success
? (loadingState.value as Success).response
: [];
List dataList = currentPage == 1
? response.response.list
: currentList + response.response.list;
loadingState.value = LoadingState.success(dataList);
if (currentPage != 1 && loadingState.value is Success) {
response.response.list
?.insertAll(0, (loadingState.value as Success).response);
}
loadingState.value = LoadingState.success(response.response.list);
return true;
}

View File

@@ -64,11 +64,9 @@ abstract class ReplyController extends CommonController {
}
}
cursor = replies.cursor;
if (currentPage != 1) {
List<ReplyInfo> list = loadingState.value is Success
? (loadingState.value as Success).response.replies
: <ReplyInfo>[];
replies.replies.insertAll(0, list);
if (currentPage != 1 && loadingState.value is Success) {
replies.replies
.insertAll(0, (loadingState.value as Success).response.replies);
}
isEnd = replies.replies.isEmpty ||
replies.cursor.isEnd ||

View File

@@ -31,13 +31,12 @@ class DynamicsTabController extends CommonController {
@override
bool customHandleResponse(Success response) {
List currentList = loadingState.value is Success
? (loadingState.value as Success).response
: [];
loadingState.value = offset == ''
? LoadingState.success(response.response.items)
: LoadingState.success(currentList + response.response.items);
offset = response.response.offset;
if (currentPage != 1 && loadingState.value is Success) {
response.response.items
.insertAll(0, (loadingState.value as Success).response);
}
loadingState.value = LoadingState.success(response.response.items);
return true;
}

View File

@@ -33,12 +33,11 @@ class FansController extends CommonController {
response.response.list.isEmpty) {
isEnd = true;
}
List currentList = loadingState.value is Success
? (loadingState.value as Success).response
: [];
loadingState.value = currentPage == 1
? LoadingState.success(response.response.list)
: LoadingState.success(currentList + response.response.list);
if (currentPage != 1 && loadingState.value is Success) {
response.response.list
.insertAll(0, (loadingState.value as Success).response);
}
loadingState.value = LoadingState.success(response.response.list);
return true;
}

View File

@@ -33,12 +33,11 @@ class FavController extends CommonController {
@override
bool customHandleResponse(Success response) {
hasMore = response.response.hasMore;
List currentList = loadingState.value is Success
? (loadingState.value as Success).response
: [];
loadingState.value = currentPage == 1
? LoadingState.success(response.response.list)
: LoadingState.success(currentList + response.response.list);
if (currentPage != 1 && loadingState.value is Success) {
response.response.list
.insertAll(0, (loadingState.value as Success).response);
}
loadingState.value = LoadingState.success(response.response.list);
return true;
}

View File

@@ -48,14 +48,12 @@ class FavDetailController extends CommonController {
isOwner.value = response.response.info.mid ==
GStorage.userInfo.get('userInfoCache')?.mid;
}
List currentList = loadingState.value is Success
? (loadingState.value as Success).response
: [];
List dataList = currentPage == 1
? response.response.medias
: currentList + response.response.medias;
loadingState.value = LoadingState.success(dataList);
if (dataList.length >= response.response.info.mediaCount) {
if (currentPage != 1 && loadingState.value is Success) {
response.response.medias
.insertAll(0, (loadingState.value as Success).response);
}
loadingState.value = LoadingState.success(response.response.medias);
if (response.response.medias.length >= response.response.info.mediaCount) {
loadingText.value = '没有更多了';
}
return true;

View File

@@ -140,7 +140,6 @@ class _FavSearchPageState extends State<FavSearchPage> {
videoItem: loadingState.response[index],
ctr: _favSearchCtr,
onChoose: null,
onUpdateMultiple: () => null,
);
},
childCount: loadingState.response.length,

View File

@@ -1,53 +1,47 @@
import 'package:PiliPalaX/http/loading_state.dart';
import 'package:PiliPalaX/pages/common/common_controller.dart';
import 'package:PiliPalaX/utils/extension.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:hive/hive.dart';
import 'package:PiliPalaX/http/user.dart';
import 'package:PiliPalaX/models/user/history.dart';
import 'package:PiliPalaX/utils/storage.dart';
class HistoryController extends GetxController {
final ScrollController scrollController = ScrollController();
RxList<HisListItem> historyList = <HisListItem>[].obs;
RxBool isLoadingMore = false.obs;
class HistoryController extends CommonController {
RxBool pauseStatus = false.obs;
Box localCache = GStorage.localCache;
RxBool isLoading = false.obs;
RxBool enableMultiple = false.obs;
RxInt checkedCount = 0.obs;
int? max;
int? viewAt;
@override
void onInit() {
super.onInit();
historyStatus();
queryData();
}
Future queryHistoryList({type = 'init'}) async {
int max = 0;
int viewAt = 0;
if (type == 'onload' && historyList.isNotEmpty) {
max = historyList.last.history!.oid!;
viewAt = historyList.last.viewAt!;
}
isLoadingMore.value = true;
var res = await UserHttp.historyList(max, viewAt);
isLoadingMore.value = false;
if (res['status']) {
if (type == 'onload') {
historyList.addAll(res['data'].list);
} else {
historyList.value = res['data'].list;
}
}
return res;
@override
Future onRefresh() {
max = null;
viewAt = null;
return super.onRefresh();
}
Future onLoad() async {
queryHistoryList(type: 'onload');
@override
bool customHandleResponse(Success response) {
HistoryData data = response.response;
isEnd = data.list.isNullOrEmpty || data.list!.length < 20;
max = data.list?.lastOrNull?.history?.oid;
viewAt = data.list?.lastOrNull?.viewAt;
if (currentPage != 1 && loadingState.value is Success) {
data.list?.insertAll(0, (loadingState.value as Success).response);
}
Future onRefresh() async {
queryHistoryList(type: 'onRefresh');
loadingState.value = LoadingState.success(data.list);
return true;
}
// 暂停观看历史
@@ -60,7 +54,7 @@ class HistoryController extends GetxController {
content:
Text(!pauseStatus.value ? '啊叻?你要暂停历史记录功能吗?' : '啊叻?要恢复历史记录功能吗?'),
actions: [
TextButton(onPressed: () => Get.back(), child: const Text('取消')),
TextButton(onPressed: Get.back, child: const Text('取消')),
TextButton(
onPressed: () async {
SmartDialog.showLoading(msg: '请求中');
@@ -70,7 +64,8 @@ class HistoryController extends GetxController {
SmartDialog.showToast(
!pauseStatus.value ? '暂停观看历史' : '恢复观看历史');
pauseStatus.value = !pauseStatus.value;
localCache.put(LocalCacheKey.historyPause, pauseStatus.value);
GStorage.localCache
.put(LocalCacheKey.historyPause, pauseStatus.value);
}
Get.back();
},
@@ -87,7 +82,7 @@ class HistoryController extends GetxController {
var res = await UserHttp.historyStatus();
if (res['status']) {
pauseStatus.value = res['data'];
localCache.put(LocalCacheKey.historyPause, res['data']);
GStorage.localCache.put(LocalCacheKey.historyPause, res['data']);
} else {
SmartDialog.showToast(res['msg']);
}
@@ -102,7 +97,7 @@ class HistoryController extends GetxController {
title: const Text('提示'),
content: const Text('啊叻?你要清空历史记录功能吗?'),
actions: [
TextButton(onPressed: () => Get.back(), child: const Text('取消')),
TextButton(onPressed: Get.back, child: const Text('取消')),
TextButton(
onPressed: () async {
SmartDialog.showLoading(msg: '请求中');
@@ -112,7 +107,7 @@ class HistoryController extends GetxController {
SmartDialog.showToast('清空观看历史');
}
Get.back();
historyList.clear();
loadingState.value = LoadingState.success(<HisListItem>[]);
},
child: const Text('确认清空'),
)
@@ -124,48 +119,38 @@ class HistoryController extends GetxController {
// 删除某条历史记录
Future delHistory(kid, business) async {
// String resKid = 'archive_$kid';
// if (business == 'live') {
// resKid = 'live_$kid';
// } else if (business.contains('article')) {
// resKid = 'article_$kid';
// }
// var res = await UserHttp.delHistory(resKid);
// if (res['status']) {
// historyList.removeWhere((e) => e.kid == kid);
// SmartDialog.showToast(res['msg']);
// }
_onDelete(historyList.where((e) => e.kid == kid).toList());
_onDelete(((loadingState.value as Success).response as List<HisListItem>)
.where((e) => e.kid == kid)
.toList());
}
// 删除已看历史记录
void onDelHistory() {
if (loadingState.value is Success) {
List<HisListItem> list =
historyList.where((e) => e.progress == -1).toList();
((loadingState.value as Success).response as List<HisListItem>)
.where((e) => e.progress == -1)
.toList();
if (list.isNotEmpty) {
_onDelete(list);
} else {
SmartDialog.showToast('无已看记录');
}
}
}
void _onDelete(List<HisListItem> result) async {
SmartDialog.showLoading(msg: '请求中');
List kidList = result.map((item) {
// String tag = 'archive';
// if (item.history?.business == 'live') {
// tag = 'live';
// } else if (item.history?.business?.contains('article') == true) {
// tag = 'article-list';
// }
return '${item.history?.business}_${item.kid}';
}).toList();
dynamic response = await UserHttp.delHistory(kidList);
if (response['status']) {
Set<HisListItem> remainList =
historyList.toSet().difference(result.toSet());
historyList.value = remainList.toList();
((loadingState.value as Success).response as List<HisListItem>)
.toSet()
.difference(result.toSet());
loadingState.value = LoadingState.success(remainList.toList());
if (enableMultiple.value) {
checkedCount.value = 0;
enableMultiple.value = false;
@@ -185,7 +170,7 @@ class HistoryController extends GetxController {
content: const Text('确认删除所选历史记录吗?'),
actions: [
TextButton(
onPressed: () => Get.back(),
onPressed: Get.back,
child: Text(
'取消',
style: TextStyle(
@@ -196,7 +181,10 @@ class HistoryController extends GetxController {
TextButton(
onPressed: () async {
Get.back();
_onDelete(historyList.where((e) => e.checked!).toList());
_onDelete(((loadingState.value as Success).response
as List<HisListItem>)
.where((e) => e.checked)
.toList());
},
child: const Text('确认'),
)
@@ -207,8 +195,6 @@ class HistoryController extends GetxController {
}
@override
void onClose() {
scrollController.dispose();
super.onClose();
}
Future<LoadingState> customGetData() =>
UserHttp.historyList(max: max, viewAt: viewAt);
}

View File

@@ -1,6 +1,9 @@
import 'package:PiliPalaX/common/widgets/http_error.dart';
import 'package:PiliPalaX/common/widgets/refresh_indicator.dart';
import 'package:PiliPalaX/http/loading_state.dart';
import 'package:PiliPalaX/models/user/history.dart';
import 'package:PiliPalaX/pages/fav_search/view.dart' show SearchType;
import 'package:PiliPalaX/utils/extension.dart';
import 'package:easy_debounce/easy_throttle.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
@@ -19,43 +22,35 @@ class HistoryPage extends StatefulWidget {
}
class _HistoryPageState extends State<HistoryPage> {
final HistoryController _historyController = Get.put(HistoryController());
Future? _futureBuilderFuture;
final _historyController = Get.put(HistoryController());
@override
void initState() {
super.initState();
_futureBuilderFuture = _historyController.queryHistoryList();
_historyController.scrollController.addListener(
() {
if (_historyController.scrollController.position.pixels >=
_historyController.scrollController.position.maxScrollExtent -
300) {
if (!_historyController.isLoadingMore.value) {
EasyThrottle.throttle('history', const Duration(seconds: 1), () {
_historyController.onLoad();
_historyController.onLoadMore();
});
}
}
},
);
_historyController.enableMultiple.listen((p0) {
setState(() {});
});
}
// 选中
onChoose(index) {
_historyController.historyList[index].checked =
!_historyController.historyList[index].checked!;
List<HisListItem> list =
(_historyController.loadingState.value as Success).response;
list[index].checked = list[index].checked.not;
_historyController.checkedCount.value =
_historyController.historyList.where((item) => item.checked!).length;
_historyController.historyList.refresh();
list.where((item) => item.checked).length;
_historyController.loadingState.value = LoadingState.success(list);
if (_historyController.checkedCount.value == 0) {
_historyController.enableMultiple.value = false;
}
// 更新多选状态
onUpdateMultiple() {
setState(() {});
}
@override
@@ -66,7 +61,15 @@ class _HistoryPageState extends State<HistoryPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
return Obx(
() => PopScope(
canPop: _historyController.enableMultiple.value.not,
onPopInvokedWithResult: (didPop, result) {
if (_historyController.enableMultiple.value) {
_handleSelect();
}
},
child: Scaffold(
appBar: AppBarWidget(
visible: _historyController.enableMultiple.value,
child1: AppBar(
@@ -74,9 +77,12 @@ class _HistoryPageState extends State<HistoryPage> {
actions: [
IconButton(
tooltip: '搜索',
onPressed: () => Get.toNamed('/favSearch', arguments: {
onPressed: () => Get.toNamed(
'/favSearch',
arguments: {
'searchType': SearchType.history,
}),
},
),
icon: const Icon(Icons.search_outlined),
),
PopupMenuButton<String>(
@@ -94,18 +100,19 @@ class _HistoryPageState extends State<HistoryPage> {
break;
case 'multiple':
_historyController.enableMultiple.value = true;
setState(() {});
break;
default:
}
},
itemBuilder: (BuildContext context) => <PopupMenuEntry<String>>[
itemBuilder: (BuildContext context) =>
<PopupMenuEntry<String>>[
PopupMenuItem<String>(
value: 'pause',
child: Obx(
() => Text(!_historyController.pauseStatus.value
() => Text(
!_historyController.pauseStatus.value
? '暂停观看记录'
: '恢复观看记录'),
: '恢复观看记录',
),
),
),
const PopupMenuItem<String>(
@@ -128,14 +135,7 @@ class _HistoryPageState extends State<HistoryPage> {
child2: AppBar(
leading: IconButton(
tooltip: '取消',
onPressed: () {
_historyController.enableMultiple.value = false;
for (var item in _historyController.historyList) {
item.checked = false;
}
_historyController.checkedCount.value = 0;
setState(() {});
},
onPressed: _handleSelect,
icon: const Icon(Icons.close_outlined),
),
title: Obx(
@@ -145,21 +145,16 @@ class _HistoryPageState extends State<HistoryPage> {
),
actions: [
TextButton(
onPressed: () {
for (var item in _historyController.historyList) {
item.checked = true;
}
_historyController.checkedCount.value =
_historyController.historyList.length;
_historyController.historyList.refresh();
},
onPressed: () => _handleSelect(true),
child: const Text('全选'),
),
TextButton(
onPressed: () => _historyController.onDelCheckedHistory(context),
onPressed: () =>
_historyController.onDelCheckedHistory(context),
child: Text(
'删除',
style: TextStyle(color: Theme.of(context).colorScheme.error),
style:
TextStyle(color: Theme.of(context).colorScheme.error),
),
),
const SizedBox(width: 6),
@@ -169,91 +164,84 @@ class _HistoryPageState extends State<HistoryPage> {
body: refreshIndicator(
onRefresh: () async {
await _historyController.onRefresh();
return;
},
child: CustomScrollView(
physics: const AlwaysScrollableScrollPhysics(),
controller: _historyController.scrollController,
slivers: [
FutureBuilder(
future: _futureBuilderFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.data == null) {
return const SliverToBoxAdapter(child: SizedBox());
}
Map data = snapshot.data;
if (data['status']) {
return Obx(
() => _historyController.historyList.isNotEmpty
? SliverGrid(
gridDelegate:
SliverGridDelegateWithExtentAndRatio(
mainAxisSpacing: StyleString.cardSpace,
crossAxisSpacing: StyleString.safeSpace,
maxCrossAxisExtent: Grid.maxRowWidth * 2,
childAspectRatio:
StyleString.aspectRatio * 2.4,
mainAxisExtent: 0),
delegate: SliverChildBuilderDelegate(
(context, index) {
return HistoryItem(
videoItem:
_historyController.historyList[index],
ctr: _historyController,
onChoose: () => onChoose(index),
onUpdateMultiple: () => onUpdateMultiple(),
);
},
childCount:
_historyController.historyList.length),
)
: _historyController.isLoadingMore.value
? const SliverToBoxAdapter(
child: Center(child: Text('加载中')),
)
: HttpError(
callback: () => setState(() {
_futureBuilderFuture =
_historyController.queryHistoryList();
}),
Obx(() => _buildBody(_historyController.loadingState.value)),
SliverToBoxAdapter(
child: SizedBox(
height: MediaQuery.of(context).padding.bottom + 10,
),
),
],
),
),
),
),
);
} else {
return HttpError(
errMsg: data['msg'],
callback: () => setState(() {
_futureBuilderFuture =
_historyController.queryHistoryList();
}),
);
}
} else {
// 骨架屏
return SliverGrid(
Widget _buildBody(LoadingState loadingState) {
return switch (loadingState) {
Loading() => SliverGrid(
gridDelegate: SliverGridDelegateWithExtentAndRatio(
mainAxisSpacing: StyleString.cardSpace,
crossAxisSpacing: StyleString.safeSpace,
maxCrossAxisExtent: Grid.maxRowWidth * 2,
childAspectRatio: StyleString.aspectRatio * 2.4,
mainAxisExtent: 0),
delegate: SliverChildBuilderDelegate((context, index) {
return const VideoCardHSkeleton();
}, childCount: 10),
);
}
},
mainAxisExtent: 0,
),
SliverToBoxAdapter(
child: SizedBox(
height: MediaQuery.of(context).padding.bottom + 10,
delegate: SliverChildBuilderDelegate(
(context, index) {
return const VideoCardHSkeleton();
},
childCount: 10,
),
),
Success() => (loadingState.response as List?)?.isNotEmpty == true
? SliverGrid(
gridDelegate: SliverGridDelegateWithExtentAndRatio(
mainAxisSpacing: StyleString.cardSpace,
crossAxisSpacing: StyleString.safeSpace,
maxCrossAxisExtent: Grid.maxRowWidth * 2,
childAspectRatio: StyleString.aspectRatio * 2.4,
mainAxisExtent: 0,
),
delegate: SliverChildBuilderDelegate(
(context, index) {
return HistoryItem(
videoItem: loadingState.response[index],
ctr: _historyController,
onChoose: () => onChoose(index),
);
},
childCount: loadingState.response.length,
),
)
],
: HttpError(
callback: _historyController.onReload,
),
Error() => HttpError(
errMsg: loadingState.errMsg,
callback: _historyController.onReload,
),
// bottomNavigationBar: BottomAppBar(),
);
LoadingState() => throw UnimplementedError(),
};
}
void _handleSelect([bool checked = false]) {
List<HisListItem>? list =
(_historyController.loadingState.value as Success?)?.response;
if (list.isNullOrEmpty.not) {
_historyController.loadingState.value = LoadingState.success(
list!.map((item) => item..checked = checked).toList());
_historyController.checkedCount.value = checked ? list.length : 0;
}
if (checked.not) {
_historyController.enableMultiple.value = false;
}
}
}

View File

@@ -19,13 +19,11 @@ class HistoryItem extends StatelessWidget {
final dynamic videoItem;
final dynamic ctr;
final Function? onChoose;
final Function? onUpdateMultiple;
const HistoryItem({
super.key,
required this.videoItem,
this.ctr,
this.onChoose,
this.onUpdateMultiple,
});
@override
@@ -159,7 +157,6 @@ class HistoryItem extends StatelessWidget {
feedBack();
ctr!.enableMultiple.value = true;
onChoose?.call();
onUpdateMultiple?.call();
}
},
child: Column(
@@ -190,6 +187,7 @@ class HistoryItem extends StatelessWidget {
Hero(
tag: heroTag,
child: NetworkImgLayer(
radius: 12,
src: (videoItem.cover != ''
? videoItem.cover
: videoItem.covers.first),
@@ -226,19 +224,14 @@ class HistoryItem extends StatelessWidget {
},
),
),
Obx(
() => Positioned.fill(
Positioned.fill(
child: AnimatedOpacity(
opacity: ctr!.enableMultiple.value ? 1 : 0,
opacity: videoItem.checked ? 1 : 0,
duration: const Duration(milliseconds: 200),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12),
color: Colors.black.withOpacity(
ctr!.enableMultiple.value &&
videoItem.checked
? 0.6
: 0),
color: Colors.black.withOpacity(0.6),
),
child: Center(
child: SizedBox(
@@ -257,7 +250,9 @@ class HistoryItem extends StatelessWidget {
backgroundColor:
WidgetStateProperty.resolveWith(
(states) {
return Colors.white
return Theme.of(context)
.colorScheme
.surface
.withOpacity(0.8);
},
),
@@ -277,7 +272,6 @@ class HistoryItem extends StatelessWidget {
),
),
),
),
if (videoItem.duration != null &&
videoItem.duration != 0 &&
videoItem.progress != 0)

View File

@@ -19,12 +19,11 @@ class LaterController extends CommonController {
if (currentPage == 1) {
count.value = response.response['count'];
}
List currentList = loadingState.value is Success
? (loadingState.value as Success).response
: [];
loadingState.value = currentPage == 1
? LoadingState.success(response.response['list'])
: LoadingState.success(currentList + response.response['list']);
if (currentPage != 1 && loadingState.value is Success) {
response.response['list']
.insertAll(0, (loadingState.value as Success).response);
}
loadingState.value = LoadingState.success(response.response['list']);
return true;
}

View File

@@ -13,9 +13,8 @@ class MemberDynamicCtr extends CommonController {
bool customHandleResponse(Success response) {
DynSpaceRsp res = response.response;
isEnd = !res.hasMore;
if (currentPage != 1) {
res.list.insertAll(
0, (loadingState.value as Success?)?.response ?? <DynamicItem>[]);
if (currentPage != 1 && loadingState.value is Success) {
res.list.insertAll(0, (loadingState.value as Success).response);
}
loadingState.value = LoadingState.success(res.list);
return true;

View File

@@ -36,17 +36,11 @@ class SearchPanelController extends CommonController {
searchResultController.count[SearchType.values.indexOf(searchType!)] =
response.response.numResults;
if (response.response.list != null) {
List currentList = currentPage != 1 && loadingState.value is Success
? (loadingState.value as Success).response
: [];
List dataList = currentPage == 1
? response.response.list
: currentList + response.response.list;
if (dataList.isNotEmpty) {
loadingState.value = LoadingState.success(dataList);
} else {
loadingState.value = LoadingState.success([]);
if (currentPage != 1 && loadingState.value is Success) {
response.response.list
?.insertAll(0, (loadingState.value as Success).response);
}
loadingState.value = LoadingState.success(response.response.list);
if (currentPage == 1) {
onPushDetail(response.response.list);
}