Created
July 26, 2021 08:22
-
-
Save AlexGladkov/a00e228271b93a819b85c19e71736139 to your computer and use it in GitHub Desktop.
SwiftUI + Kotlin Flow
This file contains 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
struct ExampleView: View { | |
let viewModel: AuthViewModel = AuthViewModel() | |
var body: some View { | |
ObservingView(statePublisher: asPublisher(viewModel.viewStates()), | |
actionPublisher: asPublisher(viewModel.viewActions()), | |
content: { state, action in | |
// your view here | |
}) | |
} |
This file contains 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
private class ObservableModel<State, Action>: ObservableObject { | |
@Published var state: State? | |
@Published var action: Action? | |
init(statePublisher: AnyPublisher<State, Never>, | |
actionPublisher: AnyPublisher<Action, Never>) { | |
statePublisher | |
.compactMap { $0 } | |
.receive(on: DispatchQueue.main) | |
.assign(to: &$state) | |
actionPublisher | |
.compactMap { $0 } | |
.receive(on: DispatchQueue.main) | |
.assign(to: &$action) | |
} | |
} |
This file contains 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
public struct ObservingView<State, Action, Content>: View where Content: View { | |
@ObservedObject private var model: ObservableModel<State, Action> | |
private let content: (State, Action?) -> Content | |
public init(statePublisher: AnyPublisher<State, Never>, | |
actionPublisher: AnyPublisher<Action, Never>, | |
@ViewBuilder content: @escaping (State, Action?) -> Content) { | |
self.model = ObservableModel(statePublisher: statePublisher, | |
actionPublisher: actionPublisher) | |
self.content = content | |
} | |
public var body: some View { | |
let view: AnyView | |
if let state = self.model.state { | |
view = AnyView(content(state, model.action)) | |
} else { | |
view = AnyView(Text("Render error")) | |
} | |
return view | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment