Skip to content

Commit a4ea13c

Browse files
committed
Fix support for setting default flag values
1 parent 23848ff commit a4ea13c

2 files changed

Lines changed: 18 additions & 27 deletions

File tree

Sources/OnboardingKit/OnboardingEvent.swift

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,6 @@ public protocol OnboardingEventProtocol {
2020
/// - Warning: Don’t call this method yourself.
2121
func check()
2222

23-
/// Attempts to set the specified flag to a default value.
24-
///
25-
/// This method should fail silently if no default value can be inferred.
26-
/// - Parameter keyPath: The key path at which to set the default value.
27-
/// - Warning: Don’t call this method yourself.
28-
func setDefaultValue<Flags, Value>(at keyPath: ReferenceWritableKeyPath<Flags, Value>)
29-
30-
}
31-
32-
extension OnboardingEventProtocol {
33-
34-
public func setDefaultValue<Flags, Value>(at: ReferenceWritableKeyPath<Flags, Value>) { }
35-
3623
}
3724

3825
/// An event that occurs when all of its constituent conditions are satisfied.
@@ -103,8 +90,10 @@ public final class OnboardingEvent<Flags, Value>: OnboardingEventProtocol where
10390
if let keyPath = self.keyPath {
10491
self.flags[keyPath: keyPath] = self.value
10592
}
106-
} else if let keyPath = self.keyPath {
107-
self.setDefaultValue(at: keyPath)
93+
} else if let keyPath = self.keyPath as? ReferenceWritableKeyPath<Flags, Bool> {
94+
self.flags[keyPath: keyPath] = false
95+
} else if let keyPath = self.keyPath, let defaultValue = (Value.self as? OptionalProtocol.Type)?.none as? Value {
96+
self.flags[keyPath: keyPath] = defaultValue
10897
}
10998
}
11099

@@ -130,16 +119,4 @@ public extension OnboardingEvent where Value == Bool {
130119
self.init(flags: flags, settingFlagAt: keyPath, to: true, conditions: conditions)
131120
}
132121

133-
func setDefaultValue(at keyPath: ReferenceWritableKeyPath<Flags, Bool>) {
134-
self.flags[keyPath: keyPath] = false
135-
}
136-
137-
}
138-
139-
public extension OnboardingEvent where Value: ExpressibleByNilLiteral {
140-
141-
func setDefaultValue(at keyPath: ReferenceWritableKeyPath<Flags, Value>) {
142-
self.flags[keyPath: keyPath] = nil
143-
}
144-
145122
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// OptionalProtocol.swift
3+
// OnboardingKit
4+
//
5+
// Created by Gabriel Jacoby-Cooper on 12/23/21.
6+
//
7+
8+
protocol OptionalProtocol {
9+
10+
static var none: Self { get }
11+
12+
}
13+
14+
extension Optional: OptionalProtocol { }

0 commit comments

Comments
 (0)