Skip to content

Instantly share code, notes, and snippets.

@AdamMc331
Last active September 16, 2020 16:00
Show Gist options
  • Save AdamMc331/24e06eb84d0d43732813446df5daeecd to your computer and use it in GitHub Desktop.
Save AdamMc331/24e06eb84d0d43732813446df5daeecd to your computer and use it in GitHub Desktop.
Shows a possible example for using a TypedEpoxyController with the currentData property. Not sure if this is best pracitce.
class MyEpoxyController : TypedEpoxyController<ViewState> {
/**
* This buildModels method takes in the current ViewState and builds all of the Epoxy Models
* that need to be displayed for this state.
*/
override fun buildModels(data: ViewState) {
data.usersAndItems.forEach { userItems ->
addUserItemsModels(userItems)
}
}
/**
* Given a user and their items, put the user row first followed by each item they have.
*/
private fun addUserItemsModels(userItems: UserItems) {
addUserModel(userItems.user)
userItems.items.forEach { item ->
addItemModel(item)
}
}
/**
* This is where I become unsure of best practice. I don't want to reference `currentData` here because it's nullable.
* The alternative is to pass the state all the way through from the call to `buildModels` but that feels tedious.
*
* The second concern, is how do I unit test this class? Should I use robolectric, or should I pull out this
* model building logic into a separate class that I can write regular Junit tests for?
*/
private fun addItemModel(item: Item) {
val isInBag = currentData?.cartItems?.items?.contains(item) == true
ItemModel_()
.inBag(isInBag)
.addTo(this)
}
}
/**
* This maps a single User with all of the Items that are associated with that user.
*/
data class UserItems(
val user: User,
val items: List<Item>
)
/**
* Maintains all of the items that are in the user's cart for checkout.
*/
data class CartItems(
val items: Set<Item>
)
/**
* This ViewState encapsules all of our users and their items, as well as the cart information
* so we know which items are inside the cart.
*/
data class ViewState(
val usersAndItems: List<UserItems>,
val cartItems: CartItems
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment