添加更新数据
This commit is contained in:
@@ -23,6 +23,7 @@ import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.rememberCoroutineScope
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
@@ -35,30 +36,54 @@ import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.tooling.preview.Preview
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import androidx.paging.Pager
|
||||
import androidx.paging.PagingConfig
|
||||
import androidx.paging.PagingData
|
||||
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.CommentPagingSource
|
||||
import com.aiosman.riderpro.data.CommentRemoteDataSource
|
||||
import com.aiosman.riderpro.data.CommentService
|
||||
import com.aiosman.riderpro.data.TestCommentServiceImpl
|
||||
import com.aiosman.riderpro.ui.modifiers.noRippleClickable
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
|
||||
class CommentModalViewModel(
|
||||
postId: Int?
|
||||
):ViewModel(){
|
||||
val commentService:CommentService = TestCommentServiceImpl()
|
||||
val commentsFlow: Flow<PagingData<Comment>> = Pager(
|
||||
config = PagingConfig(pageSize = 20, enablePlaceholders = false),
|
||||
pagingSourceFactory = {
|
||||
CommentPagingSource(
|
||||
CommentRemoteDataSource(commentService),
|
||||
postId
|
||||
)
|
||||
}
|
||||
).flow.cachedIn(viewModelScope)
|
||||
}
|
||||
@Preview
|
||||
@Composable
|
||||
fun CommentModalContent(onDismiss: () -> Unit = {}) {
|
||||
var commentSource = CommentPagingSource(
|
||||
CommentRemoteDataSource(TestCommentServiceImpl())
|
||||
fun CommentModalContent(postId: Int? = null, onDismiss: () -> Unit = {}) {
|
||||
val model = viewModel<CommentModalViewModel>(
|
||||
factory = object : ViewModelProvider.Factory {
|
||||
override fun <T : ViewModel> create(modelClass: Class<T>): T {
|
||||
return CommentModalViewModel(postId) as T
|
||||
}
|
||||
}
|
||||
)
|
||||
val commentsFlow: Flow<PagingData<Comment>> = Pager(
|
||||
config = PagingConfig(pageSize = 5, enablePlaceholders = false),
|
||||
pagingSourceFactory = { commentSource }
|
||||
).flow
|
||||
val comments = commentsFlow.collectAsLazyPagingItems()
|
||||
|
||||
|
||||
val scope = rememberCoroutineScope()
|
||||
val comments = model.commentsFlow.collectAsLazyPagingItems()
|
||||
val insets = WindowInsets
|
||||
val imePadding = insets.ime.getBottom(density = LocalDensity.current)
|
||||
var bottomPadding by remember { mutableStateOf(0.dp) }
|
||||
@@ -70,7 +95,14 @@ fun CommentModalContent(onDismiss: () -> Unit = {}) {
|
||||
onDismiss()
|
||||
}
|
||||
}
|
||||
|
||||
var commentText by remember { mutableStateOf("") }
|
||||
suspend fun sendComment() {
|
||||
if (commentText.isNotEmpty()) {
|
||||
model.commentService.createComment(postId!!, commentText, 1)
|
||||
commentText = ""
|
||||
}
|
||||
comments.refresh()
|
||||
}
|
||||
Column(
|
||||
modifier = Modifier
|
||||
) {
|
||||
@@ -97,8 +129,13 @@ fun CommentModalContent(onDismiss: () -> Unit = {}) {
|
||||
.padding(horizontal = 16.dp)
|
||||
.weight(1f)
|
||||
) {
|
||||
|
||||
CommentsSection(lazyPagingItems = comments) {
|
||||
CommentsSection(lazyPagingItems = comments, onLike = {
|
||||
comment: Comment ->
|
||||
scope.launch {
|
||||
model.commentService.likeComment(comment.id)
|
||||
comments.refresh()
|
||||
}
|
||||
}) {
|
||||
|
||||
}
|
||||
|
||||
@@ -127,8 +164,8 @@ fun CommentModalContent(onDismiss: () -> Unit = {}) {
|
||||
|
||||
) {
|
||||
BasicTextField(
|
||||
value = "",
|
||||
onValueChange = { },
|
||||
value = commentText,
|
||||
onValueChange = { text -> commentText = text },
|
||||
modifier = Modifier
|
||||
.fillMaxWidth(),
|
||||
textStyle = TextStyle(
|
||||
@@ -144,7 +181,11 @@ fun CommentModalContent(onDismiss: () -> Unit = {}) {
|
||||
contentDescription = "Send",
|
||||
modifier = Modifier
|
||||
.size(32.dp)
|
||||
|
||||
.noRippleClickable {
|
||||
scope.launch {
|
||||
sendComment()
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user