Skip to content

Commit 09ad3ba

Browse files
author
Patrick Jackson
committed
before presenter refactor
1 parent 1907a90 commit 09ad3ba

File tree

8 files changed

+479
-6
lines changed

8 files changed

+479
-6
lines changed

android/src/main/res/layout/fragment_search.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@
3232
<androidx.constraintlayout.widget.ConstraintLayout
3333
android:id="@+id/loading_spinner"
3434
android:layout_width="match_parent"
35-
android:layout_height="match_parent"
35+
android:layout_height="0dp"
3636
android:visibility="gone"
3737
app:layout_constraintTop_toBottomOf="@+id/txt_search"
38+
app:layout_constraintBottom_toBottomOf="parent"
3839
android:background="@color/white">
3940

4041
<ProgressBar
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.willowtreeapps.common
2+
3+
import com.willowtreeapps.common.ui.ViewUpdater
4+
import org.reduxkotlin.Store
5+
import org.reduxkotlin.StoreSubscriber
6+
7+
class SelectorSubscriberBuilder<S : Any, View>(val store: Store) {
8+
val state: S
9+
get() = store.getState() as S
10+
11+
var withAnyChangeFun: (() -> Unit)? = null
12+
13+
fun withAnyChange(f: () -> Unit) {
14+
withAnyChangeFun = f
15+
}
16+
17+
val selectorList = mutableMapOf<Selector<S, Any>, (Any) -> Unit>()
18+
val updaterList = mutableMapOf<Selector<S, Any>, Any>()
19+
20+
fun withSingleField(selector: (S) -> Any, action: (Any) -> Unit) {
21+
val selBuilder = SelectorBuilder<S>()
22+
val sel = selBuilder.withSingleField(selector)
23+
selectorList[sel] = action
24+
}
25+
26+
fun withSingleField2(selector: (S) -> Any, action: View.()-> Unit) {
27+
val selBuilder = SelectorBuilder<S>()
28+
val sel = selBuilder.withSingleField(selector)
29+
updaterList[sel] = action
30+
}
31+
32+
infix operator fun SelectorSubscriberBuilder<S, View>.plus(selector: (S) -> Any): AbstractSelector<S, Any> {
33+
val selBuilder = SelectorBuilder<S>()
34+
val sel = selBuilder.withSingleField(selector)
35+
return sel
36+
}
37+
38+
infix operator fun AbstractSelector<S, Any>.plus(action: (Any) -> Unit) {
39+
selectorList[this] = action
40+
}
41+
42+
infix operator fun AbstractSelector<S, Any>.invoke(action: (Any) -> Unit) {
43+
selectorList[this] = action
44+
}
45+
}
46+
47+
/**
48+
* Helper function that creates a DSL for subscribing to changes in specific state fields and actions to take.
49+
* Inside the lambda there is access to the current state through the var `state`
50+
*
51+
* ex:
52+
* SelectorSubscriberFn {
53+
* withSingleField({it.foo}, { actionWhenFooChanges() }
54+
*
55+
* withAnyChange {
56+
* //called whenever any change happens to state
57+
* view.setMessage(state.barMsg) //state is current state
58+
* }
59+
* }
60+
*/
61+
fun <S : Any, View> SelectorSubscriberFn(store: Store, selectorSubscriberBuilderInit: SelectorSubscriberBuilder<S, View>.() -> Unit): StoreSubscriber {
62+
val subBuilder = SelectorSubscriberBuilder<S, View>(store)
63+
subBuilder.selectorSubscriberBuilderInit()
64+
return {
65+
subBuilder.selectorList.forEach { entry ->
66+
entry.key.onChangeIn(store.getState() as S) { entry.value(store.getState()) }
67+
}
68+
subBuilder.withAnyChangeFun?.invoke()
69+
}
70+
}

0 commit comments

Comments
 (0)