Saturday, December 19, 2020

More Flexible PYDroid

PYDroid 22.2.8 was recently released which allows you to create a component which is composed of multiple UiViews which all handle different subsets of a UiViewEvent instead of handling the same UiViewEvent. This allows you to separate UiViewEvent models up into things like just events for list loading, versus just events for error message displays, and the like.

Imagine the following:


sealed class MyViewEvent : UiViewEvent {

  object ButtonEvent : MyViewEvent()
  object ListEvent : MyViewEvent()

}

class MyViewModel : UiViewModel<MyState, MyViewEvent, MyControllerEvent> {

  handleViewEvent(event: MyViewEvent) {
    return when (event) {
      -> ListEvent -> listWork()
      -> ButtonEvent -> buttonWork()
    }
  }
}

class MyButton : BaseUiView<MyState, MyViewEvent, MyControllerEvent> {

  // Because it implements MyViewEvent, which was 
  // a requirement to be in a Component,
  // it is aware of, and can wrongly use MyViewEvent.ListEvent

}

class MyList : BaseUiView<MyState, MyViewEvent, MyControllerEvent> {

  // Because it implements MyViewEvent, which was 
  // a requirement to be in a Component,
  // it is aware of, and can wrongly use MyViewEvent.ButtonEvent

}


you can now separate the view concerns like so


sealed class MyViewEvent: UiViewEvent {

  sealed class ButtonEvents : MyViewEvent() {

    object Clicked : ButtonEvents()

  }
  
  sealed class ListEvents : MyViewEvent() {

    object Loaded: ListEvents()
  }

}

class MyViewModel : UiViewModel<MyState, MyViewEvent, MyControllerEvent> {

  handleViewEvent(event: MyViewEvent) {
    return when (event) {
      -> ListEvents.Loaded -> listWork()
      -> ButtonEvents.Clicked -> buttonWork()
    }
  }
}

class MyButton : BaseUiView<MyState, MyViewEvent.ButtonEvents, MyControllerEvent> {

  // Because it implements MyViewEvent.ButtonEvents, 
  // it can only use the Clicked action
  // and it can still be created into a component
  // with MyViewModel, so no code changes in the
  // createComponent callsite.

}

class MyList : BaseUiView<MyState, MyViewEvent.ListEvents, MyControllerEvent> {

  // Because it implements MyViewEvent.ListEvents,
  // it can only use the Loaded action
  // and it can still be created into a component
  // with MyViewModel, so no code changes in the
  // createComponent callsite.

}


More changes will be coming soon to PYDroid to help you better share your component views between different ViewModels and UiControllers.

Stay tuned!

========================
Follow pyamsoft around the Web for updates and announcements about the newest applications!
Like what I do?

Send me an email at: pyam.soft@gmail.com
Or find me online at: https://pyamsoft.blogspot.com

Follow my Facebook Page
Check out my code on GitHub
=========================