Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull to refresh doesn't work when there are no tasks #934

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ import androidx.lifecycle.compose.ExperimentalLifecycleComposeApi
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.example.android.architecture.blueprints.todoapp.R
import com.example.android.architecture.blueprints.todoapp.util.AddEditTaskTopAppBar
import com.google.accompanist.swiperefresh.SwipeRefresh
import com.google.accompanist.swiperefresh.rememberSwipeRefreshState
import com.example.android.architecture.blueprints.todoapp.util.LoadingContent

@OptIn(ExperimentalLifecycleComposeApi::class)
@Composable
Expand Down Expand Up @@ -112,11 +111,12 @@ private fun AddEditTaskContent(
modifier: Modifier = Modifier
) {
if (loading) {
SwipeRefresh(
// Show the loading spinner—`loading` is `true` in this code path
state = rememberSwipeRefreshState(true),
LoadingContent(
loading = true,
onRefresh = { /* DO NOTHING */ },
content = { },
empty = false,
emptyContent = { /* DO NOTHING */ },
content = {}
)
} else {
Column(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
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.material.Checkbox
import androidx.compose.material.FloatingActionButton
import androidx.compose.material.Icon
Expand Down Expand Up @@ -215,7 +217,7 @@ private fun TasksEmptyContent(
modifier: Modifier = Modifier
) {
Column(
modifier = modifier.fillMaxSize(),
modifier = modifier.fillMaxSize().verticalScroll(rememberScrollState()),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@

package com.example.android.architecture.blueprints.todoapp.util

import androidx.compose.foundation.layout.Box
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.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import com.google.accompanist.swiperefresh.SwipeRefresh
import com.google.accompanist.swiperefresh.rememberSwipeRefreshState

val primaryDarkColor: Color = Color(0xFF263238)

Expand All @@ -34,6 +38,7 @@ val primaryDarkColor: Color = Color(0xFF263238)
* @param modifier the modifier to apply to this layout.
* @param content (slot) the main content to show
*/
@OptIn(ExperimentalMaterialApi::class)
@Composable
fun LoadingContent(
loading: Boolean,
Expand All @@ -43,14 +48,13 @@ fun LoadingContent(
modifier: Modifier = Modifier,
content: @Composable () -> Unit
) {
if (empty) {
emptyContent()
} else {
SwipeRefresh(
state = rememberSwipeRefreshState(loading),
onRefresh = onRefresh,
modifier = modifier,
content = content,
)
val pullRefreshState = rememberPullRefreshState(loading, onRefresh)
Box(modifier = modifier.pullRefresh(pullRefreshState)) {
if (empty) {
emptyContent()
} else {
content()
}
PullRefreshIndicator(loading, pullRefreshState, Modifier.align(Alignment.TopCenter))
}
}