diff --git a/app/src/main/java/com/aiosman/riderpro/data/CommentService.kt b/app/src/main/java/com/aiosman/riderpro/data/CommentService.kt index 3a17b56..9684262 100644 --- a/app/src/main/java/com/aiosman/riderpro/data/CommentService.kt +++ b/app/src/main/java/com/aiosman/riderpro/data/CommentService.kt @@ -139,8 +139,8 @@ class CommentServiceImpl : CommentService { selfNotice: Boolean? ): ListContainer { val resp = ApiClient.api.getComments( - pageNumber, - postId, + page = pageNumber, + postId = postId, postUser = postUser, selfNotice = selfNotice?.let { if (it) 1 else 0 diff --git a/app/src/main/java/com/aiosman/riderpro/ui/comment/CommentModal.kt b/app/src/main/java/com/aiosman/riderpro/ui/comment/CommentModal.kt index a04072f..c7cf12a 100644 --- a/app/src/main/java/com/aiosman/riderpro/ui/comment/CommentModal.kt +++ b/app/src/main/java/com/aiosman/riderpro/ui/comment/CommentModal.kt @@ -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,22 +66,37 @@ class CommentModalViewModel( val postId: Int? ) : ViewModel() { val commentService: CommentService = CommentServiceImpl() - val commentsFlow: Flow> = Pager( - config = PagingConfig(pageSize = 20, enablePlaceholders = false), - pagingSourceFactory = { - CommentPagingSource( - CommentRemoteDataSource(commentService), - postId - ) + var commentText by mutableStateOf("") + + val commentsFlow = MutableStateFlow>(PagingData.empty()) + init { + reloadComments() + } + fun reloadComments() { + viewModelScope.launch { + Pager( + config = PagingConfig(pageSize = 20, enablePlaceholders = false), + pagingSourceFactory = { + CommentPagingSource( + CommentRemoteDataSource(commentService), + postId + ) + } + ).flow.cachedIn(viewModelScope).collectLatest { + commentsFlow.value = it + } } - ).flow.cachedIn(viewModelScope) + + } /** * 创建评论 */ - 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( diff --git a/app/src/main/java/com/aiosman/riderpro/ui/index/tabs/moment/Moment.kt b/app/src/main/java/com/aiosman/riderpro/ui/index/tabs/moment/Moment.kt index f53b328..f8a5fc4 100644 --- a/app/src/main/java/com/aiosman/riderpro/ui/index/tabs/moment/Moment.kt +++ b/app/src/main/java/com/aiosman/riderpro/ui/index/tabs/moment/Moment.kt @@ -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 diff --git a/app/src/main/java/com/aiosman/riderpro/ui/post/Post.kt b/app/src/main/java/com/aiosman/riderpro/ui/post/Post.kt index 5c92037..682d33c 100644 --- a/app/src/main/java/com/aiosman/riderpro/ui/post/Post.kt +++ b/app/src/main/java/com/aiosman/riderpro/ui/post/Post.kt @@ -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}",