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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
/*
* Copyright (c) 2019. Robert Jaros
*/
package pl.treksoft.kvision.state
import pl.treksoft.kvision.core.Container
import pl.treksoft.kvision.core.Widget
import pl.treksoft.kvision.core.Widget.Companion.bindState
/**
* An extension function which binds the widget to the observable state.
*
* @param S the state type
* @param W the widget type
* @param observableState the state
* @param factory a function which re-creates the view based on the given state
*/
fun <S : Any, W : Widget> W.bind(
observableState: ObservableState<S>,
factory: (W.(S) -> Unit)
): W {
return this.bindState(observableState, factory)
}
/**
* 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
*/
@Deprecated("Use bind function instead.")
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)
}
}
}
}
internal fun setUpdateState(updateState: (S, CONTENT) -> Unit) {
this.updateState = updateState
}
override fun dispose() {
unsubscribe()
super.dispose()
}
}
/**
* DSL builder extension function.
*
* It takes the same parameters as the constructor of the built component.
*/
@Suppress("DEPRECATION")
@Deprecated(
"Use bind function instead.",
replaceWith = ReplaceWith("bind(observableState, factory)", "pl.treksoft.kvision.state.bind")
)
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.
*/
@Suppress("DEPRECATION")
@Deprecated("Use bind function instead.")
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.
*/
@Deprecated("Use bind function instead.")
class Updateable<S : Any, CONTENT>(private val setUpdateState: ((S, CONTENT) -> Unit) -> Unit) {
infix fun updateWith(updateState: (S, CONTENT) -> Unit) {
setUpdateState(updateState)
}
}
|