Skip to content

Commit 76b4ad5

Browse files
author
Patrick Jackson
committed
fix ktlint errors
1 parent 73eca55 commit 76b4ad5

File tree

10 files changed

+84
-105
lines changed

10 files changed

+84
-105
lines changed

README.md

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,38 +18,53 @@ Documentation to come.
1818

1919
Example usage:
2020

21+
Subscribe to a single substate:
22+
2123
```
22-
val subscriber: StoreSubscriber = SelectorSubscriberFn<AppState>(store) {
23-
withSingleField({ it.isLoadingItems }) { <-- only called when isLoadingItems changes from previous state.
24-
if (state.isLoadingItems) {
25-
view?.showLoading()
26-
} else {
27-
view?.hideLoading()
28-
}
29-
}
24+
//selecting a substate subscribes to changes. Lambda block will be executed when there is
25+
//a change in value of `isLoading`
26+
val subscriber: StoreSubscriber = store.select({ it.isLoading }) {
27+
loadingIndicator.visibility = if (store.state.isLoading) View.VISIBLE else View.GONE
28+
}
3029
31-
store.subscribe(subscriber)
30+
//invoking subcription unsubscribes. Do this when appropriate for component lifecycle
31+
subscriber()
32+
```
3233

34+
Subscribe to multiple substates:
3335
```
36+
val multiSubscription = store.selectors {
37+
select({ it.isLoading }) {
38+
loadingIndicator.visibility = if (store.state.isLoading) View.VISIBLE else View.GONE
39+
}
40+
select({ it.name }) {
41+
nameTextView.text = store.state.name
42+
}
43+
}
3444
45+
//unsubscribe when appropriate
46+
multiSubscription()
47+
```
3548
__How to add to project:__
3649

50+
Requires Kotlin 1.4.0.
3751
Artifacts are hosted on maven central. For multiplatform, add the following to your shared module:
3852

53+
3954
```
4055
kotlin {
4156
sourceSets {
4257
commonMain { // <--- name may vary on your project
4358
dependencies {
44-
implementation "org.reduxkotlin:redux-kotlin-reselect:0.4.0"
59+
implementation "org.reduxkotlin:redux-kotlin-reselect:0.5.5"
4560
}
4661
}
4762
}
4863
```
4964

5065
For JVM only:
5166
```
52-
implementation "org.reduxkotlin:redux-kotlin-jvm-reselect:0.4.0"
67+
implementation "org.reduxkotlin:redux-kotlin-jvm-reselect:0.5.5"
5368
```
5469

5570
[badge-android]: http://img.shields.io/badge/platform-android-brightgreen.svg?style=flat

