refactor: reply item

This commit is contained in:
bggRGjQaUbCoE
2024-10-11 18:03:20 +08:00
parent 39a63cf5ce
commit b3f86b1d94
8 changed files with 1483 additions and 72 deletions

View File

@@ -1,3 +1,4 @@
import 'dart:convert';
import 'dart:ffi';
import 'package:PiliPalaX/grpc/app/main/community/reply/v1/reply.pb.dart';
@@ -9,6 +10,9 @@ import 'package:PiliPalaX/models/common/reply_type.dart';
import 'package:PiliPalaX/utils/feed_back.dart';
import 'package:easy_debounce/easy_throttle.dart';
import 'package:fixnum/fixnum.dart' as $fixnum;
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
class VideoReplyController extends ReplyController {
VideoReplyController(
@@ -59,7 +63,16 @@ class VideoReplyController extends ReplyController {
@override
bool customHandleResponse(Success response) {
MainListReply replies = response.response;
if (cursor == null) {
count.value = replies.subjectControl.count.toInt();
}
cursor = replies.cursor;
// replies.replies.clear();
// showDialog(
// context: Get.context!,
// builder: (_) => AlertDialog(
// content: SelectableText(jsonEncode(replies.toProto3Json())),
// ));
if (replies.replies.isNotEmpty) {
noMore.value = '加载中...';
if (replies.cursor.isEnd) {
@@ -75,7 +88,7 @@ class VideoReplyController extends ReplyController {
: <ReplyInfo>[];
replies.replies.insertAll(0, list);
}
loadingState.value = LoadingState.success(replies.replies);
loadingState.value = LoadingState.success(replies);
return true;
}

View File

@@ -1,5 +1,6 @@
import 'package:PiliPalaX/common/widgets/http_error.dart';
import 'package:PiliPalaX/http/loading_state.dart';
import 'package:PiliPalaX/pages/video/detail/reply/widgets/reply_item_grpc.dart';
import 'package:easy_debounce/easy_throttle.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
@@ -128,7 +129,7 @@ class _VideoReplyPanelState extends State<VideoReplyPanel>
CustomScrollView(
controller: _videoReplyController.scrollController,
physics: const AlwaysScrollableScrollPhysics(),
key: const PageStorageKey<String>('评论'),
// key: const PageStorageKey<String>('评论'),
slivers: <Widget>[
SliverPersistentHeader(
pinned: false,
@@ -204,7 +205,7 @@ class _VideoReplyPanelState extends State<VideoReplyPanel>
delegate: SliverChildBuilderDelegate(
(BuildContext context, index) {
double bottom = MediaQuery.of(context).padding.bottom;
if (index == loadingState.response.length) {
if (index == loadingState.response.replies.length) {
return Container(
padding: EdgeInsets.only(bottom: bottom),
height: bottom + 100,
@@ -221,27 +222,25 @@ class _VideoReplyPanelState extends State<VideoReplyPanel>
),
);
} else {
return ListTile(
title: Text(loadingState.response[index].content.message),
return ReplyItemGrpc(
replyItem: loadingState.response.replies[index],
showReplyRow: true,
replyLevel: replyLevel,
replyReply: widget.replyReply,
replyType: ReplyType.video,
onReply: () {
_videoReplyController.onReply(
context,
replyItem: loadingState.response.replies[index],
index: index,
);
},
onDelete: _videoReplyController.onMDelete,
upMid: loadingState.response.subjectControl.upMid,
);
// return ReplyItem(
// replyItem: loadingState.response[index],
// showReplyRow: true,
// replyLevel: replyLevel,
// replyReply: widget.replyReply,
// replyType: ReplyType.video,
// onReply: () {
// _videoReplyController.onReply(
// context,
// replyItem: loadingState.response[index],
// index: index,
// );
// },
// onDelete: _videoReplyController.onMDelete,
// );
}
},
childCount: loadingState.response.length + 1,
childCount: loadingState.response.replies.length + 1,
),
)
: loadingState is Error

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,175 @@
import 'package:PiliPalaX/grpc/app/main/community/reply/v1/reply.pb.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:PiliPalaX/http/reply.dart';
import 'package:PiliPalaX/models/common/reply_type.dart';
import 'package:PiliPalaX/models/video/reply/item.dart';
import 'package:PiliPalaX/utils/feed_back.dart';
import 'package:fixnum/fixnum.dart' as $fixnum;
class ZanButtonGrpc extends StatefulWidget {
const ZanButtonGrpc({
super.key,
required this.replyItem,
this.replyType,
});
final ReplyInfo replyItem;
final ReplyType? replyType;
@override
State<ZanButtonGrpc> createState() => _ZanButtonGrpcState();
}
class _ZanButtonGrpcState extends State<ZanButtonGrpc> {
Future onHateReply() async {
feedBack();
// SmartDialog.showLoading(msg: 'pilipala ...');
final int oid = widget.replyItem.oid.toInt();
final int rpid = widget.replyItem.id.toInt();
// 1 已点赞 2 不喜欢 0 未操作
final int action =
widget.replyItem.replyControl.action.toInt() != 2 ? 2 : 0;
final res = await ReplyHttp.hateReply(
type: widget.replyType!.index,
action: action == 2 ? 1 : 0,
oid: oid,
rpid: rpid,
);
// SmartDialog.dismiss();
if (res['status']) {
SmartDialog.showToast(
widget.replyItem.replyControl.action.toInt() != 2 ? '点踩成功' : '取消踩');
if (action == 2) {
if (widget.replyItem.replyControl.action.toInt() == 1) {
widget.replyItem.like =
$fixnum.Int64(widget.replyItem.like.toInt() - 1);
}
widget.replyItem.replyControl.action = $fixnum.Int64(2);
} else {
// replyItem.like = replyItem.like! - 1;
widget.replyItem.replyControl.action = $fixnum.Int64(0);
}
setState(() {});
} else {
SmartDialog.showToast(res['msg']);
}
}
// 评论点赞
Future onLikeReply() async {
feedBack();
// SmartDialog.showLoading(msg: 'pilipala ...');
final int oid = widget.replyItem.oid.toInt();
final int rpid = widget.replyItem.id.toInt();
// 1 已点赞 2 不喜欢 0 未操作
final int action =
widget.replyItem.replyControl.action.toInt() != 1 ? 1 : 0;
final res = await ReplyHttp.likeReply(
type: widget.replyType!.index,
oid: oid,
rpid: rpid,
action: action,
);
// SmartDialog.dismiss();
if (res['status']) {
SmartDialog.showToast(
widget.replyItem.replyControl.action.toInt() != 1 ? '点赞成功' : '取消赞');
if (action == 1) {
widget.replyItem.like =
$fixnum.Int64(widget.replyItem.like.toInt() + 1);
widget.replyItem.replyControl.action = $fixnum.Int64(1);
} else {
widget.replyItem.like =
$fixnum.Int64(widget.replyItem.like.toInt() - 1);
widget.replyItem.replyControl.action = $fixnum.Int64(0);
}
setState(() {});
} else {
SmartDialog.showToast(res['msg']);
}
}
bool isProcessing = false;
void Function()? handleState(Future Function() action) {
return isProcessing
? null
: () async {
setState(() => isProcessing = true);
await action();
setState(() => isProcessing = false);
};
}
@override
Widget build(BuildContext context) {
final ThemeData t = Theme.of(context);
final Color color = t.colorScheme.outline;
final Color primary = t.colorScheme.primary;
return Row(
mainAxisSize: MainAxisSize.min,
children: [
SizedBox(
height: 32,
child: TextButton(
onPressed: handleState(onHateReply),
child: Icon(
widget.replyItem.replyControl.action.toInt() == 2
? FontAwesomeIcons.solidThumbsDown
: FontAwesomeIcons.thumbsDown,
size: 16,
color: widget.replyItem.replyControl.action.toInt() == 2
? primary
: color,
semanticLabel: widget.replyItem.replyControl.action.toInt() == 2
? '已踩'
: '点踩',
),
),
),
SizedBox(
height: 32,
child: TextButton(
onPressed: handleState(onLikeReply),
child: Row(
children: [
Icon(
widget.replyItem.replyControl.action.toInt() == 1
? FontAwesomeIcons.solidThumbsUp
: FontAwesomeIcons.thumbsUp,
size: 16,
color: widget.replyItem.replyControl.action.toInt() == 1
? primary
: color,
semanticLabel:
widget.replyItem.replyControl.action.toInt() == 1
? '已赞'
: '点赞',
),
const SizedBox(width: 4),
AnimatedSwitcher(
duration: const Duration(milliseconds: 400),
transitionBuilder:
(Widget child, Animation<double> animation) {
return ScaleTransition(scale: animation, child: child);
},
child: Text(
widget.replyItem.like.toString(),
// key: ValueKey<int>(widget.replyItem!.like!),
style: TextStyle(
color: widget.replyItem.replyControl.action.toInt() == 1
? primary
: color,
fontSize: t.textTheme.labelSmall!.fontSize,
),
),
),
],
),
),
),
],
);
}
}