Skip to content

Instantly share code, notes, and snippets.

@dmytrodanylyk
Last active October 6, 2017 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmytrodanylyk/f02197815a0a3adf5ab70798796aafeb to your computer and use it in GitHub Desktop.
Save dmytrodanylyk/f02197815a0a3adf5ab70798796aafeb to your computer and use it in GitHub Desktop.
Test
class NewPresenter(private val todoDao: TodoDao) {
private var description: CharSequence? = null
fun bind(view: TodoView) {
todoDao.all()
.observeOn(AndroidSchedulers.mainThread())
.subscribe { view.updateTodos(it) }
}
fun onSaveClicked() {
todoDao.add(Todo().apply { text = description.toString() })
description = null
}
fun onDescriptionChanged(text: CharSequence) {
description = text
}
fun onTodoStateChanged(todo: Todo) = todoDao.update(todo)
}
class OldPresenter(private val todoDao: TodoDao) {
private val description = BehaviorSubject.create<CharSequence>()
fun bind(view: TodoView, disposables: CompositeDisposable) {
disposables += view.addClicks
.withLatestFrom(description, BiFunction<Unit, CharSequence, CharSequence> { _, description -> description })
.observeOn(Schedulers.io())
.subscribe {
todoDao.add(Todo().apply { text = it.toString() })
description.onNext("")
}
disposables += view.description
.subscribe { description.onNext(it) }
disposables += view.checkedTodo
.observeOn(Schedulers.io())
.map {
it.done = !it.done
it
}
.subscribe {
todoDao.update(it)
}
disposables += todoDao.all()
.switchMap { list ->
description.first("").toFlowable()
.map { ViewState(list, it) }
}
.observeOn(AndroidSchedulers.mainThread())
.subscribe(view.render)
}
}
@dmytrodanylyk
Copy link
Author

dmytrodanylyk commented Sep 19, 2017

  1. Hard to understand which event's view deliver to OldPresenter, in case of NewPresenter we have function with clear name onSaveClicked
  2. withLatestFrom would become huge if you have more instance variables like description
  3. Whole view is redrawn whenever changes happens (bad for performance)
  4. With more complex screen activity and presenter will contains huge number of Observables, Subjects with event's floating around and eventually ends up in:
    https://github.com/kickstarter/android-oss/blob/master/app/src/main/java/com/kickstarter/viewmodels/ActivityFeedViewModel.java#L170
    and
    https://github.com/kickstarter/android-oss/blob/master/app/src/main/java/com/kickstarter/viewmodels/ActivityFeedViewModel.java#L51
  5. Overall Presenter become more complex, hard to understand and maintain, because of rx transformations, passing event's here and there, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment