mirror of
https://gitlab.opencode.de/bwi/bundesmessenger/clients/bundesmessenger-ios.git
synced 2026-04-18 15:38:28 +02:00
Add counter example to show viewActions modifying the state.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
//
|
||||
//
|
||||
// Copyright 2021 New Vector Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@@ -34,37 +34,35 @@ import Combine
|
||||
/// It provides a nice layer of consistency and also safety. As we are not passing the `ViewModel` to the view directly, shortcuts/hacks
|
||||
/// can't be made into the `ViewModel`.
|
||||
@available(iOS 14, *)
|
||||
@dynamicMemberLookup
|
||||
class ViewModelContext<ViewState:BindableState, ViewAction>: ObservableObject {
|
||||
// MARK: - Properties
|
||||
|
||||
|
||||
// MARK: Private
|
||||
|
||||
|
||||
private var cancellables = Set<AnyCancellable>()
|
||||
fileprivate let viewActions: PassthroughSubject<ViewAction, Never>
|
||||
|
||||
|
||||
// MARK: Public
|
||||
|
||||
/// Set-able/Bindable `Published` property for the bindable portion of the `ViewState`
|
||||
@Published var bindings: ViewState.BindStateType
|
||||
|
||||
/// Get-able/Observable `Published` property for the `ViewState`
|
||||
@Published fileprivate(set) var viewState: ViewState
|
||||
|
||||
// MARK: Setup
|
||||
|
||||
init(initialViewState: ViewState) {
|
||||
self.bindings = initialViewState.bindings
|
||||
self.viewActions = PassthroughSubject()
|
||||
self.viewState = initialViewState
|
||||
if !(initialViewState.bindings is Void) {
|
||||
// If we have bindable state defined, forward its updates on to the `ViewState`
|
||||
self.$bindings
|
||||
.weakAssign(to: \.viewState.bindings, on: self)
|
||||
.store(in: &cancellables)
|
||||
}
|
||||
/// Set-able/Bindable access to the bindable state.
|
||||
subscript<T>(dynamicMember keyPath: WritableKeyPath<ViewState.BindStateType, T>) -> T {
|
||||
get { viewState.bindings[keyPath: keyPath] }
|
||||
set { viewState.bindings[keyPath: keyPath] = newValue }
|
||||
}
|
||||
|
||||
// MARK: Setup
|
||||
|
||||
init(initialViewState: ViewState) {
|
||||
self.viewActions = PassthroughSubject()
|
||||
self.viewState = initialViewState
|
||||
}
|
||||
|
||||
// MARK: Public
|
||||
|
||||
|
||||
/// Send a `ViewAction` to the `ViewModel` for processing.
|
||||
/// - Parameter viewAction: The `ViewAction` to send to the `ViewModel`.
|
||||
func send(viewAction: ViewAction) {
|
||||
@@ -80,14 +78,10 @@ class ViewModelContext<ViewState:BindableState, ViewAction>: ObservableObject {
|
||||
/// we can do it in this centralised place.
|
||||
@available(iOS 14, *)
|
||||
class StateStoreViewModel<State: BindableState, StateAction, ViewAction> {
|
||||
|
||||
|
||||
typealias Context = ViewModelContext<State, ViewAction>
|
||||
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
// MARK: Private
|
||||
|
||||
private let state: CurrentValueSubject<State, Never>
|
||||
|
||||
// MARK: Public
|
||||
|
||||
@@ -95,39 +89,38 @@ class StateStoreViewModel<State: BindableState, StateAction, ViewAction> {
|
||||
///
|
||||
/// Left as public for `ViewModel` implementations convenience.
|
||||
var cancellables = Set<AnyCancellable>()
|
||||
|
||||
|
||||
/// Constrained interface for passing to Views.
|
||||
var context: Context
|
||||
|
||||
// MARK: Setup
|
||||
/// State can be read within the 'ViewModel' but not modified outside of the reducer.
|
||||
var state: State {
|
||||
context.viewState
|
||||
}
|
||||
|
||||
// MARK: Setup
|
||||
|
||||
init(initialViewState: State) {
|
||||
self.context = Context(initialViewState: initialViewState)
|
||||
self.state = CurrentValueSubject(initialViewState)
|
||||
// Connect the state to context viewState, that view uses for observing (but not modifying directly) the state.
|
||||
self.state
|
||||
.weakAssign(to: \.context.viewState, on: self)
|
||||
.store(in: &cancellables)
|
||||
// Receive events from the view and pass on to the `ViewModel` for processing.
|
||||
self.context.viewActions.sink { [weak self] action in
|
||||
guard let self = self else { return }
|
||||
self.process(viewAction: action)
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
}
|
||||
|
||||
|
||||
/// Send state actions to modify the state within the reducer.
|
||||
/// - Parameter action: The state action to send to the reducer.
|
||||
func dispatch(action: StateAction) {
|
||||
Self.reducer(state: &state.value, action: action)
|
||||
Self.reducer(state: &context.viewState, action: action)
|
||||
}
|
||||
|
||||
|
||||
/// Send state actions from a publisher to modify the state within the reducer.
|
||||
/// - Parameter actionPublisher: The publisher that produces actions to be sent to the reducer
|
||||
func dispatch(actionPublisher: AnyPublisher<StateAction, Never>) {
|
||||
actionPublisher.sink { [weak self] action in
|
||||
guard let self = self else { return }
|
||||
Self.reducer(state: &self.state.value, action: action)
|
||||
Self.reducer(state: &self.context.viewState, action: action)
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
}
|
||||
@@ -141,7 +134,7 @@ class StateStoreViewModel<State: BindableState, StateAction, ViewAction> {
|
||||
class func reducer(state: inout State, action: StateAction) {
|
||||
//Default implementation, -no-op
|
||||
}
|
||||
|
||||
|
||||
/// Override to handles incoming `ViewAction`s from the `ViewModel`.
|
||||
/// - Parameter viewAction: The `ViewAction` to be processed in `ViewModel` implementation.
|
||||
func process(viewAction: ViewAction) {
|
||||
|
||||
Reference in New Issue
Block a user