aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin
diff options
context:
space:
mode:
authorRobert Jaros <rjaros@finn.pl>2019-10-13 00:04:39 +0200
committerRobert Jaros <rjaros@finn.pl>2019-10-13 00:04:39 +0200
commit32551e8918b22762c067a895382a4faee53b9003 (patch)
treee0722fdd5a6c01079cfbf22ccba38a3e21a779b9 /src/main/kotlin
parent6ea600defefbe16e59c421785d9a09989e672083 (diff)
downloadkvision-32551e8918b22762c067a895382a4faee53b9003.tar.gz
kvision-32551e8918b22762c067a895382a4faee53b9003.tar.bz2
kvision-32551e8918b22762c067a895382a4faee53b9003.zip
Refactor StateBinding class with ObservableState interface.
Diffstat (limited to 'src/main/kotlin')
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/state/ObservableState.kt32
-rw-r--r--src/main/kotlin/pl/treksoft/kvision/state/StateBinding.kt88
2 files changed, 120 insertions, 0 deletions
diff --git a/src/main/kotlin/pl/treksoft/kvision/state/ObservableState.kt b/src/main/kotlin/pl/treksoft/kvision/state/ObservableState.kt
new file mode 100644
index 00000000..9dc9af0d
--- /dev/null
+++ b/src/main/kotlin/pl/treksoft/kvision/state/ObservableState.kt
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2017-present Robert Jaros
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+package pl.treksoft.kvision.state
+
+/**
+ * An interface of observable state.
+ */
+interface ObservableState<S> {
+ /**
+ * Subscribe for the state change notifications.
+ */
+ fun subscribe(observer: (S) -> Unit)
+}
diff --git a/src/main/kotlin/pl/treksoft/kvision/state/StateBinding.kt b/src/main/kotlin/pl/treksoft/kvision/state/StateBinding.kt
new file mode 100644
index 00000000..7df82adc
--- /dev/null
+++ b/src/main/kotlin/pl/treksoft/kvision/state/StateBinding.kt
@@ -0,0 +1,88 @@
+/*
+ * 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()) {
+
+ init {
+ 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
+ }
+
+ 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)
+ }
+}