Skip to content

Commit

Permalink
Merge pull request #2 from hpost/feature/base-model-extensions
Browse files Browse the repository at this point in the history
BaseModel extensions
  • Loading branch information
hpost authored May 30, 2018
2 parents b61148f + 6b368a4 commit d1a9b8a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ and:

```gradle
dependencies {
implementation "cc.femto:kommon-mvi:1.0.0"
implementation "cc.femto:kommon-mvi:2.1.0"
}
```
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

buildscript {
ext.gradle_version = '3.0.1'
ext.kotlin_version = '1.2.10'
ext.kotlin_version = '1.2.41'
ext.rxjava_version = '2.1.7'
ext.rxandroid_version = '2.0.1'

repositories {
google()
Expand Down
11 changes: 5 additions & 6 deletions mvi/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ buildscript {
}

android {
compileSdkVersion 26
buildToolsVersion "26.0.2"
compileSdkVersion 27
buildToolsVersion "27.0.3"

defaultConfig {
minSdkVersion 16
targetSdkVersion 26
versionCode 3
versionName "2.0.1"
targetSdkVersion 27
versionCode 4
versionName "2.1.0"
}
buildTypes {
release {
Expand All @@ -37,7 +37,6 @@ android {
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "io.reactivex.rxjava2:rxjava:$rxjava_version"
implementation "io.reactivex.rxjava2:rxandroid:$rxandroid_version"
}

task sourcesJar(type: Jar) {
Expand Down
35 changes: 30 additions & 5 deletions mvi/src/main/kotlin/cc/femto/kommon/mvi/BaseModel.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cc.femto.kommon.mvi

import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.subjects.BehaviorSubject
import io.reactivex.subjects.PublishSubject
Expand All @@ -11,21 +10,47 @@ abstract class BaseModel<INTENT : Intent, ACTION : Action, VM> : Model<INTENT, A
protected val disposables = CompositeDisposable()
protected val viewModel: BehaviorSubject<VM> = BehaviorSubject.create()
protected val actions: PublishSubject<ACTION> = PublishSubject.create()
private val events: PublishSubject<Event> = PublishSubject.create()

override fun viewModel(): Observable<VM> = viewModel.observeOn(AndroidSchedulers.mainThread())
override fun viewModel(): Observable<VM> = viewModel

override fun actions(): Observable<ACTION> = actions.observeOn(AndroidSchedulers.mainThread())
override fun actions(): Observable<ACTION> = actions

override fun detach() {
disposables.clear()
}

protected fun <T : Event> makeViewModel(events: Observable<out T>, initialViewModel: VM, reducer: (VM, T) -> VM) {
disposables.add(events.scan(initialViewModel, reducer)
/**
* Exposes the internal [Event] stream that passes through the reducer
*/
protected fun events(): Observable<Event> = events

/**
* Dispatch an event to the reducer
*/
fun dispatchEvent(event: Event) = events.onNext(event)

/**
* Dispatch to set the current view model, e.g. when testing
*/
fun dispatchViewModel(viewModel: VM) {
this.viewModel.onNext(viewModel)
}

/**
* Subscribes internal event relay to supplied event stream and sets up [viewModel]
*/
protected fun makeViewModel(events: Observable<out Event>, initialViewModel: VM, reducer: (VM, Event) -> VM) {
disposables.add(this.events
.scan(initialViewModel, reducer)
.distinctUntilChanged()
.subscribe(viewModel::onNext))
disposables.add(events.subscribe(this.events::onNext))
}

/**
* Subscribes internal action relay to supplied action stream and sets up [actions]
*/
protected fun makeActions(actions: Observable<out ACTION>) {
disposables.add(actions.subscribe(this.actions::onNext))
}
Expand Down
14 changes: 13 additions & 1 deletion mvi/src/main/kotlin/cc/femto/kommon/mvi/Model.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ interface Model<INTENT : Intent, ACTION : Action, VM> {
* <code>
* override fun onCreate(savedInstanceState: Bundle?) {
* super.onCreate(savedInstanceState)
* view.attach(model.viewModel(), model.actions())
* view.attach(
* model.viewModel().observeOn(AndroidSchedulers.mainThread()),
* model.actions().observeOn(AndroidSchedulers.mainThread())
* )
* model.attach(view.intents())
* }
* </code>
Expand All @@ -32,6 +35,15 @@ interface Model<INTENT : Intent, ACTION : Action, VM> {
*/
fun detach()

/**
* ViewModel stream
*
* NB: Observe on main thread
*/
fun viewModel(): Observable<VM>

/**
* Actions stream
*/
fun actions(): Observable<ACTION>
}

0 comments on commit d1a9b8a

Please sign in to comment.