Created
December 21, 2019 13:07
-
-
Save nalexn/610600555e4d47e92f676ac88935d725 to your computer and use it in GitHub Desktop.
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
struct ContentView: View { | |
// The local view's state encapsulated in one container: | |
@State private var state = ViewState() | |
// The app's state injection | |
@Environment(\.injected) private var injected: AppState.Injection | |
var body: some View { | |
Text("Value: \(state.value)") | |
.onReceive(stateUpdate) { self.state = $0 } | |
} | |
// The state update filtering | |
private var stateUpdate: AnyPublisher<ViewState, Never> { | |
injected.appState.map { $0.viewState } | |
.removeDuplicates().eraseToAnyPublisher() | |
} | |
} | |
// Container for the local view state encapsulation | |
private extension ContentView { | |
struct ViewState: Equatable { | |
var value: Int = 0 | |
} | |
} | |
// Convenient mapping from AppState to ViewState | |
private extension AppState { | |
var viewState: ContentView.ViewState { | |
return .init(value: value1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment