Share
Embed
Compose everything with rx & kotlin
https://github.com/importre/kotlin-maze
A simple way to implement applications using observable streams
Jaewe Heo
March 20, 2017
Transcript
149 slides
-
Compose everything Jaewe Heo · Riiid! · @importre Mar. 25,
2017 with Rx & Kotlin -
Functional • Avoid intricate stateful programs, using clean input/output functions
over observable streams. http://reactivex.io/ -
Functional • Avoid intricate stateful programs, using clean input/output functions
over observable streams. http://reactivex.io/ -
Functional • Avoid intricate stateful programs, using clean input/output functions
over observable streams. http://reactivex.io/ -
Functional • Avoid intricate stateful programs, using clean input/output functions
over observable streams. http://reactivex.io/ -
class HelloFragment : BaseFragment() { override val layoutId: Int =
R.layout.fragment_hello override fun onViewCreated(view: View, savedInstanceState: Bundle?) { toolbar .navigationClicks() .subscribe { activity.onBackPressed() } inputName .textChanges() .subscribe { val hello = getString(R.string.hello_message) textHello.text = hello.format(it) } } } -
class HelloFragment : BaseFragment() { override val layoutId: Int =
R.layout.fragment_hello override fun onViewCreated(view: View, savedInstanceState: Bundle?) { toolbar .navigationClicks() .subscribe { activity.onBackPressed() } inputName .textChanges() .subscribe { val hello = getString(R.string.hello_message) textHello.text = hello.format(it) } } } Home As Up -
class HelloFragment : BaseFragment() { override val layoutId: Int =
R.layout.fragment_hello override fun onViewCreated(view: View, savedInstanceState: Bundle?) { toolbar .navigationClicks() .subscribe { activity.onBackPressed() } inputName .textChanges() .subscribe { val hello = getString(R.string.hello_message) textHello.text = hello.format(it) } } } EditText -
class HelloFragment : BaseFragment() { override val layoutId: Int =
R.layout.fragment_hello override fun onViewCreated(view: View, savedInstanceState: Bundle?) { toolbar .navigationClicks() .subscribe { activity.onBackPressed() } inputName .textChanges() .subscribe { val hello = getString(R.string.hello_message) textHello.text = hello.format(it) } } } Kotlin Android Extensions -
class HelloFragment : BaseFragment() { override val layoutId: Int =
R.layout.fragment_hello override fun onViewCreated(view: View, savedInstanceState: Bundle?) { toolbar .navigationClicks() .subscribe { activity.onBackPressed() } inputName .textChanges() .subscribe { val hello = getString(R.string.hello_message) textHello.text = hello.format(it) } } } RxBinding - Kotlin -
class HelloFragment : BaseFragment() { override val layoutId: Int =
R.layout.fragment_hello override fun onViewCreated(view: View, savedInstanceState: Bundle?) { toolbar .navigationClicks() .subscribe { activity.onBackPressed() } inputName .textChanges() .subscribe { val hello = getString(R.string.hello_message) textHello.text = hello.format(it) } } } Do Actions -
class CounterFragment : BaseFragment() { override val layoutId: Int =
R.layout.fragment_counter override fun onViewCreated(view: View, savedInstanceState: Bundle?) { val inc = buttonInc.clicks() .map { +1 } val dec = buttonDec.clicks() .map { -1 } Observable.merge(inc, dec) .scan(0) { acc: Int, value: Int -> acc + value } .subscribe { textValue.text = it.toString() } } } -
class CounterFragment : BaseFragment() { override val layoutId: Int =
R.layout.fragment_counter override fun onViewCreated(view: View, savedInstanceState: Bundle?) { val inc = buttonInc.clicks() .map { +1 } val dec = buttonDec.clicks() .map { -1 } Observable.merge(inc, dec) .scan(0) { acc: Int, value: Int -> acc + value } .subscribe { textValue.text = it.toString() } } } Plus / Minus -
class CounterFragment : BaseFragment() { override val layoutId: Int =
R.layout.fragment_counter override fun onViewCreated(view: View, savedInstanceState: Bundle?) { val inc = buttonInc.clicks() .map { +1 } val dec = buttonDec.clicks() .map { -1 } Observable.merge(inc, dec) .scan(0) { acc: Int, value: Int -> acc + value } .subscribe { textValue.text = it.toString() } } } Merge -
class CounterFragment : BaseFragment() { override val layoutId: Int =
R.layout.fragment_counter override fun onViewCreated(view: View, savedInstanceState: Bundle?) { val inc = buttonInc.clicks() .map { +1 } val dec = buttonDec.clicks() .map { -1 } Observable.merge(inc, dec) .scan(0) { acc: Int, value: Int -> acc + value } .subscribe { textValue.text = it.toString() } } } Scan -
class CounterFragment : BaseFragment() { override val layoutId: Int =
R.layout.fragment_counter override fun onViewCreated(view: View, savedInstanceState: Bundle?) { val inc = buttonInc.clicks() .map { +1 } val dec = buttonDec.clicks() .map { -1 } Observable.merge(inc, dec) .scan(0) { acc: Int, value: Int -> acc + value } .subscribe { textValue.text = it.toString() } } } Do Actions -
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { progress.hide() buttonUser.clicks() .doOnNext
{ progress.show() } .switchMap { api.getUsers() } .observeOn(AndroidSchedulers.mainThread()) .subscribe({ users -> val index = Random().nextInt(users.size) val user = users[index] textName.text = user.name textEmail.text = user.email progress.hide() }, { progress.hide() }) } -
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { progress.hide() buttonUser.clicks() .doOnNext
{ progress.show() } .switchMap { api.getUsers() } .observeOn(AndroidSchedulers.mainThread()) .subscribe({ users -> val index = Random().nextInt(users.size) val user = users[index] textName.text = user.name textEmail.text = user.email progress.hide() }, { progress.hide() }) } Kotlin Android Extensions / RxBinding -
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { progress.hide() buttonUser.clicks() .doOnNext
{ progress.show() } .switchMap { api.getUsers() } .observeOn(AndroidSchedulers.mainThread()) .subscribe({ users -> val index = Random().nextInt(users.size) val user = users[index] textName.text = user.name textEmail.text = user.email progress.hide() }, { progress.hide() }) } Show Progress -
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { progress.hide() buttonUser.clicks() .doOnNext
{ progress.show() } .switchMap { api.getUsers() } .observeOn(AndroidSchedulers.mainThread()) .subscribe({ users -> val index = Random().nextInt(users.size) val user = users[index] textName.text = user.name textEmail.text = user.email progress.hide() }, { progress.hide() }) } Retrofit / RxJava2 Adapter -
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { progress.hide() buttonUser.clicks() .doOnNext
{ progress.show() } .switchMap { api.getUsers() } .observeOn(AndroidSchedulers.mainThread()) .subscribe({ users -> val index = Random().nextInt(users.size) val user = users[index] textName.text = user.name textEmail.text = user.email progress.hide() }, { progress.hide() }) } Switch -
!!!!!!!!!… D/OkHttp: --> GET https://jsonplaceholder.typicode.com/users http/1.1 D/OkHttp: --> END GET
D/OkHttp: <-- HTTP FAILED: java.io.IOException: Canceled D/OkHttp: --> GET https://jsonplaceholder.typicode.com/users http/1.1 D/OkHttp: --> END GET D/OkHttp: <-- HTTP FAILED: java.io.IOException: Canceled D/OkHttp: --> GET https://jsonplaceholder.typicode.com/users http/1.1 D/OkHttp: --> END GET D/OkHttp: <-- HTTP FAILED: java.io.IOException: Canceled D/OkHttp: --> GET https://jsonplaceholder.typicode.com/users http/1.1 D/OkHttp: --> END GET D/OkHttp: <-- HTTP FAILED: java.io.IOException: Canceled ... -
!!!!!!!!!… D/OkHttp: --> GET https://jsonplaceholder.typicode.com/users http/1.1 D/OkHttp: --> END GET
D/OkHttp: <-- HTTP FAILED: java.io.IOException: Canceled D/OkHttp: --> GET https://jsonplaceholder.typicode.com/users http/1.1 D/OkHttp: --> END GET D/OkHttp: <-- HTTP FAILED: java.io.IOException: Canceled D/OkHttp: --> GET https://jsonplaceholder.typicode.com/users http/1.1 D/OkHttp: --> END GET D/OkHttp: <-- HTTP FAILED: java.io.IOException: Canceled D/OkHttp: --> GET https://jsonplaceholder.typicode.com/users http/1.1 D/OkHttp: --> END GET D/OkHttp: <-- HTTP FAILED: java.io.IOException: Canceled ... -
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { progress.hide() buttonUser.clicks() .doOnNext
{ progress.show() } .switchMap { api.getUsers() } .observeOn(AndroidSchedulers.mainThread()) .subscribe({ users -> val index = Random().nextInt(users.size) val user = users[index] textName.text = user.name textEmail.text = user.email progress.hide() }, { progress.hide() }) } Change Scheduler -
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { progress.hide() buttonUser.clicks() .doOnNext
{ progress.show() } .switchMap { api.getUsers() } .observeOn(AndroidSchedulers.mainThread()) .subscribe({ users -> val index = Random().nextInt(users.size) val user = users[index] textName.text = user.name textEmail.text = user.email progress.hide() }, { progress.hide() }) } Do Actions -
Example: Long-term jobs override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
// Printed for 1 hour if not killed Observable.interval(1, TimeUnit.SECONDS) .take(3600) .subscribe(::println) } -
Example: RxBinding /** * ... * * Warning: The created
observable keeps a strong * reference to view. Unsubscribe to free this reference. * * Warning: The created observable uses * View.setOnClickListener to observe clicks. * Only one observable can be used for a view at a time. */ public static Observable<Object> clicks(@NonNull View view) { // ... } -
Simple solution: CompositeDisposable abstract class BaseFragment : Fragment() { protected
val disposables by lazy { CompositeDisposable() } // ... override fun onDestroyView() { disposables.clear() super.onDestroyView() } } -
CompositeDisposable abstract class BaseFragment : Fragment() { protected val disposables
by lazy { CompositeDisposable() } // ... override fun onDestroyView() { disposables.clear() super.onDestroyView() } } -
CompositeDisposable abstract class BaseFragment : Fragment() { protected val disposables
by lazy { CompositeDisposable() } // ... override fun onDestroyView() { disposables.clear() super.onDestroyView() } } -
Long-term jobs override fun onViewCreated(view: View, savedInstanceState: Bundle?) { Observable.interval(1,
TimeUnit.SECONDS) .take(3600) .subscribe(::println) } -
Add disposable to disposables using apply override fun onViewCreated(view: View,
savedInstanceState: Bundle?) { Observable.interval(1, TimeUnit.SECONDS) .take(3600) .subscribe(::println) .apply { disposables.add(this) } } Function Literals with Receiver -
Apply public inline fun <T> T.apply(block: T.() -> Unit): T
{ block(); return this } Standard.kt -
RxBinding class MainFragment : BaseFragment() { // ... override fun
onViewCreated(view: View, savedInstanceState: Bundle?) { val hello = buttonHello.clicks() .map { HelloActivity::class } val counter = buttonCounter.clicks() .map { CounterActivity::class } Observable.mergeArray(hello, counter) .subscribe { start(it) } .apply { disposables.add(this) } } // ... } -
Create streams class MainFragment : BaseFragment() { // ... override
fun onViewCreated(view: View, savedInstanceState: Bundle?) { val hello = buttonHello.clicks() .map { HelloActivity::class } val counter = buttonCounter.clicks() .map { CounterActivity::class } Observable.mergeArray(hello, counter) .subscribe { start(it) } .apply { disposables.add(this) } } // ... } -
Merge & Add to CompositeDisposable class MainFragment : BaseFragment() {
// ... override fun onViewCreated(view: View, savedInstanceState: Bundle?) { val hello = buttonHello.clicks() .map { HelloActivity::class } val counter = buttonCounter.clicks() .map { CounterActivity::class } Observable.mergeArray(hello, counter) .subscribe { start(it) } .apply { disposables.add(this) } } // ... } -
Merge class MainFragment : BaseFragment() { // ... override fun
onViewCreated(view: View, savedInstanceState: Bundle?) { val hello = buttonHello.clicks() .map { HelloActivity::class } val counter = buttonCounter.clicks() .map { CounterActivity::class } Observable.mergeArray(hello, counter) .subscribe { start(it) } .apply { disposables.add(this) } } // ... } -
startActivity() class MainFragment : BaseFragment() { // ... override fun
onViewCreated(view: View, savedInstanceState: Bundle?) { val hello = buttonHello.clicks() .map { HelloActivity::class } val counter = buttonCounter.clicks() .map { CounterActivity::class } Observable.mergeArray(hello, counter) .subscribe { start(it) } .apply { disposables.add(this) } } // ... } -
Add disposable to CompositeDisposable using apply class MainFragment : BaseFragment()
{ // ... override fun onViewCreated(view: View, savedInstanceState: Bundle?) { val hello = buttonHello.clicks() .map { HelloActivity::class } val counter = buttonCounter.clicks() .map { CounterActivity::class } Observable.mergeArray(hello, counter) .subscribe { start(it) } .apply { disposables.add(this) } } // ... } -
Merge & Add it to CompositeDisposable class MainFragment : BaseFragment()
{ // ... override fun onViewCreated(view: View, savedInstanceState: Bundle?) { val hello = buttonHello.clicks() .map { HelloActivity::class } val counter = buttonCounter.clicks() .map { CounterActivity::class } Observable.mergeArray(hello, counter) .subscribe { start(it) } .apply { disposables.add(this) } } // ... } All streams will be completed -
Case #1 view ----+---------| -------> \ http ----1| switch ----------1---|
-------> Recreation -
Case #2 view ----+---| -------> \ http --| switch --------|
-------> Click -
Case #2 view ----+---| -------> \ http --| switch --------|
-------> Loading -
Case #2 view ----+---| -------> \ http --| switch --------|
-------> Rotation -
Make proxy User events (e.g. clicks, textChanges …) ——————————————————————————————— ———
View Model Stream, Proxy Event Stream ———> ——————————————————————————————— Long-term jobs (e.g. http, database, preferences …) -
Area of Activity / Fragment User events (e.g. clicks, textChanges
…) ——————————————————————————————— ——— View Model Stream, Proxy Event Stream ———> ——————————————————————————————— Long-term jobs (e.g. http, database, preferences …) -
Outside of Activity/Fragment User events (e.g. clicks, textChanges …) ———————————————————————————————
——— View Model Stream, Proxy Event Stream ———> ——————————————————————————————— Long-term jobs (e.g. http, database, preferences …) -
Case #1: Subscribe after http is terminated view1 ----+---| \
http ------1 \---\------> view2 1-----> switch 1-----> -
Case #1: Subscribe after http is terminated view1 ----+---| \
http ------1 \---\------> view2 1-----> switch 1-----> Click -
Case #1: Subscribe after http is terminated view1 ----+---| \
http ------1 \---\------> view2 1-----> switch 1-----> Subject -
Case #1: Subscribe after http is terminated view1 ----+---| \
http ------1 \---\------> view2 1-----> switch 1-----> Send event to proxy using onNext -
Case #1: Subscribe after http is terminated view1 ----+---| \
http ------1 \---\------> view2 1-----> switch 1-----> Rotation -
Case #1: Subscribe after http is terminated view1 ----+---| \
http ------1 \---\------> view2 1-----> switch 1-----> Recreation, No Loading -
Case #1: Subscribe after http is terminated view1 ----+---| \
http ------1 \---\------> view2 1-----> switch 1-----> Re-connect to previous stream -
Case #1: Subscribe after http is terminated view1 ----+---| \
http ------1 \---\------> view2 1-----> switch 1-----> Result -
Case #2: Subscribe before http is terminated view1 ----+---| \
http ------1 \ view2 ----1--> switch ----1--> Click -
Case #2: Subscribe before http is terminated view1 ----+---| \
http ------1 \ view2 ----1--> switch ----1--> Subject -
Case #2: Subscribe before http is terminated view1 ----+---| \
http ------1 \ view2 ----1--> switch ----1--> Send event to proxy using onNext -
Case #2: Subscribe before http is terminated view1 ----+---| \
http ------1 \ view2 ----1--> switch ----1--> Rotation -
Case #2: Subscribe before http is terminated view1 ----+---| \
http ------1 \ view2 ----1--> switch ----1--> Recreation -
Case #2: Subscribe before http is terminated view1 ----+---| \
http ------1 \ view2 ----1--> switch ----1--> Re-connect to previous stream -
Case #2: Subscribe before http is terminated view1 ----+---| \
http ------1 \ view2 ----1--> switch ----1--> Loading -
Case #2: Subscribe before http is terminated view1 ----+---| \
http ------1 \ view2 ----1--> switch ----1--> Result -
UsersModel - immutable data class UsersModel( val user: User =
User(0, "", ""), val loading: Boolean = false, ) Data Class -
class UsersFragment : BaseFragment(), MazeListener<UsersModel> { override val layoutId: Int
= R.layout.fragment_users private val maze by lazy { Maze(UsersModel()) } @Inject lateinit var api: Api override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) MazeApp.comp.inject(this) } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { maze.attach(this, arrayOf( toolbar.navigationClicks() .map { ClickEvent(R.id.homeAsUp) }, buttonUser.clicks() .map { ClickEvent(R.id.buttonUser) } )) } override fun onDestroyView() { maze.detach() super.onDestroyView() } override fun main(sources: Sources<UsersModel>) = usersMain(sources, api) override fun render(prev: UsersModel, curr: UsersModel) { if (curr.loading) { progress.show() return } progress.hide() textName.text = curr.user.name textEmail.text = curr.user.email textName.setTextSize(TypedValue.COMPLEX_UNIT_SP, curr.nameSize) textEmail.setTextSize(TypedValue.COMPLEX_UNIT_SP, curr.nameSize) } override fun navigate(navigation: Navigation) { when (navigation) { is Back -> activity?.onBackPressed() } } override fun finish() = maze.finish() override fun error(t: Throwable) { t.printStackTrace() } } -
Initialize maze with initial model class UsersFragment : BaseFragment(), MazeListener<UsersModel>
{ override val layoutId: Int = R.layout.fragment_users private val maze by lazy { Maze(UsersModel()) } @Inject lateinit var api: Api override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) MazeApp.comp.inject(this) } -
Initialize maze with initial model class UsersFragment : BaseFragment(), MazeListener<UsersModel>
{ override val layoutId: Int = R.layout.fragment_users private val maze by lazy { Maze(UsersModel()) } @Inject lateinit var api: Api override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) MazeApp.comp.inject(this) } -
Attach w/ user-event-streams, Detach override fun onViewCreated(view: View, savedInstanceState: Bundle?)
{ maze.attach(this, arrayOf( toolbar.navigationClicks() .map { ClickEvent(R.id.homeAsUp) }, buttonUser.clicks() .map { ClickEvent(R.id.buttonUser) } )) } override fun onDestroyView() { maze.detach() super.onDestroyView() } -
Attach w/ user-event-streams, Detach override fun onViewCreated(view: View, savedInstanceState: Bundle?)
{ maze.attach(this, arrayOf( toolbar.navigationClicks() .map { ClickEvent(R.id.homeAsUp) }, buttonUser.clicks() .map { ClickEvent(R.id.buttonUser) } )) } override fun onDestroyView() { maze.detach() super.onDestroyView() } -
Attach w/ user-event-streams, Detach override fun onViewCreated(view: View, savedInstanceState: Bundle?)
{ maze.attach(this, arrayOf( toolbar.navigationClicks() .map { ClickEvent(R.id.homeAsUp) }, buttonUser.clicks() .map { ClickEvent(R.id.buttonUser) } )) } override fun onDestroyView() { maze.detach() super.onDestroyView() } -
Implement main function, Rendering view override fun main(sources: Sources<UsersModel>) =
usersMain(sources, api) override fun render(prev: UsersModel, curr: UsersModel) { if (curr.loading) { progress.show() return } progress.hide() textName.text = curr.user.name textEmail.text = curr.user.email } -
Implement main function, Rendering view override fun main(sources: Sources<UsersModel>) =
usersMain(sources, api) override fun render(prev: UsersModel, curr: UsersModel) { if (curr.loading) { progress.show() return } progress.hide() textName.text = curr.user.name textEmail.text = curr.user.email } -
Implement main function, Rendering view override fun main(sources: Sources<UsersModel>) =
usersMain(sources, api) override fun render(prev: UsersModel, curr: UsersModel) { if (curr.loading) { progress.show() return } progress.hide() textName.text = curr.user.name textEmail.text = curr.user.email } -
Implement main function, Rendering view override fun main(sources: Sources<UsersModel>) =
usersMain(sources, api) override fun render(prev: UsersModel, curr: UsersModel) { if (curr.loading) { progress.show() return } progress.hide() textName.text = curr.user.name textEmail.text = curr.user.email } -
Navigate something, Clear resources override fun navigate(navigation: Navigation) { when
(navigation) { is Back -> activity?.onBackPressed() } } override fun finish() = maze.finish() override fun error(t: Throwable) { t.printStackTrace() } } -
fun usersMain(sources: Sources<UsersModel>, api: Api): Sinks<UsersModel> { val click =
sources.event .clicks(R.id.buttonUser) .shareReplay(1) val loading = click .withLatestFrom(sources.model, BiFunction { _: ClickEvent, model: UsersModel -> model.copy(loading = true) }) val users = click .switchMap { api.getUsers() } .withLatestFrom(sources.model, BiFunction { users: Users, model: UsersModel -> val index = Random().nextInt(users.size) model.copy(user = users[index], loading = false) }) val back = sources.event .clicks(R.id.homeAsUp) .map { Back() } val model = Observable .merge(loading, users) .cacheWithInitialCapacity(1) return Sinks(model, back) } -
Main function: Input -> Output fun usersMain(sources: Sources<UsersModel>, api: Api):
Sinks<UsersModel> { // data flow return Sinks(model, navigation) } -
Main function: Sources -> Sinks fun usersMain(sources: Sources<UsersModel>, api: Api):
Sinks<UsersModel> { // data flow return Sinks(model, navigation) } -
Input val click = sources.event .clicks(R.id.buttonUser) .shareReplay(1) val loading =
click .withLatestFrom(sources.model, BiFunction { _: ClickEvent, model: UsersModel -> model.copy(loading = true) }) val users = click .switchMap { api.getUsers() } .withLatestFrom(sources.model, BiFunction { users: Users, model: UsersModel -> val index = Random().nextInt(users.size) model.copy(user = users[index], loading = false) }) -
Input val click = sources.event .clicks(R.id.buttonUser) .shareReplay(1) val loading =
click .withLatestFrom(sources.model, BiFunction { _: ClickEvent, model: UsersModel -> model.copy(loading = true) }) val users = click .switchMap { api.getUsers() } .withLatestFrom(sources.model, BiFunction { users: Users, model: UsersModel -> val index = Random().nextInt(users.size) model.copy(user = users[index], loading = false) }) -
Input val click = sources.event .clicks(R.id.buttonUser) .shareReplay(1) val loading =
click .withLatestFrom(sources.model, BiFunction { _: ClickEvent, model: UsersModel -> model.copy(loading = true) }) val users = click .switchMap { api.getUsers() } .withLatestFrom(sources.model, BiFunction { users: Users, model: UsersModel -> val index = Random().nextInt(users.size) model.copy(user = users[index], loading = false) }) -
Input val click = sources.event .clicks(R.id.buttonUser) .shareReplay(1) val loading =
click .withLatestFrom(sources.model, BiFunction { _: ClickEvent, model: UsersModel -> model.copy(loading = true) }) val users = click .switchMap { api.getUsers() } .withLatestFrom(sources.model, BiFunction { users: Users, model: UsersModel -> val index = Random().nextInt(users.size) model.copy(user = users[index], loading = false) }) -
UserModel: Input -> Output -> Input -> Output fun usersMain(sources:
Sources<UsersModel>, api: Api): Sinks<UsersModel> { // data flow return Sinks(model, navigation) } -
Data Flow: Initial data { "loading": false, "user": { "email":
"", "id": 0, "name": "" } } -
Data Flow: Click, Http { "loading": true, "user": { "email":
"", "id": 0, "name": "" } } -
Data Flow: Done { "loading": false, "user": { "email": "[email protected]",
"id": 4, "name": "Patricia Lebsack" } } -
More Examples • Counter • Progress • Http + +
+ • Animation • Previous, Current Model • Infinite Scroll • ... https://github.com/importre/kotlin-maze -
@Test fun testUiStream() { val user = User(0, "name", "[email protected]")
val users = Observable.just(listOf(user)) given(api.getUsers()).willReturn(users) // 1. initialize maze streams val sinks = usersMain(sources, api) // 2. make test observer val testObserver = makeTestObserver(sources, sinks) // 3. click sources.event(ClickEvent(R.id.buttonUser)) // 4. tests testObserver.assertNoErrors() testObserver.assertValues( UsersModel(loading = true), UsersModel(user = user, loading = false) ) testObserver.onComplete() } -
@Test fun testUiStream() { val user = User(0, "name", "[email protected]")
val users = Observable.just(listOf(user)) given(api.getUsers()).willReturn(users) // 1. initialize maze streams val sinks = usersMain(sources, api) // 2. make test observer val testObserver = makeTestObserver(sources, sinks) // 3. click sources.event(ClickEvent(R.id.buttonUser)) // 4. tests testObserver.assertNoErrors() testObserver.assertValues( UsersModel(loading = true), UsersModel(user = user, loading = false) ) testObserver.onComplete() } -
@Test fun testUiStream() { val user = User(0, "name", "[email protected]")
val users = Observable.just(listOf(user)) given(api.getUsers()).willReturn(users) // 1. initialize maze streams val sinks = usersMain(sources, api) // 2. make test observer val testObserver = makeTestObserver(sources, sinks) // 3. click sources.event(ClickEvent(R.id.buttonUser)) // 4. tests testObserver.assertNoErrors() testObserver.assertValues( UsersModel(loading = true), UsersModel(user = user, loading = false) ) testObserver.onComplete() } -
@Test fun testUiStream() { val user = User(0, "name", "[email protected]")
val users = Observable.just(listOf(user)) given(api.getUsers()).willReturn(users) // 1. initialize maze streams val sinks = usersMain(sources, api) // 2. make test observer val testObserver = makeTestObserver(sources, sinks) // 3. click sources.event(ClickEvent(R.id.buttonUser)) // 4. tests testObserver.assertNoErrors() testObserver.assertValues( UsersModel(loading = true), UsersModel(user = user, loading = false) ) testObserver.onComplete() } -
@Test fun testUiStream() { val user = User(0, "name", "[email protected]")
val users = Observable.just(listOf(user)) given(api.getUsers()).willReturn(users) // 1. initialize maze streams val sinks = usersMain(sources, api) // 2. make test observer val testObserver = makeTestObserver(sources, sinks) // 3. click sources.event(ClickEvent(R.id.buttonUser)) // 4. tests testObserver.assertNoErrors() testObserver.assertValues( UsersModel(loading = true), UsersModel(user = user, loading = false) ) testObserver.onComplete() } -
@Test fun testUiStream() { val user = User(0, "name", "[email protected]")
val users = Observable.just(listOf(user)) given(api.getUsers()).willReturn(users) // 1. initialize maze streams val sinks = usersMain(sources, api) // 2. make test observer val testObserver = makeTestObserver(sources, sinks) // 3. click sources.event(ClickEvent(R.id.buttonUser)) // 4. tests testObserver.assertNoErrors() testObserver.assertValues( UsersModel(loading = true), UsersModel(user = user, loading = false) ) testObserver.onComplete() } -
Conclusion • рױೣ • ݫੋ ೣࣻ • ചݶ Ӓܻӝ •
ࢎਊ • ण ࠺ਊ • ࢎૉ -
Conclusion • рױೣ • ݫੋ ೣࣻ • ചݶ Ӓܻӝ •
ࢎਊ • ण ࠺ਊ • ࢎૉ • ০ ߑೱ?!