更新动态加载逻辑

This commit is contained in:
2024-12-01 09:40:13 +08:00
parent 6c19f83cfb
commit 79fccda1aa
25 changed files with 657 additions and 524 deletions

View File

@@ -24,7 +24,7 @@ fun AccountProfileV2(id: String){
isSelf = true
}
ProfileV3(
sharedFlow = model.momentsFlow,
moments = model.moments,
profile = model.profile,
isSelf = isSelf,
onChatClick = {

View File

@@ -5,34 +5,29 @@ 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 com.aiosman.ravenow.data.AccountService
import com.aiosman.ravenow.data.AccountServiceImpl
import com.aiosman.ravenow.data.MomentService
import com.aiosman.ravenow.data.UserServiceImpl
import com.aiosman.ravenow.entity.AccountProfileEntity
import com.aiosman.ravenow.entity.MomentEntity
import com.aiosman.ravenow.entity.MomentPagingSource
import com.aiosman.ravenow.entity.MomentRemoteDataSource
import com.aiosman.ravenow.entity.MomentServiceImpl
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.collectLatest
import com.aiosman.ravenow.entity.MomentLoader
import com.aiosman.ravenow.entity.MomentLoaderExtraArgs
import kotlinx.coroutines.launch
class AccountProfileViewModel : ViewModel() {
var profileId by mutableStateOf(0)
val accountService: AccountService = AccountServiceImpl()
val momentService: MomentService = MomentServiceImpl()
val userService = UserServiceImpl()
var profile by mutableStateOf<AccountProfileEntity?>(null)
private var _momentsFlow = MutableStateFlow<PagingData<MomentEntity>>(PagingData.empty())
var momentsFlow = _momentsFlow.asStateFlow()
var refreshing by mutableStateOf(false)
var momentLoader = MomentLoader().apply {
onListChanged = {
moments = it
}
}
var moments by mutableStateOf<List<MomentEntity>>(listOf())
fun loadProfile(id: String, pullRefresh: Boolean = false) {
viewModelScope.launch {
if (pullRefresh) {
@@ -43,16 +38,12 @@ class AccountProfileViewModel : ViewModel() {
}
profile = userService.getUserProfile(id)
refreshing = false
Pager(
config = PagingConfig(pageSize = 5, enablePlaceholders = false),
pagingSourceFactory = {
MomentPagingSource(
MomentRemoteDataSource(momentService),
author = profile!!.id
)
profile?.let {
try {
momentLoader.loadData(MomentLoaderExtraArgs(authorId = it.id))
} catch (e: Exception) {
e.printStackTrace()
}
).flow.cachedIn(viewModelScope).collectLatest {
_momentsFlow.value = it
}
}
}
@@ -72,8 +63,6 @@ class AccountProfileViewModel : ViewModel() {
}
}
val followerCount get() = profile?.followerCount ?: 0
val followingCount get() = profile?.followingCount ?: 0
val bio get() = profile?.bio ?: ""
val nickName get() = profile?.nickName ?: ""
val avatar get() = profile?.avatar