buildSrc/src/main/kotlin/Libs.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ object Libs {
1111
* https://developer.android.com/studio
1212
*/
1313
const val com_android_tools_build_gradle: String = "com.android.tools.build:gradle:" +
14-
Versions.com_android_tools_build_gradle
14+
Versions.com_android_tools_build_gradle
1515

1616
const val dokka_gradle_plugin: String = "org.jetbrains.dokka:dokka-gradle-plugin:" +
1717
Versions.dokka_gradle_plugin
@@ -102,5 +102,4 @@ object Libs {
102102

103103
const val redux_kotlin: String = "org.reduxkotlin:redux-kotlin:" +
104104
Versions.org_reduxkotlin_redux_kotlin
105-
106105
}

reselect/src/commonMain/kotlin/org/reduxkotlin/Reselect.kt

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package org.reduxkotlin
22

33
import kotlin.jvm.JvmField
44

5-
private typealias EqualityCheckFn = (a:Any, b:Any) -> Boolean
5+
private typealias EqualityCheckFn = (a: Any, b: Any) -> Boolean
66

77
/**
88
* A rewrite for kotlin of https://github.com/reactjs/reselect library for redux (https://github.com/reactjs/redux)
@@ -27,18 +27,18 @@ interface Memoizer<T> {
2727
fun <T> computationMemoizer(computeFn: (Array<out Any>) -> T) = object : Memoizer<T> {
2828
var lastArgs: Array<out Any>? = null
2929
var lastResult: T? = null
30-
override fun memoize(state:Any,vararg inputs: SelectorInput<Any, Any>): T {
31-
val nInputs=inputs.size
32-
val args=Array<Any>(nInputs) { inputs[it].invoke(state) }
33-
if(lastArgs!=null && lastArgs!!.size==inputs.size) {
34-
var bMatchedArgs=true
35-
for(i in 0 until nInputs) {
36-
if(!inputs[i].equalityCheck(args[i],lastArgs!![i])) {
37-
bMatchedArgs=false
30+
override fun memoize(state: Any, vararg inputs: SelectorInput<Any, Any>): T {
31+
val nInputs = inputs.size
32+
val args = Array<Any>(nInputs) { inputs[it].invoke(state) }
33+
if (lastArgs != null && lastArgs!!.size == inputs.size) {
34+
var bMatchedArgs = true
35+
for (i in 0 until nInputs) {
36+
if (!inputs[i].equalityCheck(args[i], lastArgs!![i])) {
37+
bMatchedArgs = false
3838
break
3939
}
4040
}
41-
if(bMatchedArgs) {
41+
if (bMatchedArgs) {
4242
return lastResult!!
4343
}
4444
}
@@ -48,19 +48,18 @@ fun <T> computationMemoizer(computeFn: (Array<out Any>) -> T) = object : Memoize
4848
}
4949
}
5050

51-
52-
5351
/**
5452
* specialization for the case of single input (a little bit faster)
5553
*/
56-
fun <T> singleInputMemoizer(func: (Array<out Any>) -> T)=object: Memoizer<T> {
57-
var lastArg:Any?=null
58-
var lastResult:T?=null
54+
fun <T> singleInputMemoizer(func: (Array<out Any>) -> T) = object : Memoizer<T> {
55+
var lastArg: Any? = null
56+
var lastResult: T? = null
5957
override fun memoize(state: Any, vararg inputs: SelectorInput<Any, Any>): T {
60-
val input=inputs[0]
61-
val arg=input.invoke(state)
58+
val input = inputs[0]
59+
val arg = input.invoke(state)
6260
if (lastArg != null &&
63-
input.equalityCheck(arg,lastArg!!)){
61+
input.equalityCheck(arg, lastArg!!)
62+
) {
6463
return lastResult!!
6564
}
6665
lastArg = arg
@@ -69,7 +68,6 @@ fun <T> singleInputMemoizer(func: (Array<out Any>) -> T)=object: Memoizer<T> {
6968
}
7069
}
7170

72-
7371
interface SelectorInput<S, I> {
7472
operator fun invoke(state: S): I
7573
val equalityCheck: EqualityCheckFn
@@ -78,11 +76,10 @@ interface SelectorInput<S, I> {
7876
/**
7977
* a selector function is a function that map a field in state object to the input for the selector compute function
8078
*/
81-
class InputField<S, I>(val fn: S.() -> I,override val equalityCheck: EqualityCheckFn) : SelectorInput<S, I> {
79+
class InputField<S, I>(val fn: S.() -> I, override val equalityCheck: EqualityCheckFn) : SelectorInput<S, I> {
8280
override operator fun invoke(state: S): I = state.fn()
8381
}
8482

85-
8683
/**
8784
* note: [Selector] inherit from [SelectorInput] because of support for composite selectors
8885
*/
@@ -107,7 +104,6 @@ interface Selector<S, O> : SelectorInput<S, O> {
107104
fun onChangeIn(state: S, blockfn: (O) -> Unit) {
108105
getIfChangedIn(state)?.let(blockfn)
109106
}
110-
111107
}
112108

113109
/**
@@ -130,37 +126,34 @@ abstract class AbstractSelector<S, O> : Selector<S, O> {
130126
recomputationsLastChanged = _recomputations
131127
}
132128

133-
134129
protected abstract val computeAndCount: (i: Array<out Any>) -> O
135130
/**
136131
* 'lazy' because computeandcount is abstract. Cannot reference to it before it is initialized in concrete selectors
137132
* 'open' because we can provide a custom memoizer if needed
138133
*/
139134
open val memoizer by lazy { computationMemoizer(computeAndCount) }
140-
141135
}
142136

143-
144137
/**
145138
* wrapper class for Selector factory methods , that basically is used only to capture
146139
* type information for the state parameter
147140
*/
148-
class SelectorBuilder<S:Any> {
141+
class SelectorBuilder<S : Any> {
149142
/**
150143
* special single input selector that should be used when you just want to retrieve a single field:
151144
* Warning: Don't use this with primitive type fields, use [withSingleFieldByValue] instead!!!
152145
*/
153146
fun <I : Any> withSingleField(fn: S.() -> I) = object : AbstractSelector<S, I>() {
154147
@Suppress("UNCHECKED_CAST")
155-
private val inputField= InputField(fn, byRefEqualityCheck) as SelectorInput<Any, Any>
148+
private val inputField = InputField(fn, byRefEqualityCheck) as SelectorInput<Any, Any>
156149
override val computeAndCount = fun(i: Array<out Any>): I {
157150
++_recomputations
158151
@Suppress("UNCHECKED_CAST")
159152
return i[0] as I
160153
}
161154

162155
override operator fun invoke(state: S): I {
163-
return memoizer.memoize(state,inputField)
156+
return memoizer.memoize(state, inputField)
164157
}
165158
override val equalityCheck: EqualityCheckFn
166159
get() = byRefEqualityCheck
@@ -174,14 +167,14 @@ class SelectorBuilder<S:Any> {
174167
*/
175168
fun <I : Any> withSingleFieldByValue(fn: S.() -> I) = object : AbstractSelector<S, I>() {
176169
@Suppress("UNCHECKED_CAST")
177-
private val inputField= InputField(fn, byValEqualityCheck) as SelectorInput<Any, Any>
170+
private val inputField = InputField(fn, byValEqualityCheck) as SelectorInput<Any, Any>
178171
override val computeAndCount = fun(i: Array<out Any>): I {
179172
++_recomputations
180173
@Suppress("UNCHECKED_CAST")
181174
return i[0] as I
182175
}
183176
override operator fun invoke(state: S): I {
184-
return memoizer.memoize(state,inputField)
177+
return memoizer.memoize(state, inputField)
185178
}
186179
override val equalityCheck: EqualityCheckFn
187180
get() = byValEqualityCheck
@@ -209,7 +202,7 @@ class SelectorBuilder<S:Any> {
209202
* }
210203
* }
211204
*/
212-
fun <State: Any>Store<State>.selectors(selectorSubscriberBuilderInit: SelectorSubscriberBuilder<State>.() -> Unit): StoreSubscriber {
205+
fun <State : Any> Store<State>.selectors(selectorSubscriberBuilderInit: SelectorSubscriberBuilder<State>.() -> Unit): StoreSubscriber {
213206
val subscriberBuilder: SelectorSubscriberBuilder<State> = SelectorSubscriberBuilder(this)
214207
subscriberBuilder.selectorSubscriberBuilderInit()
215208
val sub = {
@@ -220,13 +213,15 @@ fun <State: Any>Store<State>.selectors(selectorSubscriberBuilderInit: SelectorSu
220213
subscriberBuilder.withAnyChangeFun?.invoke()
221214
Unit
222215
}
223-
//call subscriber immediately when subscribing
216+
// call subscriber immediately when subscribing
224217
sub()
225218
return this.subscribe(sub)
226219
}
227220

228-
fun <State: Any>Store<State>.select(selector: (State) -> Any, action: (Any) -> Unit): StoreSubscriber {
229-
return subscribe(this.selectors {
230-
select(selector, action)
231-
})
221+
fun <State : Any> Store<State>.select(selector: (State) -> Any, action: (Any) -> Unit): StoreSubscriber {
222+
return subscribe(
223+
this.selectors {
224+
select(selector, action)
225+
}
226+
)
232227
}

reselect/src/commonMain/kotlin/org/reduxkotlin/SelectorSubscriberBuilder.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class SelectorSubscriberBuilder<State : Any>(val store: Store<State>) {
1111

1212
val selectorList = mutableMapOf<Selector<State, Any>, (Any) -> Unit>()
1313

14-
//state is here to make available to lambda with receiver in DSL
14+
// state is here to make available to lambda with receiver in DSL
1515
val state: State
1616
get() = store.getState()
1717

sample/src/androidTest/java/org/reduxkotlin/reselect/sample/ExampleInstrumentedTest.kt

Lines changed: 0 additions & 24 deletions
This file was deleted.

sample/src/main/java/org/reduxkotlin/reselect/sample/AppState.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ package org.reduxkotlin.reselect.sample
33
data class AppState(
44
val counter: Int = 0,
55
val isLoading: Boolean = false
6-
)
6+
)

sample/src/main/java/org/reduxkotlin/reselect/sample/FirstFragment.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
package org.reduxkotlin.reselect.sample
22

33
import android.os.Bundle
4-
import androidx.fragment.app.Fragment
54
import android.view.LayoutInflater
65
import android.view.View
76
import android.view.ViewGroup
87
import android.widget.Button
8+
import androidx.fragment.app.Fragment
99
import androidx.navigation.fragment.findNavController
10-
import org.reduxkotlin.*
1110

1211
/**
1312
* A simple [Fragment] subclass as the default destination in the navigation.
1413
*/
1514
class FirstFragment : Fragment() {
1615

1716
override fun onCreateView(
18-
inflater: LayoutInflater, container: ViewGroup?,
17+
inflater: LayoutInflater,
18+
container: ViewGroup?,
1919
savedInstanceState: Bundle?
2020
): View? {
2121
// Inflate the layout for this fragment

sample/src/main/java/org/reduxkotlin/reselect/sample/MainActivity.kt

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
package org.reduxkotlin.reselect.sample
22

33
import android.os.Bundle
4-
import androidx.appcompat.app.AppCompatActivity
54
import android.view.Menu
65
import android.view.MenuItem
76
import android.view.View
87
import android.widget.ProgressBar
8+
import androidx.appcompat.app.AppCompatActivity
99
import com.google.android.material.snackbar.Snackbar
10-
1110
import kotlinx.android.synthetic.main.activity_main.*
12-
import kotlinx.coroutines.*
13-
import org.reduxkotlin.*
11+
import kotlinx.coroutines.Dispatchers
12+
import kotlinx.coroutines.GlobalScope
13+
import kotlinx.coroutines.delay
14+
import kotlinx.coroutines.launch
15+
import kotlinx.coroutines.withContext
16+
import org.reduxkotlin.Reducer
17+
import org.reduxkotlin.StoreSubscription
18+
import org.reduxkotlin.Thunk
19+
import org.reduxkotlin.applyMiddleware
20+
import org.reduxkotlin.createStore
21+
import org.reduxkotlin.createThunkMiddleware
22+
import org.reduxkotlin.select
23+
import org.reduxkotlin.selectors
1424

1525
class MainActivity : AppCompatActivity() {
1626

@@ -27,12 +37,12 @@ class MainActivity : AppCompatActivity() {
2737
.setAction("Action", null).show()
2838
}
2939

30-
subscription = store.select({it.isLoading}) {
31-
loadingIndicator.visibility = if (store.state.isLoading) View.VISIBLE else View.GONE
32-
}
40+
// subscription = store.select({ it.isLoading }) {
41+
// loadingIndicator.visibility = if (store.state.isLoading) View.VISIBLE else View.GONE
42+
// }
3343

3444
multiSubscription = store.selectors {
35-
select({ it.isLoading}) {
45+
select({ it.isLoading }) {
3646
loadingIndicator.visibility = if (store.state.isLoading) View.VISIBLE else View.GONE
3747
}
3848
}

sample/src/main/java/org/reduxkotlin/reselect/sample/SecondFragment.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package org.reduxkotlin.reselect.sample
22

33
import android.os.Bundle
4-
import androidx.fragment.app.Fragment
54
import android.view.LayoutInflater
65
import android.view.View
76
import android.view.ViewGroup
87
import android.widget.Button
8+
import androidx.fragment.app.Fragment
99
import androidx.navigation.fragment.findNavController
1010

1111
/**
@@ -14,7 +14,8 @@ import androidx.navigation.fragment.findNavController
1414
class SecondFragment : Fragment() {
1515

1616
override fun onCreateView(
17-
inflater: LayoutInflater, container: ViewGroup?,
17+
inflater: LayoutInflater,
18+
container: ViewGroup?,
1819
savedInstanceState: Bundle?
1920
): View? {
2021
// Inflate the layout for this fragment

0 commit comments

Comments
 (0)