Skip to content

Commit

Permalink
Fix bug in Observing SwiftUI view.
Browse files Browse the repository at this point in the history
We incorrectly used ObservedObject instead of StateObject.
  • Loading branch information
TadeasKriz committed Oct 2, 2024
1 parent ea66ad0 commit a4c53d5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ object SupportedFlowRuntimeGenerator {
SirClass(
baseName = "SkieSwiftFlowProtocol",
kind = SirClass.Kind.Protocol,
superTypes = listOf(sirBuiltins._Concurrency.AsyncSequence.defaultType),
superTypes = listOf(sirBuiltins._Concurrency.AsyncSequence.defaultType, sirBuiltins.Swift.AnyObject.defaultType),
).apply {
val elementTypeParameter = SirTypeParameter(
name = "Element",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ object SwiftUIFlowObservingGenerator {
type = skieSwiftFlowObserver.defaultType,
visibility = SirVisibility.Private,
attributes = listOf(
"SwiftUI.ObservedObject"
"SwiftUI.StateObject"
),
isMutable = true,
)
Expand All @@ -659,7 +659,10 @@ object SwiftUIFlowObservingGenerator {

bodyBuilder.add {
addStatement("self.content = content")
addStatement("self.observer = %T(flows: flows)", skieSwiftFlowObserver.defaultType.evaluate().swiftPoetTypeName)
addStatement(
"self._observer = SwiftUI.StateObject(wrappedValue: %T(flows: flows))",
skieSwiftFlowObserver.defaultType.evaluate().swiftPoetTypeName,
)
}
}

Expand Down Expand Up @@ -780,6 +783,11 @@ object SwiftUIFlowObservingGenerator {
type = sirBuiltins.Swift.Array.toType(sirBuiltins.Skie.SkieSwiftFlowProtocol.defaultType.toExistential()),
)

SirProperty(
identifier = "flowIds",
type = sirBuiltins.Swift.Array.toType(sirBuiltins.Swift.ObjectIdentifier.defaultType),
)

SirProperty(
identifier = "initialContent",
type = initialContentFactoryType.copy(isEscaping = false),
Expand Down Expand Up @@ -812,6 +820,11 @@ object SwiftUIFlowObservingGenerator {
type = sirBuiltins.Swift.Array.toType(sirBuiltins.Skie.SkieSwiftFlowProtocol.defaultType.toExistential()),
)

SirValueParameter(
name = "flowIds",
type = sirBuiltins.Swift.Array.toType(sirBuiltins.Swift.ObjectIdentifier.defaultType),
)

SirValueParameter(
name = "initialContent",
type = initialContentFactoryType,
Expand All @@ -832,6 +845,7 @@ object SwiftUIFlowObservingGenerator {
bodyBuilder.add {
addCode("""
self.flows = flows
self.flowIds = flowIds
self.initialContent = initialContent
self.content = content
self.extractValues = extractValues
Expand All @@ -852,6 +866,7 @@ object SwiftUIFlowObservingGenerator {
initialContent()
}
}
.id(flowIds)
""".trimIndent())
}
}
Expand Down Expand Up @@ -950,6 +965,7 @@ object SwiftUIFlowObservingGenerator {
+"self.init("
indented {
+"flows: [${flowValueParameters.joinToString { it.name }}],\n"
+"flowIds: [${flowValueParameters.joinToString { "Swift.ObjectIdentifier(${it.name})" }}],\n"
+"initialContent: initialContent,\n"
+"content: content\n"
}
Expand Down Expand Up @@ -1054,6 +1070,7 @@ object SwiftUIFlowObservingGenerator {
+"self.init("
indented {
+"flows: [${flowValueParameters.joinToString { "${it.name}.flow" }}],\n"
+"flowIds: [${flowValueParameters.joinToString { "Swift.ObjectIdentifier(${it.name}.flow)" }}],\n"
+"initialContent: SwiftUI.EmptyView.init,\n"
+"content: content\n"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class SirBuiltins(

val Void by Struct()
val Never by Struct()
val ObjectIdentifier by Struct()

val UnsafeMutableRawPointer by Struct(superTypes = listOf(Hashable.defaultType))

Expand Down

0 comments on commit a4c53d5

Please sign in to comment.