mirror of
https://github.com/HChaZZY/PiliPlus.git
synced 2025-12-06 09:13:48 +08:00
feat: custom grpc reply (#25)
Signed-off-by: bggRGjQaUbCoE <githubaccount56556@proton.me>
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
import 'package:PiliPalaX/grpc/app/main/community/reply/v1/reply.pb.dart';
|
||||
import 'package:PiliPalaX/http/loading_state.dart';
|
||||
import 'package:PiliPalaX/models/common/reply_type.dart';
|
||||
import 'package:PiliPalaX/models/video/reply/data.dart';
|
||||
import 'package:PiliPalaX/pages/common/common_controller.dart';
|
||||
import 'package:PiliPalaX/pages/video/detail/reply_new/reply_page.dart';
|
||||
import 'package:PiliPalaX/utils/extension.dart';
|
||||
import 'package:PiliPalaX/utils/global_data.dart';
|
||||
import 'package:PiliPalaX/utils/utils.dart';
|
||||
import 'package:easy_debounce/easy_throttle.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
@@ -14,11 +17,9 @@ import 'package:PiliPalaX/utils/storage.dart';
|
||||
import 'package:get/get_navigation/src/dialog/dialog_route.dart';
|
||||
|
||||
abstract class ReplyController extends CommonController {
|
||||
String nextOffset = "";
|
||||
String nextOffset = '';
|
||||
bool isLoadingMore = false;
|
||||
RxInt count = (-1).obs;
|
||||
// 当前回复的回复
|
||||
ReplyItemModel? currentReplyItem;
|
||||
|
||||
ReplySortType sortType = ReplySortType.time;
|
||||
RxString sortTypeTitle = ReplySortType.time.titles.obs;
|
||||
@@ -26,10 +27,10 @@ abstract class ReplyController extends CommonController {
|
||||
|
||||
late final savedReplies = {};
|
||||
|
||||
bool isLogin = GStorage.userInfo.get('userInfoCache') != null;
|
||||
late bool isLogin = GStorage.userInfo.get('userInfoCache') != null;
|
||||
|
||||
CursorReply? cursor;
|
||||
Mode mode = Mode.MAIN_LIST_HOT;
|
||||
late Mode mode = Mode.MAIN_LIST_HOT;
|
||||
bool hasUpTop = false;
|
||||
|
||||
@override
|
||||
@@ -58,23 +59,53 @@ abstract class ReplyController extends CommonController {
|
||||
|
||||
@override
|
||||
bool customHandleResponse(Success response) {
|
||||
MainListReply replies = response.response;
|
||||
if (cursor == null) {
|
||||
count.value = replies.subjectControl.count.toInt();
|
||||
hasUpTop = replies.hasUpTop();
|
||||
if (replies.hasUpTop()) {
|
||||
replies.replies.insert(0, replies.upTop);
|
||||
if (GlobalData().grpcReply) {
|
||||
MainListReply replies = response.response;
|
||||
if (cursor == null) {
|
||||
count.value = replies.subjectControl.count.toInt();
|
||||
hasUpTop = replies.hasUpTop();
|
||||
if (replies.hasUpTop()) {
|
||||
replies.replies.insert(0, replies.upTop);
|
||||
}
|
||||
}
|
||||
cursor = replies.cursor;
|
||||
if (currentPage != 1 && loadingState.value is Success) {
|
||||
replies.replies
|
||||
.insertAll(0, (loadingState.value as Success).response.replies);
|
||||
}
|
||||
isEnd = replies.replies.isEmpty ||
|
||||
replies.cursor.isEnd ||
|
||||
replies.replies.length >= count.value;
|
||||
loadingState.value = LoadingState.success(replies);
|
||||
} else {
|
||||
List<ReplyItemModel> replies = response.response.replies;
|
||||
if (isLogin.not) {
|
||||
nextOffset = response.response.cursor.paginationReply.nextOffset ?? '';
|
||||
}
|
||||
count.value = isLogin.not
|
||||
? response.response.cursor.allCount
|
||||
: response.response.page.count ?? 0;
|
||||
if (replies.isEmpty) {
|
||||
isEnd = true;
|
||||
}
|
||||
if (currentPage == 1) {
|
||||
if (response.response.upper.top != null) {
|
||||
final bool flag = response.response.topReplies.any(
|
||||
(ReplyItemModel reply) =>
|
||||
reply.rpid == response.response.upper.top.rpid) as bool;
|
||||
if (!flag) {
|
||||
replies.insert(0, response.response.upper.top);
|
||||
}
|
||||
}
|
||||
replies.insertAll(0, response.response.topReplies);
|
||||
} else if (loadingState.value is Success) {
|
||||
replies.insertAll(0, (loadingState.value as Success).response.replies);
|
||||
}
|
||||
if (replies.length >= count.value) {
|
||||
isEnd = true;
|
||||
}
|
||||
loadingState.value = LoadingState.success(response.response);
|
||||
}
|
||||
cursor = replies.cursor;
|
||||
if (currentPage != 1 && loadingState.value is Success) {
|
||||
replies.replies
|
||||
.insertAll(0, (loadingState.value as Success).response.replies);
|
||||
}
|
||||
isEnd = replies.replies.isEmpty ||
|
||||
replies.cursor.isEnd ||
|
||||
replies.replies.length >= count.value;
|
||||
loadingState.value = LoadingState.success(replies);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -95,7 +126,7 @@ abstract class ReplyController extends CommonController {
|
||||
}
|
||||
sortTypeTitle.value = sortType.titles;
|
||||
sortTypeLabel.value = sortType.labels;
|
||||
nextOffset = "";
|
||||
nextOffset = '';
|
||||
loadingState.value = LoadingState.loading();
|
||||
onRefresh();
|
||||
});
|
||||
@@ -108,24 +139,40 @@ abstract class ReplyController extends CommonController {
|
||||
int index = 0,
|
||||
ReplyType? replyType,
|
||||
}) {
|
||||
dynamic key = oid ?? replyItem.oid + replyItem.id;
|
||||
dynamic key = oid ??
|
||||
replyItem.oid +
|
||||
(GlobalData().grpcReply ? replyItem.id : replyItem.rpid);
|
||||
Navigator.of(context)
|
||||
.push(
|
||||
GetDialogRoute(
|
||||
pageBuilder: (buildContext, animation, secondaryAnimation) {
|
||||
return ReplyPage(
|
||||
oid: oid ?? replyItem.oid.toInt(),
|
||||
root: oid != null ? 0 : replyItem.id.toInt(),
|
||||
parent: oid != null ? 0 : replyItem.id.toInt(),
|
||||
replyType: replyItem != null
|
||||
? ReplyType.values[replyItem.type.toInt()]
|
||||
: replyType,
|
||||
replyItem: replyItem,
|
||||
savedReply: savedReplies[key],
|
||||
onSaveReply: (reply) {
|
||||
savedReplies[key] = reply;
|
||||
},
|
||||
);
|
||||
return GlobalData().grpcReply
|
||||
? ReplyPage(
|
||||
oid: oid ?? replyItem.oid.toInt(),
|
||||
root: oid != null ? 0 : replyItem.id.toInt(),
|
||||
parent: oid != null ? 0 : replyItem.id.toInt(),
|
||||
replyType: replyItem != null
|
||||
? ReplyType.values[replyItem.type.toInt()]
|
||||
: replyType,
|
||||
replyItem: replyItem,
|
||||
savedReply: savedReplies[key],
|
||||
onSaveReply: (reply) {
|
||||
savedReplies[key] = reply;
|
||||
},
|
||||
)
|
||||
: ReplyPage(
|
||||
oid: oid ?? replyItem.oid,
|
||||
root: oid != null ? 0 : replyItem.rpid,
|
||||
parent: oid != null ? 0 : replyItem.rpid,
|
||||
replyType: replyItem != null
|
||||
? ReplyType.values[replyItem.type.toInt()]
|
||||
: replyType,
|
||||
replyItem: replyItem,
|
||||
savedReply: savedReplies[key],
|
||||
onSaveReply: (reply) {
|
||||
savedReplies[key] = reply;
|
||||
},
|
||||
);
|
||||
},
|
||||
transitionDuration: const Duration(milliseconds: 500),
|
||||
transitionBuilder: (context, animation, secondaryAnimation, child) {
|
||||
@@ -147,37 +194,72 @@ abstract class ReplyController extends CommonController {
|
||||
(res) {
|
||||
if (res != null) {
|
||||
savedReplies[key] = null;
|
||||
ReplyInfo replyInfo = Utils.replyCast(res);
|
||||
MainListReply response =
|
||||
(loadingState.value as Success?)?.response ?? MainListReply();
|
||||
if (oid != null) {
|
||||
response.replies.insert(hasUpTop ? 1 : 0, replyInfo);
|
||||
if (GlobalData().grpcReply) {
|
||||
ReplyInfo replyInfo = Utils.replyCast(res);
|
||||
MainListReply response =
|
||||
(loadingState.value as Success?)?.response ?? MainListReply();
|
||||
if (oid != null) {
|
||||
response.replies.insert(hasUpTop ? 1 : 0, replyInfo);
|
||||
} else {
|
||||
response.replies[index].replies.add(replyInfo);
|
||||
}
|
||||
count.value += 1;
|
||||
loadingState.value = LoadingState.success(response);
|
||||
} else {
|
||||
response.replies[index].replies.add(replyInfo);
|
||||
ReplyData response =
|
||||
(loadingState.value as Success?)?.response ?? ReplyData();
|
||||
response.replies ??= <ReplyItemModel>[];
|
||||
if (oid != null) {
|
||||
response.replies
|
||||
?.insert(hasUpTop ? 1 : 0, ReplyItemModel.fromJson(res, ''));
|
||||
} else {
|
||||
response.replies?[index].replies ??= <ReplyItemModel>[];
|
||||
response.replies?[index].replies
|
||||
?.add(ReplyItemModel.fromJson(res, ''));
|
||||
}
|
||||
count.value += 1;
|
||||
loadingState.value = LoadingState.success(response);
|
||||
}
|
||||
count.value += 1;
|
||||
loadingState.value = LoadingState.success(response);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
onMDelete(rpid, frpid) {
|
||||
MainListReply response = (loadingState.value as Success).response;
|
||||
if (frpid == null) {
|
||||
response.replies.removeWhere((item) {
|
||||
return item.id.toInt() == rpid;
|
||||
});
|
||||
if (GlobalData().grpcReply) {
|
||||
MainListReply response = (loadingState.value as Success).response;
|
||||
if (frpid == null) {
|
||||
response.replies.removeWhere((item) {
|
||||
return item.id.toInt() == rpid;
|
||||
});
|
||||
} else {
|
||||
response.replies.map((item) {
|
||||
if (item.id == frpid) {
|
||||
return item
|
||||
..replies.removeWhere((reply) => reply.id.toInt() == rpid);
|
||||
} else {
|
||||
return item;
|
||||
}
|
||||
}).toList();
|
||||
}
|
||||
count.value -= 1;
|
||||
loadingState.value = LoadingState.success(response);
|
||||
} else {
|
||||
response.replies.map((item) {
|
||||
if (item.id == frpid) {
|
||||
return item..replies.removeWhere((reply) => reply.id.toInt() == rpid);
|
||||
} else {
|
||||
return item;
|
||||
}
|
||||
}).toList();
|
||||
ReplyData response = (loadingState.value as Success).response;
|
||||
response.replies = frpid == null
|
||||
? response.replies?.where((item) => item.rpid != rpid).toList()
|
||||
: response.replies?.map((item) {
|
||||
if (item.rpid == frpid) {
|
||||
return item
|
||||
..replies = item.replies
|
||||
?.where((reply) => reply.rpid != rpid)
|
||||
.toList();
|
||||
} else {
|
||||
return item;
|
||||
}
|
||||
}).toList();
|
||||
count.value -= 1;
|
||||
loadingState.value = LoadingState.success(response);
|
||||
}
|
||||
count.value -= 1;
|
||||
loadingState.value = LoadingState.success(response);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user