Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit 70d4574

Browse files
committed
#309 fix usage of moko-fields
1 parent 2a4a7b1 commit 70d4574

File tree

8 files changed

+39
-42
lines changed

8 files changed

+39
-42
lines changed

widgets-media/src/iosMain/kotlin/dev/icerock/moko/widgets/media/MediaControllerExt.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,4 @@ actual fun Screen<*>.createMediaPickerController(permissionsController: Permissi
1616
)
1717
}
1818

19-
actual fun MediaPickerController.bind(screen: Screen<*>) {
20-
// nothing todo - bind only for android
21-
}
19+
actual fun MediaPickerController.bind(screen: Screen<*>) = Unit

widgets/src/androidMain/kotlin/dev/icerock/moko/widgets/core/factory/SystemSingleChoiceViewFactory.kt

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,16 @@ actual class SystemSingleChoiceViewFactory actual constructor(
129129
spinner.setPopupBackgroundDrawable(it.buildBackground(context))
130130
}
131131

132-
// TODO fixme
133-
// widget.field.data.mergeWith(widget.values) { index, values ->
134-
// if (index == null) null
135-
// else values[index]
136-
// }.bind(lifecycleOwner) { stringDesc ->
137-
// val string = stringDesc?.toString(context)
138-
//
139-
// if (editText.text?.toString() == string) return@bind
140-
//
141-
// editText.setText(string)
142-
// }
132+
widget.field.data.mergeWith(widget.values) { index, values ->
133+
if (index == null) null
134+
else values[index]
135+
}.bind(lifecycleOwner) { stringDesc ->
136+
val string = stringDesc?.toString(context)
137+
138+
if (editText.text?.toString() == string) return@bind
139+
140+
editText.setText(string)
141+
}
143142
widget.field.observeError(lifecycleOwner) { error ->
144143
textInputLayout.error = error?.toString(context)
145144
textInputLayout.isErrorEnabled = error != null

widgets/src/commonMain/kotlin/dev/icerock/moko/widgets/core/widget/InputWidget.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
package dev.icerock.moko.widgets.core.widget
66

7-
import dev.icerock.moko.fields.core.FormField
7+
import dev.icerock.moko.fields.livedata.FormField
88
import dev.icerock.moko.mvvm.livedata.LiveData
99
import dev.icerock.moko.resources.desc.StringDesc
1010
import dev.icerock.moko.widgets.core.RequireId

widgets/src/commonMain/kotlin/dev/icerock/moko/widgets/core/widget/SingleChoiceWidget.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
package dev.icerock.moko.widgets.core.widget
66

7-
import dev.icerock.moko.fields.core.FormField
7+
import dev.icerock.moko.fields.livedata.FormField
88
import dev.icerock.moko.mvvm.livedata.LiveData
99
import dev.icerock.moko.resources.desc.StringDesc
1010
import dev.icerock.moko.widgets.core.RequireId

widgets/src/iosMain/kotlin/dev/icerock/moko/widgets/core/factory/BaseInputViewFactory.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
package dev.icerock.moko.widgets.core.factory
66

7-
import dev.icerock.moko.fields.core.FormField
7+
import dev.icerock.moko.fields.livedata.FormField
88
import dev.icerock.moko.mvvm.livedata.LiveData
9+
import dev.icerock.moko.mvvm.utils.bind
910
import dev.icerock.moko.resources.desc.StringDesc
1011
import dev.icerock.moko.widgets.core.ViewBundle
1112
import dev.icerock.moko.widgets.core.ViewFactory
1213
import dev.icerock.moko.widgets.core.ViewFactoryContext
1314
import dev.icerock.moko.widgets.core.style.view.MarginValues
1415
import dev.icerock.moko.widgets.core.style.view.WidgetSize
15-
import dev.icerock.moko.widgets.core.utils.bind
1616
import dev.icerock.moko.widgets.core.utils.setEventHandler
1717
import dev.icerock.moko.widgets.core.widget.InputWidget
1818
import platform.Foundation.NSMakeRange
@@ -50,33 +50,33 @@ abstract class BaseInputViewFactory<V : UIView> : ViewFactory<InputWidget<out Wi
5050
rootView: V,
5151
textField: UITextField
5252
) {
53-
enabled?.bind { textField.enabled = it }
53+
if (enabled == null) return
54+
textField.bind(enabled) { this.enabled = it }
5455
}
5556

