Skip to content

Commit

Permalink
Fix support for setting default flag values
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerzer committed Dec 24, 2021
1 parent 23848ff commit a4ea13c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 27 deletions.
31 changes: 4 additions & 27 deletions Sources/OnboardingKit/OnboardingEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,6 @@ public protocol OnboardingEventProtocol {
/// - Warning: Don’t call this method yourself.
func check()

/// Attempts to set the specified flag to a default value.
///
/// This method should fail silently if no default value can be inferred.
/// - Parameter keyPath: The key path at which to set the default value.
/// - Warning: Don’t call this method yourself.
func setDefaultValue<Flags, Value>(at keyPath: ReferenceWritableKeyPath<Flags, Value>)

}

extension OnboardingEventProtocol {

public func setDefaultValue<Flags, Value>(at: ReferenceWritableKeyPath<Flags, Value>) { }

}

/// An event that occurs when all of its constituent conditions are satisfied.
Expand Down Expand Up @@ -103,8 +90,10 @@ public final class OnboardingEvent<Flags, Value>: OnboardingEventProtocol where
if let keyPath = self.keyPath {
self.flags[keyPath: keyPath] = self.value
}
} else if let keyPath = self.keyPath {
self.setDefaultValue(at: keyPath)
} else if let keyPath = self.keyPath as? ReferenceWritableKeyPath<Flags, Bool> {
self.flags[keyPath: keyPath] = false
} else if let keyPath = self.keyPath, let defaultValue = (Value.self as? OptionalProtocol.Type)?.none as? Value {
self.flags[keyPath: keyPath] = defaultValue
}
}

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

func setDefaultValue(at keyPath: ReferenceWritableKeyPath<Flags, Bool>) {
self.flags[keyPath: keyPath] = false
}

}

public extension OnboardingEvent where Value: ExpressibleByNilLiteral {

func setDefaultValue(at keyPath: ReferenceWritableKeyPath<Flags, Value>) {
self.flags[keyPath: keyPath] = nil
}

}
14 changes: 14 additions & 0 deletions Sources/OnboardingKit/OptionalProtocol.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// OptionalProtocol.swift
// OnboardingKit
//
// Created by Gabriel Jacoby-Cooper on 12/23/21.
//

protocol OptionalProtocol {

static var none: Self { get }

}

extension Optional: OptionalProtocol { }

0 comments on commit a4ea13c

Please sign in to comment.