Skip to content

Commit ab0a475

Browse files
author
Patrick Jackson
committed
renaming and working urnary operator
1 parent 29547e5 commit ab0a475

File tree

6 files changed

+39
-53
lines changed

6 files changed

+39
-53
lines changed

common/src/commonMain/kotlin/com/willowtreeapps/common/builders.kt

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ import org.reduxkotlin.Store
55
import org.reduxkotlin.StoreSubscriber
66
import kotlin.reflect.KProperty1
77

8-
typealias ViewUpdater<View> = (View) -> (Store) -> StoreSubscriber
9-
typealias ViewUpdaterBuilder<State, View> = ((View.() -> ((SelectorSubscriberBuilder<State>.() -> Unit))))
8+
typealias Presenter<View> = (View) -> (Store) -> StoreSubscriber
9+
/**
10+
*
11+
*/
12+
typealias PresenterBuilder<State, View> = ((View.() -> ((SelectorSubscriberBuilder<State>.() -> Unit))))
1013

1114
class SelectorSubscriberBuilder<S : Any>(val store: Store, val view: View<S>) {
1215
//available to lambda with receiver in DSL
@@ -36,30 +39,14 @@ class SelectorSubscriberBuilder<S : Any>(val store: Store, val view: View<S>) {
3639
fun tmp(selector: ((S) -> Any)-> AbstractSelector<S, Any>) {
3740

3841
}
39-
operator fun (((AppState) -> Unit).()-> Unit).unaryPlus() {
4042

43+
operator fun (()-> Any).unaryPlus():AbstractSelector<S, Any> {
44+
val that = this
45+
val selBuilder = SelectorBuilder<S>()
46+
val sel = selBuilder.withSingleField{ that() }
47+
return sel
4148
}
4249

43-
// operator fun <R> ((S) -> R).unaryPlus() {
44-
// TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
45-
// }
46-
// operator fun SelectorSubscriberBuilder<S, View>.unaryPlus(): ()->AbstractSelector<S, Any> {
47-
//
48-
// }
49-
50-
//operator fun <R> ((() -> R).()->Unit).unaryPlus() {
51-
// TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
52-
// }
53-
// operator fun <R> (() -> R).unaryPlus() {
54-
// TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
55-
// }
56-
// operator fun SelectorSubscriberBuilder<S, View>.unaryPlus(): ((S)-> Any)->AbstractSelector<S, Any> {
57-
// val selBuilder
58-
// = SelectorBuilder<S>()
59-
// val sel = selBuilder.withSingleField(selector)
60-
// return sel
61-
// }
62-
6350
infix operator fun AbstractSelector<S, Any>.plus(action: (Any) -> Unit) {
6451
selectorList[this] = action
6552
}
@@ -145,7 +132,15 @@ fun <State : Any > selectFun(store: Store, actions: (SelectorSubscriberBuilder<S
145132
}
146133
*/
147134

148-
fun <State : Any, V: View<State>> viewUpdater(actions: ViewUpdaterBuilder<State, V>): ViewUpdater<V> {
135+
/**
136+
* @param actions - a PresenterBuilder describing actions to be taken on state changes.
137+
* usage:
138+
* val myPresenter = createGenericPresenter {{
139+
* //TODO
140+
* }}
141+
* @return a Presenter function
142+
*/
143+
fun <State : Any, V: View<State>> createGenericPresenter(actions: PresenterBuilder<State, V>): Presenter<V> {
149144
return { view: V ->
150145
{ store: Store ->
151146
val actions2 = actions(view)//(store)
@@ -172,7 +167,7 @@ val searchPresenterSubscriberD2: SelectorSubscriberD<AppState, SearchView> = {
172167
}//(store)
173168
}
174169
}
175-
val searchPresenterSubscriberD3 = viewUpdater<AppState, SearchView> {
170+
val searchPresenterSubscriberD3 = createGenericPresenter<AppState, SearchView> {
176171
{
177172
plus { it.isLoadingItems } + { showLoading() }
178173
}

common/src/commonMain/kotlin/com/willowtreeapps/common/reselect.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ fun <S,O> S.whenChangeOf(selector: Selector<S, O>, blockfn: (O) -> Unit) {
138138
* abstract base class for all selectors
139139
*/
140140
abstract class AbstractSelector<S, O> : Selector<S, O> {
141+
fun invoke(tmp: ()-> Unit): O {
142+
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
143+
}
144+
141145
@JvmField protected var recomputationsLastChanged = 0L
142146
@JvmField protected var _recomputations = 0L
143147
override val recomputations: Long get() = _recomputations

common/src/commonMain/kotlin/com/willowtreeapps/common/ui/DetailsPresenter.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.willowtreeapps.common.ui
22

3-
import com.willowtreeapps.common.ViewUpdater
43
import com.willowtreeapps.common.boundary.toBookListViewState
54

65
val detailsPresenter = presenter<DetailsView> {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.willowtreeapps.common.ui
2+
3+
import com.willowtreeapps.common.AppState
4+
import com.willowtreeapps.common.Presenter
5+
import com.willowtreeapps.common.PresenterBuilder
6+
import com.willowtreeapps.common.createGenericPresenter
7+
8+
//a Presenter typed to our app's State type for convenience
9+
fun <V : LibraryView> presenter(actions: PresenterBuilder<AppState, V>): Presenter<View<AppState>> {
10+
return createGenericPresenter(actions) as Presenter<View<AppState>>
11+
}

common/src/commonMain/kotlin/com/willowtreeapps/common/ui/PresenterFactory.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ interface LibraryProvider {
9494
interface View<S : Any> {
9595
var dispatch: Dispatcher
9696
var selectorBuilder: SelectorSubscriberBuilder<S>
97-
fun viewUpdater(): ViewUpdater<View<AppState>> = throw NotImplementedError("Must implement this method to provide a presenterBuilder for ${this::class}")
97+
fun viewUpdater(): Presenter<View<AppState>> = throw NotImplementedError("Must implement this method to provide a presenterBuilder for ${this::class}")
9898

9999
}
100100

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,20 @@
11
package com.willowtreeapps.common.ui
22

3-
import com.willowtreeapps.common.*
43
import com.willowtreeapps.common.boundary.toBookListViewState
54

6-
//a viewupdater typed to our app's AppState for convenience
7-
fun <V: LibraryView> presenter(actions: ViewUpdaterBuilder<AppState,V>): ViewUpdater<View<AppState>> {
8-
return viewUpdater(actions) as ViewUpdater<View<AppState>>
9-
}
105

116
val searchPresenter = presenter<SearchView> {
127
{
13-
14-
on{it.isLoadingItems} + { showLoading() }
15-
16-
on{ it.isLoadingItems} + {
8+
+{ state.isLoadingItems } + {
179
if (state.isLoadingItems) {
1810
showLoading()
1911
} else {
2012
hideLoading()
2113
}
2214
}
2315

16+
+{ state.errorLoadingItems } + { showError(state.errorMsg) }
2417

25-
withSingleField({ it.isLoadingItems }) {
26-
if (state.isLoadingItems) {
27-
showLoading()
28-
} else {
29-
hideLoading()
30-
}
31-
}
32-
33-
withSingleField({ it.errorLoadingItems }) {
34-
showError(state.errorMsg)
35-
}
36-
37-
withSingleField({it.searchBooks}) {
38-
showResults(state.searchBooks.toBookListViewState())
39-
}
18+
+{ state.searchBooks }+{ showResults(state.searchBooks.toBookListViewState()) }
4019
}
4120
}
42-
43-

0 commit comments

Comments
 (0)