Contentment prevents flickering and short-lived progress indicators to ensure a smoother user experience.
Contentment {
when (uiState) {
is Loading -> indicator { CircularProgressIndicator() }
is Loaded -> content { ScreenContent(uiState) }
}
}| don’t | do | do2 |
|---|---|---|
![]() |
![]() |
![]() |
implementation "me.rmyhal.contentment:contentment:2.0.1"The Contentment composable manages the transition between loading and loaded states, allows specifying minimum display time and delay before showing the loading indicator:
- If the content finishes loading before the
delayMillisthreshold, theindicator {}will not be shown. - If the content loading exceeds the
delayMillisthreshold, theindicator {}will be displayed, andcontent {}will appear only after theminShowTimeMillisduration has passed.
If your screen is built with a sealed UI state:
@Composable
fun Screen(viewModel: ViewModel) {
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
ScreenContent(uiState)
}
@Composable
fun ScreenContent(uiState: UiState) {
Contentment(
minShowTimeMillis = 700L,
delayMillis = 500L
) {
when (uiState) {
is Loading -> indicator { CircularProgressIndicator() }
is Loaded -> content { LoadedContent(uiState) }
// multiple content's are possible too
is Failed -> content { FailedContent(uiState) }
}
}
}A direct adaptation of ContentLoadingProgressBar. for Compose. Use this when you only need to wrap the indicator itself.
var loading = remember { mutableStateOf(true) }
ContentLoadingIndicator(
loading = loading,
) {
CircularProgressIndicator()
}Copyright 2024 Ruslan Myhal
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.


