Skip to content

Instantly share code, notes, and snippets.

@eboudrant
Last active May 12, 2020 16:05
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 eboudrant/4dcd821d410dc1656b7696a0e12424b4 to your computer and use it in GitHub Desktop.
Save eboudrant/4dcd821d410dc1656b7696a0e12424b4 to your computer and use it in GitHub Desktop.
// New attribute
@Nullable
private List<EpoxyModel<?>> modelsPending_List = null;
// New method, not declared in `CarouselNoSnapModelBuilder` interface.
public CarouselNoSnapModelBuilder add(@NonNull EpoxyModel<?> model) {
if (model == null) {
throw new IllegalArgumentException("model cannot be null");
}
if (modelsPending_List != null) {
modelsPending_List = new ArrayList<>();
}
modelsPending_List.add(model);
return this;
}
// New method, called by `addTo`
private void addPendingModels() {
if (modelsPending_List != null && modelsPending_List.size() > 0) {
if (models_List != null && models_List.size() > 0) {
throw new IllegalArgumentException("Need an error message");
}
models(modelsPending_List);
}
}
@Override
public void addTo(EpoxyController controller) {
super.addTo(controller);
addPendingModels(); // flush models added by DSL
addWithDebugValidation(controller);
if (!assignedAttributes_epoxyGeneratedModel.get(6)) {
throw new IllegalStateException("A value is required for setModels");
}
}
// One extenstion generated per epoxy model, ex for `CarouselItemCustomView`
// Extension on concrete implementation `CarouselNoSnapModel_` to prevent call to `CarouselItemCustomViewModelBuilder.add(model)` from DSL
// The trick is when to generate this method, how to detect carousel / nested list. What if we introduce a `NestedList` interface?
inline fun CarouselNoSnapModel_.carouselItemCustomView(
modelInitializer:
CarouselItemCustomViewModelBuilder.() -> Unit
) {
add(
CarouselItemCustomViewModel_().apply {
modelInitializer()
}
)
}
carouselNoSnap {
val lastPage = 10
for (j in 0 until lastPage) {
carouselItemCustomView {
id("carousel $i-$j")
title("Page $j / $lastPage")
}
}
}
// Epoxy model to implements to activate the nested dsl
interface NestedList {
fun setModels(models: List<EpoxyModel<*>?>)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment