This commit is contained in:
2024-08-11 17:15:17 +08:00
parent 2dc0ee3307
commit 19527f17c3
32 changed files with 1082 additions and 417 deletions

View File

@@ -47,7 +47,7 @@ import androidx.paging.cachedIn
import androidx.paging.compose.collectAsLazyPagingItems
import com.aiosman.riderpro.ui.post.CommentsSection
import com.aiosman.riderpro.R
import com.aiosman.riderpro.data.Comment
import com.aiosman.riderpro.data.CommentEntity
import com.aiosman.riderpro.data.CommentPagingSource
import com.aiosman.riderpro.data.CommentRemoteDataSource
import com.aiosman.riderpro.data.CommentService
@@ -57,10 +57,10 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.launch
class CommentModalViewModel(
postId: Int?
val postId: Int?
) : ViewModel() {
val commentService: CommentService = TestCommentServiceImpl()
val commentsFlow: Flow<PagingData<Comment>> = Pager(
val commentsFlow: Flow<PagingData<CommentEntity>> = Pager(
config = PagingConfig(pageSize = 20, enablePlaceholders = false),
pagingSourceFactory = {
CommentPagingSource(
@@ -69,6 +69,12 @@ class CommentModalViewModel(
)
}
).flow.cachedIn(viewModelScope)
suspend fun createComment(content: String) {
postId?.let {
commentService.createComment(postId, content)
}
}
}
@Preview
@@ -104,8 +110,7 @@ fun CommentModalContent(
var commentText by remember { mutableStateOf("") }
suspend fun sendComment() {
if (commentText.isNotEmpty()) {
model.commentService.createComment(postId!!, commentText, 1)
commentText = ""
model.createComment(commentText)
}
comments.refresh()
onCommentAdded()
@@ -136,13 +141,16 @@ fun CommentModalContent(
.padding(horizontal = 16.dp)
.weight(1f)
) {
CommentsSection(lazyPagingItems = comments, onLike = { comment: Comment ->
CommentsSection(lazyPagingItems = comments, onLike = { commentEntity: CommentEntity ->
scope.launch {
model.commentService.likeComment(comment.id)
if (commentEntity.liked) {
model.commentService.dislikeComment(commentEntity.id)
} else {
model.commentService.likeComment(commentEntity.id)
}
comments.refresh()
}
}) {
}
}