更新个人主页

- 使用ViewModel重构个人主页
- 优化个人主页UI细节
- 新增关注功能图片资源
This commit is contained in:
2024-09-06 18:08:13 +08:00
parent 1fc487c73d
commit b5abc72682
13 changed files with 47 additions and 26 deletions

View File

@@ -0,0 +1,9 @@
package com.aiosman.riderpro.exp
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
inline fun <VM : ViewModel> viewModelFactory(crossinline f: () -> VM) =
object : ViewModelProvider.Factory {
override fun <T : ViewModel> create(aClass: Class<T>):T = f() as T
}

View File

@@ -8,7 +8,6 @@ import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.ExperimentalMaterialApi import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.pullrefresh.PullRefreshIndicator import androidx.compose.material.pullrefresh.PullRefreshIndicator
@@ -24,8 +23,10 @@ import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.paging.compose.collectAsLazyPagingItems import androidx.paging.compose.collectAsLazyPagingItems
import com.aiosman.riderpro.R import com.aiosman.riderpro.R
import com.aiosman.riderpro.exp.viewModelFactory
import com.aiosman.riderpro.ui.composables.CustomAsyncImage import com.aiosman.riderpro.ui.composables.CustomAsyncImage
import com.aiosman.riderpro.ui.index.tabs.profile.MomentPostUnit import com.aiosman.riderpro.ui.index.tabs.profile.MomentPostUnit
import com.aiosman.riderpro.ui.index.tabs.profile.UserInformation import com.aiosman.riderpro.ui.index.tabs.profile.UserInformation
@@ -34,7 +35,9 @@ import kotlinx.coroutines.launch
@OptIn(ExperimentalMaterialApi::class) @OptIn(ExperimentalMaterialApi::class)
@Composable @Composable
fun AccountProfile(id: String) { fun AccountProfile(id: String) {
val model = AccountProfileViewModel val model: AccountProfileViewModel = viewModel(factory = viewModelFactory {
AccountProfileViewModel()
}, key = "viewModel_${id}")
val scope = rememberCoroutineScope() val scope = rememberCoroutineScope()
val items = model.momentsFlow.collectAsLazyPagingItems() val items = model.momentsFlow.collectAsLazyPagingItems()
val state = rememberPullRefreshState(model.refreshing, onRefresh = { val state = rememberPullRefreshState(model.refreshing, onRefresh = {
@@ -45,12 +48,13 @@ fun AccountProfile(id: String) {
} }
Box( Box(
modifier = Modifier modifier = Modifier
.fillMaxSize().background(Color.White).pullRefresh(state) .fillMaxSize()
.pullRefresh(state)
.background(Color(0xFFF5F5F5))
) { ) {
LazyColumn( LazyColumn(
modifier = Modifier modifier = Modifier
.fillMaxSize() .fillMaxSize(),
.padding(bottom = 16.dp),
horizontalAlignment = Alignment.CenterHorizontally, horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Top, verticalArrangement = Arrangement.Top,
) { ) {
@@ -60,6 +64,9 @@ fun AccountProfile(id: String) {
.fillMaxWidth() .fillMaxWidth()
) { ) {
if (model.profile == null) {
Spacer(modifier = Modifier.height(400.dp))
} else {
val banner = model.profile?.banner val banner = model.profile?.banner
if (banner != null) { if (banner != null) {
CustomAsyncImage( CustomAsyncImage(
@@ -82,6 +89,8 @@ fun AccountProfile(id: String) {
) )
} }
} }
}
Spacer(modifier = Modifier.height(32.dp)) Spacer(modifier = Modifier.height(32.dp))
model.profile?.let { model.profile?.let {
UserInformation( UserInformation(
@@ -105,6 +114,9 @@ fun AccountProfile(id: String) {
val momentItem = items[idx] ?: return@items val momentItem = items[idx] ?: return@items
MomentPostUnit(momentItem) MomentPostUnit(momentItem)
} }
item {
Spacer(modifier = Modifier.height(32.dp))
}
} }
PullRefreshIndicator(model.refreshing, state, Modifier.align(Alignment.TopCenter)) PullRefreshIndicator(model.refreshing, state, Modifier.align(Alignment.TopCenter))

View File

@@ -23,7 +23,7 @@ import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
object AccountProfileViewModel : ViewModel() { class AccountProfileViewModel : ViewModel() {
var profileId by mutableStateOf(0) var profileId by mutableStateOf(0)
val accountService: AccountService = AccountServiceImpl() val accountService: AccountService = AccountServiceImpl()
val momentService: MomentService = MomentServiceImpl() val momentService: MomentService = MomentServiceImpl()

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB