更新评论

This commit is contained in:
2024-09-09 13:06:05 +08:00
parent 7617c48f54
commit f804e512a9
6 changed files with 79 additions and 30 deletions

View File

@@ -41,7 +41,8 @@ interface CommentService {
postId: Int,
content: String,
parentCommentId: Int? = null,
replyUserId: Int? = null
replyUserId: Int? = null,
replyCommentId: Int? = null
): CommentEntity
/**
@@ -203,10 +204,16 @@ class CommentServiceImpl : CommentService {
content: String,
parentCommentId: Int?,
replyUserId: Int?,
replyCommentId: Int?
): CommentEntity {
val resp = ApiClient.api.createComment(
postId,
CommentRequestBody(content, parentCommentId, replyUserId),
CommentRequestBody(
content = content,
parentCommentId = parentCommentId,
replyUserId = replyUserId,
replyCommentId = replyCommentId
),
)
val body = resp.body() ?: throw ServiceException("Failed to create comment")
return body.data.toCommentEntity()

View File

@@ -66,6 +66,8 @@ data class CommentRequestBody(
val parentCommentId: Int? = null,
@SerializedName("replyUserId")
val replyUserId: Int? = null,
@SerializedName("replyCommentId")
val replyCommentId: Int? = null,
)
data class ChangePasswordRequestBody(

View File

@@ -42,7 +42,8 @@ object MessageListViewModel : ViewModel() {
pagingSourceFactory = {
CommentPagingSource(
CommentRemoteDataSource(commentService),
selfNotice = true
selfNotice = true,
order="latest"
)
}
).flow.cachedIn(viewModelScope).collectLatest {

View File

@@ -124,10 +124,19 @@ class CommentsViewModel(
}
suspend fun createComment(
content: String, parentCommentId: Int? = null, replyUserId: Int? = null
content: String,
parentCommentId: Int? = null,
replyUserId: Int? = null,
replyCommentId: Int? = null
) {
val comment =
commentService.createComment(postId.toInt(), content, parentCommentId, replyUserId)
commentService.createComment(
postId = postId.toInt(),
content = content,
parentCommentId = parentCommentId,
replyUserId = replyUserId,
replyCommentId = replyCommentId
)
MomentViewModel.updateCommentCount(postId.toInt())
addedCommentList = addedCommentList.plus(comment)
}

View File

@@ -158,17 +158,24 @@ fun PostScreen(
if (replyComment?.parentCommentId != null) {
// 第三级评论
viewModel.createComment(
it,
content = it,
parentCommentId = replyComment?.parentCommentId,
replyUserId = replyComment?.author?.toInt()
replyUserId = replyComment?.author?.toInt(),
replyCommentId = replyComment?.id
)
} else {
// 子级评论
viewModel.createComment(it, replyComment?.id)
viewModel.createComment(
content = it,
parentCommentId = replyComment?.id,
replyCommentId = replyComment?.id
)
}
} else {
// 顶级评论
viewModel.createComment(it)
viewModel.createComment(
content = it
)
}
showCommentModal = false
}
@@ -307,18 +314,19 @@ fun PostScreen(
}
}
@Composable
fun CommentContent(
viewModel:CommentsViewModel,
viewModel: CommentsViewModel,
onLongClick: (CommentEntity) -> Unit,
onReply: (CommentEntity, Long?, String?, String?) -> Unit
){
) {
val commentsPagging = viewModel.commentsFlow.collectAsLazyPagingItems()
val addedTopLevelComment = viewModel.addedCommentList.filter {
it.parentCommentId == null
}
for (item in addedTopLevelComment){
for (item in addedTopLevelComment) {
Box(
modifier = Modifier.padding(horizontal = 16.dp)
) {
@@ -340,7 +348,12 @@ fun CommentContent(
onLongClick(item)
},
onReply = { parentComment, _, _, _ ->
onReply(parentComment, parentComment.author, parentComment.name, parentComment.avatar)
onReply(
parentComment,
parentComment.author,
parentComment.name,
parentComment.avatar
)
},
onLoadMoreSubComments = {
viewModel.viewModelScope.launch {
@@ -352,7 +365,7 @@ fun CommentContent(
}
}
for (idx in 0 until commentsPagging.itemCount){
for (idx in 0 until commentsPagging.itemCount) {
val item = commentsPagging[idx] ?: return
Box(
modifier = Modifier.padding(horizontal = 16.dp)
@@ -375,7 +388,12 @@ fun CommentContent(
onLongClick(item)
},
onReply = { parentComment, _, _, _ ->
onReply(parentComment, parentComment.author, parentComment.name, parentComment.avatar)
onReply(
parentComment,
parentComment.author,
parentComment.name,
parentComment.avatar
)
},
onLoadMoreSubComments = {
viewModel.viewModelScope.launch {
@@ -389,6 +407,7 @@ fun CommentContent(
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun Header(
@@ -757,6 +776,7 @@ fun CommentItem(
color = Color.Gray
)
Spacer(modifier = Modifier.width(8.dp))
if (AppState.UserId?.toLong() != commentEntity.author) {
Text(
text = "Reply",
fontSize = 12.sp,
@@ -773,6 +793,8 @@ fun CommentItem(
}
}
}
Spacer(modifier = Modifier.width(16.dp))
Column(
horizontalAlignment = Alignment.CenterHorizontally

View File

@@ -58,9 +58,17 @@ class PostViewModel(
}
suspend fun createComment(
content: String, parentCommentId: Int? = null, replyUserId: Int? = null
content: String,
parentCommentId: Int? = null,
replyUserId: Int? = null,
replyCommentId: Int? = null
) {
commentsViewModel.createComment(content, parentCommentId, replyUserId)
commentsViewModel.createComment(
content = content,
parentCommentId = parentCommentId,
replyUserId = replyUserId,
replyCommentId = replyCommentId
)
}
suspend fun likeMoment() {