Skip to content

Commit 9ead128

Browse files
authored
Update State example (#192)
* Fix Transaction.current crash issue * Add StateTests case
1 parent d0603f6 commit 9ead128

File tree

6 files changed

+67
-37
lines changed

6 files changed

+67
-37
lines changed

Example/HostingExample/ViewController.swift

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,28 +47,29 @@ struct ContentView: View {
4747
@State private var first = true
4848

4949
var body: some View {
50-
if first {
51-
Color.red
52-
.onAppear {
53-
print("Red appear")
54-
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
55-
first.toggle()
56-
}
50+
// if first {
51+
Color(uiColor: first ? .red : .blue)
52+
.onAppear {
53+
print("View appear")
54+
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
55+
first.toggle()
5756
}
58-
.onDisappear {
59-
print("Red disappear")
60-
}
61-
} else {
62-
Color.blue
63-
.onAppear {
64-
print("Blue appear")
65-
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
66-
first.toggle()
67-
}
68-
}
69-
.onDisappear {
70-
print("Blue disappear")
71-
}
72-
}
57+
}
58+
// .id(first)
59+
// .onDisappear {
60+
// print("Red disappear")
61+
// }
62+
// } else {
63+
// Color.blue
64+
// .onAppear {
65+
// print("Blue appear")
66+
// DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
67+
// first.toggle()
68+
// }
69+
// }
70+
// .onDisappear {
71+
// print("Blue disappear")
72+
// }
73+
// }
7374
}
7475
}

Sources/OpenSwiftUICore/Data/Transaction/Transaction.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ public struct Transaction {
9191
}
9292

9393
package static var current: Transaction {
94-
if let data = _threadTransactionData() as? AnyObject {
95-
Transaction(plist: PropertyList(data: data))
94+
if let data = _threadTransactionData() {
95+
Transaction(plist: PropertyList(data: data as? AnyObject))
9696
} else {
9797
Transaction()
9898
}

Sources/OpenSwiftUICore/Graph/GraphHost.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ extension GraphHost {
371371
style: _GraphMutation_Style = .deferred,
372372
mayDeferUpdate: Bool = true
373373
) where T: GraphMutation {
374-
preconditionFailure("TODO")
374+
// preconditionFailure("TODO")
375375
}
376376

377377
package final func asyncTransaction(

Tests/OpenSwiftUICompatibilityTests/SwiftUICore/Data/Environment/EnvironmentValuesTest.swift renamed to Tests/OpenSwiftUICompatibilityTests/SwiftUICore/Data/Environment/EnvironmentValuesTests.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
//
2-
// EnvironmentValuesTest.swift
3-
//
4-
//
5-
// Created by Kyle on 2023/11/21.
6-
//
2+
// EnvironmentValuesTests.swift
3+
// OpenSwiftUICompatibilityTests
74

85
import Testing
96

10-
struct EnvironmentValuesTest {
7+
struct EnvironmentValuesTests {
118
struct BoolKey: EnvironmentKey {
129
fileprivate static var name: String { "EnvironmentPropertyKey<BoolKey>" }
1310

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// StateTests.swift
3+
// OpenSwiftUICompatibilityTests
4+
5+
import Testing
6+
7+
#if canImport(Darwin)
8+
struct StateTests {
9+
@Test
10+
func appear() async throws {
11+
struct ContentView: View {
12+
var confirmation: Confirmation
13+
14+
@State private var toggle = false
15+
16+
var body: some View {
17+
AnyView(EmptyView())
18+
.onAppear {
19+
toggle.toggle()
20+
if toggle {
21+
confirmation()
22+
}
23+
}
24+
}
25+
}
26+
27+
#if os(iOS)
28+
await confirmation { @MainActor confirmation in
29+
let vc = UIHostingController(rootView: ContentView(confirmation: confirmation))
30+
vc.triggerLayout()
31+
workaroundIssue87(vc)
32+
}
33+
#endif
34+
}
35+
36+
// TODO: Add disappear support and test case
37+
}
38+
#endif

Tests/OpenSwiftUICompatibilityTests/SwiftUICore/Modifier/ViewModifier/AppearanceActionModifierTests.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ import Testing
88
struct AppearanceActionModifierTests {
99
@Test
1010
func appear() async throws {
11-
guard #unavailable(iOS 18) else {
12-
withKnownIssue {
13-
Issue.record("Known crash issue on iOS 18")
14-
}
15-
return
16-
}
1711
struct ContentView: View {
1812
var confirmation: Confirmation
1913

0 commit comments

Comments
 (0)