-
Notifications
You must be signed in to change notification settings - Fork 1
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
38 changed files
with
546 additions
and
354 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// swift-tools-version:5.5 | ||
// swift-tools-version:5.9 | ||
// swiftformat:disable all | ||
import PackageDescription | ||
|
||
|
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
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
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
57 changes: 57 additions & 0 deletions
57
Source/PropertyWrapper/DIPropertyWrapper/DIObservedObject.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,57 @@ | ||
#if canImport(SwiftUI) | ||
import Combine | ||
import SwiftUI | ||
|
||
@propertyWrapper | ||
public struct DIObservedObject<Value: ObservableObject>: DynamicProperty { | ||
@EnvironmentObject | ||
private var container: ObservableResolver | ||
@ObservedObject | ||
private var holder: InstanceHolder<Value> = .init() | ||
private let parametersHolder: EnvParametersHolder = .init() | ||
|
||
public var wrappedValue: Value { | ||
return resolveInstance() | ||
} | ||
|
||
public init(named name: String? = nil, | ||
with arguments: Arguments? = nil) { | ||
parametersHolder.name = name | ||
parametersHolder.arguments = arguments | ||
} | ||
|
||
public var projectedValue: Binding<Value> { | ||
resolveInstance() | ||
return $holder.instance | ||
} | ||
|
||
@discardableResult | ||
private func resolveInstance() -> Value { | ||
if let instance = holder.instance { | ||
return instance | ||
} | ||
|
||
let instance: Value = container.resolve(named: parametersHolder.name, with: parametersHolder.arguments) | ||
holder.instance = instance | ||
// @ObservedObject is recreated each time the view is redrawn | ||
// parametersHolder.cleanup() | ||
return instance | ||
} | ||
} | ||
|
||
private final class InstanceHolder<Value: ObservableObject>: ObservableObject { | ||
private var observers: Set<AnyCancellable> = [] | ||
|
||
var instance: Value! { | ||
didSet { | ||
if oldValue !== instance { | ||
assert(observers.isEmpty, "Subscribed to `objectWillChange` multiple times. Should never happen.") | ||
observers = [] | ||
instance.objectWillChange.sink { [unowned self] _ in | ||
objectWillChange.send() | ||
}.store(in: &observers) | ||
} | ||
} | ||
} | ||
} | ||
#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,36 @@ | ||
#if canImport(SwiftUI) | ||
import Foundation | ||
import SwiftUI | ||
|
||
public struct DIProviderOptions { | ||
public let name: String? | ||
public let arguments: Arguments? | ||
} | ||
|
||
@propertyWrapper | ||
public struct DIProvider<Value>: DynamicProperty { | ||
@EnvironmentObject | ||
private var container: ObservableResolver | ||
private let parametersHolder: EnvParametersHolder = .init() | ||
|
||
public var wrappedValue: Value { | ||
return container.resolve(named: parametersHolder.name, with: parametersHolder.arguments) | ||
} | ||
|
||
public init(named name: String? = nil, | ||
with arguments: Arguments? = nil) { | ||
parametersHolder.name = name | ||
parametersHolder.arguments = arguments | ||
} | ||
|
||
public var projectedValue: DIProviderOptions { | ||
get { | ||
return .init(name: parametersHolder.name, arguments: parametersHolder.arguments) | ||
} | ||
set { | ||
parametersHolder.name = newValue.name | ||
parametersHolder.arguments = newValue.arguments | ||
} | ||
} | ||
} | ||
#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,50 @@ | ||
#if canImport(SwiftUI) | ||
import Combine | ||
import SwiftUI | ||
|
||
@propertyWrapper | ||
public struct DIState<Value>: DynamicProperty { | ||
@EnvironmentObject | ||
private var container: ObservableResolver | ||
@State | ||
private var holder: InstanceHolder<Value> = .init() | ||
private let parametersHolder: EnvParametersHolder = .init() | ||
|
||
public var wrappedValue: Value { | ||
return resolveInstance() | ||
} | ||
|
||
public init(named name: String? = nil, | ||
with arguments: Arguments? = nil) { | ||
parametersHolder.name = name | ||
parametersHolder.arguments = arguments | ||
} | ||
|
||
public var projectedValue: Binding<Value> { | ||
resolveInstance() | ||
return $holder.instance | ||
} | ||
|
||
@discardableResult | ||
private func resolveInstance() -> Value { | ||
if let instance = holder.instance { | ||
return instance | ||
} | ||
|
||
let instance: Value = container.resolve(named: parametersHolder.name, with: parametersHolder.arguments) | ||
holder.instance = instance | ||
parametersHolder.cleanup() | ||
return instance | ||
} | ||
} | ||
|
||
private final class InstanceHolder<Value>: ObservableObject { | ||
var instance: Value! { | ||
didSet { | ||
if oldValue != nil { | ||
objectWillChange.send() | ||
} | ||
} | ||
} | ||
} | ||
#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
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,13 @@ | ||
#if canImport(SwiftUI) | ||
import Foundation | ||
|
||
internal final class EnvParametersHolder { | ||
var name: String? | ||
var arguments: Arguments? | ||
|
||
func cleanup() { | ||
name = nil | ||
arguments = nil | ||
} | ||
} | ||
#endif |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.