Decorator

When you need to extend or modify the behaviour of a PoqSDK class you will find that most implementations are internal so you cannot extend it or call its constructor. To modify the behaviour of the PoqSDK you will need to apply the delegate pattern. For example, if you need to add an extra method to a ViewModel you need to:

  1. Create a new interface defining your new method which extends the PoqSDK ViewModel.
    interface CustomViewModel: PoqViewModelInterface {
    fun newMethod()
    }
  2. Create your ViewModel implementation which implements both interfaces and extend base CustomPoqViewModel class passing the delegate on the constructor.
  3. Inject PoqSDK ViewModel interface via the constructor (learn how to get PoqSDK instances)
  4. Use the Kotlin keyword by to delegate the implementation of the PoqSDK ViewModel to the instance injected in the constructor.
    class CustomViewModelImpl(
    delegate: PoqViewModelInterface
    ): CustomPoqViewModel(delegate), CustomViewModel, PoqViewModelInterface by delegate {
    override fun newMethod() {...}
    }
  5. Whenever you need to call your new method, you can cast the PoqSDK ViewModel to your new interface.
    (poqViewModelInterface as CustomViewModel).newMethod()

Decorate state and event flows

When you are decorating a ViewModel and you need to send new states or events, you can create a new flow and merge it with the delegated flow:

class CustomViewModelImpl(
delegate: PoqViewModelInterface
): CustomPoqViewModel(delegate), CustomViewModel, PoqViewModelInterface by delegate {
private val customEvent = MutableSharedFlow<UiEvent>()
override val uiEvent = merge(customEvent, delegate.uiEvent)
}