DefaultsAdapter.swift 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //
  2. // SwiftyUserDefaults
  3. //
  4. // Copyright (c) 2015-present Radosław Pietruszewski, Łukasz Mróz
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in all
  14. // copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. // SOFTWARE.
  23. //
  24. import Foundation
  25. /// A UserDefaults wrapper. It makes KeyPath dynamicMemberLookup usable with UserDefaults in Swift 5.1 or greater.
  26. /// If Swift 5.0 or less, It works as ordinary SwiftyUserDefaults.
  27. ///
  28. /// - seealso: https://github.com/apple/swift-evolution/blob/master/proposals/0252-keypath-dynamic-member-lookup.md
  29. ///
  30. /// Here is a example:
  31. ///
  32. /// ```
  33. /// extension DefaultsKeys {
  34. /// var launchCount: DefaultsKey<Int> {
  35. /// return .init("launchCount", defaultValue: 0)
  36. /// }
  37. /// }
  38. ///
  39. /// Defaults.launchCount += 1
  40. /// ```
  41. @dynamicMemberLookup
  42. public struct DefaultsAdapter<KeyStore: DefaultsKeyStore> {
  43. public let defaults: UserDefaults
  44. public let keyStore: KeyStore
  45. public init(defaults: UserDefaults, keyStore: KeyStore) {
  46. self.defaults = defaults
  47. self.keyStore = keyStore
  48. }
  49. @available(*, unavailable)
  50. public subscript(dynamicMember member: String) -> Never {
  51. fatalError()
  52. }
  53. public func hasKey<T: DefaultsSerializable>(_ key: DefaultsKey<T>) -> Bool {
  54. return defaults.hasKey(key)
  55. }
  56. public func hasKey<T: DefaultsSerializable>(_ keyPath: KeyPath<KeyStore, DefaultsKey<T>>) -> Bool {
  57. return defaults.hasKey(keyStore[keyPath: keyPath])
  58. }
  59. public func remove<T: DefaultsSerializable>(_ key: DefaultsKey<T>) {
  60. defaults.remove(key)
  61. }
  62. public func remove<T: DefaultsSerializable>(_ keyPath: KeyPath<KeyStore, DefaultsKey<T>>) {
  63. defaults.remove(keyStore[keyPath: keyPath])
  64. }
  65. public func removeAll() {
  66. defaults.removeAll()
  67. }
  68. }