Skip to content

Commit 6658b1c

Browse files
committed
Remove when(action) statement or expression
1 parent 175a7fd commit 6658b1c

File tree

8 files changed

+98
-22
lines changed

8 files changed

+98
-22
lines changed

app/src/main/java/com/github/skyfe79/android/library/app/MainViewModel.kt

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.github.skyfe79.android.library.app
22

33
import android.app.Application
4-
import android.util.Log
4+
import com.github.skyfe79.android.library.app.action.*
55
import com.github.skyfe79.android.reactcomponentkit.redux.Output
66
import com.github.skyfe79.android.reactcomponentkit.redux.State
7-
import com.github.skyfe79.android.library.app.redux.routeReducer
7+
import com.github.skyfe79.android.library.app.redux.*
88
import com.github.skyfe79.android.reactcomponentkit.redux.RCKViewModel
99

1010
enum class MainRoute {
@@ -27,14 +27,16 @@ class MainViewModel(application: Application): RCKViewModel<MainState>(applicati
2727
initialState = MainState(MainRoute.None),
2828
reducers = arrayOf(::routeReducer)
2929
)
30+
31+
store.map(ClickCounterExampleButtonAction, ::routeClickCounterExampleButtonAction)
32+
store.map(ClickCounterExample2ButtonAction, ::routeClickCounterExample2ButtonAction)
33+
store.map(ClickRecyclerViewExampleButtonAction, ::routeClickRecyclerViewExampleButtonAction)
34+
store.map(ClickEmojiExampleButtonAction, ::routeClickEmojiExampleButtonAction)
35+
store.map(ClickCollectionViewExampleButtonAction, { it.copy(route = MainRoute.CollectionViewExample) })
3036
}
3137
}
3238

3339
override fun on(newState: MainState) {
3440
route.accept(newState.route).afterReset(MainRoute.None)
3541
}
36-
37-
fun hello() {
38-
Log.d("TAG", "HELLO")
39-
}
4042
}

app/src/main/java/com/github/skyfe79/android/library/app/examples/collectionview/CollectionViewActivity.kt

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import android.os.Bundle
66
import androidx.lifecycle.ViewModelProviders
77
import com.github.skyfe79.android.library.app.R
88
import com.github.skyfe79.android.library.app.examples.collectionview.action.LoadAction
9+
import com.github.skyfe79.android.library.app.examples.collectionview.reducer.loadEmoji2
10+
import com.github.skyfe79.android.library.app.examples.collectionview.reducer.makeSectionModels2
911
import com.github.skyfe79.android.library.app.examples.emojicollection.components.EmojiViewComponent
1012
import com.github.skyfe79.android.library.app.examples.recyclerview.component.SectionViewComponent
1113
import com.github.skyfe79.android.reactcomponentkit.collectionview.CollectionViewAdapter

app/src/main/java/com/github/skyfe79/android/library/app/examples/collectionview/CollectionViewModel.kt

+18-6
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,29 @@ class CollectionViewModel(application: Application): RCKViewModel<CollectionStat
2121
override fun setupStore() {
2222
initStore { store ->
2323
store.set(
24-
initialState = CollectionState(),
25-
reducers = arrayOf(::loadEmoji, ::makeSectionModels)
24+
initialState = CollectionState()
25+
//reducers = arrayOf(::loadEmoji, ::makeSectionModels)
2626
)
27-
}
2827

28+
// map action to reducers
29+
store.map(LoadAction, ::loadEmoji2, ::makeSectionModels2)
30+
// or
31+
store.map(LoadAction,
32+
{ state ->
33+
//it's reducer
34+
state
35+
},
36+
{
37+
//it's reducer
38+
it
39+
}
40+
)
41+
}
2942
}
3043

3144
override fun beforeDispatch(action: Action): Action = when(action) {
32-
is LoadAction -> {
33-
VoidAction
34-
//if (store.state.emojis.isNotEmpty()) VoidAction else action
45+
is LoadAction -> withState { state ->
46+
if (state.emojis.isNotEmpty()) VoidAction else action
3547
}
3648
else -> action
3749
}

app/src/main/java/com/github/skyfe79/android/library/app/examples/collectionview/reducer/Reducers.kt

+42
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,46 @@ fun CollectionViewModel.makeSectionModels(action: Action) = setState { state ->
6666
}
6767
else -> state
6868
}
69+
}
70+
71+
fun CollectionViewModel.loadEmoji2(state: CollectionState): CollectionState {
72+
val emojiCollection = (1..5)
73+
.map {
74+
(1..(40..80).random()).map { EmojiHelper.emoji }
75+
}
76+
return state.copy(emojis = emojiCollection)
77+
}
78+
79+
fun CollectionViewModel.makeSectionModels2(state: CollectionState): CollectionState {
80+
val colors = listOf(
81+
0xffd01774.toInt(),
82+
0xfff7f93c.toInt(),
83+
0xfff07777.toInt(),
84+
0xfffcce62.toInt(),
85+
0xff58c8d8.toInt()
86+
)
87+
88+
val sectionModels = state.emojis.mapIndexed { index, list ->
89+
val emojiBoxModels = list.map { EmojiBoxModel(it) }
90+
DefaultSectionModel(
91+
emojiBoxModels,
92+
header = TextMessage("Section Header #$index", colors[index], true),
93+
footer = TextMessage("Section Footer #$index", colors[index], false)
94+
)
95+
{ recyclerView ->
96+
recyclerView.layoutManager = when(index) {
97+
0 -> GridLayoutManager(getApplication(), 8)
98+
1 -> GridLayoutManager(getApplication(), 4)
99+
2 -> StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
100+
3 -> GridLayoutManager(getApplication(), 3)
101+
else -> LinearLayoutManager(getApplication(), LinearLayoutManager.HORIZONTAL, false)
102+
}
103+
104+
if (index == 4) {
105+
val snapHelper = LinearSnapHelper()
106+
snapHelper.attachToRecyclerView(recyclerView)
107+
}
108+
}
109+
}
110+
return state.copy(sections = sectionModels)
69111
}

