Skip to content

Commit

Permalink
add pull to refresh and indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
iakanoe committed Nov 2, 2023
1 parent d2135fd commit da4d0f3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ dependencies {
implementation("androidx.compose.ui:ui-graphics")
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.compose.material3:material3")
implementation("androidx.compose.material:material")
debugImplementation("androidx.compose.ui:ui-tooling")
debugImplementation("androidx.compose.ui:ui-test-manifest")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.List
import androidx.compose.material.icons.filled.Refresh
import androidx.compose.material.pullrefresh.PullRefreshIndicator
import androidx.compose.material.pullrefresh.PullRefreshState
import androidx.compose.material.pullrefresh.pullRefresh
import androidx.compose.material.pullrefresh.rememberPullRefreshState
import androidx.compose.material3.Button
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedCard
import androidx.compose.material3.Surface
Expand Down Expand Up @@ -55,6 +59,7 @@ import java.util.Calendar
import kotlin.math.floor
import kotlin.math.sign

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun ExchangeRatesScreen(
onNavigateToSettings: () -> Unit,
Expand All @@ -63,6 +68,13 @@ fun ExchangeRatesScreen(
) {
val viewState by viewModel.viewState.collectAsState()

val refreshing by remember { derivedStateOf { viewState is ExchangeRatesViewState.Loading } }

val pullRefreshState = rememberPullRefreshState(
refreshing = refreshing,
onRefresh = { viewModel.update() }
)

val now by currentTime.collectAsState(initial = Calendar.getInstance().timeInMillis)

val lastUpdated by remember {
Expand Down Expand Up @@ -106,17 +118,24 @@ fun ExchangeRatesScreen(
contentAlignment = Alignment.Center
) {
when (val state = viewState) {
is ExchangeRatesViewState.Loading -> CircularProgressIndicator()

is ExchangeRatesViewState.Error -> ErrorContent(
onRetryClick = { viewModel.update() }
)

is ExchangeRatesViewState.Loaded -> ExchangeRateList(
pullRefreshState = pullRefreshState,
exchangeRates = state.exchangeRates,
hiddenExchangeRates = state.hiddenExchangeRates
hiddenExchangeRates = state.hiddenExchangeRates,
)

else -> {}
}

PullRefreshIndicator(
refreshing = refreshing,
state = pullRefreshState,
modifier = Modifier.align(Alignment.TopCenter)
)
}
}
}
Expand All @@ -137,15 +156,19 @@ fun ErrorContent(onRetryClick: () -> Unit) {
}
}

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun ExchangeRateList(
pullRefreshState: PullRefreshState,
exchangeRates: List<ExchangeRate>,
hiddenExchangeRates: List<ExchangeRate>
) {
var isExpanded by rememberSaveable { mutableStateOf(false) }

LazyColumn(
modifier = Modifier.fillMaxSize(),
modifier = Modifier
.fillMaxSize()
.pullRefresh(pullRefreshState),
contentPadding = PaddingValues(16.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
Expand Down

0 comments on commit da4d0f3

Please sign in to comment.