Created
December 11, 2020 01:34
-
-
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 file contains hidden or 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
/** | |
* 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