Skip to content

rmyhal/contentment

Repository files navigation

contentment

Maven Central Version GitHub Actions Workflow Status

Are you fed up with progress indicators that run for 159 milliseconds and then disappear and are a real pain in the neck for both you and your users? Contentment effectively manages frustrating progress indicators, enhancing the user experience.

Contentment {
  when (uiState) {
    is Loading -> indicator { CircularProgressIndicator() }
    is Loaded -> content { ScreenContent(uiState) }
  }
}
don’t do do2

Usage

implementation "me.rmyhal.contentment:contentment:<version>"

The library handles content loading with customizable behavior for showing loading indicators. Allows specifying minimum display time and delay before showing the loading indicator.

  • If the content finishes loading before the delayMillis threshold, the indicator {} will not be shown.
  • If the content loading exceeds the delayMillis threshold, the indicator {} will be displayed, and content {} will appear only after the minShowTimeMillis duration has passed.

If you 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) }
    }
  }  
}

Apart from that, the library provides a Jetpack Compose adaptation of the native ContentLoadingProgressBar.

var loading = remember { mutableStateOf(true) }
ContentLoadingIndicator(
  loading = loading,
) {
  CircularProgressIndicator()
}

License

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.