Skip to content

Commit

Permalink
Merge pull request #20 from apptekstudios/main
Browse files Browse the repository at this point in the history
Suggested simplified SwiftUI integration
  • Loading branch information
krzysztofzablocki authored Apr 21, 2022
2 parents b21e462 + 753c630 commit 3f84e4f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ You can either add `import Inject` in individual files in your project or use
#### **SwiftUI**
Just 2 steps to enable injection in your `SwiftUI` Views

- Add `@ObservedObject private var iO = Inject.observer` variable
- call `.enableInjection()` at the end of your body definition
- add `@ObserveInjection var inject` to your view struct

> *Remember you **don't need** to remove this code when you are done, it's NO-OP in production builds.*
Expand Down
15 changes: 14 additions & 1 deletion Sources/Inject/Integrations/SwiftUI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,27 @@ public extension SwiftUI.View {
}
}

@propertyWrapper
public struct ObserveInjection: DynamicProperty {
@ObservedObject private var iO = Inject.observer
public init() {}
public private(set) var wrappedValue: Inject.Type = Inject.self
}

#else
public extension SwiftUI.View {
@inlinable @inline(__always)
func enableInjection() -> Self { self }

@inlinable @inline(__always)
func onInjection(callback: @escaping (Self) -> Void) -> some SwiftUI.View {
func onInjection(callback: @escaping (Self) -> Void) -> Self {
self
}
}

@propertyWrapper
public struct ObserveInjection {
public init() {}
public private(set) var wrappedValue: Inject.Type = Inject.self
}
#endif

0 comments on commit 3f84e4f

Please sign in to comment.