mirror of
https://github.com/msfjarvis/compose-lobsters
synced 2025-08-13 21:56:59 +05:30
feat(android): replace Accompanist swiperefresh with Compose Material
This commit is contained in:
parent
2c1fdd51f8
commit
f7b2b0f9bc
3 changed files with 20 additions and 28 deletions
|
@ -51,9 +51,9 @@ dependencies {
|
|||
implementation(projects.database)
|
||||
implementation(projects.metadataExtractor)
|
||||
implementation(projects.model)
|
||||
implementation(libs.accompanist.swiperefresh)
|
||||
implementation(libs.accompanist.sysuicontroller)
|
||||
implementation(libs.androidx.activity.compose)
|
||||
implementation(libs.androidx.compose.material)
|
||||
implementation(libs.androidx.compose.material3)
|
||||
implementation(libs.androidx.core.splashscreen)
|
||||
implementation(libs.androidx.lifecycle.compose)
|
||||
|
|
|
@ -10,6 +10,10 @@ import androidx.compose.foundation.layout.Box
|
|||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.LazyListState
|
||||
import androidx.compose.material.ExperimentalMaterialApi
|
||||
import androidx.compose.material.pullrefresh.PullRefreshIndicator
|
||||
import androidx.compose.material.pullrefresh.pullRefresh
|
||||
import androidx.compose.material.pullrefresh.rememberPullRefreshState
|
||||
import androidx.compose.material3.Divider
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.contentColorFor
|
||||
|
@ -19,15 +23,13 @@ import androidx.compose.ui.Modifier
|
|||
import androidx.paging.LoadState
|
||||
import androidx.paging.compose.LazyPagingItems
|
||||
import androidx.paging.compose.items
|
||||
import com.google.accompanist.swiperefresh.SwipeRefresh
|
||||
import com.google.accompanist.swiperefresh.SwipeRefreshIndicator
|
||||
import com.google.accompanist.swiperefresh.rememberSwipeRefreshState
|
||||
import dev.msfjarvis.claw.common.posts.PostActions
|
||||
import dev.msfjarvis.claw.common.posts.toDbModel
|
||||
import dev.msfjarvis.claw.common.ui.NetworkError
|
||||
import dev.msfjarvis.claw.database.local.SavedPost
|
||||
import dev.msfjarvis.claw.model.LobstersPost
|
||||
|
||||
@OptIn(ExperimentalMaterialApi::class)
|
||||
@Composable
|
||||
fun NetworkPosts(
|
||||
items: LazyPagingItems<LobstersPost>,
|
||||
|
@ -39,33 +41,17 @@ fun NetworkPosts(
|
|||
) {
|
||||
val loadState = items.loadState.refresh
|
||||
val isRefreshing = loadState == LoadState.Loading
|
||||
SwipeRefresh(
|
||||
state = rememberSwipeRefreshState(isRefreshing),
|
||||
onRefresh = reloadPosts,
|
||||
indicator = { state, trigger ->
|
||||
val backgroundColor = MaterialTheme.colorScheme.surface
|
||||
SwipeRefreshIndicator(
|
||||
state = state,
|
||||
refreshTriggerDistance = trigger,
|
||||
backgroundColor = backgroundColor,
|
||||
contentColor = contentColorFor(backgroundColor)
|
||||
val pullRefreshState = rememberPullRefreshState(isRefreshing, reloadPosts)
|
||||
Box(modifier = modifier.fillMaxSize().pullRefresh(pullRefreshState)) {
|
||||
if (items.itemCount == 0 && loadState is LoadState.Error) {
|
||||
NetworkError(
|
||||
label = "Failed to load posts",
|
||||
error = loadState.error,
|
||||
modifier = Modifier.align(Alignment.Center),
|
||||
)
|
||||
}
|
||||
) {
|
||||
if (items.itemCount == 0) {
|
||||
Box(modifier = Modifier.fillMaxSize()) {
|
||||
if (loadState is LoadState.Error) {
|
||||
NetworkError(
|
||||
label = "Failed to load posts",
|
||||
error = loadState.error,
|
||||
modifier = Modifier.align(Alignment.Center),
|
||||
)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
LazyColumn(
|
||||
state = listState,
|
||||
modifier = modifier,
|
||||
) {
|
||||
items(items) { item ->
|
||||
if (item != null) {
|
||||
|
@ -81,5 +67,12 @@ fun NetworkPosts(
|
|||
}
|
||||
}
|
||||
}
|
||||
PullRefreshIndicator(
|
||||
refreshing = isRefreshing,
|
||||
state = pullRefreshState,
|
||||
modifier = Modifier.align(Alignment.TopCenter),
|
||||
backgroundColor = MaterialTheme.colorScheme.surface,
|
||||
contentColor = contentColorFor(MaterialTheme.colorScheme.surface),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,7 +20,6 @@ workmanager = "2.8.0-beta02"
|
|||
|
||||
[libraries]
|
||||
accompanist-flowlayout = { module = "com.google.accompanist:accompanist-flowlayout", version.ref = "accompanist" }
|
||||
accompanist-swiperefresh = { module = "com.google.accompanist:accompanist-swiperefresh", version.ref = "accompanist" }
|
||||
accompanist-sysuicontroller = { module = "com.google.accompanist:accompanist-systemuicontroller", version.ref = "accompanist" }
|
||||
androidx-activity-compose = "androidx.activity:activity-compose:1.7.0-alpha02"
|
||||
androidx-benchmark-macro-junit4 = "androidx.benchmark:benchmark-macro-junit4:1.2.0-alpha07"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue