aslan-flutter/lib/chatvibe_managers/dynamic_content_manager.dart
2026-07-01 18:25:58 +08:00

234 lines
6.9 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:aslan/app_localizations.dart';
import 'package:aslan/chatvibe_ui/components/at_tts.dart';
import 'package:aslan/chatvibe_core/utilities/at_loading_manager.dart';
import 'package:aslan/chatvibe_data/sources/repositories/dynamic_repository_imp.dart';
import 'package:aslan/chatvibe_domain/models/res/at_comment_list_res.dart';
class ChatVibeDynamicContentManager extends ChangeNotifier {
bool showEmoji = false;
bool showGift = false;
Map<num, int> pageMap = {};
///主评论
List<CommentListRecords> subCommentList = [];
///子评论
Map<num, List<CommentListRecords>> childCommentListMap = {};
bool commentIsLoading = true;
///主评论列表
void dynamicListComment(
String dynamicId,
int pageNumber,
Function(bool hasMoer) loadSuccCallBack,
Function loadFailCallBack,
) {
commentIsLoading = true;
if (pageNumber < 2) {
reset();
}
DynamicRepositoryImp()
.dynamicListComment(dynamicId, pageNumber, 10)
.then((res) {
if (pageNumber < 2) {
subCommentList = res.records ?? [];
} else {
subCommentList.addAll(res.records ?? []);
}
commentIsLoading = false;
if ((res.records?.length ?? 0) < 10) {
loadSuccCallBack(true);
} else {
loadSuccCallBack(false);
}
res.records?.forEach((element) {
if ((element.childCommentCount ?? 0) > 0) {
///表示有子评论
DynamicRepositoryImp()
.dynamicListCommentChildren(element.rootCommentId ?? 0, 1)
.then((value) {
var ls =
childCommentListMap[element.rootCommentId ?? 0] ?? [];
ls.addAll(value.records ?? []);
childCommentListMap[element.rootCommentId ?? 0] = ls;
pageMap[element.rootCommentId ?? 0] = 1;
loadSuccCallBack(false);
});
}
});
})
.catchError((e) {
commentIsLoading = false;
loadFailCallBack();
});
}
void dynamicListCommentChildren(num rootCommentId, Function callBack) {
int pageNumber = (pageMap[rootCommentId] ?? 1) + 1;
ATLoadingManager.exhibitOperation();
///表示有子评论
DynamicRepositoryImp()
.dynamicListCommentChildren(rootCommentId, pageNumber)
.then((value) {
var ls = childCommentListMap[rootCommentId] ?? [];
ls.addAll(value.records ?? []);
childCommentListMap[rootCommentId] = ls;
pageMap[rootCommentId] = pageNumber;
callBack.call();
ATLoadingManager.veilRoutine();
})
.catchError((_) {
ATLoadingManager.veilRoutine();
});
}
///评论更新一级评论
void updateRootComment(CommentListRecords cm, Function callBack) {
subCommentList.insert(0, cm);
callBack.call();
}
///更新子评论
void updateChildComment(
CommentListRecords cm,
num rootCommentId,
String toUserId,
Function callBack,
) {
var cList = subCommentList;
for (var element in cList) {
if (element.rootCommentId == cm.rootCommentId) {
element.setChildCommentCount((element.childCommentCount ?? 0) + 1);
break;
}
}
List<CommentListRecords> childCommentList =
childCommentListMap[rootCommentId] ?? [];
childCommentList.insert(0, cm);
childCommentListMap[rootCommentId] = childCommentList;
callBack.call();
}
void updateShowEmojiState(bool state, Function callBack) {
showEmoji = state;
callBack.call();
}
void updateShowGiftState(bool state, Function callBack) {
showGift = state;
callBack.call();
}
void withdraw(num rootCommentId, Function callBack) {
childCommentListMap[rootCommentId] = [];
pageMap[rootCommentId] = 0;
callBack.call();
}
void reset() {
subCommentList.clear();
childCommentListMap.clear();
pageMap.clear();
}
///点赞评论
void likeComment(
BuildContext context,
String dynamicId,
String id,
num rootCommentId,
String toUserId,
bool like,
bool isRootComment,
Function callBack,
) {
DynamicRepositoryImp()
.likeComment(dynamicId, id, rootCommentId, toUserId, like)
.then((res) {
if (isRootComment) {
var cList = subCommentList;
for (var element in cList) {
if (element.id == id) {
element.setLike(like);
String likeStrQuantity = "";
if (like) {
likeStrQuantity =
"${int.parse(element.likeStrQuantity ?? "0") + 1}";
} else {
likeStrQuantity =
"${int.parse(element.likeStrQuantity ?? "0") - 1}";
}
element.setLikeStrQuantity(likeStrQuantity);
break;
}
}
} else {
var cList = childCommentListMap[rootCommentId] ?? [];
for (var element in cList) {
if (element.id == id) {
element.setLike(like);
String likeStrQuantity = "";
if (like) {
likeStrQuantity =
"${int.parse(element.likeStrQuantity ?? "0") + 1}";
} else {
likeStrQuantity =
"${int.parse(element.likeStrQuantity ?? "0") - 1}";
}
element.setLikeStrQuantity(likeStrQuantity);
break;
}
}
}
ATTts.show(ATAppLocalizations.of(context)!.operationSuccessful);
callBack.call();
})
.catchError((_) {});
}
void deleteComment(
BuildContext context,
String dynamicId,
String id,
num commentQuantity,
bool isRootComment,
Function callBack, {
Function(num count)? deleteCall,
}) {
if (isRootComment) {
DynamicRepositoryImp()
.deleteComment(id)
.then((res) {
int index = -1;
for (var element in subCommentList) {
index++;
if (element.id == id) {
break;
}
}
if (index > -1) {
num count = subCommentList[index].childCommentCount ?? 0;
subCommentList.removeAt(index);
deleteCall?.call(count + 1);
ATTts.show(ATAppLocalizations.of(context)!.deleteSuccessful);
callBack.call();
}
})
.catchError((_) {});
} else {
DynamicRepositoryImp().deleteComment(id).then((resCount) {
num deleteCount = commentQuantity - resCount;
ATTts.show(ATAppLocalizations.of(context)!.deleteSuccessful);
deleteCall?.call(deleteCount);
callBack.call();
});
}
}
}