更新评论

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

View File

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

View File

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

View File

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

View File

@@ -158,17 +158,24 @@ fun PostScreen(
if (replyComment?.parentCommentId != null) { if (replyComment?.parentCommentId != null) {
// 第三级评论 // 第三级评论
viewModel.createComment( viewModel.createComment(
it, content = it,
parentCommentId = replyComment?.parentCommentId, parentCommentId = replyComment?.parentCommentId,
replyUserId = replyComment?.author?.toInt() replyUserId = replyComment?.author?.toInt(),
replyCommentId = replyComment?.id
) )
} else { } else {
// 子级评论 // 子级评论
viewModel.createComment(it, replyComment?.id) viewModel.createComment(
content = it,
parentCommentId = replyComment?.id,
replyCommentId = replyComment?.id
)
} }
} else { } else {
// 顶级评论 // 顶级评论
viewModel.createComment(it) viewModel.createComment(
content = it
)
} }
showCommentModal = false showCommentModal = false
} }
@@ -307,6 +314,7 @@ fun PostScreen(
} }
} }
@Composable @Composable
fun CommentContent( fun CommentContent(
viewModel: CommentsViewModel, viewModel: CommentsViewModel,
@@ -340,7 +348,12 @@ fun CommentContent(
onLongClick(item) onLongClick(item)
}, },
onReply = { parentComment, _, _, _ -> onReply = { parentComment, _, _, _ ->
onReply(parentComment, parentComment.author, parentComment.name, parentComment.avatar) onReply(
parentComment,
parentComment.author,
parentComment.name,
parentComment.avatar
)
}, },
onLoadMoreSubComments = { onLoadMoreSubComments = {
viewModel.viewModelScope.launch { viewModel.viewModelScope.launch {
@@ -375,7 +388,12 @@ fun CommentContent(
onLongClick(item) onLongClick(item)
}, },
onReply = { parentComment, _, _, _ -> onReply = { parentComment, _, _, _ ->
onReply(parentComment, parentComment.author, parentComment.name, parentComment.avatar) onReply(
parentComment,
parentComment.author,
parentComment.name,
parentComment.avatar
)
}, },
onLoadMoreSubComments = { onLoadMoreSubComments = {
viewModel.viewModelScope.launch { viewModel.viewModelScope.launch {
@@ -389,6 +407,7 @@ fun CommentContent(
} }
@OptIn(ExperimentalMaterial3Api::class) @OptIn(ExperimentalMaterial3Api::class)
@Composable @Composable
fun Header( fun Header(
@@ -757,6 +776,7 @@ fun CommentItem(
color = Color.Gray color = Color.Gray
) )
Spacer(modifier = Modifier.width(8.dp)) Spacer(modifier = Modifier.width(8.dp))
if (AppState.UserId?.toLong() != commentEntity.author) {
Text( Text(
text = "Reply", text = "Reply",
fontSize = 12.sp, fontSize = 12.sp,
@@ -773,6 +793,8 @@ fun CommentItem(
} }
} }
}
Spacer(modifier = Modifier.width(16.dp)) Spacer(modifier = Modifier.width(16.dp))
Column( Column(
horizontalAlignment = Alignment.CenterHorizontally horizontalAlignment = Alignment.CenterHorizontally

View File

@@ -58,9 +58,17 @@ class PostViewModel(
} }
suspend fun createComment( 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() { suspend fun likeMoment() {