更新个人主页
- 使用ViewModel重构个人主页 - 优化个人主页UI细节 - 新增关注功能图片资源
9
app/src/main/java/com/aiosman/riderpro/exp/ViewModel.kt
Normal 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
|
||||||
|
}
|
||||||
@@ -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,23 +35,26 @@ 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 = {
|
||||||
model.loadProfile(id,pullRefresh = true)
|
model.loadProfile(id, pullRefresh = true)
|
||||||
})
|
})
|
||||||
LaunchedEffect(Unit) {
|
LaunchedEffect(Unit) {
|
||||||
model.loadProfile(id)
|
model.loadProfile(id)
|
||||||
}
|
}
|
||||||
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,27 +64,32 @@ fun AccountProfile(id: String) {
|
|||||||
.fillMaxWidth()
|
.fillMaxWidth()
|
||||||
|
|
||||||
) {
|
) {
|
||||||
val banner = model.profile?.banner
|
if (model.profile == null) {
|
||||||
if (banner != null) {
|
Spacer(modifier = Modifier.height(400.dp))
|
||||||
CustomAsyncImage(
|
|
||||||
LocalContext.current,
|
|
||||||
banner,
|
|
||||||
modifier = Modifier
|
|
||||||
.fillMaxWidth()
|
|
||||||
.height(400.dp),
|
|
||||||
contentDescription = "",
|
|
||||||
contentScale = ContentScale.Crop
|
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
Image(
|
val banner = model.profile?.banner
|
||||||
painter = painterResource(id = R.drawable.rider_pro_moment_demo_2),
|
if (banner != null) {
|
||||||
modifier = Modifier
|
CustomAsyncImage(
|
||||||
.fillMaxWidth()
|
LocalContext.current,
|
||||||
.height(400.dp),
|
banner,
|
||||||
contentDescription = "",
|
modifier = Modifier
|
||||||
contentScale = ContentScale.Crop
|
.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
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Spacer(modifier = Modifier.height(32.dp))
|
Spacer(modifier = Modifier.height(32.dp))
|
||||||
model.profile?.let {
|
model.profile?.let {
|
||||||
@@ -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))
|
||||||
|
|||||||
@@ -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()
|
||||||
|
|||||||
BIN
app/src/main/res/mipmap-hdpi/rider_pro_follow_grey.png
Normal file
|
After Width: | Height: | Size: 2.3 KiB |
BIN
app/src/main/res/mipmap-hdpi/rider_pro_follow_red.png
Normal file
|
After Width: | Height: | Size: 5.6 KiB |
BIN
app/src/main/res/mipmap-mdpi/rider_pro_follow_grey.png
Normal file
|
After Width: | Height: | Size: 1.4 KiB |
BIN
app/src/main/res/mipmap-mdpi/rider_pro_follow_red.png
Normal file
|
After Width: | Height: | Size: 2.9 KiB |
BIN
app/src/main/res/mipmap-xhdpi/rider_pro_follow_grey.png
Normal file
|
After Width: | Height: | Size: 3.0 KiB |
BIN
app/src/main/res/mipmap-xhdpi/rider_pro_follow_red.png
Normal file
|
After Width: | Height: | Size: 7.2 KiB |
BIN
app/src/main/res/mipmap-xxhdpi/rider_pro_follow_grey.png
Normal file
|
After Width: | Height: | Size: 5.3 KiB |
BIN
app/src/main/res/mipmap-xxhdpi/rider_pro_follow_red.png
Normal file
|
After Width: | Height: | Size: 13 KiB |
BIN
app/src/main/res/mipmap-xxxhdpi/rider_pro_follow_grey.png
Normal file
|
After Width: | Height: | Size: 7.2 KiB |
BIN
app/src/main/res/mipmap-xxxhdpi/rider_pro_follow_red.png
Normal file
|
After Width: | Height: | Size: 16 KiB |