Question about molecule - I have a renderer where ...
# squarelibraries
d
Question about molecule - I have a renderer where the model renders a horizontal pager where each item representing a date. Is it okay to pass a presenter in my model there and call present on it in my renderer? Something like the snippet in the thread
ex model
Copy code
data class Model(
        val dayPresenter: DayPresenter,
        val initialPage: Int = Int.MAX_VALUE / 2
    ) : BaseModel
Copy code
@Inject
@ContributesRenderer
class TimelineRenderer(
    private val rendererFactory: RendererFactory
) : ComposeRenderer<TimelinePresenter.Model>() {

    @Composable
    override fun Compose(model: TimelinePresenter.Model) {
        HorizontalPager { pageIndex ->
            // 1. Calculate the specific date based on the page index offset
            val date = remember(pageIndex) { 
                calculateDateFromIndex(pageIndex, model.initialPage) 
            }

            // 2. Call the child presenter. Molecule turns this into a reactive stream.
            // When this page leaves the 'beyondViewport' range, this call stops, 
            // and the child presenter's lifecycle ends.
            val dayModel = model.dayPresenter.present(DayPresenter.Input(date))
            
            // 3. Render the computed sub-model
            rendererFactory.getComposeRenderer(dayModel).renderCompose(dayModel)
        }
    }
}
Is it an anti-pattern to pass a presenter to a model or should models only contain models and presents should only contain presenters
s
I usually recommend keeping UI models limited to computed values. Anything beyond that tends to make screenshot testing and composing complex UI hierarchies more difficult. That said, is this a personal project? If so, go for it. Try it out and see what ends up breaking or becomes harder to manage in the future.
➕ 1
d
It's an app for a customer, so far it seems okay. We probably won't add screenshot tests
It seems fine so far. The reason for passing the presenter to the model was because there's a pager that can be scrolled infinitely and there are only 3 pages at any given time. Each page has its own state which I only want computed when the page is in the view port. Conceptually it seems okay but I feel like I'm doing something wrong. Anyways thanks for the guidance @saket
💚 1