Last active
February 16, 2021 17:52
-
-
Save erikbasargin/a4cc7d2e78b6a177ad70a370057de676 to your computer and use it in GitHub Desktop.
Store resolver for MVI architecture pattern in SwiftUI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Swinject | |
| // MARK: - Stores Assembly | |
| final class StoresAssembly: Assembly { | |
| func assemble(container: Container) { | |
| container.register(PageStore.self) { resolver in | |
| PageStore(dataManager: resolver.resolve(PageDataManager.self)!) | |
| }.inObjectScope(.transient) | |
| // transient - A new instance is always created by the `Container` when a type is resolved. | |
| // | |
| // weak - An instance provided by the `Container` is shared within the `Container` and its child `Container`s | |
| // as long as there are strong references to given instance. Otherwise new instance is created when resolving the type. | |
| } | |
| } | |
| // MARK: - Stores Assembler | |
| public struct StoresAssembler { | |
| public static var shared: Resolver { | |
| assembler.resolver | |
| } | |
| public static let assembler: Assembler = { | |
| Assembler([ | |
| StoresAssembly() | |
| ]) | |
| }() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import struct SwiftUI.ObservedObject | |
| import protocol SwiftUI.ObservableObject | |
| import protocol SwiftUI.DynamicProperty | |
| @available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *) | |
| @propertyWrapper public struct Store<ObjectType: ObservableObject>: DynamicProperty { | |
| // MARK: - Initialization | |
| public init(name: String? = nil) { | |
| self.object = StoresAssembler.shared.resolve(ObjectType.self, name: name)! | |
| } | |
| // MARK: - Internal properties | |
| @ObservedObject var object: ObjectType = StoresAssembler.shared.resolve(ObjectType.self)! | |
| // MARK: - Public properties | |
| public var wrappedValue: ObjectType { object } | |
| public var projectedValue: ObservedObject<ObjectType>.Wrapper { $object } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment