1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
/*
* Copyright (c) 2019. Robert Jaros
*/
package pl.treksoft.kvision.state
import pl.treksoft.kvision.core.Container
import pl.treksoft.kvision.core.Widget
/**
* A class which binds the given container to the observable state.
*
* @constructor Creates StateBinding which binds the given container to the observable state.
* @param S the state type
* @param CONT container type
* @param observableState the state
* @param container the container
* @param factory a function which re-creates the view based on the given state
*/
class StateBinding<S : Any, CONT : Container, CONTENT>(
observableState: ObservableState<S>,
private val container: CONT,
private val factory: (CONT.(S) -> CONTENT)
) : Widget(setOf()) {
private val unsubscribe: () -> Unit
init {
unsubscribe = observableState.subscribe { update(it) }
}
private var updateState: ((S, CONTENT) -> Unit)? = null
private var content: CONTENT? = null
/**
* Updates view based on the current state.
*/
@Suppress("ComplexMethod")
fun update(state: S) {
singleRender {
if (updateState == null || content == null) {
container.removeAll()
container.add(this)
content = container.factory(state)
} else {
content?.let {
updateState?.invoke(state, it)
}
}
}
}
private fun setUpdateState(updateState: (S, CONTENT) -> Unit) {
this.updateState = updateState
}
override fun dispose() {
unsubscribe()
super.dispose()
}
companion object {
/**
* DSL builder extension function.
*
* It takes the same parameters as the constructor of the built component.
*/
fun <S : Any, CONT : Container> CONT.stateBinding(
observableState: ObservableState<S>,
factory: (CONT.(S) -> Unit)
): StateBinding<S, CONT, Unit> {
return StateBinding(observableState, this, factory)
}
/**
* DSL builder extension function for updateable redux content.
*
* It takes the same parameters as the constructor of the built component.
*/
fun <S : Any, CONT : Container, CONTENT> CONT.stateUpdate(
observableState: ObservableState<S>,
factory: (CONT.(S) -> CONTENT)
): Updateable<S, CONTENT> {
return Updateable(StateBinding(observableState, this, factory)::setUpdateState)
}
}
}
/**
* A helper class for updateable content.
*/
class Updateable<S : Any, CONTENT>(private val setUpdateState: ((S, CONTENT) -> Unit) -> Unit) {
infix fun updateWith(updateState: (S, CONTENT) -> Unit) {
setUpdateState(updateState)
}
}
|