Factory

fun interface Factory(source)

A factory that produces presenters for a given Screen. Circuit instances use the created presenter and connects it to a given Ui for the same Screen.

Factories should be simple aggregate multiple presenters for a canonical "whole screen". That is to say, they should be hand-written and aggregate all the presenters responsible for the UI visible within the surface this presents on.

Example

Consider this example of a Profile UI.

                              ┌────────────────────┐
                         ┌─── │                    │
                         │    ├────────────────────┤◄──┐
                         │    │ X                  │   │
                         │    │                    │ ProfileHeaderPresenter
                         │    │ Fred Rogers        │   │
                         │    ├────────────────────┤◄──┘
                         │    │ ┌───────┐  ┌────┐  │
    ProfilePresenterFactory   │ │Message│  │Call│◄─┼─── ProfileActionsPresenter
                         │    │ └───────┘  └────┘  │
                         │    │                    │
                         │    │  - - - - - - - - ◄─┼────┐
                         │    │  - - - - - - - -   │    │
                         │    │  - - - - - - - -   │  ProfileDetailsPresenter
                         │    │  - - - - - - - - ◄─┼────┘
                         └─── │                    │
                              └────────────────────┘

This would be represented by the following factory implementation:

class ProfilePresenter.Factory @Inject constructor(
  val headerPresenter: ProfilerHeaderPresenter.Factory,
  val actionsPresenter: ProfilerActionsPresenter.Factory,
  val detailsPresenter: ProfilerDetailsPresenter.Factory,
  val callScreenRouter: CallScreenRouter.Factory
) : Presenter.Factory {
  override fun create(screen: Screen, navigator: Navigator, context: CircuitContext): Presenter<*, *>? {
    return when (screen) {
      is ProfileHeader -> headerPresenter.create(screen)
      is ProfileActions -> actionsPresenter.create(screen, callScreenRouter.create(navigator))
      is ProfileDetails -> detailsPresenter.create(screen)
      else -> null
    }
  }
}

Functions

Link copied to clipboard
abstract fun create(screen: Screen, navigator: Navigator, context: CircuitContext): Presenter<*>?

Creates a Presenter for the given screen if it can handle it, or returns null if it cannot handle the given screen.