app/src/main/java/com/github/skyfe79/android/library/app/redux/Reducers.kt

+13-1
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,16 @@ fun MainViewModel.routeReducer(action: Action): MainState = setState { state ->
3232
}
3333
else -> state
3434
}
35-
}
35+
}
36+
37+
fun MainViewModel.routeClickCounterExampleButtonAction(state: MainState) = state.copy(route = MainRoute.CounterExample)
38+
39+
fun MainViewModel.routeClickCounterExample2ButtonAction(state: MainState) = state.copy(route = MainRoute.CounterExample2)
40+
41+
fun MainViewModel.routeClickRecyclerViewExampleButtonAction(state: MainState) = state.copy(route = MainRoute.RecyclerViewExample)
42+
43+
fun MainViewModel.routeClickEmojiExampleButtonAction(state: MainState) = state.copy(route = MainRoute.EmojiCollectionExample)
44+
45+
fun MainViewModel.routeClickCollectionViewExampleButtonAction(state: MainState) = state.copy(route = MainRoute.CollectionViewExample)
46+
47+

reactcomponentkit/src/main/java/com/github/skyfe79/android/reactcomponentkit/redux/RCKViewModel.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import com.github.skyfe79.android.reactcomponentkit.eventbus.Token
77
import com.jakewharton.rxrelay2.BehaviorRelay
88
import io.reactivex.android.schedulers.AndroidSchedulers
99
import io.reactivex.disposables.CompositeDisposable
10+
import io.reactivex.rxkotlin.toObservable
11+
import io.reactivex.schedulers.Schedulers
1012
import java.util.concurrent.atomic.AtomicBoolean
1113
import java.util.concurrent.locks.ReentrantLock
1214

@@ -175,14 +177,12 @@ abstract class RCKViewModel<S: State>(application: Application): AndroidViewMode
175177
}
176178
}
177179

178-
fun withState(block: RCKViewModel<S>.(S) -> Unit) {
180+
fun <R> withState(block: RCKViewModel<S>.(S) -> R): R {
179181
readLock.lock()
180182
try {
181-
block(this.store.state)
183+
return block(this.store.state)
182184
} finally {
183185
readLock.unlock()
184186
}
185187
}
186-
187-
188188
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
package com.github.skyfe79.android.reactcomponentkit.redux
22

33
typealias Reducer<STATE> = (Action) -> STATE
4+
typealias Reducer2<STATE> = (STATE) -> STATE

reactcomponentkit/src/main/java/com/github/skyfe79/android/reactcomponentkit/redux/Store.kt

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.github.skyfe79.android.reactcomponentkit.redux
22

3-
import io.reactivex.Observable
43
import io.reactivex.Single
5-
import io.reactivex.android.schedulers.AndroidSchedulers
64
import io.reactivex.disposables.CompositeDisposable
75
import io.reactivex.plugins.RxJavaPlugins
86
import io.reactivex.rxkotlin.subscribeBy
@@ -14,6 +12,7 @@ class Store<S: State> {
1412
lateinit var state: S
1513
private set
1614
private lateinit var reducers: Array<Reducer<S>>
15+
private lateinit var reducers2: MutableMap<Action, List<Reducer2<S>>>
1716
private lateinit var afters: Array<After<S>>
1817
private val disposables = CompositeDisposable()
1918

@@ -30,15 +29,21 @@ class Store<S: State> {
3029
afters: Array<After<S>> = emptyArray()) {
3130
this.state = initialState
3231
this.reducers = reducers
32+
this.reducers2 = mutableMapOf()
3333
this.afters = afters
3434
}
3535

3636
fun deinitialize() {
3737
reducers = emptyArray()
38+
reducers2 = mutableMapOf()
3839
afters = emptyArray()
3940
disposables.clear()
4041
}
4142

43+
fun map(action: Action, vararg r: Reducer2<S>) {
44+
reducers2[action] = r.toList()
45+
}
46+
4247
/**
4348
* Do something after dispatching new state.
4449
* For example, reset route on Android
@@ -55,12 +60,12 @@ class Store<S: State> {
5560
return Single.create { single ->
5661
// reset error
5762
this@Store.state.error = null
58-
59-
val disposable = reducers.toObservable()
63+
val reducersForAction: List<Reducer2<S>> = reducers2[action] ?: emptyList()
64+
val disposable = reducersForAction.toObservable()
6065
.subscribeOn(Schedulers.single())
6166
.observeOn(Schedulers.single())
6267
.map { reducer ->
63-
reducer(action)
68+
reducer(this@Store.state)
6469
}
6570
.doOnNext { modifiedState ->
6671
this@Store.state = modifiedState

0 commit comments

Comments
 (0)