Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions CBInjection.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
objects = {

/* Begin PBXBuildFile section */
D30B80852367A32000D3D508 /* ViewControllerInjectionKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = D30B80842367A32000D3D508 /* ViewControllerInjectionKey.swift */; };
E74DE55C22333E83008CC5EE /* CBInjection.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E74DE55222333E83008CC5EE /* CBInjection.framework */; };
E74DE56122333E83008CC5EE /* DependenciesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E74DE56022333E83008CC5EE /* DependenciesTests.swift */; };
E74DE57D22333F9D008CC5EE /* InjectionParameterName.swift in Sources */ = {isa = PBXBuildFile; fileRef = E74DE56D22333F9D008CC5EE /* InjectionParameterName.swift */; };
Expand All @@ -32,7 +31,6 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
D30B80842367A32000D3D508 /* ViewControllerInjectionKey.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewControllerInjectionKey.swift; sourceTree = "<group>"; };
E74DE55222333E83008CC5EE /* CBInjection.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = CBInjection.framework; sourceTree = BUILT_PRODUCTS_DIR; };
E74DE55B22333E83008CC5EE /* CBInjectionTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = CBInjectionTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
E74DE56022333E83008CC5EE /* DependenciesTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DependenciesTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -114,7 +112,6 @@
E74DE56D22333F9D008CC5EE /* InjectionParameterName.swift */,
E74DE56E22333F9D008CC5EE /* InjectionKey.swift */,
E74DE56F22333F9D008CC5EE /* InjectionScope.swift */,
D30B80842367A32000D3D508 /* ViewControllerInjectionKey.swift */,
);
path = Models;
sourceTree = "<group>";
Expand Down Expand Up @@ -264,7 +261,6 @@
E74DE58322333F9D008CC5EE /* UINavigationController+Injectables.swift in Sources */,
E74DE57D22333F9D008CC5EE /* InjectionParameterName.swift in Sources */,
E74DE57F22333F9D008CC5EE /* InjectionScope.swift in Sources */,
D30B80852367A32000D3D508 /* ViewControllerInjectionKey.swift in Sources */,
E74DE58722333F9D008CC5EE /* Dependencies.swift in Sources */,
E74DE58422333F9D008CC5EE /* UIViewController+Injectables.swift in Sources */,
E74DE57E22333F9D008CC5EE /* InjectionKey.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public extension UINavigationController {
/// Initialize the navigation controller with an injected root view controller
///
/// - Parameter rootViewControllerKey: the injection key for the root view controller
convenience init(rootViewControllerKey: ViewControllerInjectionKey) {
convenience init(rootViewControllerKey: InjectionKey<UIViewController>) {
guard let viewController = try? Dependencies.shared.provide(rootViewControllerKey) else {
fatalError("View controller not found")
}
Expand All @@ -22,7 +22,7 @@ public extension UINavigationController {
///
/// - Returns: Instance of ViewController generated using provided injection key
@discardableResult
func pushViewController(_ key: ViewControllerInjectionKey, animated: Bool) -> UIViewController? {
func pushViewController<T: UIViewController>(_ key: InjectionKey<T>, animated: Bool) -> T? {
guard let viewController = try? Dependencies.shared.provide(key) else {
assertionFailure("Unable to push view controller for key \(key)")
return nil
Expand All @@ -38,7 +38,7 @@ public extension UINavigationController {
/// - Parameters:
/// - keys: Injection keys for the view controllers
/// - animated: true if the transition should be animated
func setViewControllers(_ keys: [ViewControllerInjectionKey], animated: Bool) {
func setViewControllers<T: UIViewController>(_ keys: [InjectionKey<T>], animated: Bool) {
let viewControllers = keys.compactMap { key -> UIViewController? in
guard let viewController: UIViewController = try? Dependencies.shared.provide(key) else {
assertionFailure("Unable to push view controller for key \(key)")
Expand All @@ -58,7 +58,7 @@ public extension UINavigationController {
/// - animated: true if the transition should be animated
///
/// - Returns: Instance of ViewController generated using provided injection key
func setRootViewController(_ key: ViewControllerInjectionKey, animated: Bool) -> UIViewController? {
func setRootViewController<T: UIViewController>(_ key: InjectionKey<T>, animated: Bool) -> T? {
guard let viewController = try? Dependencies.shared.provide(key) else {
assertionFailure("Unable to push view controller for key \(key)")
return nil
Expand Down
11 changes: 6 additions & 5 deletions CBInjection/Extensions/UIViewController+Injectables.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ public extension UIViewController {
///
/// - Returns: Instance of ViewController generated using provided injection key
@discardableResult
func present(
_ key: ViewControllerInjectionKey,
func present<T: UIViewController>(
_ key: InjectionKey<T>,
animated: Bool,
modalPresentationStyle: UIModalPresentationStyle = .fullScreen,
completion: (() -> Void)? = nil
) -> UIViewController? {
) -> T? {
guard let viewController = try? Dependencies.shared.provide(key) else {
assertionFailure("Unable to present view controller for key \(key)")
return nil
}

viewController.modalPresentationStyle = key.modalPresentationStyle
viewController.modalPresentationStyle = modalPresentationStyle

present(viewController, animated: animated, completion: completion)

Expand All @@ -36,7 +37,7 @@ public extension UIViewController {
/// - key: Key used to create view controller
///
/// - Returns: Instance of ViewController generated using provided injection key
func addChild(_ key: ViewControllerInjectionKey) -> UIViewController? {
func addChild<T: UIViewController>(_ key: InjectionKey<T>) -> T? {
guard let viewController = try? Dependencies.shared.provide(key) else {
assertionFailure("Unable to present view controller for key \(key)")
return nil
Expand Down
7 changes: 4 additions & 3 deletions CBInjection/Models/InjectionKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import Foundation

/// Represents and injection key used indentify a dependency.
public class InjectionKey<T>: InjectionKeys {
public final class InjectionKey<T>: InjectionKeys {
/// Injection which contains the code for resolving a dependency
let injection: DependencyInjection<T>?

Expand Down Expand Up @@ -32,13 +32,14 @@ public class InjectionKey<T>: InjectionKeys {
}

/// Closure-based constructor
public required init(
public init(
uuid: String = NSUUID().uuidString,
scope: InjectionScope,
parameters: [InjectionParameterName: Any] = [:],
closure: @escaping ((Dependencies) throws -> T)
) {
self.scope = scope
self.parameters = [:]
self.parameters = parameters
self.closure = closure
injection = nil
super.init(uuid: uuid)
Expand Down
48 changes: 0 additions & 48 deletions CBInjection/Models/ViewControllerInjectionKey.swift

This file was deleted.