Skip to content
Snippets Groups Projects
Select Git revision
  • 294122eafb68acbd904edef7df0588c6dd1a72cc
  • release default
  • 11-22-implement-kv-interface-defined-in-collectiveversionedkvgo
  • master protected
  • XX-4688/DbEncoding
  • hotfix/update
  • @XX-4682/Files
  • hotfix/XX-4655
  • dev protected
  • project/HavenNotifications
  • XX-4602/SilentMessageType
  • jono/npmTest
  • wasmTest2
  • XX-4461/FileUpload
  • XX-4505/blockuser
  • XX-4441
  • Jakub/Emoji-CI-Test
  • testing/websockets
  • fastReg
  • fast-registration
  • NewHostPool
  • v0.3.22
  • v0.3.21
  • v0.3.20
  • v0.3.18
  • v0.3.17
  • v0.3.16
  • v0.3.15
  • v0.3.14
  • v0.3.13
  • v0.3.12
  • v0.3.11
  • v0.3.10
  • v0.3.9
  • v0.3.8
  • v0.3.7
  • v0.3.6
  • v0.3.5
  • v0.3.4
  • 812b395df518ce096d01d5292596ca26f8fe92d9c4487ddfa515e190a51aa1a1
  • 76ba08e2dfa1798412a265404fa271840b52c035869111fce8e8cdb23a036a5a
41 results

utils_test.go

Blame
  • KeyObject.swift 2.41 KiB
    import Foundation
    import DependencyInjection
    
    public enum Key: String {
        // MARK: Profile
    
        case email
        case phone
        case avatar
        case username
    
        case sharingEmail
        case sharingPhone
    
        // MARK: Notifications
    
        case requestCounter
        case pushNotifications
        case inappnotifications
    
        // MARK: General
    
        case theme
        case acceptedTerms
    
        // MARK: Requests
    
        case isShowingHiddenRequests
    
        // MARK: Backup
    
        case backupSettings
    
        // MARK: Settings
    
        case biometrics
        case hideAppList
        case recordingLogs
        case crashReporting
        case icognitoKeyboard
    
        case dummyTrafficOn
        case askedDummyTrafficOnce
    }
    
    public struct KeyObjectStore {
        var objectForKey: (String) -> Any?
        var setObjectForKey: (Any?, String) -> Void
        var removeObjectForKey: (String) -> Void
    
        public init(
            objectForKey: @escaping (String) -> Any?,
            setObjectForKey: @escaping (Any?, String) -> Void,
            removeObjectForKey: @escaping (String) -> Void
        ) {
            self.objectForKey = objectForKey
            self.setObjectForKey = setObjectForKey
            self.removeObjectForKey = removeObjectForKey
        }
    }
    
    public extension KeyObjectStore {
        static func mock(dictionary: NSMutableDictionary) -> Self {
            Self(objectForKey: { dictionary[$0] },
                 setObjectForKey: { dictionary[$1] = $0 },
                 removeObjectForKey: { dictionary[$0] = nil })
        }
    
        static let userDefaults = Self(
            objectForKey: UserDefaults.standard.object(forKey:),
            setObjectForKey: UserDefaults.standard.set(_:forKey:),
            removeObjectForKey: UserDefaults.standard.removeObject(forKey:)
        )
    }
    
    @propertyWrapper
    public struct KeyObject<T> {
        let key: String
        let defaultValue: T
    
        @Dependency var store: KeyObjectStore
    
        public init(_ key: Key, defaultValue: T) {
            self.key = key.rawValue
            self.defaultValue = defaultValue
        }
    
        public var wrappedValue: T {
            get {
                store.objectForKey(key) as? T ?? defaultValue
            }
            set {
                if let value = newValue as? OptionalProtocol, value.isNil() {
                    store.removeObjectForKey(key)
                } else {
                    store.setObjectForKey(newValue, key)
                }
            }
        }
    }
    
    fileprivate protocol OptionalProtocol {
        func isNil() -> Bool
    }
    
    extension Optional : OptionalProtocol {
        func isNil() -> Bool {
            return self == nil
        }
    }