Skip to content

Commit ea17b0a

Browse files
authored
Add PreferenceKeys and update HostPreferencesKey (#35)
* Update gitignore file * Add PreferenceKeys and update Preference folder structure * Implement HostPreferencesKey reduce * Add HostPreferencesKeyTests test case
1 parent d8fb280 commit ea17b0a

File tree

12 files changed

+296
-11
lines changed

12 files changed

+296
-11
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ Example/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
1010
/.build-*
1111
/Benchmarks/.build
1212
/Example/.build
13-
Example/Package.resolved
13+
Example/Package.resolved
14+
TODO.md
File renamed without changes.
File renamed without changes.

Sources/OpenSwiftUI/DataAndStorage/Preferences/Internal/HostPreferencesKey.swift renamed to Sources/OpenSwiftUI/DataAndStorage/Preferences/HostPreferencesKey.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@
44
//
55
// Created by Kyle on 2023/1/6.
66
// Lastest Version: iOS 15.5
7-
// Status: WIP
7+
// Status: Complete
88
// ID: 7429200566949B8FB892A77E01A988C8
99

1010
struct HostPreferencesKey: PreferenceKey {
11-
static var defaultValue: PreferenceList {
12-
PreferenceList()
13-
}
11+
private static var nodeId: UInt32 = .zero
1412

15-
static func reduce(value: inout PreferenceList, nextValue: () -> PreferenceList) {
16-
// TODO:
13+
@inline(__always)
14+
static func makeNodeID() -> UInt32 {
15+
defer { nodeId &+= 1 }
16+
return nodeId
1717
}
18-
19-
private static var nodeId: UInt32 = .zero
2018
}
File renamed without changes.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//
2+
// PreferenceKeys.swift
3+
// OpenSwiftUI
4+
//
5+
// Created by Kyle on 2024/2/2.
6+
// Lastest Version: iOS 15.5
7+
// Status: Complete
8+
9+
struct PreferenceKeys {
10+
private var keys: [AnyPreferenceKey.Type]
11+
12+
init() {
13+
self.keys = []
14+
}
15+
}
16+
17+
extension PreferenceKeys: RandomAccessCollection, MutableCollection {
18+
var startIndex: Int { keys.startIndex }
19+
var endIndex: Int { keys.endIndex }
20+
21+
mutating func add<Key: PreferenceKey>(_: Key.Type) {
22+
keys.append(_AnyPreferenceKey<Key>.self)
23+
}
24+
25+
mutating func add(_ key: AnyPreferenceKey.Type) {
26+
keys.append(key)
27+
}
28+
29+
func contains<Key: PreferenceKey>(_: Key.Type) -> Bool {
30+
contains(_AnyPreferenceKey<Key>.self)
31+
}
32+
33+
func contains(_ key: AnyPreferenceKey.Type) -> Bool {
34+
keys.contains { $0 == key }
35+
}
36+
37+
mutating func remove<Key: PreferenceKey>(_: Key.Type) {
38+
remove(_AnyPreferenceKey<Key>.self)
39+
}
40+
41+
mutating func remove(_ key: AnyPreferenceKey.Type) {
42+
for index in keys.indices {
43+
if keys[index] == key {
44+
keys.remove(at: index)
45+
return
46+
}
47+
}
48+
}
49+
50+
var isEmpty: Bool { keys.isEmpty }
51+
52+
subscript(position: Int) -> AnyPreferenceKey.Type {
53+
get { keys[position] }
54+
set { keys[position] = newValue }
55+
}
56+
}
57+
58+
extension PreferenceKeys: Equatable {
59+
static func == (lhs: PreferenceKeys, rhs: PreferenceKeys) -> Bool {
60+
guard lhs.keys.count == rhs.keys.count else {
61+
return false
62+
}
63+
guard !lhs.keys.isEmpty else {
64+
return true
65+
}
66+
for index in lhs.indices {
67+
guard lhs[index] == rhs[index] else {
68+
return false
69+
}
70+
}
71+
return true
72+
}
73+
}

Sources/OpenSwiftUI/DataAndStorage/Preferences/Internal/PreferenceList.swift renamed to Sources/OpenSwiftUI/DataAndStorage/Preferences/PreferenceList.swift

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// ID: C1C63C2F6F2B9F3EB30DD747F0605FBD
99

1010
struct PreferenceList: CustomStringConvertible {
11-
private var first: PreferenceNode?
11+
fileprivate var first: PreferenceNode?
1212

1313
subscript<Key: PreferenceKey>(_ keyType: Key.Type) -> Value<Key.Value> {
1414
get {
@@ -174,3 +174,34 @@ private class _PreferenceNode<Key: PreferenceKey>: PreferenceNode {
174174
"\(Key.self) = \(value)"
175175
}
176176
}
177+
178+
extension HostPreferencesKey {
179+
static var defaultValue: PreferenceList {
180+
PreferenceList()
181+
}
182+
183+
static func reduce(value: inout PreferenceList, nextValue: () -> PreferenceList) {
184+
let newValue = nextValue()
185+
guard let newFirst = newValue.first else {
186+
return
187+
}
188+
guard let first = value.first else {
189+
value.first = newFirst
190+
return
191+
}
192+
value.first = nil
193+
first.forEach { node in
194+
if let mergedNode = node.combine(from: newFirst, next: value.first) {
195+
value.first = mergedNode
196+
} else {
197+
value.first = node.copy(next: value.first)
198+
}
199+
}
200+
newFirst.forEach { node in
201+
guard node.find(from: first) == nil else {
202+
return
203+
}
204+
value.first = node.copy(next: value.first)
205+
}
206+
}
207+
}
File renamed without changes.
File renamed without changes.

Sources/OpenSwiftUI/DataAndStorage/Preferences/View_Preference.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//
55
// Created by Kyle on 2023/9/24.
66
// Lastest Version: iOS 15.5
7-
// Status: WIP
7+
// Status: Complete
88

99
extension View {
1010
/// Sets a value for the given preference.

0 commit comments

Comments
 (0)