更新动态加载逻辑
This commit is contained in:
@@ -200,7 +200,7 @@ class CommentsViewModel(
|
||||
replyUserId = replyUserId,
|
||||
replyCommentId = replyCommentId
|
||||
)
|
||||
TimelineMomentViewModel.updateCommentCount(postId.toInt())
|
||||
// TimelineMomentViewModel.updateCommentCount(postId.toInt())
|
||||
// add to first
|
||||
addedCommentList = listOf(comment) + addedCommentList
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import com.aiosman.ravenow.data.MomentService
|
||||
import com.aiosman.ravenow.entity.MomentServiceImpl
|
||||
import com.aiosman.ravenow.data.UploadImage
|
||||
import com.aiosman.ravenow.entity.MomentEntity
|
||||
import com.aiosman.ravenow.event.MomentAddEvent
|
||||
import com.aiosman.ravenow.exp.rotate
|
||||
import com.aiosman.ravenow.ui.index.tabs.moment.tabs.timeline.TimelineMomentViewModel
|
||||
import com.aiosman.ravenow.ui.index.tabs.profile.MyProfileViewModel
|
||||
@@ -21,6 +22,7 @@ import com.aiosman.ravenow.ui.modification.Modification
|
||||
import com.aiosman.ravenow.utils.FileUtil
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.greenrobot.eventbus.EventBus
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
import java.io.IOException
|
||||
@@ -141,10 +143,10 @@ object NewPostViewModel : ViewModel() {
|
||||
onUploadProgress(((index / imageList.size).toFloat())) // progressValue 是当前上传进度,例如 0.5 表示 50%
|
||||
index += 1
|
||||
}
|
||||
momentService.createMoment(textContent, 1, uploadImageList, relPostId)
|
||||
val result = momentService.createMoment(textContent, 1, uploadImageList, relPostId)
|
||||
// 刷新个人动态
|
||||
MyProfileViewModel.loadProfile(pullRefresh = true)
|
||||
TimelineMomentViewModel.refreshPager()
|
||||
EventBus.getDefault().post(MomentAddEvent(result))
|
||||
}
|
||||
|
||||
suspend fun init() {
|
||||
|
||||
@@ -13,9 +13,14 @@ import com.aiosman.ravenow.data.UserServiceImpl
|
||||
import com.aiosman.ravenow.entity.AccountProfileEntity
|
||||
import com.aiosman.ravenow.entity.MomentEntity
|
||||
import com.aiosman.ravenow.entity.MomentServiceImpl
|
||||
import com.aiosman.ravenow.event.MomentFavouriteChangeEvent
|
||||
import com.aiosman.ravenow.event.MomentLikeChangeEvent
|
||||
import com.aiosman.ravenow.event.MomentRemoveEvent
|
||||
import com.aiosman.ravenow.ui.index.tabs.moment.tabs.timeline.TimelineMomentViewModel
|
||||
import com.aiosman.ravenow.ui.index.tabs.profile.MyProfileViewModel
|
||||
import kotlinx.coroutines.launch
|
||||
import org.greenrobot.eventbus.EventBus
|
||||
import org.greenrobot.eventbus.Subscribe
|
||||
|
||||
|
||||
class PostViewModel(
|
||||
@@ -31,13 +36,16 @@ class PostViewModel(
|
||||
var commentsViewModel: CommentsViewModel = CommentsViewModel(postId)
|
||||
var isError by mutableStateOf(false)
|
||||
var isFirstLoad by mutableStateOf(true)
|
||||
init {
|
||||
EventBus.getDefault().register(this)
|
||||
}
|
||||
|
||||
fun reloadComment() {
|
||||
commentsViewModel.reloadComment()
|
||||
}
|
||||
|
||||
suspend fun initData(highlightCommentId: Int? = null) {
|
||||
if (!isFirstLoad) {
|
||||
if (!isFirstLoad) {
|
||||
return
|
||||
}
|
||||
isFirstLoad = false
|
||||
@@ -77,37 +85,80 @@ class PostViewModel(
|
||||
moment = moment?.copy(commentCount = moment?.commentCount?.plus(1) ?: 0)
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
fun onMomentLikeChangeEvent(event: MomentLikeChangeEvent) {
|
||||
moment?.let {
|
||||
if (event.postId == it.id) {
|
||||
moment = it.copy(likeCount = event.likeCount ?: it.likeCount, liked = event.isLike)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun likeMoment() {
|
||||
moment?.let {
|
||||
service.likeMoment(it.id)
|
||||
moment = moment?.copy(likeCount = moment?.likeCount?.plus(1) ?: 0, liked = true)
|
||||
TimelineMomentViewModel.updateLikeCount(it.id)
|
||||
EventBus.getDefault().post(
|
||||
MomentLikeChangeEvent(
|
||||
postId = it.id,
|
||||
likeCount = it.likeCount + 1,
|
||||
isLike = true
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun dislikeMoment() {
|
||||
moment?.let {
|
||||
service.dislikeMoment(it.id)
|
||||
moment = moment?.copy(likeCount = moment?.likeCount?.minus(1) ?: 0, liked = false)
|
||||
// update home list
|
||||
TimelineMomentViewModel.updateDislikeMomentById(it.id)
|
||||
EventBus.getDefault().post(
|
||||
MomentLikeChangeEvent(
|
||||
postId = it.id,
|
||||
likeCount = it.likeCount - 1,
|
||||
isLike = false
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
fun onMomentFavouriteChangeEvent(event: MomentFavouriteChangeEvent) {
|
||||
moment?.let {
|
||||
if (event.postId == it.id) {
|
||||
val favouriteCount = if (event.isFavourite) {
|
||||
it.favoriteCount + 1
|
||||
} else {
|
||||
it.favoriteCount - 1
|
||||
}
|
||||
moment = it.copy(
|
||||
favoriteCount = favouriteCount,
|
||||
isFavorite = event.isFavourite
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun favoriteMoment() {
|
||||
moment?.let {
|
||||
service.favoriteMoment(it.id)
|
||||
moment =
|
||||
moment?.copy(favoriteCount = moment?.favoriteCount?.plus(1) ?: 0, isFavorite = true)
|
||||
EventBus.getDefault().post(
|
||||
MomentFavouriteChangeEvent(
|
||||
postId = it.id,
|
||||
isFavourite = true
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun unfavoriteMoment() {
|
||||
moment?.let {
|
||||
service.unfavoriteMoment(it.id)
|
||||
moment = moment?.copy(
|
||||
favoriteCount = moment?.favoriteCount?.minus(1) ?: 0, isFavorite = false
|
||||
EventBus.getDefault().post(
|
||||
MomentFavouriteChangeEvent(
|
||||
postId = it.id,
|
||||
isFavourite = false
|
||||
)
|
||||
)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,7 +182,6 @@ class PostViewModel(
|
||||
commentsViewModel.deleteComment(commentId)
|
||||
moment = moment?.copy(commentCount = moment?.commentCount?.minus(1) ?: 0)
|
||||
moment?.let {
|
||||
TimelineMomentViewModel.updateMomentCommentCount(it.id, -1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -160,8 +210,7 @@ class PostViewModel(
|
||||
viewModelScope.launch {
|
||||
moment?.let {
|
||||
service.deleteMoment(it.id)
|
||||
TimelineMomentViewModel.deleteMoment(it.id)
|
||||
MyProfileViewModel.deleteMoment(it.id)
|
||||
EventBus.getDefault().post(MomentRemoveEvent(it.id))
|
||||
}
|
||||
callback()
|
||||
}
|
||||
@@ -171,4 +220,8 @@ class PostViewModel(
|
||||
commentsViewModel.loadMoreSubComments(commentId)
|
||||
}
|
||||
|
||||
override fun onCleared() {
|
||||
super.onCleared()
|
||||
EventBus.getDefault().unregister(this)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user