更新评论

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,18 +314,19 @@ fun PostScreen(
} }
} }
@Composable @Composable
fun CommentContent( fun CommentContent(
viewModel:CommentsViewModel, viewModel: CommentsViewModel,
onLongClick: (CommentEntity) -> Unit, onLongClick: (CommentEntity) -> Unit,
onReply: (CommentEntity, Long?, String?, String?) -> Unit onReply: (CommentEntity, Long?, String?, String?) -> Unit
){ ) {
val commentsPagging = viewModel.commentsFlow.collectAsLazyPagingItems() val commentsPagging = viewModel.commentsFlow.collectAsLazyPagingItems()
val addedTopLevelComment = viewModel.addedCommentList.filter { val addedTopLevelComment = viewModel.addedCommentList.filter {
it.parentCommentId == null it.parentCommentId == null
} }
for (item in addedTopLevelComment){ for (item in addedTopLevelComment) {
Box( Box(
modifier = Modifier.padding(horizontal = 16.dp) modifier = Modifier.padding(horizontal = 16.dp)
) { ) {
@@ -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 {
@@ -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 val item = commentsPagging[idx] ?: return
Box( Box(
modifier = Modifier.padding(horizontal = 16.dp) modifier = Modifier.padding(horizontal = 16.dp)
@@ -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,19 +776,22 @@ fun CommentItem(
color = Color.Gray color = Color.Gray
) )
Spacer(modifier = Modifier.width(8.dp)) Spacer(modifier = Modifier.width(8.dp))
Text( if (AppState.UserId?.toLong() != commentEntity.author) {
text = "Reply", Text(
fontSize = 12.sp, text = "Reply",
color = Color.Gray, fontSize = 12.sp,
modifier = Modifier.noRippleClickable { color = Color.Gray,
onReply( modifier = Modifier.noRippleClickable {
commentEntity, onReply(
commentEntity.replyUserId, commentEntity,
commentEntity.replyUserNickname, commentEntity.replyUserId,
commentEntity.replyUserAvatar commentEntity.replyUserNickname,
) commentEntity.replyUserAvatar
} )
) }
)
}
} }
} }

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() {