Skip to content

Instantly share code, notes, and snippets.

@Nimrodda
Created April 14, 2020 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nimrodda/acbdda32b534d0a9ed1e8aa02889687f to your computer and use it in GitHub Desktop.
Save Nimrodda/acbdda32b534d0a9ed1e8aa02889687f to your computer and use it in GitHub Desktop.
class WorkoutSummaryController : TypedEpoxyController<WorkoutSummaryViewState>(
EpoxyAsyncUtil.getAsyncBackgroundHandler(),
EpoxyAsyncUtil.getAsyncBackgroundHandler()) {
override fun buildModels(viewState: WorkoutSummaryViewState) {
addLikes(viewState)
addLoadingIndicatorIfNeed(viewState)
}
private fun addLikes(viewState: WorkoutSummaryViewState) {
val likesItem = viewState.likesItem.data
// We verify that the state is loaded, we can use the state to show a loading indicator
// only for this item if we want
if (viewState.likesItem is ViewState.Loaded && likesItem != null) {
likes {
id("likes")
likesItem(likesItem)
onLikeClicked { model, _, _, _ ->
model.likesItem().onClickHandler(viewState.workoutId)
}
}
} else if (viewState.likesItem is ViewState.Error) {
// Instead of adding the likes item, we can use the state of the item to add
// a generic error item instead
addErrorItem(/* pass here some data to decide the message */)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment