-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreferencePlaygrounds.swift
More file actions
53 lines (45 loc) · 1.69 KB
/
Copy pathPreferencePlaygrounds.swift
File metadata and controls
53 lines (45 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#if canImport(AndroidSwiftUI)
import AndroidSwiftUI
#else
import SwiftUI
#endif
/// Keeps the largest value any descendant published.
struct WidestKey: PreferenceKey {
static var defaultValue: Double { 0 }
static func reduce(value: inout Double, nextValue: () -> Double) {
value = max(value, nextValue())
}
}
/// Gathers every descendant's label, in tree order.
struct RowNamesKey: PreferenceKey {
static var defaultValue: [String] { [] }
static func reduce(value: inout [String], nextValue: () -> [String]) {
value += nextValue()
}
}
struct PreferencePlayground: View {
@State private var rows = 3
@State private var widest = 0.0
@State private var names: [String] = []
var body: some View {
VStack(alignment: .leading, spacing: 14) {
Text("Children publish values; an ancestor reads them reduced.")
VStack(alignment: .leading, spacing: 6) {
ForEach(0..<rows, id: \.self) { index in
Text("row \(index) publishes \((index + 1) * 30)")
.preference(key: WidestKey.self, value: Double((index + 1) * 30))
.preference(key: RowNamesKey.self, value: ["row \(index)"])
}
}
.onPreferenceChange(WidestKey.self) { widest = $0 }
.onPreferenceChange(RowNamesKey.self) { names = $0 }
Divider()
Text("Largest published: \(Int(widest))")
Text("Collected: \(names.joined(separator: ", "))")
Button("Add a row") { rows += 1 }
Button("Remove a row") { if rows > 1 { rows -= 1 } }
}
.padding()
.navigationTitle("Preferences")
}
}