refa: live page

Signed-off-by: bggRGjQaUbCoE <githubaccount56556@proton.me>
This commit is contained in:
bggRGjQaUbCoE
2025-05-05 17:03:49 +08:00
parent 7689fe8aa4
commit 562f9035e8
31 changed files with 1334 additions and 461 deletions

View File

@@ -1,109 +1,26 @@
import 'package:PiliPlus/http/live.dart';
import 'package:PiliPlus/http/loading_state.dart';
import 'package:PiliPlus/models/live/follow.dart';
import 'package:PiliPlus/models/live/item.dart';
import 'package:PiliPlus/models/live/live_feed_index/card_list.dart';
import 'package:PiliPlus/models/live/live_feed_index/data.dart';
import 'package:PiliPlus/pages/common/common_list_controller.dart';
import 'package:PiliPlus/utils/extension.dart';
import 'package:PiliPlus/utils/request_utils.dart';
import 'package:PiliPlus/utils/storage.dart';
import 'package:get/get_rx/src/rx_types/rx_types.dart';
class LiveController
extends CommonListController<List<LiveItemModel>?, LiveItemModel> {
class LiveController extends CommonListController<LiveIndexData, LiveCardList> {
@override
void onInit() {
super.onInit();
queryData();
if (isLogin.value) {
fetchLiveFollowing();
}
}
String? gaiaVtoken;
final RxBool isLogin = Accounts.main.isLogin.obs;
@override
Future<LoadingState<List<LiveItemModel>?>> customGetData() =>
LiveHttp.liveList(pn: currentPage, gaiaVtoken: gaiaVtoken);
@override
bool handleError(String? errMsg) {
if (errMsg?.startsWith('voucher') == true) {
loadingState.value = LoadingState.error(' -352 ');
RequestUtils.validate(
errMsg!,
(gaiaVtoken) {
this.gaiaVtoken = gaiaVtoken;
onReload();
},
);
return true;
}
return false;
List<LiveCardList>? getDataList(LiveIndexData response) {
return response.cardList;
}
@override
Future onRefresh() {
fetchLiveFollowing();
return super.onRefresh();
}
@override
Future onReload() {
if (loadingState.value is Error) {
String? errMsg = (loadingState.value as Error).errMsg;
if (errMsg == '-352') {
gaiaVtoken = null;
}
}
return super.onReload();
}
late RxBool isLogin = Accounts.main.isLogin.obs;
late Rx<LoadingState<List<LiveFollowingItemModel>?>> followListState =
Rx(LoadingState.loading());
late int followPage = 1;
late bool followEnd = false;
late RxInt liveCount = 0.obs;
Future fetchLiveFollowing([bool isRefresh = true]) async {
if (!isLogin.value || (isRefresh.not && followEnd)) {
return;
}
if (isRefresh) {
followPage = 1;
followEnd = false;
}
dynamic res = await LiveHttp.liveFollowing(pn: followPage, ps: 20);
if (res['status']) {
LiveFollowingModel data = res['data'];
liveCount.value = data.liveCount ?? 0;
List<LiveFollowingItemModel>? dataList = data.list
?.where((LiveFollowingItemModel item) =>
item.liveStatus == 1 && item.recordLiveTime == 0)
.toList();
if (dataList.isNullOrEmpty) {
followEnd = true;
if (isRefresh) {
followListState.value = LoadingState.success(dataList);
}
return;
}
if (isRefresh) {
if (dataList!.length >= liveCount.value) {
followEnd = true;
}
followListState.value = LoadingState.success(dataList);
} else if (followListState.value is Success) {
List<LiveFollowingItemModel> list = followListState.value.data!
..addAll(dataList!);
if (list.length >= liveCount.value) {
followEnd = true;
}
followListState.refresh();
}
followPage++;
} else if (isRefresh) {
followListState.value = LoadingState.error(res['msg']);
}
}
Future<LoadingState<LiveIndexData>> customGetData() =>
LiveHttp.liveFeedIndex(pn: currentPage, isLogin: isLogin.value);
}

View File

@@ -2,17 +2,17 @@ import 'package:PiliPlus/common/constants.dart';
import 'package:PiliPlus/common/skeleton/video_card_v.dart';
import 'package:PiliPlus/common/widgets/image/network_img_layer.dart';
import 'package:PiliPlus/common/widgets/loading_widget/http_error.dart';
import 'package:PiliPlus/common/widgets/loading_widget/loading_widget.dart';
import 'package:PiliPlus/common/widgets/refresh_indicator.dart';
import 'package:PiliPlus/common/widgets/self_sized_horizontal_list.dart';
import 'package:PiliPlus/http/loading_state.dart';
import 'package:PiliPlus/models/live/item.dart';
import 'package:PiliPlus/models/live/live_feed_index/card_data_list_item.dart';
import 'package:PiliPlus/models/live/live_feed_index/card_list.dart';
import 'package:PiliPlus/pages/common/common_page.dart';
import 'package:PiliPlus/pages/live/controller.dart';
import 'package:PiliPlus/pages/live/widgets/live_item.dart';
import 'package:PiliPlus/pages/live/widgets/live_item_follow.dart';
import 'package:PiliPlus/pages/live/widgets/live_item_app.dart';
import 'package:PiliPlus/pages/live_follow/view.dart';
import 'package:PiliPlus/utils/extension.dart';
import 'package:PiliPlus/utils/grid.dart';
import 'package:PiliPlus/utils/utils.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
@@ -49,18 +49,6 @@ class _LivePageState extends CommonPageState<LivePage, LiveController>
controller: controller.scrollController,
physics: const AlwaysScrollableScrollPhysics(),
slivers: [
Obx(
() => controller.isLogin.value
? SliverPadding(
padding: const EdgeInsets.symmetric(
vertical: StyleString.cardSpace,
),
sliver: SliverToBoxAdapter(
child: _buildFollowList(),
),
)
: const SliverToBoxAdapter(),
),
SliverPadding(
padding: EdgeInsets.only(
top: StyleString.cardSpace,
@@ -74,7 +62,7 @@ class _LivePageState extends CommonPageState<LivePage, LiveController>
);
}
Widget _buildBody(LoadingState<List<LiveItemModel>?> loadingState) {
Widget _buildBody(LoadingState<List<LiveCardList>?> loadingState) {
return switch (loadingState) {
Loading() => SliverGrid(
gridDelegate: SliverGridDelegateWithExtentAndRatio(
@@ -92,25 +80,48 @@ class _LivePageState extends CommonPageState<LivePage, LiveController>
),
),
Success() => loadingState.response?.isNotEmpty == true
? SliverGrid(
gridDelegate: SliverGridDelegateWithExtentAndRatio(
mainAxisSpacing: StyleString.cardSpace,
crossAxisSpacing: StyleString.cardSpace,
maxCrossAxisExtent: Grid.smallCardWidth,
childAspectRatio: StyleString.aspectRatio,
mainAxisExtent: MediaQuery.textScalerOf(context).scale(90),
),
delegate: SliverChildBuilderDelegate(
(context, index) {
if (index == loadingState.response!.length - 1) {
controller.onLoadMore();
}
return LiveCardV(liveItem: loadingState.response![index]);
},
childCount: loadingState.response!.length,
),
)
: scrollErrorWidget(onReload: controller.onReload),
? Builder(builder: (context) {
List<LiveCardList> list = loadingState.response!;
LiveCardList? followItem;
if (list.first.cardType == 'my_idol_v1') {
followItem = list.first;
list = list.sublist(1);
}
return SliverMainAxisGroup(
slivers: [
if (followItem != null)
SliverPadding(
padding: const EdgeInsets.only(bottom: 12),
sliver: SliverToBoxAdapter(
child: _buildFollowList(followItem),
),
),
if (list.isNotEmpty)
SliverGrid(
gridDelegate: SliverGridDelegateWithExtentAndRatio(
mainAxisSpacing: StyleString.cardSpace,
crossAxisSpacing: StyleString.cardSpace,
maxCrossAxisExtent: Grid.smallCardWidth,
childAspectRatio: StyleString.aspectRatio,
mainAxisExtent:
MediaQuery.textScalerOf(context).scale(90),
),
delegate: SliverChildBuilderDelegate(
(context, index) {
if (index == list.length - 1) {
controller.onLoadMore();
}
return LiveCardVApp(
item: list[index].cardData!.smallCardV1!,
);
},
childCount: list.length,
),
),
],
);
})
: HttpError(onReload: controller.onReload),
Error() => HttpError(
errMsg: loadingState.errMsg,
onReload: controller.onReload,
@@ -118,7 +129,7 @@ class _LivePageState extends CommonPageState<LivePage, LiveController>
};
}
Widget _buildFollowList() {
Widget _buildFollowList(LiveCardList item) {
final theme = Theme.of(context);
return Column(
mainAxisSize: MainAxisSize.min,
@@ -126,33 +137,32 @@ class _LivePageState extends CommonPageState<LivePage, LiveController>
children: [
Row(
children: [
Obx(
() => Text.rich(
TextSpan(
children: [
const TextSpan(text: '我的关注 '),
TextSpan(
text: '${controller.liveCount.value}',
style: TextStyle(
fontSize: 13,
color: theme.colorScheme.primary,
),
Text.rich(
TextSpan(
children: [
const TextSpan(text: '我的关注 '),
TextSpan(
text:
'${item.cardData?.myIdolV1?.extraInfo?.totalCount ?? 0}',
style: TextStyle(
fontSize: 13,
color: theme.colorScheme.primary,
),
TextSpan(
text: '人正在直播',
style: TextStyle(
fontSize: 13,
color: theme.colorScheme.outline,
),
),
TextSpan(
text: '人正在直播',
style: TextStyle(
fontSize: 13,
color: theme.colorScheme.outline,
),
],
),
),
],
),
),
const Spacer(),
GestureDetector(
onTap: () {
Get.to(_buildFollowListPage);
Get.to(const LiveFollowPage());
},
child: Row(
mainAxisSize: MainAxisSize.min,
@@ -173,161 +183,69 @@ class _LivePageState extends CommonPageState<LivePage, LiveController>
),
],
),
Obx(() => _buildFollowBody(theme, controller.followListState.value)),
_buildFollowBody(theme, item.cardData?.myIdolV1?.list),
],
);
}
Widget _buildFollowBody(ThemeData theme, LoadingState loadingState) {
return switch (loadingState) {
Loading() => SizedBox(
height: 80,
child: loadingWidget,
),
Success() => (loadingState.response as List?)?.isNotEmpty == true
? MediaQuery.removePadding(
context: context,
removeLeft: context.orientation == Orientation.landscape,
child: SelfSizedHorizontalList(
gapSize: 5,
childBuilder: (index) {
if (index == loadingState.response.length - 1) {
controller.fetchLiveFollowing(false);
}
return SizedBox(
width: 65,
child: GestureDetector(
onTap: () {
Get.toNamed(
'/liveRoom?roomid=${loadingState.response[index].roomId}',
);
},
onLongPress: () {
Feedback.forLongPress(context);
Get.toNamed(
'/member?mid=${loadingState.response[index].uid}',
arguments: {
'face': loadingState.response[index].face,
'heroTag': Utils.makeHeroTag(
loadingState.response[index].uid)
},
);
},
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const SizedBox(height: 8),
Container(
margin: const EdgeInsets.all(2),
padding: const EdgeInsets.all(2),
decoration: BoxDecoration(
border: Border.all(
width: 1.5,
color: theme.colorScheme.primary,
strokeAlign: BorderSide.strokeAlignOutside,
),
shape: BoxShape.circle,
),
child: NetworkImgLayer(
type: 'avatar',
width: 45,
height: 45,
src: loadingState.response[index].face,
),
),
const SizedBox(height: 4),
Text(
loadingState.response[index].uname,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: const TextStyle(fontSize: 12),
textAlign: TextAlign.center,
),
],
),
),
);
},
itemCount: loadingState.response.length,
),
)
: const SizedBox.shrink(),
Error() => GestureDetector(
onTap: () {
controller.fetchLiveFollowing();
},
child: Container(
height: 80,
alignment: Alignment.center,
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Text(
loadingState.errMsg,
textAlign: TextAlign.center,
),
),
),
};
}
Widget get _buildFollowListPage => Scaffold(
appBar: AppBar(
title: Obx(
() => Text('${controller.liveCount.value}人正在直播'),
),
),
body: SafeArea(
top: false,
bottom: false,
child: CustomScrollView(
slivers: [
Obx(() => _buildFollowListBody(controller.followListState.value)),
],
),
),
);
Widget _buildFollowListBody(LoadingState loadingState) {
return switch (loadingState) {
Success() || Loading() => SliverPadding(
padding: EdgeInsets.only(
top: StyleString.safeSpace,
left: StyleString.safeSpace,
right: StyleString.safeSpace,
bottom: MediaQuery.paddingOf(context).bottom + 80,
),
sliver: SliverGrid(
gridDelegate: SliverGridDelegateWithExtentAndRatio(
mainAxisSpacing: StyleString.cardSpace,
crossAxisSpacing: StyleString.cardSpace,
maxCrossAxisExtent: Grid.smallCardWidth,
childAspectRatio: StyleString.aspectRatio,
mainAxisExtent: MediaQuery.textScalerOf(context).scale(90),
),
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
if (loadingState is Success &&
index == loadingState.response.length - 1) {
controller.fetchLiveFollowing(false);
}
return loadingState is Success
? LiveCardVFollow(
liveItem: loadingState.response[index],
)
: const VideoCardVSkeleton();
Widget _buildFollowBody(ThemeData theme, List<CardLiveItem>? list) {
if (list.isNullOrEmpty) {
return const SizedBox.shrink();
}
return MediaQuery.removePadding(
context: context,
removeLeft: context.orientation == Orientation.landscape,
child: SelfSizedHorizontalList(
gapSize: 5,
childBuilder: (index) {
final item = list[index];
return SizedBox(
width: 65,
child: GestureDetector(
onTap: () {
Get.toNamed('/liveRoom?roomid=${item.roomid}');
},
childCount:
loadingState is Success ? loadingState.response.length : 10,
onLongPress: () {
Feedback.forLongPress(context);
Get.toNamed('/member?mid=${item.uid}');
},
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const SizedBox(height: 8),
Container(
margin: const EdgeInsets.all(2),
padding: const EdgeInsets.all(2),
decoration: BoxDecoration(
border: Border.all(
width: 1.5,
color: theme.colorScheme.primary,
strokeAlign: BorderSide.strokeAlignOutside,
),
shape: BoxShape.circle,
),
child: NetworkImgLayer(
type: 'avatar',
width: 45,
height: 45,
src: item.face,
),
),
const SizedBox(height: 4),
Text(
item.uname!,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: const TextStyle(fontSize: 12),
textAlign: TextAlign.center,
),
],
),
),
),
),
Error() => HttpError(
errMsg: loadingState.errMsg,
onReload: () {
controller
..followListState.value = LoadingState.loading()
..fetchLiveFollowing();
},
),
};
);
},
itemCount: list!.length,
),
);
}
}

View File

@@ -1,33 +1,33 @@
import 'package:PiliPlus/common/constants.dart';
import 'package:PiliPlus/common/widgets/image/image_save.dart';
import 'package:PiliPlus/common/widgets/image/network_img_layer.dart';
import 'package:PiliPlus/models/live/follow.dart';
import 'package:PiliPlus/models/live/live_feed_index/card_data_list_item.dart';
import 'package:PiliPlus/utils/utils.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
// -
class LiveCardVFollow extends StatelessWidget {
final LiveFollowingItemModel liveItem;
class LiveCardVApp extends StatelessWidget {
final CardLiveItem item;
const LiveCardVFollow({
const LiveCardVApp({
super.key,
required this.liveItem,
required this.item,
});
@override
Widget build(BuildContext context) {
String heroTag = Utils.makeHeroTag(liveItem.roomId);
String heroTag = Utils.makeHeroTag(item.id);
return Card(
clipBehavior: Clip.hardEdge,
margin: EdgeInsets.zero,
child: InkWell(
onTap: () {
Get.toNamed('/liveRoom?roomid=${liveItem.roomId}');
Get.toNamed('/liveRoom?roomid=${item.id}');
},
onLongPress: () => imageSaveDialog(
title: liveItem.title,
cover: liveItem.roomCover,
title: item.title,
cover: item.cover,
),
child: Column(
children: [
@@ -42,7 +42,7 @@ class LiveCardVFollow extends StatelessWidget {
Hero(
tag: heroTag,
child: NetworkImgLayer(
src: liveItem.roomCover!,
src: item.cover!,
width: maxWidth,
height: maxHeight,
),
@@ -79,7 +79,7 @@ class LiveCardVFollow extends StatelessWidget {
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'${liveItem.title}',
'${item.title}',
textAlign: TextAlign.start,
style: const TextStyle(
letterSpacing: 0.3,
@@ -91,7 +91,7 @@ class LiveCardVFollow extends StatelessWidget {
children: [
Expanded(
child: Text(
'${liveItem.uname}',
'${item.uname}',
textAlign: TextAlign.start,
style: TextStyle(
fontSize: theme.textTheme.labelMedium!.fontSize,
@@ -128,13 +128,14 @@ class LiveCardVFollow extends StatelessWidget {
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'${liveItem.areaName}',
style: const TextStyle(fontSize: 11, color: Colors.white),
),
Text(
liveItem.textSmall ?? '',
'${item.areaName}',
style: const TextStyle(fontSize: 11, color: Colors.white),
),
if (item.watchedShow?.textSmall != null)
Text(
'${Utils.numFormat(item.watchedShow!.textSmall)}围观',
style: const TextStyle(fontSize: 11, color: Colors.white),
),
],
),
);