新增 PostViewModel 及其相关逻辑
This commit is contained in:
202
app/src/main/java/com/aiosman/riderpro/ui/post/PostViewModel.kt
Normal file
202
app/src/main/java/com/aiosman/riderpro/ui/post/PostViewModel.kt
Normal file
@@ -0,0 +1,202 @@
|
|||||||
|
package com.aiosman.riderpro.ui.post
|
||||||
|
|
||||||
|
import androidx.compose.runtime.getValue
|
||||||
|
import androidx.compose.runtime.mutableStateOf
|
||||||
|
import androidx.compose.runtime.setValue
|
||||||
|
import androidx.lifecycle.ViewModel
|
||||||
|
import androidx.lifecycle.viewModelScope
|
||||||
|
import androidx.paging.Pager
|
||||||
|
import androidx.paging.PagingConfig
|
||||||
|
import androidx.paging.PagingData
|
||||||
|
import androidx.paging.cachedIn
|
||||||
|
import androidx.paging.map
|
||||||
|
import com.aiosman.riderpro.data.AccountService
|
||||||
|
import com.aiosman.riderpro.data.AccountServiceImpl
|
||||||
|
import com.aiosman.riderpro.data.CommentRemoteDataSource
|
||||||
|
import com.aiosman.riderpro.data.CommentService
|
||||||
|
import com.aiosman.riderpro.data.CommentServiceImpl
|
||||||
|
import com.aiosman.riderpro.data.MomentService
|
||||||
|
import com.aiosman.riderpro.data.UserService
|
||||||
|
import com.aiosman.riderpro.data.UserServiceImpl
|
||||||
|
import com.aiosman.riderpro.entity.AccountProfileEntity
|
||||||
|
import com.aiosman.riderpro.entity.CommentEntity
|
||||||
|
import com.aiosman.riderpro.entity.CommentPagingSource
|
||||||
|
import com.aiosman.riderpro.entity.MomentEntity
|
||||||
|
import com.aiosman.riderpro.entity.MomentServiceImpl
|
||||||
|
import com.aiosman.riderpro.ui.index.tabs.moment.MomentViewModel
|
||||||
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
|
import kotlinx.coroutines.flow.asStateFlow
|
||||||
|
import kotlinx.coroutines.flow.collectLatest
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
|
object PostViewModel : ViewModel() {
|
||||||
|
var service: MomentService = MomentServiceImpl()
|
||||||
|
var commentService: CommentService = CommentServiceImpl()
|
||||||
|
var userService: UserService = UserServiceImpl()
|
||||||
|
private var _commentsFlow = MutableStateFlow<PagingData<CommentEntity>>(PagingData.empty())
|
||||||
|
val commentsFlow = _commentsFlow.asStateFlow()
|
||||||
|
var postId: String = ""
|
||||||
|
|
||||||
|
// 预加载的 moment
|
||||||
|
|
||||||
|
var accountProfileEntity by mutableStateOf<AccountProfileEntity?>(null)
|
||||||
|
var moment by mutableStateOf<MomentEntity?>(null)
|
||||||
|
var accountService: AccountService = AccountServiceImpl()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 预加载,在跳转到 PostScreen 之前设置好内容
|
||||||
|
*/
|
||||||
|
fun preTransit(momentEntity: MomentEntity?) {
|
||||||
|
this.postId = momentEntity?.id.toString()
|
||||||
|
this.moment = momentEntity
|
||||||
|
viewModelScope.launch {
|
||||||
|
Pager(
|
||||||
|
config = PagingConfig(pageSize = 5, enablePlaceholders = false),
|
||||||
|
pagingSourceFactory = {
|
||||||
|
CommentPagingSource(
|
||||||
|
CommentRemoteDataSource(commentService),
|
||||||
|
postId = postId.toInt()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
).flow.cachedIn(viewModelScope).collectLatest {
|
||||||
|
_commentsFlow.value = it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
// accountProfileEntity = userService.getUserProfile(it.authorId.toString())
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun likeComment(commentId: Int) {
|
||||||
|
commentService.likeComment(commentId)
|
||||||
|
val currentPagingData = commentsFlow.value
|
||||||
|
val updatedPagingData = currentPagingData.map { comment ->
|
||||||
|
if (comment.id == commentId) {
|
||||||
|
comment.copy(liked = !comment.liked, likes = comment.likes + 1)
|
||||||
|
} else {
|
||||||
|
comment
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_commentsFlow.value = updatedPagingData
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun unlikeComment(commentId: Int) {
|
||||||
|
commentService.dislikeComment(commentId)
|
||||||
|
val currentPagingData = commentsFlow.value
|
||||||
|
val updatedPagingData = currentPagingData.map { comment ->
|
||||||
|
if (comment.id == commentId) {
|
||||||
|
comment.copy(liked = !comment.liked, likes = comment.likes - 1)
|
||||||
|
} else {
|
||||||
|
comment
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_commentsFlow.value = updatedPagingData
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun createComment(content: String) {
|
||||||
|
commentService.createComment(postId.toInt(), content)
|
||||||
|
this.moment = service.getMomentById(postId.toInt())
|
||||||
|
MomentViewModel.updateCommentCount(postId.toInt())
|
||||||
|
reloadComment()
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun likeMoment() {
|
||||||
|
moment?.let {
|
||||||
|
service.likeMoment(it.id)
|
||||||
|
moment = moment?.copy(likeCount = moment?.likeCount?.plus(1) ?: 0, liked = true)
|
||||||
|
MomentViewModel.updateLikeCount(it.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun dislikeMoment() {
|
||||||
|
moment?.let {
|
||||||
|
service.dislikeMoment(it.id)
|
||||||
|
moment = moment?.copy(likeCount = moment?.likeCount?.minus(1) ?: 0, liked = false)
|
||||||
|
// update home list
|
||||||
|
MomentViewModel.updateDislikeMomentById(it.id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun favoriteMoment() {
|
||||||
|
moment?.let {
|
||||||
|
service.favoriteMoment(it.id)
|
||||||
|
moment =
|
||||||
|
moment?.copy(favoriteCount = moment?.favoriteCount?.plus(1) ?: 0, isFavorite = true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun unfavoriteMoment() {
|
||||||
|
moment?.let {
|
||||||
|
service.unfavoriteMoment(it.id)
|
||||||
|
moment = moment?.copy(
|
||||||
|
favoriteCount = moment?.favoriteCount?.minus(1) ?: 0,
|
||||||
|
isFavorite = false
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun followUser() {
|
||||||
|
accountProfileEntity?.let {
|
||||||
|
userService.followUser(it.id.toString())
|
||||||
|
accountProfileEntity = accountProfileEntity?.copy(isFollowing = true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun unfollowUser() {
|
||||||
|
accountProfileEntity?.let {
|
||||||
|
userService.unFollowUser(it.id.toString())
|
||||||
|
accountProfileEntity = accountProfileEntity?.copy(isFollowing = false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var avatar: String? = null
|
||||||
|
get() {
|
||||||
|
accountProfileEntity?.avatar?.let {
|
||||||
|
return it
|
||||||
|
}
|
||||||
|
moment?.avatar?.let {
|
||||||
|
return it
|
||||||
|
}
|
||||||
|
return field
|
||||||
|
}
|
||||||
|
var nickname: String? = null
|
||||||
|
get() {
|
||||||
|
accountProfileEntity?.nickName?.let {
|
||||||
|
return it
|
||||||
|
}
|
||||||
|
moment?.nickname?.let {
|
||||||
|
return it
|
||||||
|
}
|
||||||
|
return field
|
||||||
|
}
|
||||||
|
fun deleteMoment(callback: () -> Unit) {
|
||||||
|
viewModelScope.launch {
|
||||||
|
moment?.let {
|
||||||
|
service.deleteMoment(it.id)
|
||||||
|
MomentViewModel.deleteMoment(it.id)
|
||||||
|
}
|
||||||
|
callback()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user