Skip to content

Instantly share code, notes, and snippets.

@vinaysshenoy
Last active May 8, 2018 07:08
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 vinaysshenoy/41a0ac57ae72418848f0b48ae5e9df1b to your computer and use it in GitHub Desktop.
Save vinaysshenoy/41a0ac57ae72418848f0b48ae5e9df1b to your computer and use it in GitHub Desktop.
MVP-Rx-Kotlin
sealed class PresenterToViewMsg
data class Msg1(val prop: Int): PresenterToViewMsg()
data class Msg2(val prop: String): PresenterToViewMsg()
sealed class ViewToPresenterMsg
data class Msg3(val prop: Int): ViewToPresenterMsg()
data class Msg4(val prop: String): ViewToPresenterMsg()
class Presenter {
val messages = PublishSubject.create<PresenterToViewMsg>() as Observable<PresenterToViewMsg>
fun onViewReady(view) {
view.messages.subscribe {
when(it) {
is Msg3 -> messages.onNext(Msg1(4))
is Msg4 -> messages.onNext(Msg2("Test"))
}
}
}
}
class MyActivity: AppCompatActivity {
val messages = PublishSubject.create<ViewToPresenterMsg>() as Observable<PresenterToViewMsg>
val presenter = Presenter()
onStart() {
presenter.messages.subscribe {
when(it) {
is Msg1 -> tv_1.visibility = View.VISIBLE
is Msg2 -> tv_1.visibility = VIEW.GONE
}
}
presenter.onViewReady(this)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment