Skip to content

Commit

Permalink
Update State example (#192)
Browse files Browse the repository at this point in the history
* Fix Transaction.current crash issue

* Add StateTests case
  • Loading branch information
Kyle-Ye authored Feb 5, 2025
1 parent d0603f6 commit 9ead128
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 37 deletions.
45 changes: 23 additions & 22 deletions Example/HostingExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,29 @@ struct ContentView: View {
@State private var first = true

var body: some View {
if first {
Color.red
.onAppear {
print("Red appear")
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
first.toggle()
}
// if first {
Color(uiColor: first ? .red : .blue)
.onAppear {
print("View appear")
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
first.toggle()
}
.onDisappear {
print("Red disappear")
}
} else {
Color.blue
.onAppear {
print("Blue appear")
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
first.toggle()
}
}
.onDisappear {
print("Blue disappear")
}
}
}
// .id(first)
// .onDisappear {
// print("Red disappear")
// }
// } else {
// Color.blue
// .onAppear {
// print("Blue appear")
// DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
// first.toggle()
// }
// }
// .onDisappear {
// print("Blue disappear")
// }
// }
}
}
4 changes: 2 additions & 2 deletions Sources/OpenSwiftUICore/Data/Transaction/Transaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ public struct Transaction {
}

package static var current: Transaction {
if let data = _threadTransactionData() as? AnyObject {
Transaction(plist: PropertyList(data: data))
if let data = _threadTransactionData() {
Transaction(plist: PropertyList(data: data as? AnyObject))
} else {
Transaction()
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenSwiftUICore/Graph/GraphHost.swift
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ extension GraphHost {
style: _GraphMutation_Style = .deferred,
mayDeferUpdate: Bool = true
) where T: GraphMutation {
preconditionFailure("TODO")
// preconditionFailure("TODO")
}

package final func asyncTransaction(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
//
// EnvironmentValuesTest.swift
//
//
// Created by Kyle on 2023/11/21.
//
// EnvironmentValuesTests.swift
// OpenSwiftUICompatibilityTests

import Testing

struct EnvironmentValuesTest {
struct EnvironmentValuesTests {
struct BoolKey: EnvironmentKey {
fileprivate static var name: String { "EnvironmentPropertyKey<BoolKey>" }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// StateTests.swift
// OpenSwiftUICompatibilityTests

import Testing

#if canImport(Darwin)
struct StateTests {
@Test
func appear() async throws {
struct ContentView: View {
var confirmation: Confirmation

@State private var toggle = false

var body: some View {
AnyView(EmptyView())
.onAppear {
toggle.toggle()
if toggle {
confirmation()
}
}
}
}

#if os(iOS)
await confirmation { @MainActor confirmation in
let vc = UIHostingController(rootView: ContentView(confirmation: confirmation))
vc.triggerLayout()
workaroundIssue87(vc)
}
#endif
}

// TODO: Add disappear support and test case
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ import Testing
struct AppearanceActionModifierTests {
@Test
func appear() async throws {
guard #unavailable(iOS 18) else {
withKnownIssue {
Issue.record("Known crash issue on iOS 18")
}
return
}
struct ContentView: View {
var confirmation: Confirmation

Expand Down

0 comments on commit 9ead128

Please sign in to comment.