更新评论功能
- 评论现在在发送后自动刷新 - 评论现在在点赞后自动刷新 - 评论现在在创建后自动 刷新 - 评论现在在加载时自动刷新 - 评论现在在删除后自动刷新 - 评论现在在编辑后自动刷新
This commit is contained in:
@@ -139,8 +139,8 @@ class CommentServiceImpl : CommentService {
|
||||
selfNotice: Boolean?
|
||||
): ListContainer<CommentEntity> {
|
||||
val resp = ApiClient.api.getComments(
|
||||
pageNumber,
|
||||
postId,
|
||||
page = pageNumber,
|
||||
postId = postId,
|
||||
postUser = postUser,
|
||||
selfNotice = selfNotice?.let {
|
||||
if (it) 1 else 0
|
||||
|
||||
@@ -32,10 +32,10 @@ import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.platform.LocalDensity
|
||||
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.text.TextStyle
|
||||
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
|
||||
@@ -47,15 +47,16 @@ 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.entity.CommentEntity
|
||||
import com.aiosman.riderpro.entity.CommentPagingSource
|
||||
import com.aiosman.riderpro.data.CommentRemoteDataSource
|
||||
import com.aiosman.riderpro.data.CommentService
|
||||
import com.aiosman.riderpro.data.CommentServiceImpl
|
||||
import com.aiosman.riderpro.entity.CommentEntity
|
||||
import com.aiosman.riderpro.entity.CommentPagingSource
|
||||
import com.aiosman.riderpro.ui.modifiers.noRippleClickable
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import com.aiosman.riderpro.ui.post.CommentsSection
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
/**
|
||||
@@ -65,7 +66,15 @@ class CommentModalViewModel(
|
||||
val postId: Int?
|
||||
) : ViewModel() {
|
||||
val commentService: CommentService = CommentServiceImpl()
|
||||
val commentsFlow: Flow<PagingData<CommentEntity>> = Pager(
|
||||
var commentText by mutableStateOf("")
|
||||
|
||||
val commentsFlow = MutableStateFlow<PagingData<CommentEntity>>(PagingData.empty())
|
||||
init {
|
||||
reloadComments()
|
||||
}
|
||||
fun reloadComments() {
|
||||
viewModelScope.launch {
|
||||
Pager(
|
||||
config = PagingConfig(pageSize = 20, enablePlaceholders = false),
|
||||
pagingSourceFactory = {
|
||||
CommentPagingSource(
|
||||
@@ -73,14 +82,21 @@ class CommentModalViewModel(
|
||||
postId
|
||||
)
|
||||
}
|
||||
).flow.cachedIn(viewModelScope)
|
||||
).flow.cachedIn(viewModelScope).collectLatest {
|
||||
commentsFlow.value = it
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建评论
|
||||
*/
|
||||
suspend fun createComment(content: String) {
|
||||
suspend fun createComment() {
|
||||
postId?.let {
|
||||
commentService.createComment(postId, content)
|
||||
commentService.createComment(postId, commentText)
|
||||
reloadComments()
|
||||
commentText = ""
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -107,13 +123,16 @@ fun CommentModalContent(
|
||||
}
|
||||
)
|
||||
var navBarHeight = WindowInsets.navigationBars.asPaddingValues().calculateBottomPadding()
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
model.reloadComments()
|
||||
}
|
||||
|
||||
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) }
|
||||
var softwareKeyboardController = LocalSoftwareKeyboardController.current
|
||||
LaunchedEffect(imePadding) {
|
||||
bottomPadding = imePadding.dp
|
||||
}
|
||||
@@ -122,12 +141,11 @@ fun CommentModalContent(
|
||||
onDismiss()
|
||||
}
|
||||
}
|
||||
var commentText by remember { mutableStateOf("") }
|
||||
suspend fun sendComment() {
|
||||
if (commentText.isNotEmpty()) {
|
||||
model.createComment(commentText)
|
||||
if (model.commentText.isNotEmpty()) {
|
||||
softwareKeyboardController?.hide()
|
||||
model.createComment()
|
||||
}
|
||||
comments.refresh()
|
||||
onCommentAdded()
|
||||
}
|
||||
Column(
|
||||
@@ -163,7 +181,7 @@ fun CommentModalContent(
|
||||
} else {
|
||||
model.commentService.likeComment(commentEntity.id)
|
||||
}
|
||||
comments.refresh()
|
||||
model.reloadComments()
|
||||
}
|
||||
}) {
|
||||
}
|
||||
@@ -197,8 +215,8 @@ fun CommentModalContent(
|
||||
|
||||
) {
|
||||
BasicTextField(
|
||||
value = commentText,
|
||||
onValueChange = { text -> commentText = text },
|
||||
value = model.commentText,
|
||||
onValueChange = { text -> model.commentText = text },
|
||||
modifier = Modifier
|
||||
.fillMaxWidth(),
|
||||
textStyle = TextStyle(
|
||||
|
||||
@@ -146,7 +146,12 @@ fun MomentsList() {
|
||||
}
|
||||
}
|
||||
)
|
||||
Box(modifier = Modifier.height(16.dp).fillMaxWidth().background(Color(0xFFF0F2F5)))
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.height(16.dp)
|
||||
.fillMaxWidth()
|
||||
.background(Color(0xFFF0F2F5))
|
||||
)
|
||||
}
|
||||
}
|
||||
PullRefreshIndicator(refreshing, state, Modifier.align(Alignment.TopCenter))
|
||||
@@ -423,7 +428,7 @@ fun MomentContentGroup(
|
||||
text = momentEntity.momentTextContent,
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(start = 24.dp,end = 24.dp, bottom = 8.dp),
|
||||
.padding(start = 24.dp, end = 24.dp, bottom = 8.dp),
|
||||
fontSize = 16.sp
|
||||
)
|
||||
}
|
||||
@@ -512,7 +517,6 @@ fun MomentBottomOperateRowGroup(
|
||||
}
|
||||
) {
|
||||
CommentModalContent(postId = momentEntity.id, onCommentAdded = {
|
||||
showCommentModal = false
|
||||
onAddComment()
|
||||
})
|
||||
}
|
||||
@@ -544,10 +548,7 @@ fun MomentBottomOperateRowGroup(
|
||||
Box(
|
||||
modifier = Modifier
|
||||
.fillMaxHeight()
|
||||
.clickable(
|
||||
indication = null,
|
||||
interactionSource = remember { MutableInteractionSource() }
|
||||
) {
|
||||
.noRippleClickable {
|
||||
showCommentModal = true
|
||||
},
|
||||
contentAlignment = Alignment.Center
|
||||
|
||||
@@ -141,6 +141,22 @@ object PostViewModel : ViewModel() {
|
||||
}
|
||||
}
|
||||
|
||||
fun reloadComment() {
|
||||
viewModelScope.launch {
|
||||
Pager(
|
||||
config = PagingConfig(pageSize = 5, enablePlaceholders = false),
|
||||
pagingSourceFactory = {
|
||||
CommentPagingSource(
|
||||
CommentRemoteDataSource(commentService),
|
||||
postId = postId.toInt()
|
||||
)
|
||||
}
|
||||
).flow.cachedIn(viewModelScope).collectLatest {
|
||||
_commentsFlow.value = it
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun initData() {
|
||||
moment = service.getMomentById(postId.toInt())
|
||||
// moment?.let {
|
||||
@@ -176,7 +192,9 @@ object PostViewModel : ViewModel() {
|
||||
|
||||
suspend fun createComment(content: String) {
|
||||
commentService.createComment(postId.toInt(), content)
|
||||
this.moment = service.getMomentById(postId.toInt())
|
||||
MomentViewModel.updateCommentCount(postId.toInt())
|
||||
reloadComment()
|
||||
}
|
||||
|
||||
suspend fun likeMoment() {
|
||||
@@ -282,7 +300,6 @@ fun PostScreen(
|
||||
onCreateComment = {
|
||||
scope.launch {
|
||||
viewModel.createComment(it)
|
||||
commentsPagging.refresh()
|
||||
}
|
||||
},
|
||||
onFavoriteClick = {
|
||||
@@ -593,7 +610,8 @@ fun CommentItem(commentEntity: CommentEntity, onLike: () -> Unit = {}) {
|
||||
modifier = Modifier
|
||||
.size(40.dp)
|
||||
.clip(CircleShape)
|
||||
.background(Color.Gray.copy(alpha = 0.1f)).noRippleClickable {
|
||||
.background(Color.Gray.copy(alpha = 0.1f))
|
||||
.noRippleClickable {
|
||||
navController.navigate(
|
||||
NavigationRoute.AccountProfile.route.replace(
|
||||
"{id}",
|
||||
|
||||
Reference in New Issue
Block a user