改包名com.aiosman.ravenow
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package com.aiosman.ravenow.ui.profile
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import com.aiosman.ravenow.LocalNavController
|
||||
import com.aiosman.ravenow.exp.viewModelFactory
|
||||
import com.aiosman.ravenow.ui.index.tabs.profile.MyProfileViewModel
|
||||
import com.aiosman.ravenow.ui.index.tabs.profile.ProfileV3
|
||||
import com.aiosman.ravenow.ui.navigateToChat
|
||||
|
||||
@Composable
|
||||
fun AccountProfileV2(id: String){
|
||||
val model: AccountProfileViewModel = viewModel(factory = viewModelFactory {
|
||||
AccountProfileViewModel()
|
||||
}, key = "viewModel_${id}")
|
||||
val navController = LocalNavController.current
|
||||
LaunchedEffect(Unit) {
|
||||
model.loadProfile(id)
|
||||
MyProfileViewModel.loadProfile()
|
||||
}
|
||||
var isSelf = false
|
||||
if (id == MyProfileViewModel.profile?.id.toString()) {
|
||||
isSelf = true
|
||||
}
|
||||
ProfileV3(
|
||||
sharedFlow = model.momentsFlow,
|
||||
profile = model.profile,
|
||||
isSelf = isSelf,
|
||||
onChatClick = {
|
||||
model.profile?.let {
|
||||
navController.navigateToChat(it.id.toString())
|
||||
}
|
||||
},
|
||||
onFollowClick = {
|
||||
model.profile?.let {
|
||||
if (it.isFollowing) {
|
||||
model.unFollowUser(id)
|
||||
} else {
|
||||
model.followUser(id)
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package com.aiosman.ravenow.ui.profile
|
||||
|
||||
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 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 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)
|
||||
fun loadProfile(id: String, pullRefresh: Boolean = false) {
|
||||
viewModelScope.launch {
|
||||
if (pullRefresh) {
|
||||
refreshing = true
|
||||
}
|
||||
if (profileId == profile?.id) {
|
||||
return@launch
|
||||
}
|
||||
profile = userService.getUserProfile(id)
|
||||
refreshing = false
|
||||
Pager(
|
||||
config = PagingConfig(pageSize = 5, enablePlaceholders = false),
|
||||
pagingSourceFactory = {
|
||||
MomentPagingSource(
|
||||
MomentRemoteDataSource(momentService),
|
||||
author = profile!!.id
|
||||
)
|
||||
}
|
||||
).flow.cachedIn(viewModelScope).collectLatest {
|
||||
_momentsFlow.value = it
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun followUser(userId: String) {
|
||||
viewModelScope.launch {
|
||||
userService.followUser(userId)
|
||||
profile = profile?.copy(followerCount = profile!!.followerCount + 1, isFollowing = true)
|
||||
}
|
||||
}
|
||||
|
||||
fun unFollowUser(userId: String) {
|
||||
viewModelScope.launch {
|
||||
userService.unFollowUser(userId)
|
||||
profile =
|
||||
profile?.copy(followerCount = profile!!.followerCount - 1, isFollowing = false)
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user