|
| 1 | +// |
| 2 | +// FB13341321.swift |
| 3 | +// Test |
| 4 | +// |
| 5 | +// Created by Kyle on 2023/11/6. |
| 6 | +// |
| 7 | + |
| 8 | +// (4 toggles: C1T1 C1T2 C2T1 C2T2 - C1T1&C2T1 use the same truth, C1T2&C2T2 use the same truth ) |
| 9 | +// macOS 14 behavior with SwiftUI |
| 10 | +// C1T1's UI will only be updated at most 1 time if we only tap T1. |
| 11 | +// C2T1 will always reflect the latest value in UI. A tap for T2 will make C1T1's UI up to data. |
| 12 | +// Tap on C1T2 or C2T2 will update both toggle at the same time. (Expected) |
| 13 | +// |
| 14 | +// iOS 17 behavior with SwiftUI |
| 15 | +// Tap on C2T1 will only update toggle C2T1 while the UI of C1T1 remains the same. |
| 16 | +// Tap on C1T1 will update both toggle(C1T1 & C2T1) at the same time. (Expected) |
| 17 | +// Tap on C1T2 or C2T2 will update both toggle at the same time. (Expected) |
| 18 | +// |
| 19 | +// iOS 15.5 behavior with SwiftUI |
| 20 | +// Tap on C1T1 or C2T1 will update both toggle at the same time. (Expected) |
| 21 | +// Tap on C1T2 or C2T2 will update both toggle at the same time. (Expected) |
| 22 | +// But one is with transactino and the other is not. |
| 23 | + |
| 24 | +import SwiftUI |
| 25 | +import Observation |
| 26 | + |
| 27 | +@available(iOS 14, *) |
| 28 | +enum FB13341321 { |
| 29 | + public struct Section: View { |
| 30 | + |
| 31 | + public let content: AnyView |
| 32 | + |
| 33 | + public init( |
| 34 | + @ViewBuilder content: @escaping () -> some View |
| 35 | + ) { |
| 36 | + |
| 37 | + self.content = content().eraseToAnyView() |
| 38 | + } |
| 39 | + |
| 40 | + public var body: some View { |
| 41 | + content |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + @resultBuilder |
| 46 | + public struct SectionBuilder { |
| 47 | + public static func buildBlock(_ sections: Section...) -> [Section] { |
| 48 | + sections |
| 49 | + } |
| 50 | + } |
| 51 | + public struct Container: View { |
| 52 | + private let builder: () -> [Section] |
| 53 | + |
| 54 | + public init( |
| 55 | + @SectionBuilder builder: @escaping () -> [Section] |
| 56 | + ) { |
| 57 | + self.builder = builder |
| 58 | + } |
| 59 | + |
| 60 | + public var body: some View { |
| 61 | + let sections = builder() |
| 62 | + return sections[0] |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public struct Container2: View { |
| 67 | + private let sections: [Section] |
| 68 | + |
| 69 | + public init( |
| 70 | + sections: [Section] = [] |
| 71 | + ) { |
| 72 | + self.sections = sections |
| 73 | + } |
| 74 | + |
| 75 | + public var body: some View { |
| 76 | + return sections[0] |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + struct ContentView: View { |
| 81 | + @AppStorage("Test") private var toggle = false |
| 82 | + @State private var toggle2 = false |
| 83 | + var body: some View { |
| 84 | + Container { |
| 85 | + Section { |
| 86 | + Toggle("Demo Toggle", isOn: $toggle) |
| 87 | + Toggle("Demo Toggle2", isOn: $toggle2) |
| 88 | + } |
| 89 | + } |
| 90 | + Container2(sections: [ |
| 91 | + Section { |
| 92 | + Toggle("Demo Toggle", isOn: $toggle) |
| 93 | + Toggle("Demo Toggle2", isOn: $toggle2) |
| 94 | + } |
| 95 | + ]) |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +extension View { |
| 101 | + func eraseToAnyView() -> AnyView { |
| 102 | + AnyView(self) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +@available(iOS 14, *) |
| 107 | +#Preview { |
| 108 | + FB13341321.ContentView() |
| 109 | + .padding(20) |
| 110 | +} |
0 commit comments