Skip to content

Commit

Permalink
Merge pull request #3 from hpost/feature/base-model-framework
Browse files Browse the repository at this point in the history
Base model framework additions
  • Loading branch information
hpost authored Aug 8, 2018
2 parents 9758f9d + 78f22ea commit 3dbc132
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ and:

```gradle
dependencies {
implementation "cc.femto:kommon-mvi:2.1.1"
implementation "cc.femto:kommon-mvi:2.2.0"
}
```
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

buildscript {
ext.gradle_version = '3.0.1'
ext.kotlin_version = '1.2.41'
ext.kotlin_version = '1.2.60'
ext.rxjava_version = '2.1.7'

repositories {
Expand Down
4 changes: 2 additions & 2 deletions mvi/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ android {
defaultConfig {
minSdkVersion 16
targetSdkVersion 27
versionCode 5
versionName "2.1.1"
versionCode 6
versionName "2.2.0"
}
buildTypes {
release {
Expand Down
43 changes: 43 additions & 0 deletions mvi/src/main/kotlin/cc/femto/kommon/mvi/BaseModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,53 @@ abstract class BaseModel<INTENT : Intent, ACTION : Action, VM> : Model<INTENT, A

override fun actions(): Observable<ACTION> = actions

override fun attach(intents: Observable<INTENT>) {
makeViewModel(
eventsFrom(intents),
initialViewModel(),
::reduce
)
disposables.add(sideEffectsFrom(intents))
}

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

/**
* @return instance of [VM] representing the initial state
*/
protected abstract fun initialViewModel(): VM

/**
* Define state mutation events that result in view model changes
*
* NB: Operations that don't cause a state mutation reside in [sideEffectsFrom]
*
* @return [Observable] of [Event] feeding into [reduce]
*/
protected abstract fun eventsFrom(intents: Observable<INTENT>): Observable<out Event>

/**
* Reduce state mutations to updated view models
*
* NB: Should be a pure function without side effects
*/
protected abstract fun reduce(model: VM, event: Event): VM

/**
* Define side effects that don't result in state mutations, if any
*
* Default implementation results in no-op.
*
* NB: Returned subscriptions will be added to [disposables] and disposed in [detach]
*
* @return [CompositeDisposable] containing subscriptions that need to be managed
*/
protected open fun sideEffectsFrom(intents: Observable<INTENT>): CompositeDisposable {
return CompositeDisposable()
}

/**
* Exposes the internal [Event] stream that passes through the reducer
*/
Expand Down

0 comments on commit 3dbc132

Please sign in to comment.