Fix pull to refresh not reachable when no item is displayted

This commit is contained in:
Shinokuni 2023-07-27 14:53:22 +02:00
parent 1a37401139
commit d76200e292
1 changed files with 37 additions and 11 deletions

View File

@ -1,12 +1,19 @@
package com.readrops.app.compose.timelime
import androidx.compose.foundation.gestures.scrollable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import cafe.adriel.voyager.androidx.AndroidScreen
import com.google.accompanist.swiperefresh.SwipeRefresh
@ -29,24 +36,43 @@ class TimelineScreen : AndroidScreen() {
onRefresh = {
viewModel.refreshTimeline()
},
modifier = Modifier.fillMaxSize()
) {
when (state) {
is TimelineState.ErrorState -> {
Text(text = "error")
}
TimelineState.InitialState -> {}
is TimelineState.LoadedState -> {
LazyColumn {
items(
items = (state as TimelineState.LoadedState).items
) {
TimelineItem()
val items = (state as TimelineState.LoadedState).items
if (items.isNotEmpty()) {
LazyColumn {
items(
items = (state as TimelineState.LoadedState).items
) {
TimelineItem()
}
}
} else {
NoItemPlaceholder()
}
}
else -> {
NoItemPlaceholder()
}
}
}
}
}
@Composable
fun NoItemPlaceholder() {
val scrollState = rememberScrollState()
Column(
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier.fillMaxSize()
.verticalScroll(scrollState)
) {
Text(
text = "No item",
style = MaterialTheme.typography.displayMedium
)
}
}