点赞和评论

This commit is contained in:
2024-07-30 14:28:13 +08:00
parent 406caa3702
commit 0730fdea68
10 changed files with 357 additions and 93 deletions

View File

@@ -58,8 +58,8 @@ import kotlinx.coroutines.launch
class CommentModalViewModel(
postId: Int?
):ViewModel(){
val commentService:CommentService = TestCommentServiceImpl()
) : ViewModel() {
val commentService: CommentService = TestCommentServiceImpl()
val commentsFlow: Flow<PagingData<Comment>> = Pager(
config = PagingConfig(pageSize = 20, enablePlaceholders = false),
pagingSourceFactory = {
@@ -70,10 +70,16 @@ class CommentModalViewModel(
}
).flow.cachedIn(viewModelScope)
}
@Preview
@Composable
fun CommentModalContent(postId: Int? = null, onDismiss: () -> Unit = {}) {
fun CommentModalContent(
postId: Int? = null,
onCommentAdded: () -> Unit = {},
onDismiss: () -> Unit = {}
) {
val model = viewModel<CommentModalViewModel>(
key = "CommentModalViewModel_$postId",
factory = object : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return CommentModalViewModel(postId) as T
@@ -102,6 +108,7 @@ fun CommentModalContent(postId: Int? = null, onDismiss: () -> Unit = {}) {
commentText = ""
}
comments.refresh()
onCommentAdded()
}
Column(
modifier = Modifier
@@ -129,8 +136,7 @@ fun CommentModalContent(postId: Int? = null, onDismiss: () -> Unit = {}) {
.padding(horizontal = 16.dp)
.weight(1f)
) {
CommentsSection(lazyPagingItems = comments, onLike = {
comment: Comment ->
CommentsSection(lazyPagingItems = comments, onLike = { comment: Comment ->
scope.launch {
model.commentService.likeComment(comment.id)
comments.refresh()