@@ -8,6 +8,7 @@ import dev.icerock.moko.mvvm.ResourceState
8
8
import dev.icerock.moko.widgets.core.ViewBundle
9
9
import dev.icerock.moko.widgets.core.ViewFactory
10
10
import dev.icerock.moko.widgets.core.ViewFactoryContext
11
+ import dev.icerock.moko.widgets.core.Widget
11
12
import dev.icerock.moko.widgets.core.style.background.Background
12
13
import dev.icerock.moko.widgets.core.style.background.Fill
13
14
import dev.icerock.moko.widgets.core.style.view.MarginValues
@@ -18,6 +19,7 @@ import dev.icerock.moko.widgets.core.utils.applyBackgroundIfNeeded
18
19
import dev.icerock.moko.widgets.core.utils.applySizeToChild
19
20
import dev.icerock.moko.widgets.core.utils.fillChildView
20
21
import dev.icerock.moko.widgets.core.widget.StatefulWidget
22
+ import kotlinx.cinterop.ExportObjCClass
21
23
import kotlinx.cinterop.readValue
22
24
import platform.CoreGraphics.CGFloat
23
25
import platform.CoreGraphics.CGRectZero
@@ -27,6 +29,9 @@ import platform.UIKit.addSubview
27
29
import platform.UIKit.hidden
28
30
import platform.UIKit.translatesAutoresizingMaskIntoConstraints
29
31
32
+ @ExportObjCClass
33
+ private class StatefulViewContainer : UIView (frame = CGRectZero .readValue())
34
+
30
35
actual class StatefulViewFactory actual constructor(
31
36
private val margins : MarginValues ? ,
32
37
private val padding : PaddingValues ? ,
@@ -40,18 +45,20 @@ actual class StatefulViewFactory actual constructor(
40
45
): ViewBundle <WS > {
41
46
val viewController: UIViewController = viewFactoryContext
42
47
43
- val container = UIView (frame = CGRectZero .readValue() ).apply {
48
+ val container = StatefulViewContainer ( ).apply {
44
49
translatesAutoresizingMaskIntoConstraints = false
45
50
46
51
applyBackgroundIfNeeded(background)
47
52
}
48
53
49
- listOf (
50
- widget.dataWidget,
51
- widget.emptyWidget,
52
- widget.loadingWidget,
53
- widget.errorWidget
54
- ).forEach { childWidget ->
54
+ val widgetStateMap: Map <Widget <* >, (ResourceState <* , * >) -> Boolean > = mapOf (
55
+ widget.dataWidget to { it is ResourceState .Success },
56
+ widget.emptyWidget to { it is ResourceState .Empty },
57
+ widget.loadingWidget to { it is ResourceState .Loading },
58
+ widget.errorWidget to { it is ResourceState .Failed }
59
+ )
60
+
61
+ widgetStateMap.forEach { (childWidget, isRelatedState) ->
55
62
val childViewBundle = childWidget.buildView(viewController)
56
63
val childView = childViewBundle.view
57
64
childView.translatesAutoresizingMaskIntoConstraints = false
@@ -72,12 +79,7 @@ actual class StatefulViewFactory actual constructor(
72
79
}
73
80
74
81
fun updateState (state : ResourceState <* , * >) {
75
- childView.hidden = when (state) {
76
- is ResourceState .Success -> childWidget == widget.dataWidget
77
- is ResourceState .Empty -> childWidget == widget.emptyWidget
78
- is ResourceState .Failed -> childWidget == widget.errorWidget
79
- is ResourceState .Loading -> childWidget == widget.loadingWidget
80
- }.not ()
82
+ childView.hidden = isRelatedState(state).not ()
81
83
}
82
84
83
85
updateState(widget.state.value)
0 commit comments