|
| 1 | +// |
| 2 | +// SwiftUI+DataStack.swift |
| 3 | +// RCDataKit |
| 4 | +// |
| 5 | +// Created by Ryan Linn on 11/10/24. |
| 6 | +// |
| 7 | + |
| 8 | +import SwiftUI |
| 9 | + |
| 10 | +extension View { |
| 11 | + /// Adds the `DataStack` to the View's environment. |
| 12 | + /// |
| 13 | + /// This call includes adding the `DataStack`'s `viewContext` to the View environment, so child views |
| 14 | + /// can access both or either the `DataStack` and/or `managedObjectContext` through the following |
| 15 | + /// variable declarations: |
| 16 | + /// |
| 17 | + /// ```swift |
| 18 | + /// struct MyView: View { |
| 19 | + /// // Get the DataStack's viewContext: |
| 20 | + /// @Environment(\.managedObjectContext) var context |
| 21 | + /// |
| 22 | + /// // Get the DataStack |
| 23 | + /// @EnvironmentDataStack var dataStack |
| 24 | + /// } |
| 25 | + /// ``` |
| 26 | + public func dataStackEnvironment(_ stack: any DataStack) -> some View { |
| 27 | + self |
| 28 | + .environment(\.dataStack, stack) |
| 29 | + .environment(\.managedObjectContext, stack.viewContext) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +/// A property wrapper that accesses a `DataStack` from the view environment. |
| 34 | +/// |
| 35 | +/// You must set the `DataStack` into the environment in a parent view to be able to access this environment |
| 36 | +/// variable this way. Otherwise, attempting to access `EnvironmentDataStack` will cause a fatal error. |
| 37 | +/// |
| 38 | +/// ```swift |
| 39 | +/// struct ParentView: View { |
| 40 | +/// var dataStack: MyDataStack |
| 41 | +/// |
| 42 | +/// var body: some View { |
| 43 | +/// ChildView() |
| 44 | +/// .dataStackEnvironment(dataStack) |
| 45 | +/// } |
| 46 | +/// } |
| 47 | +/// |
| 48 | +/// struct ChildView: View { |
| 49 | +/// // accesses ParentView.dataStack as (any DataStack) |
| 50 | +/// @EnvironmentDataStack var dataStack |
| 51 | +/// |
| 52 | +/// // accesses ParentView.dataStack.viewContext |
| 53 | +/// @Environment(\.managedObjectContext) var viewContext |
| 54 | +/// |
| 55 | +/// var body: some View { |
| 56 | +/// Text("Hello World") |
| 57 | +/// } |
| 58 | +/// } |
| 59 | +/// ``` |
| 60 | +@propertyWrapper public struct EnvironmentDataStack: DynamicProperty { |
| 61 | + @Environment(\.dataStack) var env: (any DataStack)? |
| 62 | + |
| 63 | + public init() { |
| 64 | + _env = Environment(\.dataStack) |
| 65 | + } |
| 66 | + |
| 67 | + public var wrappedValue: any DataStack { |
| 68 | + if let env { |
| 69 | + return env |
| 70 | + } else { |
| 71 | + fatalError("Attempted to access environment DataStack, but none was found") |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +fileprivate extension EnvironmentValues { |
| 77 | + var dataStack: (any DataStack)? { |
| 78 | + get { self[DataStackKey.self] } |
| 79 | + set { self[DataStackKey.self] = newValue } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +fileprivate struct DataStackKey: EnvironmentKey { |
| 84 | + static let defaultValue: (any DataStack)? = nil |
| 85 | +} |
0 commit comments