更新个人主页

- 使用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.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.ExperimentalMaterialApi
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.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.paging.compose.collectAsLazyPagingItems
import com.aiosman.riderpro.R
import com.aiosman.riderpro.exp.viewModelFactory
import com.aiosman.riderpro.ui.composables.CustomAsyncImage
import com.aiosman.riderpro.ui.index.tabs.profile.MomentPostUnit
import com.aiosman.riderpro.ui.index.tabs.profile.UserInformation
@@ -34,23 +35,26 @@ import kotlinx.coroutines.launch
@OptIn(ExperimentalMaterialApi::class)
@Composable
fun AccountProfile(id: String) {
val model = AccountProfileViewModel
val model: AccountProfileViewModel = viewModel(factory = viewModelFactory {
AccountProfileViewModel()
}, key = "viewModel_${id}")
val scope = rememberCoroutineScope()
val items = model.momentsFlow.collectAsLazyPagingItems()
val state = rememberPullRefreshState(model.refreshing, onRefresh = {
model.loadProfile(id,pullRefresh = true)
model.loadProfile(id, pullRefresh = true)
})
LaunchedEffect(Unit) {
model.loadProfile(id)
}
Box(
modifier = Modifier
.fillMaxSize().background(Color.White).pullRefresh(state)
.fillMaxSize()
.pullRefresh(state)
.background(Color(0xFFF5F5F5))
) {
LazyColumn(
modifier = Modifier
.fillMaxSize()
.padding(bottom = 16.dp),
.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Top,
) {
@@ -60,6 +64,9 @@ fun AccountProfile(id: String) {
.fillMaxWidth()
) {
if (model.profile == null) {
Spacer(modifier = Modifier.height(400.dp))
} else {
val banner = model.profile?.banner
if (banner != null) {
CustomAsyncImage(
@@ -82,6 +89,8 @@ fun AccountProfile(id: String) {
)
}
}
}
Spacer(modifier = Modifier.height(32.dp))
model.profile?.let {
UserInformation(
@@ -105,6 +114,9 @@ fun AccountProfile(id: String) {
val momentItem = items[idx] ?: return@items
MomentPostUnit(momentItem)
}
item {
Spacer(modifier = Modifier.height(32.dp))
}
}
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.launch
object AccountProfileViewModel : ViewModel() {
class AccountProfileViewModel : ViewModel() {
var profileId by mutableStateOf(0)
val accountService: AccountService = AccountServiceImpl()
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