Last active
August 23, 2017 18:42
-
-
Save adolgiy/77f2ee9ba39f8ca403858b4307aa01a5 to your computer and use it in GitHub Desktop.
Shows one of comfort for me ways to design MVP with RxBindings
This file contains 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 BasePresenter { | |
private val detachDisposable = CompositeDisposable() | |
private val destroyDisposable = CompositeDisposable() | |
fun unsubscribeOnDetach(d: Disposable) { | |
detachDisposable.add(d) | |
} | |
fun unsubscribeOnDestroy(d: Disposable) { | |
destroyDisposable.add(d) | |
} | |
fun onDetachView() { | |
detachDisposable.clear() | |
} | |
fun onDestroy() { | |
destroyDisposable.clear() | |
} | |
} |
This file contains 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 MvpFragment : BaseFragment(), Contract.View { | |
override fun onViewCreated(view: View, state: Bundle) { | |
RxTextView.afterTextChanges(editText) | |
.map(TextViewAfterTextChangeEvent::editable) | |
.map(Editable::toString) | |
.doOnSubscribe(presenter::unsubscribeOnDetach) | |
.subscribe(presenter::onEditTextChanged) | |
} | |
} |
This file contains 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 MvpPresenter : BasePresenter, Contract.Presenter { | |
private val textChanges = RxRelay.create<String>() | |
init { | |
textChanges | |
.doOnSubscribe(this::unsubscribeOnDestroy) | |
.subscribe({ | |
// todo do stuff | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment