Skip to content
Merged
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: 4 additions & 0 deletions CBInjection.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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 @@ -31,6 +32,7 @@
/* 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 @@ -112,6 +114,7 @@
E74DE56D22333F9D008CC5EE /* InjectionParameterName.swift */,
E74DE56E22333F9D008CC5EE /* InjectionKey.swift */,
E74DE56F22333F9D008CC5EE /* InjectionScope.swift */,
D30B80842367A32000D3D508 /* ViewControllerInjectionKey.swift */,
);
path = Models;
sourceTree = "<group>";
Expand Down Expand Up @@ -261,6 +264,7 @@
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: InjectionKey<UIViewController>) {
convenience init(rootViewControllerKey: ViewControllerInjectionKey) {
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<T: UIViewController>(_ key: InjectionKey<T>, animated: Bool) -> T? {
func pushViewController(_ key: ViewControllerInjectionKey, animated: Bool) -> UIViewController? {
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<T: UIViewController>(_ keys: [InjectionKey<T>], animated: Bool) {
func setViewControllers(_ keys: [ViewControllerInjectionKey], 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<T: UIViewController>(_ key: InjectionKey<T>, animated: Bool) -> T? {
func setRootViewController(_ key: ViewControllerInjectionKey, animated: Bool) -> UIViewController? {
guard let viewController = try? Dependencies.shared.provide(key) else {
assertionFailure("Unable to push view controller for key \(key)")
return nil
Expand Down
11 changes: 5 additions & 6 deletions CBInjection/Extensions/UIViewController+Injectables.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,17 @@ public extension UIViewController {
///
/// - Returns: Instance of ViewController generated using provided injection key
@discardableResult
func present<T: UIViewController>(
_ key: InjectionKey<T>,
func present(
_ key: ViewControllerInjectionKey,
animated: Bool,
modalPresentationStyle: UIModalPresentationStyle = .fullScreen,
completion: (() -> Void)? = nil
) -> T? {
) -> UIViewController? {
guard let viewController = try? Dependencies.shared.provide(key) else {
assertionFailure("Unable to present view controller for key \(key)")
return nil
}

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

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

Expand All @@ -37,7 +36,7 @@ public extension UIViewController {
/// - key: Key used to create view controller
///
/// - Returns: Instance of ViewController generated using provided injection key
func addChild<T: UIViewController>(_ key: InjectionKey<T>) -> T? {
func addChild(_ key: ViewControllerInjectionKey) -> UIViewController? {
guard let viewController = try? Dependencies.shared.provide(key) else {
assertionFailure("Unable to present view controller for key \(key)")
return nil
Expand Down
7 changes: 3 additions & 4 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 final class InjectionKey<T>: InjectionKeys {
public class InjectionKey<T>: InjectionKeys {
/// Injection which contains the code for resolving a dependency
let injection: DependencyInjection<T>?

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

/// Closure-based constructor
public init(
public required init(
uuid: String = NSUUID().uuidString,
scope: InjectionScope,
parameters: [InjectionParameterName: Any] = [:],
closure: @escaping ((Dependencies) throws -> T)
) {
self.scope = scope
self.parameters = parameters
self.parameters = [:]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make constructor required

self.closure = closure
injection = nil
super.init(uuid: uuid)
Expand Down
48 changes: 48 additions & 0 deletions CBInjection/Models/ViewControllerInjectionKey.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) 2017-2019 Coinbase Inc. See LICENSE

import UIKit

public class ViewControllerInjectionKey: InjectionKey<UIViewController> {
/// View controller modal presentation style
let modalPresentationStyle: UIModalPresentationStyle

public required init(
uuid: String = NSUUID().uuidString,
scope: InjectionScope,
modalPresentationStyle: UIModalPresentationStyle = .fullScreen,
closure: @escaping (Dependencies) throws -> UIViewController
) {
self.modalPresentationStyle = modalPresentationStyle
super.init(uuid: uuid, scope: scope, closure: closure)
}

public required init(
uuid: String = NSUUID().uuidString,
using injectionObjectType: DependencyInjection<UIViewController>.Type,
scope: InjectionScope,
parameters: [InjectionParameterName: Any] = [:],
modalPresentationStyle: UIModalPresentationStyle = .fullScreen
) {
self.modalPresentationStyle = modalPresentationStyle
super.init(uuid: uuid, using: injectionObjectType, scope: scope, parameters: parameters)
}

@available(*, unavailable)
public required init(
uuid: String = NSUUID().uuidString,
scope: InjectionScope,
closure: @escaping ((Dependencies) throws -> UIViewController)
) {
fatalError("init(uuid:scope:closure:) has not been implemented")
}

@available(*, unavailable)
public required init(
uuid: String = NSUUID().uuidString,
using injectionObjectType: DependencyInjection<UIViewController>.Type,
scope: InjectionScope,
parameters: [InjectionParameterName : Any] = [:]
) {
fatalError("init(uuid:using:scope:parameters:) has not been implemented")
}
}