Last active
October 6, 2017 11:05
-
-
Save dmytrodanylyk/f02197815a0a3adf5ab70798796aafeb to your computer and use it in GitHub Desktop.
Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
OldPresenter, in case ofNewPresenterwe have function with clear nameonSaveClickedwithLatestFromwould become huge if you have more instance variables likedescriptionhttps://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