Last active
September 14, 2020 05:12
-
-
Save n8ebel/93bac696a18127394c272095f450c2fb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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