mirror of
https://github.com/HChaZZY/PiliPlus.git
synced 2025-12-06 09:13:48 +08:00
refactor: later
This commit is contained in:
@@ -114,25 +114,20 @@ class UserHttp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 稍后再看
|
// 稍后再看
|
||||||
static Future<dynamic> seeYouLater() async {
|
static Future<LoadingState> seeYouLater() async {
|
||||||
var res = await Request().get(Api.seeYouLater);
|
var res = await Request().get(Api.seeYouLater);
|
||||||
if (res.data['code'] == 0) {
|
if (res.data['code'] == 0) {
|
||||||
if (res.data['data']['count'] == 0) {
|
if (res.data['data']['count'] == 0) {
|
||||||
return {
|
return LoadingState.empty();
|
||||||
'status': true,
|
|
||||||
'data': {'list': [], 'count': 0}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
List<HotVideoItemModel> list = [];
|
List<HotVideoItemModel> list = [];
|
||||||
for (var i in res.data['data']['list']) {
|
for (var i in res.data['data']['list']) {
|
||||||
list.add(HotVideoItemModel.fromJson(i));
|
list.add(HotVideoItemModel.fromJson(i));
|
||||||
}
|
}
|
||||||
return {
|
return LoadingState.success(
|
||||||
'status': true,
|
{'list': list, 'count': res.data['data']['count']});
|
||||||
'data': {'list': list, 'count': res.data['data']['count']}
|
|
||||||
};
|
|
||||||
} else {
|
} else {
|
||||||
return {'status': false, 'data': [], 'msg': res.data['message']};
|
return LoadingState.error(res.data['message']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,26 +1,31 @@
|
|||||||
|
import 'package:PiliPalaX/http/loading_state.dart';
|
||||||
|
import 'package:PiliPalaX/pages/common/common_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';
|
||||||
import 'package:PiliPalaX/http/user.dart';
|
import 'package:PiliPalaX/http/user.dart';
|
||||||
import 'package:PiliPalaX/models/model_hot_video_item.dart';
|
|
||||||
|
|
||||||
class LaterController extends GetxController {
|
class LaterController extends CommonController {
|
||||||
final ScrollController scrollController = ScrollController();
|
RxInt count = (-1).obs;
|
||||||
RxList<HotVideoItemModel> laterList = <HotVideoItemModel>[].obs;
|
|
||||||
int count = 0;
|
|
||||||
RxBool isLoading = false.obs;
|
|
||||||
|
|
||||||
Future queryLaterList() async {
|
@override
|
||||||
isLoading.value = true;
|
void onInit() {
|
||||||
var res = await UserHttp.seeYouLater();
|
super.onInit();
|
||||||
if (res['status']) {
|
queryData();
|
||||||
count = res['data']['count'];
|
}
|
||||||
if (count > 0) {
|
|
||||||
laterList.value = res['data']['list'];
|
@override
|
||||||
}
|
bool customHandleResponse(Success response) {
|
||||||
|
if (currentPage == 1) {
|
||||||
|
count.value = response.response['count'];
|
||||||
}
|
}
|
||||||
isLoading.value = false;
|
List currentList = loadingState.value is Success
|
||||||
return res;
|
? (loadingState.value as Success).response
|
||||||
|
: [];
|
||||||
|
loadingState.value = currentPage == 1
|
||||||
|
? LoadingState.success(response.response['list'])
|
||||||
|
: LoadingState.success(currentList + response.response['list']);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future toViewDel(BuildContext context, {int? aid}) async {
|
Future toViewDel(BuildContext context, {int? aid}) async {
|
||||||
@@ -44,10 +49,12 @@ class LaterController extends GetxController {
|
|||||||
var res = await UserHttp.toViewDel(aid: aid);
|
var res = await UserHttp.toViewDel(aid: aid);
|
||||||
if (res['status']) {
|
if (res['status']) {
|
||||||
if (aid != null) {
|
if (aid != null) {
|
||||||
laterList.removeWhere((e) => e.aid == aid);
|
List list = (loadingState.value as Success).response;
|
||||||
|
list.removeWhere((e) => e.aid == aid);
|
||||||
|
loadingState.value = LoadingState.success(list);
|
||||||
} else {
|
} else {
|
||||||
laterList.clear();
|
loadingState.value = LoadingState.loading();
|
||||||
queryLaterList();
|
onRefresh();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Get.back();
|
Get.back();
|
||||||
@@ -81,7 +88,7 @@ class LaterController extends GetxController {
|
|||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
var res = await UserHttp.toViewClear();
|
var res = await UserHttp.toViewClear();
|
||||||
if (res['status']) {
|
if (res['status']) {
|
||||||
laterList.clear();
|
loadingState.value = LoadingState.empty();
|
||||||
}
|
}
|
||||||
Get.back();
|
Get.back();
|
||||||
SmartDialog.showToast(res['msg']);
|
SmartDialog.showToast(res['msg']);
|
||||||
@@ -93,4 +100,7 @@ class LaterController extends GetxController {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<LoadingState> customGetData() => UserHttp.seeYouLater();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import 'package:PiliPalaX/http/loading_state.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:get/get.dart';
|
import 'package:get/get.dart';
|
||||||
import 'package:PiliPalaX/common/skeleton/video_card_h.dart';
|
import 'package:PiliPalaX/common/skeleton/video_card_h.dart';
|
||||||
@@ -18,13 +19,6 @@ class LaterPage extends StatefulWidget {
|
|||||||
|
|
||||||
class _LaterPageState extends State<LaterPage> {
|
class _LaterPageState extends State<LaterPage> {
|
||||||
final LaterController _laterController = Get.put(LaterController());
|
final LaterController _laterController = Get.put(LaterController());
|
||||||
Future? _futureBuilderFuture;
|
|
||||||
|
|
||||||
@override
|
|
||||||
void initState() {
|
|
||||||
_futureBuilderFuture = _laterController.queryLaterList();
|
|
||||||
super.initState();
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
@@ -33,19 +27,14 @@ class _LaterPageState extends State<LaterPage> {
|
|||||||
titleSpacing: 0,
|
titleSpacing: 0,
|
||||||
centerTitle: false,
|
centerTitle: false,
|
||||||
title: Obx(
|
title: Obx(
|
||||||
() => _laterController.laterList.isNotEmpty
|
() => Text(
|
||||||
? Text(
|
'稍后再看${_laterController.count.value == -1 ? '' : ' (${_laterController.count.value})'}',
|
||||||
'稍后再看 (${_laterController.laterList.length})',
|
style: Theme.of(context).textTheme.titleMedium,
|
||||||
style: Theme.of(context).textTheme.titleMedium,
|
),
|
||||||
)
|
|
||||||
: Text(
|
|
||||||
'稍后再看',
|
|
||||||
style: Theme.of(context).textTheme.titleMedium,
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
actions: [
|
actions: [
|
||||||
Obx(
|
Obx(
|
||||||
() => _laterController.laterList.isNotEmpty
|
() => _laterController.count.value != -1
|
||||||
? TextButton(
|
? TextButton(
|
||||||
onPressed: () => _laterController.toViewDel(context),
|
onPressed: () => _laterController.toViewDel(context),
|
||||||
child: const Text('移除已看'),
|
child: const Text('移除已看'),
|
||||||
@@ -53,7 +42,7 @@ class _LaterPageState extends State<LaterPage> {
|
|||||||
: const SizedBox(),
|
: const SizedBox(),
|
||||||
),
|
),
|
||||||
Obx(
|
Obx(
|
||||||
() => _laterController.laterList.isNotEmpty
|
() => _laterController.count.value != -1
|
||||||
? IconButton(
|
? IconButton(
|
||||||
tooltip: '一键清空',
|
tooltip: '一键清空',
|
||||||
onPressed: () => _laterController.toViewClear(context),
|
onPressed: () => _laterController.toViewClear(context),
|
||||||
@@ -73,79 +62,63 @@ class _LaterPageState extends State<LaterPage> {
|
|||||||
controller: _laterController.scrollController,
|
controller: _laterController.scrollController,
|
||||||
slivers: [
|
slivers: [
|
||||||
SliverPadding(
|
SliverPadding(
|
||||||
padding:
|
padding: EdgeInsets.only(
|
||||||
const EdgeInsets.symmetric(horizontal: StyleString.safeSpace),
|
left: StyleString.safeSpace,
|
||||||
sliver: FutureBuilder(
|
right: StyleString.safeSpace,
|
||||||
future: _futureBuilderFuture,
|
bottom: MediaQuery.of(context).padding.bottom + 10,
|
||||||
builder: (context, snapshot) {
|
|
||||||
if (snapshot.connectionState == ConnectionState.done) {
|
|
||||||
Map data = snapshot.data as Map;
|
|
||||||
if (data['status']) {
|
|
||||||
return Obx(
|
|
||||||
() => _laterController.laterList.isNotEmpty &&
|
|
||||||
!_laterController.isLoading.value
|
|
||||||
? SliverGrid(
|
|
||||||
gridDelegate:
|
|
||||||
SliverGridDelegateWithExtentAndRatio(
|
|
||||||
mainAxisSpacing: StyleString.safeSpace,
|
|
||||||
crossAxisSpacing: StyleString.safeSpace,
|
|
||||||
maxCrossAxisExtent:
|
|
||||||
Grid.maxRowWidth * 2,
|
|
||||||
childAspectRatio:
|
|
||||||
StyleString.aspectRatio * 2.4,
|
|
||||||
mainAxisExtent: 0),
|
|
||||||
delegate: SliverChildBuilderDelegate(
|
|
||||||
(context, index) {
|
|
||||||
var videoItem =
|
|
||||||
_laterController.laterList[index];
|
|
||||||
return VideoCardH(
|
|
||||||
videoItem: videoItem,
|
|
||||||
source: 'later',
|
|
||||||
longPress: () =>
|
|
||||||
_laterController.toViewDel(context,
|
|
||||||
aid: videoItem.aid));
|
|
||||||
},
|
|
||||||
childCount:
|
|
||||||
_laterController.laterList.length),
|
|
||||||
)
|
|
||||||
: _laterController.isLoading.value
|
|
||||||
? const SliverToBoxAdapter(
|
|
||||||
child: Center(child: Text('加载中')),
|
|
||||||
)
|
|
||||||
: const NoData(),
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
return HttpError(
|
|
||||||
errMsg: data['msg'],
|
|
||||||
fn: () => setState(() {
|
|
||||||
_futureBuilderFuture =
|
|
||||||
_laterController.queryLaterList();
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// 骨架屏
|
|
||||||
return SliverGrid(
|
|
||||||
gridDelegate: SliverGridDelegateWithExtentAndRatio(
|
|
||||||
mainAxisSpacing: StyleString.safeSpace,
|
|
||||||
crossAxisSpacing: StyleString.safeSpace,
|
|
||||||
maxCrossAxisExtent: Grid.maxRowWidth * 2,
|
|
||||||
childAspectRatio: StyleString.aspectRatio * 2.4,
|
|
||||||
mainAxisExtent: 0),
|
|
||||||
delegate: SliverChildBuilderDelegate((context, index) {
|
|
||||||
return const VideoCardHSkeleton();
|
|
||||||
}, childCount: 10),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)),
|
|
||||||
SliverToBoxAdapter(
|
|
||||||
child: SizedBox(
|
|
||||||
height: MediaQuery.of(context).padding.bottom + 10,
|
|
||||||
),
|
),
|
||||||
)
|
sliver: Obx(
|
||||||
|
() => _buildBody(_laterController.loadingState.value),
|
||||||
|
),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Widget _buildBody(LoadingState loadingState) {
|
||||||
|
return loadingState is Success
|
||||||
|
? SliverGrid(
|
||||||
|
gridDelegate: SliverGridDelegateWithExtentAndRatio(
|
||||||
|
mainAxisSpacing: StyleString.safeSpace,
|
||||||
|
crossAxisSpacing: StyleString.safeSpace,
|
||||||
|
maxCrossAxisExtent: Grid.maxRowWidth * 2,
|
||||||
|
childAspectRatio: StyleString.aspectRatio * 2.4,
|
||||||
|
mainAxisExtent: 0,
|
||||||
|
),
|
||||||
|
delegate: SliverChildBuilderDelegate(
|
||||||
|
(context, index) {
|
||||||
|
var videoItem = loadingState.response[index];
|
||||||
|
return VideoCardH(
|
||||||
|
videoItem: videoItem,
|
||||||
|
source: 'later',
|
||||||
|
longPress: () => _laterController.toViewDel(context,
|
||||||
|
aid: videoItem.aid));
|
||||||
|
},
|
||||||
|
childCount: loadingState.response.length,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
: loadingState is Empty
|
||||||
|
? const NoData()
|
||||||
|
: loadingState is Error
|
||||||
|
? HttpError(
|
||||||
|
errMsg: loadingState.errMsg,
|
||||||
|
fn: _laterController.onReload,
|
||||||
|
)
|
||||||
|
: SliverGrid(
|
||||||
|
gridDelegate: SliverGridDelegateWithExtentAndRatio(
|
||||||
|
mainAxisSpacing: StyleString.safeSpace,
|
||||||
|
crossAxisSpacing: StyleString.safeSpace,
|
||||||
|
maxCrossAxisExtent: Grid.maxRowWidth * 2,
|
||||||
|
childAspectRatio: StyleString.aspectRatio * 2.4,
|
||||||
|
mainAxisExtent: 0,
|
||||||
|
),
|
||||||
|
delegate: SliverChildBuilderDelegate(
|
||||||
|
(context, index) {
|
||||||
|
return const VideoCardHSkeleton();
|
||||||
|
},
|
||||||
|
childCount: 10,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user