diff --git a/lib/http/dynamics.dart b/lib/http/dynamics.dart index b794bbe9..41fd8726 100644 --- a/lib/http/dynamics.dart +++ b/lib/http/dynamics.dart @@ -71,15 +71,12 @@ class DynamicsHttp { } } - static Future followUp() async { + static Future> followUp() async { var res = await Request().get(Api.followUp); if (res.data['code'] == 0) { - return { - 'status': true, - 'data': FollowUpModel.fromJson(res.data['data']), - }; + return Success(FollowUpModel.fromJson(res.data['data'])); } else { - return {'status': false, 'msg': res.data['message']}; + return Error(res.data['message']); } } diff --git a/lib/http/follow.dart b/lib/http/follow.dart index 59db9bc9..4d35a1cf 100644 --- a/lib/http/follow.dart +++ b/lib/http/follow.dart @@ -4,7 +4,7 @@ import 'package:PiliPlus/http/loading_state.dart'; import 'package:PiliPlus/models/follow/result.dart'; class FollowHttp { - static Future followings({ + static Future> followings({ int? vmid, int? pn, int? ps, @@ -18,12 +18,11 @@ class FollowHttp { 'order_type': orderType, }); if (res.data['code'] == 0) { - return { - 'status': true, - 'data': FollowDataModel.fromJson(res.data['data']) - }; + return Success( + FollowDataModel.fromJson(res.data['data']), + ); } else { - return {'status': false, 'msg': res.data['message']}; + return Error(res.data['message']); } } diff --git a/lib/models/dynamics/up.dart b/lib/models/dynamics/up.dart index 5c64492b..ad30effd 100644 --- a/lib/models/dynamics/up.dart +++ b/lib/models/dynamics/up.dart @@ -38,59 +38,54 @@ class LiveUsers { } } -class LiveUserItem { - LiveUserItem({ - this.face, - this.isReserveRecall, - this.jumpUrl, - this.mid, - this.roomId, - this.title, - this.uname, - }); - +sealed class UserItem { String? face; + bool? hasUpdate; + late int mid; + String? uname; + + UserItem({ + this.face, + this.hasUpdate, + int? mid, + this.uname, + }) : mid = mid ?? -1; +} + +class LiveUserItem extends UserItem { bool? isReserveRecall; String? jumpUrl; - int? mid; int? roomId; String? title; - String? uname; - bool hasUpdate = false; - String type = 'live'; LiveUserItem.fromJson(Map json) { face = json['face']; isReserveRecall = json['is_reserve_recall']; jumpUrl = json['jump_url']; - mid = json['mid']; + mid = json['mid'] ?? -1; roomId = json['room_id']; title = json['title']; uname = json['uname']; + hasUpdate = false; } } -class UpItem { +class UpItem extends UserItem { UpItem({ - this.face, - this.hasUpdate, + super.face, + super.hasUpdate, // this.isReserveRecall, - this.mid, - this.uname, + super.mid, + super.uname, }); - String? face; - bool? hasUpdate; // bool? isReserveRecall; - int? mid; - String? uname; - String type = 'up'; UpItem.fromJson(Map json) { face = json['face']; hasUpdate = json['has_update']; // isReserveRecall = json['is_reserve_recall']; - mid = json['mid']; + mid = json['mid'] ?? -1; uname = json['uname']; } } diff --git a/lib/pages/dynamics/controller.dart b/lib/pages/dynamics/controller.dart index 5b6af470..5d244691 100644 --- a/lib/pages/dynamics/controller.dart +++ b/lib/pages/dynamics/controller.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'package:PiliPlus/http/dynamics.dart'; import 'package:PiliPlus/http/follow.dart'; +import 'package:PiliPlus/http/loading_state.dart'; import 'package:PiliPlus/models/common/dynamic/dynamics_type.dart'; import 'package:PiliPlus/models/dynamics/up.dart'; import 'package:PiliPlus/models/follow/result.dart'; @@ -23,15 +24,12 @@ class DynamicsController extends GetxController Rx upData = FollowUpModel().obs; // 默认获取全部动态 RxInt mid = (-1).obs; - Rx upInfo = UpItem().obs; late TabController tabController; Set tempBannedList = {}; late List tabsPageList; - RxInt initialValue = 0.obs; RxBool isLogin = false.obs; dynamic ownerMid; dynamic face; - RxBool isLoadingDynamic = false.obs; List hasUpdatedUps = []; int allFollowedUpsPage = 1; int allFollowedUpsTotal = 0; @@ -72,10 +70,6 @@ class DynamicsController extends GetxController queryFollowUp(); } - void onSelectType(value) { - initialValue.value = value; - } - Future queryFollowing2() async { if (upData.value.upList != null && upData.value.upList!.length >= allFollowedUpsTotal) { @@ -87,13 +81,12 @@ class DynamicsController extends GetxController ps: 50, orderType: 'attention', ); - if (res['status']) { + if (res.isSuccess) { upData.value.upList ??= []; upData.value.upList!.addAll( - res['data'] - .list + res.data.list! .where((e) => hasUpdatedUps.every((e1) => e.mid != e1.mid)) - .map( + .map( (FollowItemModel e) => UpItem( face: e.face, mid: e.mid, @@ -103,10 +96,10 @@ class DynamicsController extends GetxController ), ); allFollowedUpsPage += 1; - allFollowedUpsTotal = res['data'].total; + allFollowedUpsTotal = res.data.total!; upData.refresh(); } else { - SmartDialog.showToast(res['msg']); + res.toast(); } } @@ -122,30 +115,31 @@ class DynamicsController extends GetxController if (GStorage.setting .get(SettingBoxKey.dynamicsShowAllFollowedUp, defaultValue: false)) { allFollowedUpsPage = 1; - Future f1 = DynamicsHttp.followUp(); - Future f2 = FollowHttp.followings( + final f1 = DynamicsHttp.followUp(); + final f2 = FollowHttp.followings( vmid: ownerMid, pn: allFollowedUpsPage, ps: 50, orderType: 'attention', ); - List ress = await Future.wait([f1, f2]); - if (!ress[0]['status']) { - SmartDialog.showToast("获取关注动态失败:${ress[0]['msg']}"); - upData.value.errMsg = ress[0]['msg']; + final res0 = await f1; + if (!res0.isSuccess) { + SmartDialog.showToast("获取关注动态失败:$res0"); + upData.value.errMsg = (res0 as Error).errMsg; upData.refresh(); } else { - upData.value.liveUsers = ress[0]['data'].liveUsers; + upData.value.liveUsers = res0.data.liveUsers; upData.refresh(); - hasUpdatedUps = ress[0]['data'].upList!; + hasUpdatedUps = res0.data.upList!; } List allFollowedUps = []; - if (!ress[1]['status']) { - SmartDialog.showToast("获取关注列表失败:${ress[1]['msg']}"); + final res1 = await f2; + if (!res1.isSuccess) { + SmartDialog.showToast("获取关注列表失败:$res1"); } else { - allFollowedUps = (ress[1]['data'].list as List) + allFollowedUps = res1.data.list! .where((e) => hasUpdatedUps.every((e1) => e.mid != e1.mid)) - .map( + .map( (e) => UpItem( face: e.face, mid: e.mid, @@ -155,27 +149,27 @@ class DynamicsController extends GetxController ) .toList(); allFollowedUpsPage += 1; - allFollowedUpsTotal = ress[1]['data'].total; + allFollowedUpsTotal = res1.data.total!; } upData.value.upList = hasUpdatedUps + allFollowedUps; upData.refresh(); } else { var res = await DynamicsHttp.followUp(); - if (res['status']) { - upData.value = res['data']; + if (res.isSuccess) { + upData.value = res.data; if (upData.value.upList!.isEmpty) { mid.value = -1; } } else { - upData.value.errMsg = res['msg']; + upData.value.errMsg = (res as Error).errMsg; upData.refresh(); } } isQuerying = false; } - void onSelectUp(mid) { - if (this.mid == mid) { + void onSelectUp(int mid) { + if (this.mid.value == mid) { tabController.index = (mid == -1 ? 0 : 4); if (mid == -1) { queryFollowUp(); diff --git a/lib/pages/dynamics/widgets/up_panel.dart b/lib/pages/dynamics/widgets/up_panel.dart index 357b829d..171704be 100644 --- a/lib/pages/dynamics/widgets/up_panel.dart +++ b/lib/pages/dynamics/widgets/up_panel.dart @@ -123,28 +123,34 @@ class _UpPanelState extends State { ); } - Widget upItemBuild(theme, data) { + void _onSelect(UserItem data) { + widget.dynamicsController.currentMid = data.mid; + widget.dynamicsController.onSelectUp(data.mid); + + data.hasUpdate = false; + + setState(() {}); + } + + Widget upItemBuild(ThemeData theme, UserItem data) { bool isCurrent = widget.dynamicsController.currentMid == data.mid || widget.dynamicsController.currentMid == -1; + final isLive = data is LiveUserItem; return SizedBox( height: 76, width: isTop ? 70 : null, child: InkWell( onTap: () { feedBack(); - if (data.type == 'up') { - widget.dynamicsController.currentMid = data.mid; - widget.dynamicsController - ..upInfo.value = data - ..onSelectUp(data.mid); - - data.hasUpdate = false; - - setState(() {}); - } else if (data.type == 'live') { - Get.toNamed('/liveRoom?roomid=${data.roomId}'); + switch (data) { + case UpItem(): + _onSelect(data); + break; + case LiveUserItem(): + Get.toNamed('/liveRoom?roomid=${data.roomId}'); } }, + onDoubleTap: data is LiveUserItem ? () => _onSelect(data) : null, onLongPress: () { if (data.mid == -1) { return; @@ -181,16 +187,16 @@ class _UpPanelState extends State { ), ), Positioned( - top: data.type == 'live' && !isTop ? -5 : 0, - right: data.type == 'live' ? -6 : 4, + top: isLive && !isTop ? -5 : 0, + right: isLive ? -6 : 4, child: Badge( smallSize: 8, - label: data.type == 'live' ? const Text(' Live ') : null, + label: isLive ? const Text(' Live ') : null, textColor: theme.colorScheme.onSecondaryContainer, alignment: AlignmentDirectional.topStart, - isLabelVisible: data.type == 'live' || - (data.type == 'up' && (data.hasUpdate ?? false)), - backgroundColor: data.type == 'live' + isLabelVisible: isLive || + (data is UpItem && (data.hasUpdate ?? false)), + backgroundColor: isLive ? theme.colorScheme.secondaryContainer .withValues(alpha: 0.75) : theme.colorScheme.primary, @@ -202,7 +208,7 @@ class _UpPanelState extends State { Padding( padding: const EdgeInsets.symmetric(horizontal: 4), child: Text( - isTop ? '${data.uname}\n' : data.uname, + isTop ? '${data.uname}\n' : data.uname!, maxLines: 2, textAlign: TextAlign.center, style: TextStyle( diff --git a/lib/pages/home/controller.dart b/lib/pages/home/controller.dart index e7887ae1..76914aee 100644 --- a/lib/pages/home/controller.dart +++ b/lib/pages/home/controller.dart @@ -46,7 +46,7 @@ class HomeController extends GetxController super.onInit(); final userInfo = GStorage.userInfo.get('userInfoCache'); isLogin.value = userInfo != null; - userFace.value = userInfo != null ? userInfo.face : ''; + userFace.value = userInfo != null ? userInfo.face! : ''; hideSearchBar = GStorage.setting.get(SettingBoxKey.hideSearchBar, defaultValue: true);