-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UIEnviroment property wrapper (#17)
* Add UIEnviroment * Fix unit test * adjust test.yml
- Loading branch information
1 parent
11ac426
commit b8f8d6a
Showing
25 changed files
with
752 additions
and
241 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
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
114 changes: 0 additions & 114 deletions
114
Sources/SwiftEnvironment/EnvironmentValuesResolver.swift
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
14 changes: 14 additions & 0 deletions
14
Sources/SwiftEnvironment/InstanceResolver/InstanceResolver.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,14 @@ | ||
// | ||
// InstanceResolver.swift | ||
// SwiftEnvironment | ||
// | ||
// Created by Nayanda Haberty on 14/3/24. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
protocol InstanceResolver { | ||
func resolve<V>(for type: V.Type) -> V? | ||
func assign(to view: any View, for keyPath: AnyKeyPath) -> any View | ||
} |
41 changes: 41 additions & 0 deletions
41
Sources/SwiftEnvironment/InstanceResolver/SingletonInstanceResolver.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,41 @@ | ||
// | ||
// SingletonInstanceResolver.swift | ||
// SwiftEnvironment | ||
// | ||
// Created by Nayanda Haberty on 5/11/24. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
final class SingletonInstanceResolver<Value>: InstanceResolver { | ||
|
||
private(set) var instance: Value? | ||
private var resolver: (() -> Value)? | ||
private let queue: DispatchQueue? | ||
|
||
@inlinable init(queue: DispatchQueue?, resolver: @escaping () -> Value) { | ||
self.resolver = resolver | ||
self.queue = queue | ||
} | ||
|
||
@inlinable func resolve<V>(for type: V.Type) -> V? { | ||
guard let instance else { | ||
// this resolver should not be nil in this line | ||
let resolver = self.resolver! | ||
let newInstance = queue?.safeSync(execute: resolver) ?? resolver() | ||
self.resolver = nil | ||
self.instance = newInstance | ||
return newInstance as? V | ||
} | ||
return instance as? V | ||
} | ||
|
||
func assign(to view: any View, for keyPath: AnyKeyPath) -> any View { | ||
guard let writableKeyPath = keyPath as? WritableKeyPath<EnvironmentValues, Value>, | ||
let value = resolve(for: Value.self) else { | ||
return view | ||
} | ||
return view.environment(writableKeyPath, value) | ||
} | ||
} |
Oops, something went wrong.