236 lines
7.1 KiB
Kotlin
236 lines
7.1 KiB
Kotlin
|
|
package com.aiosman.riderpro
|
||
|
|
import androidx.annotation.DrawableRes
|
||
|
|
import androidx.compose.foundation.Image
|
||
|
|
import androidx.compose.foundation.border
|
||
|
|
import androidx.compose.foundation.layout.Arrangement
|
||
|
|
import androidx.compose.foundation.layout.Box
|
||
|
|
import androidx.compose.foundation.layout.Column
|
||
|
|
import androidx.compose.foundation.layout.Row
|
||
|
|
import androidx.compose.foundation.layout.defaultMinSize
|
||
|
|
import androidx.compose.foundation.layout.fillMaxHeight
|
||
|
|
import androidx.compose.foundation.layout.fillMaxSize
|
||
|
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||
|
|
import androidx.compose.foundation.layout.height
|
||
|
|
import androidx.compose.foundation.layout.offset
|
||
|
|
import androidx.compose.foundation.layout.padding
|
||
|
|
import androidx.compose.foundation.layout.size
|
||
|
|
import androidx.compose.foundation.layout.wrapContentWidth
|
||
|
|
import androidx.compose.foundation.lazy.LazyColumn
|
||
|
|
import androidx.compose.material3.CircularProgressIndicator
|
||
|
|
import androidx.compose.material3.LocalTextStyle
|
||
|
|
import androidx.compose.material3.Text
|
||
|
|
import androidx.compose.runtime.Composable
|
||
|
|
import androidx.compose.runtime.remember
|
||
|
|
import androidx.compose.ui.Alignment
|
||
|
|
import androidx.compose.ui.Modifier
|
||
|
|
import androidx.compose.ui.graphics.Color
|
||
|
|
import androidx.compose.ui.res.painterResource
|
||
|
|
import androidx.compose.ui.res.stringResource
|
||
|
|
import androidx.compose.ui.text.PlatformTextStyle
|
||
|
|
import androidx.compose.ui.text.TextStyle
|
||
|
|
import androidx.compose.ui.text.font.FontWeight
|
||
|
|
import androidx.compose.ui.text.style.LineHeightStyle
|
||
|
|
import androidx.compose.ui.text.style.TextAlign
|
||
|
|
import androidx.compose.ui.unit.dp
|
||
|
|
import androidx.compose.ui.unit.em
|
||
|
|
import androidx.compose.ui.unit.sp
|
||
|
|
import androidx.paging.LoadState
|
||
|
|
import androidx.paging.Pager
|
||
|
|
import androidx.paging.PagingConfig
|
||
|
|
import androidx.paging.compose.collectAsLazyPagingItems
|
||
|
|
|
||
|
|
private val DATA = (0..60).toList().map { momentTestItem}
|
||
|
|
|
||
|
|
@Composable
|
||
|
|
fun PagingBackendSample() {
|
||
|
|
val myBackend = remember { TestBackend(DATA) }
|
||
|
|
val pager = remember {
|
||
|
|
Pager(
|
||
|
|
PagingConfig(
|
||
|
|
pageSize = myBackend.DataBatchSize,
|
||
|
|
enablePlaceholders = true,
|
||
|
|
maxSize = 200
|
||
|
|
)
|
||
|
|
) {
|
||
|
|
myBackend.getAllData()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
val lazyPagingItems = pager.flow.collectAsLazyPagingItems()
|
||
|
|
|
||
|
|
LazyColumn {
|
||
|
|
if (lazyPagingItems.loadState.refresh == LoadState.Loading) {
|
||
|
|
item {
|
||
|
|
MomentListLoading()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
items(count = lazyPagingItems.itemCount) { index ->
|
||
|
|
val item = lazyPagingItems[index]
|
||
|
|
if (item != null) {
|
||
|
|
MomentCard(item)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (lazyPagingItems.loadState.append == LoadState.Loading) {
|
||
|
|
item {
|
||
|
|
MomentListLoading()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@Composable
|
||
|
|
fun MomentCard(momentItem: MomentItem){
|
||
|
|
Column(
|
||
|
|
modifier = Modifier.fillMaxWidth()
|
||
|
|
){
|
||
|
|
MomentTopRowGroup(momentItem = momentItem)
|
||
|
|
MomentContentGroup(momentItem = momentItem)
|
||
|
|
val momentOperateBtnBoxModifier = Modifier.fillMaxHeight().weight(1f)
|
||
|
|
MomentBottomOperateRowGroup(momentOperateBtnBoxModifier)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@Composable
|
||
|
|
fun MomentName(name: String){
|
||
|
|
Text(
|
||
|
|
modifier = Modifier,
|
||
|
|
textAlign = TextAlign.Start,
|
||
|
|
text = name,
|
||
|
|
color = Color(0f,0f,0f),
|
||
|
|
fontSize = 16.sp, style = TextStyle(fontWeight = FontWeight.Bold))
|
||
|
|
}
|
||
|
|
|
||
|
|
@Composable
|
||
|
|
fun MomentFollowBtn(){
|
||
|
|
Box(modifier = Modifier
|
||
|
|
.size(width = 53.dp, height = 18.dp)
|
||
|
|
.padding(start = 8.dp),
|
||
|
|
contentAlignment = Alignment.Center){
|
||
|
|
Image(
|
||
|
|
modifier = Modifier
|
||
|
|
.fillMaxSize(),
|
||
|
|
painter = painterResource(id = R.drawable.follow_bg),
|
||
|
|
contentDescription = ""
|
||
|
|
)
|
||
|
|
Text(text = "Follow",
|
||
|
|
color = Color.White,
|
||
|
|
fontSize = 12.sp)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@Composable
|
||
|
|
fun MomentPostLocation(location: String){
|
||
|
|
Text(text = location,
|
||
|
|
color = Color(0f,0f,0f,0.6f),
|
||
|
|
fontSize = 12.sp)
|
||
|
|
}
|
||
|
|
|
||
|
|
@Composable
|
||
|
|
fun MomentPostTime(time: String){
|
||
|
|
Text(modifier = Modifier.padding(start = 8.dp),
|
||
|
|
text = time, color = Color(0f,0f,0f,0.6f),
|
||
|
|
fontSize = 12.sp
|
||
|
|
)
|
||
|
|
}
|
||
|
|
|
||
|
|
@Composable
|
||
|
|
fun MomentTopRowGroup(momentItem: MomentItem){
|
||
|
|
Row (modifier = Modifier
|
||
|
|
.height(40.dp)
|
||
|
|
.padding(top = 0.dp, bottom = 0.dp, start = 24.dp, end = 24.dp)
|
||
|
|
){
|
||
|
|
Image(
|
||
|
|
painter = painterResource(id = momentItem.avatar),
|
||
|
|
contentDescription = ""
|
||
|
|
)
|
||
|
|
Column(
|
||
|
|
modifier = Modifier
|
||
|
|
.defaultMinSize()
|
||
|
|
.padding(start = 12.dp, end = 12.dp)
|
||
|
|
) {
|
||
|
|
Row (modifier = Modifier
|
||
|
|
.fillMaxWidth()
|
||
|
|
.height(22.dp),
|
||
|
|
verticalAlignment = Alignment.CenterVertically
|
||
|
|
){
|
||
|
|
MomentName(momentItem.nickname)
|
||
|
|
MomentFollowBtn()
|
||
|
|
}
|
||
|
|
Row (modifier = Modifier
|
||
|
|
.fillMaxWidth()
|
||
|
|
.height(21.dp),
|
||
|
|
verticalAlignment = Alignment.CenterVertically
|
||
|
|
){
|
||
|
|
MomentPostLocation(momentItem.location)
|
||
|
|
MomentPostTime(momentItem.time)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@Composable
|
||
|
|
fun MomentContentGroup(momentItem: MomentItem){
|
||
|
|
Text(text = momentItem.momentTextContent,
|
||
|
|
modifier = Modifier
|
||
|
|
.fillMaxWidth()
|
||
|
|
.padding(top = 22.dp, bottom = 16.dp, start = 24.dp, end = 24.dp),
|
||
|
|
fontSize = 16.sp
|
||
|
|
)
|
||
|
|
Image(
|
||
|
|
modifier = Modifier
|
||
|
|
.fillMaxWidth(),
|
||
|
|
painter = painterResource(id = momentItem.momentPicture),
|
||
|
|
contentDescription = ""
|
||
|
|
)
|
||
|
|
}
|
||
|
|
|
||
|
|
@Composable
|
||
|
|
fun MomentOperateBtn(@DrawableRes icon: Int, count: String){
|
||
|
|
Row (verticalAlignment = Alignment.CenterVertically){
|
||
|
|
Image(
|
||
|
|
modifier = Modifier
|
||
|
|
.size(width = 24.dp, height = 24.dp),
|
||
|
|
painter = painterResource(id = icon),
|
||
|
|
contentDescription = "")
|
||
|
|
Text(text = count,
|
||
|
|
modifier = Modifier.padding(start = 7.dp),
|
||
|
|
fontSize = 12.sp,)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
@Composable
|
||
|
|
fun MomentBottomOperateRowGroup(modifier: Modifier){
|
||
|
|
Row (modifier = Modifier
|
||
|
|
.fillMaxWidth()
|
||
|
|
.height(56.dp)){
|
||
|
|
Box(modifier = modifier,
|
||
|
|
contentAlignment = Alignment.Center
|
||
|
|
){
|
||
|
|
MomentOperateBtn(icon = R.drawable.rider_pro_like, count = "21")
|
||
|
|
}
|
||
|
|
Box(modifier = modifier,
|
||
|
|
contentAlignment = Alignment.Center
|
||
|
|
){
|
||
|
|
MomentOperateBtn(icon = R.drawable.rider_pro_moment_comment, count = "43")
|
||
|
|
}
|
||
|
|
Box(modifier = modifier,
|
||
|
|
contentAlignment = Alignment.Center
|
||
|
|
){
|
||
|
|
MomentOperateBtn(icon = R.drawable.rider_pro_share, count = "33")
|
||
|
|
}
|
||
|
|
Box(modifier = modifier,
|
||
|
|
contentAlignment = Alignment.Center
|
||
|
|
){
|
||
|
|
MomentOperateBtn(icon = R.drawable.rider_pro_favoriate, count = "211")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@Composable
|
||
|
|
fun MomentListLoading(){
|
||
|
|
CircularProgressIndicator(
|
||
|
|
modifier =
|
||
|
|
Modifier
|
||
|
|
.fillMaxWidth()
|
||
|
|
.wrapContentWidth(Alignment.CenterHorizontally),
|
||
|
|
color = Color.Red
|
||
|
|
)
|
||
|
|
}
|