refactor: reply

This commit is contained in:
bggRGjQaUbCoE
2024-09-08 16:14:57 +08:00
parent 2bcddc1097
commit bb6aaaa480
20 changed files with 640 additions and 935 deletions

View File

@@ -1,21 +1,19 @@
import 'package:PiliPalaX/http/loading_state.dart';
import 'package:PiliPalaX/pages/common/common_controller.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:PiliPalaX/http/reply.dart';
import 'package:PiliPalaX/models/common/reply_type.dart';
import 'package:PiliPalaX/models/video/reply/item.dart';
class VideoReplyReplyController extends GetxController {
class VideoReplyReplyController extends CommonController {
VideoReplyReplyController(this.aid, this.rpid, this.replyType);
final ScrollController scrollController = ScrollController();
// 视频aid 请求时使用的oid
int? aid;
// rpid 请求楼中楼回复
String? rpid;
ReplyType replyType; // = ReplyType.video;
RxList<ReplyItemModel> replyList = <ReplyItemModel>[].obs;
// 当前页
int currentPage = 0;
bool isLoadingMore = false;
RxString noMore = ''.obs;
// 当前回复的回复
ReplyItemModel? currentReplyItem;
@@ -24,54 +22,42 @@ class VideoReplyReplyController extends GetxController {
@override
void onInit() {
super.onInit();
currentPage = 0;
}
Future queryReplyList({type = 'init'}) async {
if (type == 'init') {
currentPage = 0;
}
if (isLoadingMore) {
return;
}
isLoadingMore = true;
final res = await ReplyHttp.replyReplyList(
oid: aid!,
root: rpid!,
pageNum: currentPage + 1,
type: replyType.index,
);
if (res['status']) {
if (res['data'].root != null) root = res['data'].root;
final List<ReplyItemModel> replies = res['data'].replies;
if (replies.isNotEmpty) {
noMore.value = '加载中...';
if (replies.length == res['data'].page.count) {
noMore.value = '没有更多了';
}
currentPage++;
} else {
// 未登录状态replies可能返回null
noMore.value = currentPage == 0 ? '还没有评论' : '没有更多了';
}
if (type == 'init') {
replyList.value = replies;
} else {
// 每次回复之后,翻页请求有且只有相同的一条回复数据
if (replies.length == 1 && replies.last.rpid == replyList.last.rpid) {
return;
}
replyList.addAll(replies);
// res['data'].replies.addAll(replyList);
}
}
isLoadingMore = false;
return res;
queryData();
}
@override
void onClose() {
currentPage = 0;
super.onClose();
bool customHandleResponse(Success response) {
if (response.response.root != null) root = response.response.root;
List<ReplyItemModel> replies = response.response.replies;
if (replies.isNotEmpty) {
noMore.value = '加载中...';
if (replies.length == response.response.page.count) {
noMore.value = '没有更多了';
}
} else {
// 未登录状态replies可能返回null
noMore.value = currentPage == 1 ? '还没有评论' : '没有更多了';
}
if (currentPage != 1) {
List<ReplyItemModel> list = loadingState.value is Success
? (loadingState.value as Success).response
: <ReplyItemModel>[];
// 每次回复之后,翻页请求有且只有相同的一条回复数据
if (replies.length == 1 && replies.last.rpid == list.last.rpid) {
return true;
} else {
replies.insertAll(0, list);
}
}
loadingState.value = LoadingState.success(replies);
return true;
}
@override
Future<LoadingState> customGetData() => ReplyHttp.replyReplyList(
oid: aid!,
root: rpid!,
pageNum: currentPage,
type: replyType.index,
);
}

View File

@@ -1,3 +1,4 @@
import 'package:PiliPalaX/http/loading_state.dart';
import 'package:PiliPalaX/pages/video/detail/reply_new/reply_page.dart';
import 'package:easy_debounce/easy_throttle.dart';
import 'package:flutter/material.dart';
@@ -37,7 +38,6 @@ class VideoReplyReplyPanel extends StatefulWidget {
class _VideoReplyReplyPanelState extends State<VideoReplyReplyPanel> {
late VideoReplyReplyController _videoReplyReplyController;
Future? _futureBuilderFuture;
late final _savedReplies = {};
@override
@@ -57,13 +57,11 @@ class _VideoReplyReplyPanelState extends State<VideoReplyReplyPanel> {
300) {
EasyThrottle.throttle('replylist', const Duration(milliseconds: 200),
() {
_videoReplyReplyController.queryReplyList(type: 'onLoad');
_videoReplyReplyController.onLoadMore();
});
}
},
);
_futureBuilderFuture = _videoReplyReplyController.queryReplyList();
}
void replyReply(replyItem) {}
@@ -71,7 +69,6 @@ class _VideoReplyReplyPanelState extends State<VideoReplyReplyPanel> {
@override
void dispose() {
_videoReplyReplyController.scrollController.removeListener(() {});
_videoReplyReplyController.scrollController.dispose();
super.dispose();
}
@@ -95,7 +92,6 @@ class _VideoReplyReplyPanelState extends State<VideoReplyReplyPanel> {
tooltip: '关闭',
icon: const Icon(Icons.close, size: 20),
onPressed: () {
_videoReplyReplyController.currentPage = 0;
widget.closePanel!();
Navigator.pop(context);
},
@@ -110,9 +106,7 @@ class _VideoReplyReplyPanelState extends State<VideoReplyReplyPanel> {
Expanded(
child: RefreshIndicator(
onRefresh: () async {
setState(() {});
_videoReplyReplyController.currentPage = 0;
return await _videoReplyReplyController.queryReplyList();
await _videoReplyReplyController.onRefresh();
},
child: CustomScrollView(
controller: _videoReplyReplyController.scrollController,
@@ -126,7 +120,7 @@ class _VideoReplyReplyPanelState extends State<VideoReplyReplyPanel> {
replyLevel: '2',
showReplyRow: false,
addReply: (replyItem) {
_videoReplyReplyController.replyList.add(replyItem);
// _videoReplyReplyController.replyList.add(replyItem);
},
replyType: widget.replyType,
replyReply: replyReply,
@@ -144,129 +138,8 @@ class _VideoReplyReplyPanelState extends State<VideoReplyReplyPanel> {
),
),
],
FutureBuilder(
future: _futureBuilderFuture,
builder: (BuildContext context, snapshot) {
if (snapshot.connectionState == ConnectionState.done &&
snapshot.hasData) {
final Map data = snapshot.data as Map;
if (data['status']) {
// 请求成功
return SliverMainAxisGroup(
slivers: <Widget>[
if (widget.firstFloor == null &&
_videoReplyReplyController.root != null) ...[
SliverToBoxAdapter(
child: ReplyItem(
replyItem: _videoReplyReplyController.root,
replyLevel: '2',
showReplyRow: false,
addReply: (replyItem) {
_videoReplyReplyController.replyList
.add(replyItem);
},
replyType: widget.replyType,
replyReply: replyReply,
needDivider: false,
onReply: () {
_onReply(_videoReplyReplyController.root);
},
),
),
SliverToBoxAdapter(
child: Divider(
height: 20,
color: Theme.of(context)
.dividerColor
.withOpacity(0.1),
thickness: 6,
),
),
],
Obx(
() => SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
if (index ==
_videoReplyReplyController
.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(
_videoReplyReplyController
.noMore.value,
style: TextStyle(
fontSize: 12,
color: Theme.of(context)
.colorScheme
.outline,
),
),
),
),
);
} else {
return ReplyItem(
replyItem: _videoReplyReplyController
.replyList[index],
replyLevel: '2',
showReplyRow: false,
addReply: (replyItem) {
_videoReplyReplyController.replyList
.add(replyItem);
},
replyType: widget.replyType,
onReply: () {
_onReply(_videoReplyReplyController
.replyList[index]);
},
onDelete: (rpid, frpid) {
_videoReplyReplyController
.replyList.value =
_videoReplyReplyController
.replyList
.where((item) =>
item.rpid != rpid)
.toList();
},
);
}
},
childCount: _videoReplyReplyController
.replyList.length +
1,
),
),
),
],
);
} else {
// 请求错误
return HttpError(
errMsg: data['msg'],
fn: () => setState(() {}),
);
}
} else {
// 骨架屏
return SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
return const VideoReplySkeleton();
}, childCount: 8),
);
}
},
)
Obx(() => _buildBody(
_videoReplyReplyController.loadingState.value)),
],
),
),
@@ -320,4 +193,95 @@ class _VideoReplyReplyPanelState extends State<VideoReplyReplyPanel> {
}
});
}
Widget _buildBody(LoadingState loadingState) {
return loadingState is Success
? SliverMainAxisGroup(
slivers: <Widget>[
if (widget.firstFloor == null &&
_videoReplyReplyController.root != null) ...[
SliverToBoxAdapter(
child: ReplyItem(
replyItem: _videoReplyReplyController.root,
replyLevel: '2',
showReplyRow: false,
addReply: (replyItem) {
// _videoReplyReplyController.replyList.add(replyItem);
},
replyType: widget.replyType,
replyReply: replyReply,
needDivider: false,
onReply: () {
_onReply(_videoReplyReplyController.root);
},
),
),
SliverToBoxAdapter(
child: Divider(
height: 20,
color: Theme.of(context).dividerColor.withOpacity(0.1),
thickness: 6,
),
),
],
SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int 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(
_videoReplyReplyController.noMore.value,
style: TextStyle(
fontSize: 12,
color: Theme.of(context).colorScheme.outline,
),
),
),
),
);
} else {
return ReplyItem(
replyItem: loadingState.response[index],
replyLevel: '2',
showReplyRow: false,
addReply: (replyItem) {
// _videoReplyReplyController.replyList.add(replyItem);
},
replyType: widget.replyType,
onReply: () {
_onReply(loadingState.response[index]);
},
onDelete: (rpid, frpid) {
// _videoReplyReplyController.replyList.value =
// _videoReplyReplyController.replyList
// .where((item) => item.rpid != rpid)
// .toList();
},
);
}
},
childCount: loadingState.response.length + 1,
),
),
],
)
: loadingState is Error
? HttpError(
errMsg: loadingState.errMsg,
fn: _videoReplyReplyController.onReload,
)
: SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
return const VideoReplySkeleton();
},
childCount: 8,
),
);
}
}