Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RxPM-library

 RxPM-library

Reactive implementation of Presentation Model pattern in Android

Avatar for Dmitriy Gorbunov

Dmitriy Gorbunov

November 23, 2017
Tweet

More Decks by Dmitriy Gorbunov

Other Decks in Programming

Transcript

  1. val inProgress = State<Boolean>(initialValue = false) // Изменяем значение в

    PresentationModel inProgress.consumer.accept(true) // Подписываемся на изменения во View pm.inProgress.observable.bindTo(progressBar.visibility()) RxPM - State 4
  2. val buttonClicks = Action<Unit>() // Слушаем клики в PresentationModel buttonClicks.observable

    .subscribe { // handle click } .untilDestroy() // Привязываемся во View button.clicks().bindTo(pm.buttonClicks.consumer) RxPM - Action 5
  3. val errorMessage = Command<String>(bufferSize = 1) // Посылаем сообщение из

    PresentationModel errorMessage.consumer.accept(message) // Слушаем команды во View pm.errorMessage.observable.bindTo { // show alert dialog } RxPM - Command 6
  4. class DataPresentationModel(private val dataModel: DataModel) : PresentationModel() { val data

    = State<List<Item>>(emptyList()) val inProgress = State(false) val errorMessage = Command<String>() val refreshAction = Action<Unit>() // ... } RxPM - PresentationModel 8
  5. class DataPresentationModel(private val dataModel: DataModel) : PresentationModel() { val data

    = State<List<Item>>(emptyList()) val inProgress = State(false) val errorMessage = Command<String>() val refreshAction = Action<Unit>() override fun onCreate() { super.onCreate() refreshAction.observable .skipWhileInProgress(inProgress.observable) .flatMapSingle { dataModel.loadData() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .bindProgress(inProgress.consumer) .doOnError { errorMessage.consumer.accept("Loading data error") } } .retry() .subscribe(data.consumer) .untilDestroy() // обновляем данные при входе на экран refreshAction.consumer.accept(Unit) } } RxPM - PresentationModel 9
  6. class DataFragment : PmSupportFragment<DataPresentationModel>() { override fun providePresentationModel() = DataPresentationModel(DataModel())

    override fun onBindPresentationModel(pm: DataPresentationModel) { pm.inProgress.observable.bindTo(swipeRefreshLayout.refreshing()) pm.data.observable.bindTo { // adapter.setItems(it) } pm.errorMessage.observable.bindTo { // show alert dialog } swipeRefreshLayout.refreshes().bindTo(pm.refreshAction.consumer) } } RxPM - PmView 10
  7. RxPM - Controls 18 // Объявляем в PresentationModel val name

    = inputControl( formatter = { it.take(50).toUpperCase() } ) val checked = checkControl(initialChecked = false) // Привязываем виджеты во View pm.name bindTo editText pm.checked bindTo checkBox
  8. Спасибо за внимание! /** * @author Dmitriy Gorbunov (dmdev) *

    @author Vasili Chyrvon (Jeevuz) */ Telegram : https://t.me/Rx_PM GitHub : https://github.com/dmdevgo/RxPM Habrahabr : https://habrahabr.ru/company/mobileup/blog/326962/ https://habrahabr.ru/company/mobileup/blog/342850/ Дмитрий Горбунов: [email protected]