Skip to content

Commit 838e614

Browse files
Merge pull request #92 from iMostfa/feature/add-on-inject-hook-docs
Pass host to onInjection Hook + Add docs
2 parents 1f368dc + 7345ab8 commit 838e614

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,23 @@ rootViewController.pushViewController(Inject.ViewControllerHost(viewController),
124124
let viewController = Inject.ViewControllerHost(YourViewController())
125125
rootViewController.pushViewController(viewController, animated: true)
126126
```
127-
128127
> *Remember you **don't need** to remove this code when you are done, it's NO-OP in production builds.*
129128
129+
130+
#### **Injection Hook for UIKit**
131+
depending on the architecture used in your UIKit App, you might want to attach a hook to be executed each time a view controller is reloaded.
132+
133+
Eg. you might want to bind the `UIViewController` to the presenter each-time there's a reload, to achieve this you can use `onInjectionHook`
134+
Example:
135+
136+
```swift
137+
myView.onInjectionHook = { hostedViewController in
138+
//any thing here will be executed each time the controller is reloaded
139+
// for example, you might want to re-assign the controller to your presenter
140+
presenter.ui = hostedViewController
141+
}
142+
```
143+
130144
#### iOS 12
131145
You need to add -weak_framework SwiftUI to Other Linker Flags for iOS 12 to work.
132146

Sources/Inject/Integrations/Hosts.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,18 @@ public typealias ViewHost = _InjectableViewHost
2323
open class _InjectableViewControllerHost<Hosted: InjectViewControllerType>: InjectViewControllerType {
2424
public private(set) var instance: Hosted
2525
let constructor: () -> Hosted
26-
public var afterInjectionHook: (() -> Void)?
26+
/// Attaches a hook to be executed each time after a controller is reloaded.
27+
///
28+
/// Usage:
29+
/// ```swift
30+
/// let myView = ViewControllerHost(TestViewController())
31+
/// myView.onInjectionHook = { hostedViewController in
32+
/// //any thing here will be executed each time the controller is reloaded
33+
/// // for example, you might want to re-assign the controller to your presenter
34+
/// presenter.ui = hostedViewController
35+
/// }
36+
/// ```
37+
public var onInjectionHook: ((Hosted) -> Void)?
2738

2839
public init(_ constructor: @autoclosure @escaping () -> Hosted) {
2940
instance = constructor()
@@ -34,8 +45,9 @@ open class _InjectableViewControllerHost<Hosted: InjectViewControllerType>: Inje
3445

3546
addAsChild()
3647
onInjection { [weak self] instance in
48+
guard let self else { return }
3749
instance.resetHosted()
38-
self?.afterInjectionHook?()
50+
self.onInjectionHook?(self.instance)
3951
}
4052
}
4153

0 commit comments

Comments
 (0)