From c38b3c1ec198047fd0ac6b6807d36ef364f703f4 Mon Sep 17 00:00:00 2001 From: Javier Cicchelli Date: Mon, 5 Dec 2022 22:49:15 +0100 Subject: [PATCH 1/4] Created the DependencyService service in the Libraries package. --- Libraries/Package.swift | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Libraries/Package.swift b/Libraries/Package.swift index 99713d6..8e572f0 100644 --- a/Libraries/Package.swift +++ b/Libraries/Package.swift @@ -8,11 +8,16 @@ let package = Package( products: [ .library( name: "Libraries", - targets: ["APIService"]), + targets: [ + "APIService", + "DependencyService" + ] + ), ], dependencies: [], targets: [ .target(name: "APIService"), + .target(name: "DependencyService"), .testTarget( name: "APIServiceTests", dependencies: ["APIService"] From df4ffad68219f234a566f10d0af1980a71db82c3 Mon Sep 17 00:00:00 2001 From: Javier Cicchelli Date: Mon, 5 Dec 2022 22:49:37 +0100 Subject: [PATCH 2/4] Defined the DependencyKey protocol. --- .../Protocols/DependencyKey.swift | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Libraries/Sources/DependencyService/Protocols/DependencyKey.swift diff --git a/Libraries/Sources/DependencyService/Protocols/DependencyKey.swift b/Libraries/Sources/DependencyService/Protocols/DependencyKey.swift new file mode 100644 index 0000000..da0254c --- /dev/null +++ b/Libraries/Sources/DependencyService/Protocols/DependencyKey.swift @@ -0,0 +1,19 @@ +// +// DependencyKey.swift +// DependencyService +// +// Created by Javier Cicchelli on 05/12/2022. +// Copyright © 2022 Röck+Cöde. All rights reserved. +// + +public protocol DependencyKey { + + // MARK: Associated types + + associatedtype Value + + // MARK: Properties + + static var currentValue: Value { get set } + +} From 7849945ebfe03b91fe672e9a47699fbed0ea1b2d Mon Sep 17 00:00:00 2001 From: Javier Cicchelli Date: Mon, 5 Dec 2022 22:50:36 +0100 Subject: [PATCH 3/4] Implemented the DependencyStore struct. --- .../Structs/DependencyStore.swift | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Libraries/Sources/DependencyService/Structs/DependencyStore.swift diff --git a/Libraries/Sources/DependencyService/Structs/DependencyStore.swift b/Libraries/Sources/DependencyService/Structs/DependencyStore.swift new file mode 100644 index 0000000..88ad1e0 --- /dev/null +++ b/Libraries/Sources/DependencyService/Structs/DependencyStore.swift @@ -0,0 +1,27 @@ +// +// DependencyStore.swift +// DependencyService +// +// Created by Javier Cicchelli on 05/12/2022. +// Copyright © 2022 Röck+Cöde. All rights reserved. +// + +public struct DependencyStore { + + // MARK: Properties + + private static var current = Self() + + // MARK: Functions + + public static subscript(key: K.Type) -> K.Value where K: DependencyKey { + get { key.currentValue } + set { key.currentValue = newValue } + } + + public static subscript(_ keyPath: WritableKeyPath) -> D { + get { current[keyPath: keyPath] } + set { current[keyPath: keyPath] = newValue } + } + +} From a9c81f44faeba623c39ab7acac9280bbe4cf35af Mon Sep 17 00:00:00 2001 From: Javier Cicchelli Date: Mon, 5 Dec 2022 22:51:56 +0100 Subject: [PATCH 4/4] Implemented the Dependency property wrapper. --- .../Property Wrappers/Dependency.swift | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Libraries/Sources/DependencyService/Property Wrappers/Dependency.swift diff --git a/Libraries/Sources/DependencyService/Property Wrappers/Dependency.swift b/Libraries/Sources/DependencyService/Property Wrappers/Dependency.swift new file mode 100644 index 0000000..d8f21fd --- /dev/null +++ b/Libraries/Sources/DependencyService/Property Wrappers/Dependency.swift @@ -0,0 +1,29 @@ +// +// Dependency.swift +// DependencyService +// +// Created by Javier Cicchelli on 05/12/2022. +// Copyright © 2022 Röck+Cöde. All rights reserved. +// + +@propertyWrapper +public struct Dependency { + + // MARK: Properties + + private let keyPath: WritableKeyPath + + // MARK: Computed + + public var wrappedValue: D { + get { DependencyStore[keyPath] } + set { DependencyStore[keyPath] = newValue } + } + + // MARK: Initialisers + + public init(_ keyPath: WritableKeyPath) { + self.keyPath = keyPath + } + +}