mirror of
https://github.com/HChaZZY/PiliPlus.git
synced 2025-12-06 09:13:48 +08:00
refactor: reply
This commit is contained in:
@@ -1,30 +1,15 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:PiliPalaX/http/loading_state.dart';
|
||||
import 'package:PiliPalaX/pages/common/reply_controller.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:PiliPalaX/http/html.dart';
|
||||
import 'package:PiliPalaX/http/reply.dart';
|
||||
import 'package:PiliPalaX/models/common/reply_sort_type.dart';
|
||||
import 'package:PiliPalaX/models/video/reply/item.dart';
|
||||
import 'package:PiliPalaX/utils/feed_back.dart';
|
||||
import 'package:PiliPalaX/utils/storage.dart';
|
||||
|
||||
class DynamicDetailController extends GetxController {
|
||||
class DynamicDetailController extends ReplyController {
|
||||
DynamicDetailController(this.oid, this.type);
|
||||
int? oid;
|
||||
int? type;
|
||||
dynamic item;
|
||||
int? floor;
|
||||
String nextOffset = "";
|
||||
bool isLoadingMore = false;
|
||||
RxString noMore = ''.obs;
|
||||
RxList<ReplyItemModel> replyList = <ReplyItemModel>[].obs;
|
||||
RxInt acount = 0.obs;
|
||||
final ScrollController scrollController = ScrollController();
|
||||
|
||||
ReplySortType _sortType = ReplySortType.time;
|
||||
RxString sortTypeTitle = ReplySortType.time.titles.obs;
|
||||
RxString sortTypeLabel = ReplySortType.time.labels.obs;
|
||||
Box setting = GStorage.setting;
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
@@ -32,83 +17,10 @@ class DynamicDetailController extends GetxController {
|
||||
item = Get.arguments['item'];
|
||||
floor = Get.arguments['floor'];
|
||||
if (floor == 1) {
|
||||
acount.value =
|
||||
int.parse(item!.modules!.moduleStat!.comment!.count ?? '0');
|
||||
count.value = int.parse(item!.modules!.moduleStat!.comment!.count ?? '0');
|
||||
}
|
||||
int defaultReplySortIndex =
|
||||
setting.get(SettingBoxKey.replySortType, defaultValue: 1);
|
||||
if (defaultReplySortIndex == 2) {
|
||||
setting.put(SettingBoxKey.replySortType, 0);
|
||||
defaultReplySortIndex = 0;
|
||||
}
|
||||
_sortType = ReplySortType.values[defaultReplySortIndex];
|
||||
sortTypeTitle.value = _sortType.titles;
|
||||
sortTypeLabel.value = _sortType.labels;
|
||||
}
|
||||
|
||||
Future queryReplyList({reqType = 'init'}) async {
|
||||
if (reqType == 'init') {
|
||||
nextOffset = "";
|
||||
noMore.value = "";
|
||||
}
|
||||
if (isLoadingMore) return;
|
||||
if (noMore.value == '没有更多了') return;
|
||||
isLoadingMore = true;
|
||||
var res = await ReplyHttp.replyList(
|
||||
oid: oid!,
|
||||
nextOffset: nextOffset,
|
||||
type: type!,
|
||||
sort: _sortType.index,
|
||||
);
|
||||
isLoadingMore = false;
|
||||
if (res['status']) {
|
||||
List<ReplyItemModel> replies = res['data'].replies;
|
||||
acount.value = res['data'].cursor.allCount ?? 0;
|
||||
nextOffset = res['data'].cursor.paginationReply.nextOffset ?? "";
|
||||
if (replies.isNotEmpty) {
|
||||
noMore.value = '加载中...';
|
||||
if (res['data'].cursor.isEnd == true) {
|
||||
noMore.value = '没有更多了';
|
||||
}
|
||||
} else {
|
||||
noMore.value =
|
||||
nextOffset == "" && reqType == 'init' ? '还没有评论' : '没有更多了';
|
||||
}
|
||||
if (reqType == 'init') {
|
||||
// 添加置顶回复
|
||||
if (res['data'].upper.top != null) {
|
||||
bool flag = res['data']
|
||||
.topReplies
|
||||
.any((reply) => reply.rpid == res['data'].upper.top.rpid);
|
||||
if (!flag) {
|
||||
replies.insert(0, res['data'].upper.top);
|
||||
}
|
||||
}
|
||||
replies.insertAll(0, res['data'].topReplies);
|
||||
replyList.value = replies;
|
||||
} else {
|
||||
replyList.addAll(replies);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
// 排序搜索评论
|
||||
queryBySort() {
|
||||
feedBack();
|
||||
switch (_sortType) {
|
||||
case ReplySortType.time:
|
||||
_sortType = ReplySortType.like;
|
||||
break;
|
||||
case ReplySortType.like:
|
||||
_sortType = ReplySortType.time;
|
||||
break;
|
||||
default:
|
||||
}
|
||||
sortTypeTitle.value = _sortType.titles;
|
||||
sortTypeLabel.value = _sortType.labels;
|
||||
replyList.clear();
|
||||
queryReplyList(reqType: 'init');
|
||||
queryData();
|
||||
}
|
||||
|
||||
// 根据jumpUrl获取动态html
|
||||
@@ -116,4 +28,12 @@ class DynamicDetailController extends GetxController {
|
||||
var res = await HtmlHttp.reqHtml(id, 'opus');
|
||||
oid = res['commentId'];
|
||||
}
|
||||
|
||||
@override
|
||||
Future<LoadingState> customGetData() => ReplyHttp.replyList(
|
||||
oid: oid!,
|
||||
nextOffset: nextOffset,
|
||||
type: type!,
|
||||
sort: sortType.index,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import 'dart:async';
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:PiliPalaX/http/loading_state.dart';
|
||||
import 'package:easy_debounce/easy_throttle.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
@@ -32,7 +33,6 @@ class _DynamicDetailPageState extends State<DynamicDetailPage>
|
||||
with TickerProviderStateMixin {
|
||||
late DynamicDetailController _dynamicDetailController;
|
||||
late AnimationController fabAnimationCtr;
|
||||
Future? _futureBuilderFuture;
|
||||
late StreamController<bool> titleStreamC; // appBar title
|
||||
late ScrollController scrollController;
|
||||
bool _visibleTitle = false;
|
||||
@@ -103,8 +103,6 @@ class _DynamicDetailPageState extends State<DynamicDetailPage>
|
||||
_dynamicDetailController =
|
||||
Get.put(DynamicDetailController(oid, replyType), tag: oid.toString());
|
||||
}
|
||||
_futureBuilderFuture =
|
||||
_dynamicDetailController.queryReplyList(reqType: 'init');
|
||||
}
|
||||
|
||||
// 查看二级评论
|
||||
@@ -141,7 +139,7 @@ class _DynamicDetailPageState extends State<DynamicDetailPage>
|
||||
if (scrollController.position.pixels >=
|
||||
scrollController.position.maxScrollExtent - 300) {
|
||||
EasyThrottle.throttle('replylist', const Duration(seconds: 2), () {
|
||||
_dynamicDetailController.queryReplyList(reqType: 'onLoad');
|
||||
_dynamicDetailController.onLoadMore();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -185,7 +183,6 @@ class _DynamicDetailPageState extends State<DynamicDetailPage>
|
||||
titleStreamC.close();
|
||||
fabAnimationCtr.dispose();
|
||||
scrollController.removeListener(() {});
|
||||
scrollController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@@ -212,7 +209,7 @@ class _DynamicDetailPageState extends State<DynamicDetailPage>
|
||||
),
|
||||
body: RefreshIndicator(
|
||||
onRefresh: () async {
|
||||
await _dynamicDetailController.queryReplyList(reqType: 'init');
|
||||
await _dynamicDetailController.onRefresh();
|
||||
},
|
||||
child: Stack(
|
||||
children: [
|
||||
@@ -231,7 +228,10 @@ class _DynamicDetailPageState extends State<DynamicDetailPage>
|
||||
),
|
||||
),
|
||||
replyPersistentHeader(context),
|
||||
replyList(),
|
||||
Obx(
|
||||
() => replyList(
|
||||
_dynamicDetailController.loadingState.value),
|
||||
),
|
||||
]
|
||||
.map<Widget>((e) => SliverPadding(
|
||||
padding: EdgeInsets.symmetric(horizontal: padding),
|
||||
@@ -265,8 +265,12 @@ class _DynamicDetailPageState extends State<DynamicDetailPage>
|
||||
padding: EdgeInsets.only(right: padding / 2),
|
||||
sliver: replyPersistentHeader(context)),
|
||||
SliverPadding(
|
||||
padding: EdgeInsets.only(right: padding / 2),
|
||||
sliver: replyList()),
|
||||
padding: EdgeInsets.only(right: padding / 2),
|
||||
sliver: Obx(
|
||||
() => replyList(_dynamicDetailController
|
||||
.loadingState.value),
|
||||
),
|
||||
),
|
||||
]
|
||||
// .map<Widget>(
|
||||
// (e) => SliverPadding(padding: padding, sliver: e))
|
||||
@@ -306,14 +310,22 @@ class _DynamicDetailPageState extends State<DynamicDetailPage>
|
||||
);
|
||||
},
|
||||
).then(
|
||||
(value) => {
|
||||
(value) {
|
||||
// 完成评论,数据添加
|
||||
if (value != null && value['data'] != null)
|
||||
{
|
||||
_dynamicDetailController.replyList
|
||||
.add(value['data']),
|
||||
_dynamicDetailController.acount.value++
|
||||
if (value != null && value['data'] != null) {
|
||||
_dynamicDetailController.count.value++;
|
||||
if (value != null && value['data'] != null) {
|
||||
List list = _dynamicDetailController
|
||||
.loadingState.value is Success
|
||||
? (_dynamicDetailController.loadingState.value
|
||||
as Success)
|
||||
.response
|
||||
: [];
|
||||
list.insert(0, value['data']);
|
||||
_dynamicDetailController.loadingState.value =
|
||||
LoadingState.success(list);
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
},
|
||||
@@ -353,8 +365,8 @@ class _DynamicDetailPageState extends State<DynamicDetailPage>
|
||||
return ScaleTransition(scale: animation, child: child);
|
||||
},
|
||||
child: Text(
|
||||
'${_dynamicDetailController.acount.value}条回复',
|
||||
key: ValueKey<int>(_dynamicDetailController.acount.value),
|
||||
'${_dynamicDetailController.count.value}条回复',
|
||||
key: ValueKey<int>(_dynamicDetailController.count.value),
|
||||
),
|
||||
),
|
||||
),
|
||||
@@ -378,85 +390,57 @@ class _DynamicDetailPageState extends State<DynamicDetailPage>
|
||||
);
|
||||
}
|
||||
|
||||
FutureBuilder<dynamic> replyList() {
|
||||
return FutureBuilder(
|
||||
future: _futureBuilderFuture,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.done &&
|
||||
snapshot.hasData) {
|
||||
Map data = snapshot.data as Map;
|
||||
if (snapshot.data['status']) {
|
||||
// 请求成功
|
||||
return Obx(
|
||||
() => _dynamicDetailController.replyList.isEmpty &&
|
||||
_dynamicDetailController.isLoadingMore
|
||||
? SliverList(
|
||||
delegate: SliverChildBuilderDelegate((context, index) {
|
||||
return const VideoReplySkeleton();
|
||||
}, childCount: 8),
|
||||
)
|
||||
: SliverList(
|
||||
delegate: SliverChildBuilderDelegate(
|
||||
(context, index) {
|
||||
if (index ==
|
||||
_dynamicDetailController.replyList.length) {
|
||||
return Container(
|
||||
padding: EdgeInsets.only(
|
||||
bottom:
|
||||
MediaQuery.of(context).padding.bottom),
|
||||
height:
|
||||
MediaQuery.of(context).padding.bottom + 100,
|
||||
child: Center(
|
||||
child: Obx(
|
||||
() => Text(
|
||||
_dynamicDetailController.noMore.value,
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color:
|
||||
Theme.of(context).colorScheme.outline,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
} else {
|
||||
return ReplyItem(
|
||||
replyItem:
|
||||
_dynamicDetailController.replyList[index],
|
||||
showReplyRow: true,
|
||||
replyLevel: '1',
|
||||
replyReply: replyReply,
|
||||
replyType: ReplyType.values[replyType],
|
||||
addReply: (replyItem) {
|
||||
_dynamicDetailController
|
||||
.replyList[index].replies!
|
||||
.add(replyItem);
|
||||
},
|
||||
);
|
||||
}
|
||||
},
|
||||
childCount:
|
||||
_dynamicDetailController.replyList.length + 1,
|
||||
Widget replyList(LoadingState loadingState) {
|
||||
return loadingState is Success
|
||||
? SliverList(
|
||||
delegate: SliverChildBuilderDelegate(
|
||||
(context, index) {
|
||||
if (index == loadingState.response.length) {
|
||||
return Container(
|
||||
padding: EdgeInsets.only(
|
||||
bottom: MediaQuery.of(context).padding.bottom),
|
||||
height: MediaQuery.of(context).padding.bottom + 100,
|
||||
child: Center(
|
||||
child: Obx(
|
||||
() => Text(
|
||||
_dynamicDetailController.noMore.value,
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: Theme.of(context).colorScheme.outline,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
} else {
|
||||
// 请求错误
|
||||
return HttpError(
|
||||
errMsg: data['msg'],
|
||||
fn: () => setState(() {}),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
// 骨架屏
|
||||
return SliverList(
|
||||
delegate: SliverChildBuilderDelegate((context, index) {
|
||||
return const VideoReplySkeleton();
|
||||
}, childCount: 8),
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
);
|
||||
} else {
|
||||
return ReplyItem(
|
||||
replyItem: loadingState.response[index],
|
||||
showReplyRow: true,
|
||||
replyLevel: '1',
|
||||
replyReply: replyReply,
|
||||
replyType: ReplyType.values[replyType],
|
||||
addReply: (replyItem) {
|
||||
// loadingState.response[index].replies!.add(replyItem);
|
||||
},
|
||||
);
|
||||
}
|
||||
},
|
||||
childCount: loadingState.response.length + 1,
|
||||
),
|
||||
)
|
||||
: loadingState is Error
|
||||
? HttpError(
|
||||
errMsg: loadingState.errMsg,
|
||||
fn: _dynamicDetailController.onReload,
|
||||
)
|
||||
: SliverList(
|
||||
delegate: SliverChildBuilderDelegate(
|
||||
(context, index) {
|
||||
return const VideoReplySkeleton();
|
||||
},
|
||||
childCount: 8,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user