From a4ab6f0ab76e88e9e9ad3dbdf062f8b68faa6978 Mon Sep 17 00:00:00 2001 From: AllenTom Date: Sun, 1 Sep 2024 21:37:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=B8=AA=E4=BA=BA=E4=B8=BB?= =?UTF-8?q?=E9=A1=B5=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 优化用户主页数据加载逻辑 - 调整用户主页UI显示 - 修复关注按钮文案问题 --- .../index/tabs/profile/MyProfileViewModel.kt | 13 -- .../riderpro/ui/index/tabs/profile/Profile.kt | 9 +- .../riderpro/ui/profile/AccountProfile.kt | 141 ++++++++---------- .../ui/profile/AccountProfileViewModel.kt | 70 ++++++++- 4 files changed, 139 insertions(+), 94 deletions(-) diff --git a/app/src/main/java/com/aiosman/riderpro/ui/index/tabs/profile/MyProfileViewModel.kt b/app/src/main/java/com/aiosman/riderpro/ui/index/tabs/profile/MyProfileViewModel.kt index 666f7f5..0143ea9 100644 --- a/app/src/main/java/com/aiosman/riderpro/ui/index/tabs/profile/MyProfileViewModel.kt +++ b/app/src/main/java/com/aiosman/riderpro/ui/index/tabs/profile/MyProfileViewModel.kt @@ -32,19 +32,6 @@ object MyProfileViewModel : ViewModel() { private var _momentsFlow = MutableStateFlow>(PagingData.empty()) var momentsFlow = _momentsFlow.asStateFlow() fun loadProfile(){ - -// momentsFlow = Pager( -// config = PagingConfig(pageSize = 5, enablePlaceholders = false), -// pagingSourceFactory = { -// MomentPagingSource( -// MomentRemoteDataSource(MomentServiceImpl()), -// author = profile?.id ?: 0, -// -// ) -// } -// ).flow.cachedIn(viewModelScope).collectLatest { -// MomentViewModel._momentsFlow.value = it -// } viewModelScope.launch { profile = accountService.getMyAccountProfile() val profile = accountService.getMyAccountProfile() diff --git a/app/src/main/java/com/aiosman/riderpro/ui/index/tabs/profile/Profile.kt b/app/src/main/java/com/aiosman/riderpro/ui/index/tabs/profile/Profile.kt index c8c7626..45446e0 100644 --- a/app/src/main/java/com/aiosman/riderpro/ui/index/tabs/profile/Profile.kt +++ b/app/src/main/java/com/aiosman/riderpro/ui/index/tabs/profile/Profile.kt @@ -253,7 +253,7 @@ fun UserInformation( UserInformationBasic(userInfoModifier, accountProfileEntity) UserInformationFollowing(userInfoModifier, accountProfileEntity) } - UserInformationSlogan() + UserInformationSlogan(accountProfileEntity) CommunicationOperatorGroup( isSelf = isSelf, isFollowing = accountProfileEntity.isFollowing, @@ -370,11 +370,10 @@ fun UserInformationFollowing(modifier: Modifier, accountProfileEntity: AccountPr } @Composable -fun UserInformationSlogan() { - val model = MyProfileViewModel +fun UserInformationSlogan(accountProfileEntity: AccountProfileEntity) { Text( modifier = Modifier.padding(top = 23.dp), - text = model.bio, + text = accountProfileEntity.bio, fontSize = 13.sp, color = Color.Black, style = TextStyle(fontWeight = FontWeight.Bold) @@ -408,7 +407,7 @@ fun CommunicationOperatorGroup( contentDescription = "" ) Text( - text = if (isFollowing) "FOLLOWING" else "FOLLOW", + text = if (isFollowing) stringResource(R.string.following_upper) else stringResource(R.string.follow_upper), fontSize = 16.sp, color = Color.White, style = TextStyle(fontWeight = FontWeight.Bold) diff --git a/app/src/main/java/com/aiosman/riderpro/ui/profile/AccountProfile.kt b/app/src/main/java/com/aiosman/riderpro/ui/profile/AccountProfile.kt index 3b4c741..b5bb0f6 100644 --- a/app/src/main/java/com/aiosman/riderpro/ui/profile/AccountProfile.kt +++ b/app/src/main/java/com/aiosman/riderpro/ui/profile/AccountProfile.kt @@ -1,104 +1,95 @@ package com.aiosman.riderpro.ui.profile +import androidx.compose.foundation.Image import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect -import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf -import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope -import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.layout.ContentScale +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.painterResource import androidx.compose.ui.unit.dp -import androidx.paging.Pager -import androidx.paging.PagingConfig -import androidx.paging.PagingData import androidx.paging.compose.collectAsLazyPagingItems -import com.aiosman.riderpro.entity.AccountProfileEntity -import com.aiosman.riderpro.entity.MomentPagingSource -import com.aiosman.riderpro.entity.MomentRemoteDataSource -import com.aiosman.riderpro.entity.MomentServiceImpl -import com.aiosman.riderpro.data.UserServiceImpl -import com.aiosman.riderpro.data.UserService -import com.aiosman.riderpro.entity.MomentEntity -import com.aiosman.riderpro.ui.composables.StatusBarMaskLayout -import com.aiosman.riderpro.ui.index.tabs.profile.CarGroup +import com.aiosman.riderpro.R +import com.aiosman.riderpro.ui.composables.CustomAsyncImage import com.aiosman.riderpro.ui.index.tabs.profile.MomentPostUnit -import com.aiosman.riderpro.ui.index.tabs.profile.RidingStyle import com.aiosman.riderpro.ui.index.tabs.profile.UserInformation -import com.google.accompanist.systemuicontroller.rememberSystemUiController -import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.launch @Composable -fun AccountProfile(id:String) { - val userService: UserService = UserServiceImpl() - var userProfile by remember { mutableStateOf(null) } - val momentService = MomentServiceImpl() - var momentsFlow by remember { mutableStateOf>?>(null) } +fun AccountProfile(id: String) { + val model = AccountProfileViewModel val scope = rememberCoroutineScope() + val items = model.momentsFlow.collectAsLazyPagingItems() LaunchedEffect(Unit) { - userProfile = userService.getUserProfile(id) - momentsFlow = Pager( - config = PagingConfig(pageSize = 5, enablePlaceholders = false), - pagingSourceFactory = { - MomentPagingSource( - MomentRemoteDataSource(momentService), - author = id.toInt() - ) - } - ).flow + model.loadProfile(id) } - val items = momentsFlow?.collectAsLazyPagingItems() - val systemUiController = rememberSystemUiController() - LaunchedEffect(Unit) { - systemUiController.setNavigationBarColor( - color = androidx.compose.ui.graphics.Color.Transparent - ) - } - StatusBarMaskLayout( - modifier = Modifier.fillMaxSize(), - useNavigationBarMask = false + LazyColumn( + modifier = Modifier + .fillMaxSize() + .padding(bottom = 16.dp), + horizontalAlignment = Alignment.CenterHorizontally, + verticalArrangement = Arrangement.Top, ) { - LazyColumn( - modifier = Modifier - .fillMaxSize() - .padding(bottom = 16.dp), - horizontalAlignment = Alignment.CenterHorizontally, - verticalArrangement = Arrangement.Top, - ) { - item { - CarGroup() - userProfile?.let { - UserInformation( - isSelf = false, - accountProfileEntity = it, - onFollowClick = { - scope.launch { - if (it.isFollowing) { - userService.unFollowUser(id) - userProfile = userProfile?.copy(isFollowing = false) - } else { - userService.followUser(id) - userProfile = userProfile?.copy(isFollowing = true) - } - } - }, + item { + Box( + modifier = Modifier + .fillMaxWidth() + + ) { + val banner = model.profile?.banner + if (banner != null) { + CustomAsyncImage( + LocalContext.current, + banner, + modifier = Modifier + .fillMaxWidth() + .height(400.dp), + contentDescription = "", + contentScale = ContentScale.Crop + ) + } else { + Image( + painter = painterResource(id = R.drawable.rider_pro_moment_demo_2), + modifier = Modifier + .fillMaxWidth() + .height(400.dp), + contentDescription = "", + contentScale = ContentScale.Crop ) } + } + model.profile?.let { + UserInformation( + isSelf = false, + accountProfileEntity = it, + onFollowClick = { + scope.launch { + if (it.isFollowing) { + model.unFollowUser(id) + } else { + model.followUser(id) + } + } + }, + ) + } // RidingStyle() - } - if (items != null) { - items(items.itemCount) { idx -> - val momentItem = items[idx] ?: return@items - MomentPostUnit(momentItem) - } - } } + + items(items.itemCount) { idx -> + val momentItem = items[idx] ?: return@items + MomentPostUnit(momentItem) + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/aiosman/riderpro/ui/profile/AccountProfileViewModel.kt b/app/src/main/java/com/aiosman/riderpro/ui/profile/AccountProfileViewModel.kt index d151d5d..9961e0b 100644 --- a/app/src/main/java/com/aiosman/riderpro/ui/profile/AccountProfileViewModel.kt +++ b/app/src/main/java/com/aiosman/riderpro/ui/profile/AccountProfileViewModel.kt @@ -1,4 +1,72 @@ package com.aiosman.riderpro.ui.profile -object AccountProfileViewModel { +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.riderpro.data.AccountService +import com.aiosman.riderpro.data.AccountServiceImpl +import com.aiosman.riderpro.data.MomentService +import com.aiosman.riderpro.data.UserServiceImpl +import com.aiosman.riderpro.entity.AccountProfileEntity +import com.aiosman.riderpro.entity.MomentEntity +import com.aiosman.riderpro.entity.MomentPagingSource +import com.aiosman.riderpro.entity.MomentRemoteDataSource +import com.aiosman.riderpro.entity.MomentServiceImpl +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.flow.collectLatest +import kotlinx.coroutines.launch + +object AccountProfileViewModel : ViewModel() { + var profileId by mutableStateOf(0) + val accountService: AccountService = AccountServiceImpl() + val momentService: MomentService = MomentServiceImpl() + val userService = UserServiceImpl() + var profile by mutableStateOf(null) + private var _momentsFlow = MutableStateFlow>(PagingData.empty()) + var momentsFlow = _momentsFlow.asStateFlow() + fun loadProfile(id: String) { + viewModelScope.launch { + if (profileId == profile?.id){ + return@launch + } + profile = userService.getUserProfile(id) + 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 + } \ No newline at end of file