5657
protected open fun bindLabel(
5758
label: LiveData<StringDesc>,
5859
rootView: V,
5960
textField: UITextField
6061
) {
61-
label.bind { textField.placeholder = it.localized() }
62+
textField.bind(label) { this.placeholder = it.localized() }
6263
}
6364

6465
protected open fun bindFieldToTextField(
6566
field: FormField<String, StringDesc>,
6667
rootView: V,
6768
textField: UITextField
6869
) {
69-
// TODO check leaks?
70-
field.observeData { newValue ->
71-
val currentText = textField.text.orEmpty()
72-
val shouldApplyChange = textField.delegate?.run {
70+
textField.bind(field.data) { newValue ->
71+
val currentText = this.text.orEmpty()
72+
val shouldApplyChange = this.delegate?.run {
7373
textField(
74-
textField,
74+
this@bind,
7575
NSMakeRange(0.toULong(), currentText.length.toULong()), newValue
7676
)
7777
} ?: true
7878
if (shouldApplyChange) {
79-
textField.text = newValue
79+
this.text = newValue
8080
}
8181
}
8282

widgets/src/iosMain/kotlin/dev/icerock/moko/widgets/core/factory/FloatingLabelInputViewFactory.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
package dev.icerock.moko.widgets.core.factory
66

7-
import dev.icerock.moko.fields.core.FormField
7+
import dev.icerock.moko.fields.livedata.FormField
88
import dev.icerock.moko.graphics.Color
99
import dev.icerock.moko.graphics.toUIColor
1010
import dev.icerock.moko.mvvm.livedata.LiveData
@@ -125,15 +125,14 @@ actual class FloatingLabelInputViewFactory actual constructor(
125125
textField: UITextField
126126
) {
127127
super.bindFieldToTextField(field, rootView, textField)
128-
// TODO check leaks?
129-
field.observeData {
130-
if (!textField.isEditing()) {
128+
129+
textField.bind(field.data) {
130+
if (!isEditing()) {
131131
rootView.layoutPlaceholder()
132132
}
133133
}
134-
// TODO check leaks?
135-
field.observeError {
136-
rootView.error = it?.localized()
134+
rootView.bind(field.error) {
135+
this.error = it?.localized()
137136
}
138137
}
139138

widgets/src/iosMain/kotlin/dev/icerock/moko/widgets/core/factory/InputLengthViewFactory.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@ actual open class InputLengthViewFactory : ViewFactory<InputLengthWidget<out Wid
1919
): ViewBundle<WS> {
2020
val bundle = widget.child.buildView(viewFactoryContext) as ViewBundle<WS>
2121

22-
// TODO fixme
23-
// widget.maxLength.mergeWith(widget.child.field.data) { maxLength, userInput ->
24-
// if (maxLength != null && userInput.length > maxLength) {
25-
// widget.child.field.data.value = userInput.take(maxLength)
26-
// }
27-
// }
22+
widget.maxLength.mergeWith(widget.child.field.data) { maxLength, userInput ->
23+
if (maxLength != null && userInput.length > maxLength) {
24+
widget.child.field.data.value = userInput.take(maxLength)
25+
}
26+
}
2827

2928
return bundle
3029
}

widgets/src/iosMain/kotlin/dev/icerock/moko/widgets/core/factory/MultilineInputViewFactory.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package dev.icerock.moko.widgets.core.factory
66

77
import dev.icerock.moko.graphics.Color
88
import dev.icerock.moko.graphics.toUIColor
9+
import dev.icerock.moko.mvvm.utils.bind
910
import dev.icerock.moko.resources.desc.StringDesc
1011
import dev.icerock.moko.widgets.core.ViewBundle
1112
import dev.icerock.moko.widgets.core.ViewFactory
@@ -91,9 +92,10 @@ actual class MultilineInputViewFactory actual constructor(
9192
}
9293
}
9394

94-
widget.enabled?.bind { textView.editable = it }
95-
// TODO check leaks?
96-
widget.field.observeData { textView.text = it }
95+
widget.enabled?.let { enabled ->
96+
textView.bind(enabled) { this.editable = it }
97+
}
98+
textView.bind(widget.field.data) { this.text = it }
9799

98100
val nc = NSNotificationCenter.defaultCenter
99101
val observer = TextViewObserver(

0 commit comments

Comments
 (0)