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
| // | |
| // BindingValueSubject.swift | |
| // | |
| // | |
| // Created by Woody Liu on 2023/7/2. | |
| // | |
| import Combine | |
| @frozen public struct BindingValue<Output>: Publisher { |
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
| @MainActor | |
| class CommentViewModel: ObservableObject { | |
| let serviceHandler: CommentViewServiceDelegate | |
| let databaseHandler: CommentsDelegate | |
| let mainDispatchQueue: DispatchQueueType | |
| init(serviceHandler: CommentViewServiceDelegate = CommentViewService(), | |
| databaseHandler: CommentsDelegate = DatabaseHandler(), | |
| mainDispatchQueue: DispatchQueueType = DispatchQueue.main) { |
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 Foundation | |
| protocol SomeProtocol<Thing> | |
| { | |
| associatedtype Thing | |
| func doSomething() | |
| } | |
| struct SomeStruct<Thing>:SomeProtocol |
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
| @dynamicMemberLookup | |
| class Variable<T> { | |
| var value: T { | |
| get { sub.value } | |
| set { sub.value = newValue } | |
| } | |
| var stream: AnyPublisher<T, Never> { | |
| return sub.eraseToAnyPublisher() | |
| } |
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
| // MARK: Demontration of Usage of Injection system | |
| // 1. First step: make the key object, the current value type is the one you are trying to add to injection system. | |
| private struct BaseClassKey: InjectionKey { | |
| static var currentValue: BaseClass = RealSubClass() | |
| } | |
| // 2. How to inject the currect value | |
| func test() { | |
| var dataController = TestDataStructure() |
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
| protocol Resolver { | |
| func resolve<T>(_ type: T.Type) -> T | |
| } | |
| class MainResolver: Resolver { | |
| static let shared = MainResolver() | |
| private var dependencies: [Any] = [] |
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 Foundation | |
| @propertyWrapper | |
| struct Injected<T> { | |
| private let keyPath: WritableKeyPath<InjectedValues, T> | |
| var wrappedValue: T { | |
| get { InjectedValues[keyPath] } | |
| set { InjectedValues[keyPath] = newValue } | |
| } |
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
| // 1 | |
| public protocol DIContainer { | |
| func register<DependencyType>(type: DependencyType.Type, dependency: DependencyType) | |
| func resolve<DependencyType>(type: DependencyType.Type) -> DependencyType | |
| } | |
| // 2 | |
| public class DependencyContainer { | |
| public static let shared: DIContainer = DependencyContainer() | |
| private var registeredDependencies: [String: Any] = [:] |
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
| class HardService: ServiceImplementationProtocol { | |
| static var kind: ServiceContainerKind { .temporary } | |
| unowned let serviceLocator: ServiceLocator | |
| private let someString: String | |
| private let someService: ServiceImplementationProtocol | |
| required init(serviceLocator: ServiceLocator) { fatalError() } |
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 Foundation | |
| import Combine | |
| /// Usage example: | |
| /// | |
| /// ``` | |
| /// source | |
| /// .receiveAsyncResults(on: DispatchQueue.main) | |
| /// .sink { | |
| /// print("Value: \($0)") |
NewerOlder