Skip to content

Instantly share code, notes, and snippets.

@n8ebel
Last active September 14, 2020 05:12
Show Gist options
  • Save n8ebel/93bac696a18127394c272095f450c2fb to your computer and use it in GitHub Desktop.
Save n8ebel/93bac696a18127394c272095f450c2fb to your computer and use it in GitHub Desktop.
@BindingAdapter("uiState")
fun setUiStateForLoading(progressView: ProgressBar, uiState: UIState) {
progressView.visibility = when (uiState) {
Loading -> View.VISIBLE
else -> View.GONE
}
}
@BindingAdapter("uiState")
fun setUiStateForLoadedContent(view: View, uiState: UIState) {
view.visibility = when (uiState) {
HasData -> View.VISIBLE
else -> View.GONE
}
}
@BindingAdapter("emptyState")
fun setUiStateForEmptyView(view: View, uiState: UIState) {
view.visibility = when (uiState) {
NoData -> View.VISIBLE
else -> View.GONE
}
}
@BindingAdapter("errorState")
fun setUiStateForErrorView(view: View, uiState: UIState) {
view.visibility = when (uiState) {
is Error -> View.VISIBLE
else -> View.GONE
}
}
@BindingAdapter("errorTextState")
fun setUiStateForErrorText(textView: TextView, uiState: UIState) {
val textState = when (uiState) {
is Error -> textView.context.getString(uiState.errorMsgId) to View.VISIBLE
else -> EMPTY_STRING to View.GONE
}
textView.text = textState.first
textView.visibility = textState.second
}
/**
* Base class to represent common UI states
*/
sealed class UIState
/**
* the screen is currently loading it's data. "loading state"
*/
object Loading : UIState()
/**
* data was successfully loaded for the screen. "happy path"
*/
object HasData : UIState()
/**
* the load was successful, but there was no data. "empty state"
*/
object NoData : UIState()
/**
* some type of error occurred. "error state"
*/
class Error(@StringRes val errorMsgId:Int = 0) : UIState()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment