diff --git a/SessionUtilitiesKit/Dependency Injection/FeatureConfig.swift b/SessionUtilitiesKit/Dependency Injection/FeatureConfig.swift index aba18b33d..21cdb296e 100644 --- a/SessionUtilitiesKit/Dependency Injection/FeatureConfig.swift +++ b/SessionUtilitiesKit/Dependency Injection/FeatureConfig.swift @@ -15,6 +15,7 @@ public class FeatureConfig: FeatureStorage { /// `fileprivate` to hide when accessing via `dependencies[feature: ]` fileprivate init( identifier: String, + defaultOption: T, automaticChangeBehaviour: Feature.ChangeBehaviour? ) { self.identifier = identifier @@ -22,7 +23,7 @@ public class FeatureConfig: FeatureStorage { Feature( identifier: identifier, options: Array(T.allCases), - defaultOption: T.defaultOption, + defaultOption: defaultOption, automaticChangeBehaviour: automaticChangeBehaviour ) } @@ -34,10 +35,12 @@ public class FeatureConfig: FeatureStorage { public extension Dependencies { static func create( identifier: String, + defaultOption: T = T.defaultOption, automaticChangeBehaviour: Feature.ChangeBehaviour? = nil ) -> FeatureConfig { return FeatureConfig( identifier: identifier, + defaultOption: defaultOption, automaticChangeBehaviour: automaticChangeBehaviour ) } diff --git a/SessionUtilitiesKit/General/Feature+ServiceNetwork.swift b/SessionUtilitiesKit/General/Feature+ServiceNetwork.swift index 9b04819f0..ff623c43a 100644 --- a/SessionUtilitiesKit/General/Feature+ServiceNetwork.swift +++ b/SessionUtilitiesKit/General/Feature+ServiceNetwork.swift @@ -8,7 +8,8 @@ import Foundation public extension FeatureStorage { static let serviceNetwork: FeatureConfig = Dependencies.create( - identifier: "serviceNetwork" + identifier: "serviceNetwork", + defaultOption: .testnet ) } diff --git a/SessionUtilitiesKit/General/Feature.swift b/SessionUtilitiesKit/General/Feature.swift index 1ceb12609..e752f74f9 100644 --- a/SessionUtilitiesKit/General/Feature.swift +++ b/SessionUtilitiesKit/General/Feature.swift @@ -23,6 +23,7 @@ public extension FeatureStorage { static let updatedGroups: FeatureConfig = Dependencies.create( identifier: "updatedGroups", + defaultOption: true, automaticChangeBehaviour: Feature.ChangeBehaviour( value: true, condition: .after(timestamp: Features.legacyGroupDepricationDate.timeIntervalSince1970) @@ -124,8 +125,16 @@ public struct Feature: FeatureType { // MARK: - Functions internal func currentValue(using dependencies: Dependencies) -> T { - let selectedOption: Int = dependencies[defaults: .appGroup].integer(forKey: identifier) - let maybeSelectedOption: T? = T(rawValue: selectedOption) + let maybeSelectedOption: T? = { + // `Int` defaults to `0` and `Bool` defaults to `false` so rather than those (in case we want + // a default value that isn't `0` or `false` which might be considered valid cases) we check + // if an entry exists and return `nil` if not before retrieving an `Int` representation of + // the value and converting to the desired type + guard dependencies[defaults: .appGroup].object(forKey: identifier) != nil else { return nil } + + let selectedOption: Int = dependencies[defaults: .appGroup].integer(forKey: identifier) + return T(rawValue: selectedOption) + }() /// If we have an explicitly set `selectedOption` then we should use that, otherwise we should check if any of the /// `automaticChangeBehaviour` conditions have been met, and if so use the specified value