-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
105 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
Sources/AppState/PropertyWrappers/Dependency/ObservedDependency.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#if !os(Linux) && !os(Windows) | ||
import SwiftUI | ||
|
||
/// The `@ObservedDependency` property wrapper is a feature provided by AppState, intended to simplify dependency handling throughout your application. It makes it easy to access, share, and manage dependencies in a neat and Swift idiomatic way. It works the same as `@AppDependency`, but comes with the power of the `@ObservedObject` property wrapper. | ||
@propertyWrapper public struct ObservedDependency<Value>: DynamicProperty where Value: ObservableObject { | ||
/// Path for accessing `ObservedDependency` from Application. | ||
private let keyPath: KeyPath<Application, Application.Dependency<Value>> | ||
|
||
@ObservedObject private var observedObject: Value | ||
|
||
private let fileID: StaticString | ||
private let function: StaticString | ||
private let line: Int | ||
private let column: Int | ||
|
||
/// Represents the current value of the `ObservedDependency`. | ||
public var wrappedValue: Value { observedObject } | ||
|
||
/// A binding to the `ObservedDependency`'s value, which can be used with SwiftUI views. | ||
public var projectedValue: ObservedObject<Value>.Wrapper { $observedObject } | ||
|
||
/** | ||
Initializes the AppDependency with a `keyPath` for accessing `Dependency` in Application. | ||
|
||
- Parameter keyPath: The `KeyPath` for accessing `Dependency` in Application. | ||
*/ | ||
public init( | ||
_ keyPath: KeyPath<Application, Application.Dependency<Value>>, | ||
_ fileID: StaticString = #fileID, | ||
_ function: StaticString = #function, | ||
_ line: Int = #line, | ||
_ column: Int = #column | ||
) { | ||
self.keyPath = keyPath | ||
self.fileID = fileID | ||
self.function = function | ||
self.line = line | ||
self.column = column | ||
|
||
self.observedObject = Application.dependency( | ||
keyPath, | ||
fileID, | ||
function, | ||
line, | ||
column | ||
) | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#if !os(Linux) && !os(Windows) | ||
import SwiftUI | ||
import XCTest | ||
@testable import AppState | ||
|
||
fileprivate class ObservableService: ObservableObject { | ||
@Published var count: Int | ||
|
||
init() { | ||
count = 0 | ||
} | ||
} | ||
|
||
fileprivate extension Application { | ||
var test: Dependency<String> { | ||
dependency("!!!") | ||
} | ||
|
||
var observableService: Dependency<ObservableService> { | ||
dependency(ObservableService()) | ||
} | ||
} | ||
|
||
fileprivate struct ExampleDependencyWrapper { | ||
@ObservedDependency(\.observableService) var service | ||
|
||
func test() { | ||
service.count += 1 | ||
|
||
_ = Picker("", selection: $service.count, content: EmptyView.init) | ||
} | ||
} | ||
|
||
final class ObservedDependencyTests: XCTestCase { | ||
override class func setUp() { | ||
Application.logging(isEnabled: true) | ||
} | ||
|
||
override class func tearDown() { | ||
Application.logger.debug("ObservedDependencyTests \(Application.description)") | ||
} | ||
|
||
func testDependency() { | ||
let example = ExampleDependencyWrapper() | ||
|
||
XCTAssertEqual(example.service.count, 0) | ||
|
||
example.test() | ||
|
||
XCTAssertEqual(example.service.count, 1) | ||
} | ||
} | ||
#endif |