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