-
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.
- Loading branch information
Lee Jun Kit
committed
Oct 9, 2021
1 parent
35243b6
commit 41df4da
Showing
13 changed files
with
782 additions
and
83 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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,39 @@ | ||
// | ||
// DependencyInjector.swift | ||
// DependencyInjector | ||
// | ||
// Created by Lee Jun Kit on 24/8/21. | ||
// | ||
|
||
// https://medium.com/@hanneshertach/swiftui-dependency-injection-in-20-lines-b322457065a5 | ||
struct DependencyInjector { | ||
private static var dependencyList: [String: Any] = [:] | ||
|
||
static func resolve<T>() -> T { | ||
guard let t = dependencyList[String(describing: T.self)] as? T else { | ||
fatalError("No povider registered for type \(T.self)") | ||
} | ||
return t | ||
} | ||
|
||
static func register<T>(dependency: T) { | ||
dependencyList[String(describing: T.self)] = dependency | ||
} | ||
} | ||
|
||
@propertyWrapper struct Inject<T> { | ||
var wrappedValue: T | ||
|
||
init() { | ||
self.wrappedValue = DependencyInjector.resolve() | ||
} | ||
} | ||
|
||
@propertyWrapper struct Provider<T> { | ||
var wrappedValue: T | ||
|
||
init(wrappedValue: T) { | ||
self.wrappedValue = wrappedValue | ||
DependencyInjector.register(dependency: wrappedValue) | ||
} | ||
} |
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
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
Oops, something went wrong.