Skip to content

Instantly share code, notes, and snippets.

@adolgiy
Last active August 23, 2017 18:42
Show Gist options
  • Save adolgiy/77f2ee9ba39f8ca403858b4307aa01a5 to your computer and use it in GitHub Desktop.
Save adolgiy/77f2ee9ba39f8ca403858b4307aa01a5 to your computer and use it in GitHub Desktop.
Shows one of comfort for me ways to design MVP with RxBindings
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()
}
}
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)
}
}
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