Skip to content

Instantly share code, notes, and snippets.

@AdamMc331
Created December 11, 2020 01:34
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 AdamMc331/8d41cd024c1d4087fe12efce8ef09084 to your computer and use it in GitHub Desktop.
Save AdamMc331/8d41cd024c1d4087fe12efce8ef09084 to your computer and use it in GitHub Desktop.
Demonstrating an EpoxyGroupModel implementation and what I would like to do if possible.
/**
* This class is the current implementation.
*
* Notice that in [addPendingPrescriptionCard] and [addActivePrescriptionCard],
* we have relatively identical code:
*
* 1. Create the group.
* 2. Create all the models and add them to the group.
* 3. Add the group to the main controller.
*
* Whe I'm looking for is a way to avoid some of that duplicate code, and
* just be able to apply the models or something.
*/
class PrescriptionCardListController : TypedEpoxyController<AccountPrescriptionsDisplayModel>() {
override fun buildModels(data: AccountPrescriptionsDisplayModel) {
data.pendingPrescriptions.let(this::addPendingPrescriptionCard)
data.activePrescriptions.forEach(this::addActivePrescriptionCard)
}
private fun addPendingPrescriptionCard(pendingPrescriptions: List<PendingPrescriptionDisplayModel>) {
val groupModel = GroupModel_()
.id("PENDING_PRESCRIPTIONS_CARD")
.layout(R.layout.epoxy_list_card)
pendingPrescriptions.forEach { prescription ->
val prescriptionModel = PendingPrescriptionEpoxyModel_()
.id(prescription.prescriptionId)
.prescription(prescription)
groupModel.add(prescriptionModel)
}
groupModel.addTo(this)
}
private fun addActivePrescriptionCard(displayModel: ActivePatientPrescriptionsDisplayModel) {
val patientId = displayModel.patient.patientId
val groupModel = GroupModel_()
.id("PRESCRIPTION_CARD_$patientId")
.layout(R.layout.epoxy_list_card)
displayModel.activePrescriptions.forEach { prescription ->
val prescriptionModel = ActivePrescriptionEpoxyModel_()
.id(prescription.prescriptionId)
.prescription(prescription)
groupModel.add(prescriptionModel)
}
groupModel.addTo(this)
}
/**
* Something like this would be reusable - if I could subclass EpoxyGroupModel
* and maybe define the same layout for every group, I could avoid some boilerplate.
*
* Maybe this isn't possible and the above is the "right" way?
*/
private fun whatIWantToDo() {
EpoxyCardGroupModel_()
.id("SomeId")
.layout(R.layout.epoxy_list_card)
.models(addModelsHere)
.addTo(this)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment