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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,4 @@ fastlane/screenshots

## SwinjectStoryboard
SwinjectStoryboard.framework.zip
IDEWorkspaceChecks.plist
1 change: 1 addition & 0 deletions Sources/SwinjectStoryboard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public class SwinjectStoryboard: _SwinjectStoryboardBase, SwinjectStoryboardProt
}

private func injectDependency(to viewController: UIViewController) {
guard viewController.shouldBeInjected else { return }
guard !viewController.wasInjected else { return }
defer { viewController.wasInjected = true }

Expand Down
48 changes: 48 additions & 0 deletions Sources/ViewController+Swinject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ import ObjectiveC

private var uivcRegistrationNameKey: String = "UIViewController.swinjectRegistrationName"
private var uivcWasInjectedKey: String = "UIViewController.wasInjected"
private var uivcShouldBeInjectedKey: String = "UIViewController.shouldBeInjected"

public extension UIViewController {

@IBInspectable
var isStoryboardInjection: Bool {
get { return shouldBeInjected }
set { shouldBeInjected = newValue }
}

}

extension UIViewController: RegistrationNameAssociatable, InjectionVerifiable {
internal var swinjectRegistrationName: String? {
Expand All @@ -23,6 +34,11 @@ extension UIViewController: RegistrationNameAssociatable, InjectionVerifiable {
get { return getAssociatedBool(key: &uivcWasInjectedKey) ?? false }
set { setAssociatedBool(newValue, key: &uivcWasInjectedKey) }
}

internal var shouldBeInjected: Bool {
get { return getAssociatedBool(key: &uivcShouldBeInjectedKey) ?? false }
set { setAssociatedBool(newValue, key: &uivcShouldBeInjectedKey) }
}
}

#elseif os(OSX)
Expand All @@ -31,6 +47,28 @@ private var nsvcRegistrationNameKey: String = "NSViewController.swinjectRegistra
private var nswcRegistrationNameKey: String = "NSWindowController.swinjectRegistrationName"
private var nsvcWasInjectedKey: String = "NSViewController.wasInjected"
private var nswcWasInjectedKey: String = "NSWindowController.wasInjected"
private var nsvcShouldBeInjectedKey: String = "UIViewController.shouldBeInjected"
private var nswcShouldBeInjectedKey: String = "UIViewController.shouldBeInjected"

public extension NSViewController {

@IBInspectable
var isStoryboardInjection: Bool {
get { return shouldBeInjected }
set { shouldBeInjected = newValue }
}

}

public extension NSWindowController {

@IBInspectable
var isStoryboardInjection: Bool {
get { return shouldBeInjected }
set { shouldBeInjected = newValue }
}

}

extension NSViewController: RegistrationNameAssociatable, InjectionVerifiable {
internal var swinjectRegistrationName: String? {
Expand All @@ -42,6 +80,11 @@ extension NSViewController: RegistrationNameAssociatable, InjectionVerifiable {
get { return getAssociatedBool(key: &nsvcWasInjectedKey) ?? false }
set { setAssociatedBool(newValue, key: &nsvcWasInjectedKey) }
}

internal var shouldBeInjected: Bool {
get { return getAssociatedBool(key: &nsvcShouldBeInjectedKey) ?? false }
set { setAssociatedBool(newValue, key: &nsvcShouldBeInjectedKey) }
}
}

extension NSWindowController: RegistrationNameAssociatable, InjectionVerifiable {
Expand All @@ -54,6 +97,11 @@ extension NSWindowController: RegistrationNameAssociatable, InjectionVerifiable
get { return getAssociatedBool(key: &nswcWasInjectedKey) ?? false }
set { setAssociatedBool(newValue, key: &nswcWasInjectedKey) }
}

internal var shouldBeInjected: Bool {
get { return getAssociatedBool(key: &nswcShouldBeInjectedKey) ?? false }
set { setAssociatedBool(newValue, key: &nswcShouldBeInjectedKey) }
}
}

#endif
Expand Down