Skip to content

Commit 8073368

Browse files
authored
Remove manual form value listeners in favor of StateFlow collection (#80)
1 parent 7be7298 commit 8073368

3 files changed

Lines changed: 15 additions & 48 deletions

File tree

nubrick/src/main/kotlin/app/nubrick/nubrick/component/provider/event/event.kt

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import androidx.compose.foundation.background
1010
import androidx.compose.foundation.clickable
1111
import androidx.compose.runtime.Composable
1212
import androidx.compose.runtime.CompositionLocalProvider
13-
import androidx.compose.runtime.DisposableEffect
1413
import androidx.compose.runtime.compositionLocalOf
1514
import androidx.compose.runtime.getValue
1615
import androidx.compose.runtime.mutableStateOf
1716
import androidx.compose.runtime.remember
1817
import androidx.compose.runtime.rememberCoroutineScope
1918
import androidx.compose.runtime.setValue
19+
import androidx.lifecycle.compose.collectAsStateWithLifecycle
2020
import androidx.compose.ui.Modifier
2121
import androidx.compose.ui.composed
2222
import androidx.compose.ui.draw.alpha
@@ -26,7 +26,7 @@ import androidx.compose.ui.graphics.Color
2626
import androidx.compose.foundation.interaction.MutableInteractionSource
2727
import app.nubrick.nubrick.component.provider.container.ContainerContext
2828
import app.nubrick.nubrick.component.provider.data.DataContext
29-
import app.nubrick.nubrick.data.FormValueListener
29+
import app.nubrick.nubrick.data.toFormData
3030
import app.nubrick.nubrick.schema.UIBlockAction
3131
import kotlinx.coroutines.launch
3232
import kotlinx.serialization.json.JsonElement
@@ -85,19 +85,11 @@ internal fun Modifier.eventDispatcher(
8585
val interaction = remember { MutableInteractionSource() }
8686

8787
if (event.requiredFields != null) {
88-
val handleFormValueChange: FormValueListener = { values ->
89-
disabled = event.requiredFields.any { key ->
90-
val value = values[key]
91-
value == null || (value is JsonPrimitive && value.isString && value.content.isEmpty())
92-
}
93-
}
94-
DisposableEffect(Unit) {
95-
handleFormValueChange(container.getFormValues())
96-
container.addFormValueListener(handleFormValueChange)
97-
98-
onDispose {
99-
container.removeFormValueListener(handleFormValueChange)
100-
}
88+
val formValues by container.formValuesFlow.collectAsStateWithLifecycle()
89+
val values = formValues.toFormData()
90+
disabled = event.requiredFields.any { key ->
91+
val value = values[key]
92+
value == null || (value is JsonPrimitive && value.isString && value.content.isEmpty())
10193
}
10294
}
10395

nubrick/src/main/kotlin/app/nubrick/nubrick/data/container.kt

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import app.nubrick.nubrick.schema.Property
2121
import app.nubrick.nubrick.schema.UIBlock
2222
import app.nubrick.nubrick.template.compile
2323
import kotlinx.coroutines.Dispatchers
24+
import kotlinx.coroutines.flow.MutableStateFlow
25+
import kotlinx.coroutines.flow.StateFlow
2426
import kotlinx.coroutines.withContext
2527
import kotlinx.serialization.json.JsonElement
2628

@@ -53,11 +55,10 @@ internal interface Container {
5355
pageProperties: List<Property>?,
5456
): JsonElement
5557

58+
val formValuesFlow: StateFlow<Map<String, FormValue>>
5659
fun getFormValues(): Map<String, JsonElement>
5760
fun getFormValue(key: String): FormValue?
5861
fun setFormValue(key: String, value: FormValue)
59-
fun addFormValueListener(listener: FormValueListener)
60-
fun removeFormValueListener(listener: FormValueListener)
6162

6263
fun compileHttpRequest(
6364
req: ApiHttpRequest,
@@ -118,8 +119,8 @@ internal class ContainerImpl(
118119
): JsonElement {
119120
val userState by user.state.collectAsStateWithLifecycle()
120121
val userProperties = userState.templateProperties
121-
val formValues = formRepository?.formValues?.collectAsStateWithLifecycle()
122-
val formData = formValues?.value?.toFormData()
122+
val formValues by formValuesFlow.collectAsStateWithLifecycle()
123+
val formData = formValues.toFormData()
123124
return remember(this, data, pageProperties, userProperties, formData) {
124125
createVariableForTemplate(
125126
data = data,
@@ -132,6 +133,9 @@ internal class ContainerImpl(
132133
}
133134
}
134135

136+
override val formValuesFlow: StateFlow<Map<String, FormValue>> =
137+
formRepository?.formValues ?: MutableStateFlow(emptyMap())
138+
135139
override fun getFormValues(): Map<String, JsonElement> {
136140
return this.formRepository?.getFormData() ?: emptyMap()
137141
}
@@ -144,14 +148,6 @@ internal class ContainerImpl(
144148
this.formRepository?.setValue(key, value)
145149
}
146150

147-
override fun addFormValueListener(listener: FormValueListener) {
148-
this.formRepository?.addListener(listener)
149-
}
150-
151-
override fun removeFormValueListener(listener: FormValueListener) {
152-
this.formRepository?.removeListener(listener)
153-
}
154-
155151
override suspend fun sendHttpRequest(
156152
req: ApiHttpRequest,
157153
variable: JsonElement,

nubrick/src/main/kotlin/app/nubrick/nubrick/data/form.kt

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import kotlinx.coroutines.flow.MutableStateFlow
77
import kotlinx.coroutines.flow.StateFlow
88
import kotlinx.coroutines.flow.asStateFlow
99
import kotlinx.coroutines.flow.update
10-
import java.util.concurrent.CopyOnWriteArraySet
1110

1211
internal sealed class FormValue {
1312
class Bool(val bool: Boolean) : FormValue()
@@ -23,22 +22,16 @@ internal sealed class FormValue {
2322
}
2423
}
2524

26-
typealias FormValueListener = (values: Map<String, JsonElement>) -> Unit
27-
28-
2925
internal interface FormRepository {
3026
val formValues: StateFlow<Map<String, FormValue>>
3127
fun getFormData(): Map<String, JsonElement>
3228
fun setValue(key: String, value: FormValue)
3329
fun getValue(key: String): FormValue?
34-
fun addListener(listener: FormValueListener)
35-
fun removeListener(listener: FormValueListener)
3630
}
3731

3832
internal class FormRepositoryImpl : FormRepository {
3933
private val _formValues = MutableStateFlow<Map<String, FormValue>>(emptyMap())
4034
override val formValues: StateFlow<Map<String, FormValue>> = _formValues.asStateFlow()
41-
private val listeners: MutableSet<FormValueListener> = CopyOnWriteArraySet()
4235

4336
override fun getFormData(): Map<String, JsonElement> {
4437
return this.formValues.value.toFormData()
@@ -50,20 +43,6 @@ internal class FormRepositoryImpl : FormRepository {
5043

5144
override fun setValue(key: String, value: FormValue) {
5245
this._formValues.update { it + (key to value) }
53-
val formData = this._formValues.value.toFormData()
54-
55-
// Listeners to be updated to simply collect StateFlow in future PR
56-
listeners.forEach { listener ->
57-
listener(formData)
58-
}
59-
}
60-
61-
override fun addListener(listener: FormValueListener) {
62-
this.listeners.add(listener)
63-
}
64-
65-
override fun removeListener(listener: FormValueListener) {
66-
this.listeners.remove(listener)
6746
}
6847
}
6948

0 commit comments

Comments
 (0)