mirror of
https://gitlab.opencode.de/bwi/bundesmessenger/clients/bundesmessenger-ios.git
synced 2026-04-17 15:09:31 +02:00
Add counter example to show viewActions modifying the state.
This commit is contained in:
@@ -19,7 +19,7 @@ import Combine
|
||||
|
||||
@available(iOS 14.0, *)
|
||||
extension Publisher where Failure == Never {
|
||||
/// Sams as `assign(to:on:)` but maintains a weak reference to object
|
||||
/// Same as `assign(to:on:)` but maintains a weak reference to object
|
||||
///
|
||||
/// Useful in cases where you want to pass self and not cause a retain cycle.
|
||||
func weakAssign<T: AnyObject>(
|
||||
|
||||
@@ -17,9 +17,10 @@
|
||||
import XCTest
|
||||
import RiotSwiftUI
|
||||
|
||||
/// XCTestCase subclass to easy testing of `MockScreenState`'s.
|
||||
/// XCTestCase subclass to ease testing of `MockScreenState`.
|
||||
/// Creates a test case for each screen state, launches the app,
|
||||
/// goes to the correct screen and
|
||||
/// goes to the correct screen and provides the state and key for each
|
||||
/// invocation of the test.
|
||||
@available(iOS 14.0, *)
|
||||
class MockScreenTest: XCTestCase {
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -48,7 +48,9 @@ final class TemplateUserProfileCoordinator: Coordinator {
|
||||
|
||||
// MARK: - Public
|
||||
func start() {
|
||||
MXLog.debug("[TemplateUserProfileCoordinator] did start.")
|
||||
templateUserProfileViewModel.completion = { [weak self] result in
|
||||
MXLog.debug("[TemplateUserProfileCoordinator] TemplateUserProfileViewModel did complete with result: \(result).")
|
||||
guard let self = self else { return }
|
||||
switch result {
|
||||
case .cancel, .done:
|
||||
|
||||
@@ -17,5 +17,7 @@
|
||||
import Foundation
|
||||
|
||||
enum TemplateUserProfileStateAction {
|
||||
case incrementCount
|
||||
case decrementCount
|
||||
case updatePresence(TemplateUserProfilePresence)
|
||||
}
|
||||
|
||||
@@ -17,6 +17,8 @@
|
||||
import Foundation
|
||||
|
||||
enum TemplateUserProfileViewAction {
|
||||
case incrementCount
|
||||
case decrementCount
|
||||
case cancel
|
||||
case done
|
||||
}
|
||||
|
||||
@@ -20,4 +20,5 @@ struct TemplateUserProfileViewState: BindableState {
|
||||
let avatar: AvatarInputProtocol?
|
||||
let displayName: String?
|
||||
var presence: TemplateUserProfilePresence
|
||||
var count: Int
|
||||
}
|
||||
|
||||
@@ -25,6 +25,8 @@ protocol TemplateUserProfileServiceProtocol: Avatarable {
|
||||
var presenceSubject: CurrentValueSubject<TemplateUserProfilePresence, Never> { get }
|
||||
}
|
||||
|
||||
// MARK: Avatarable
|
||||
|
||||
@available(iOS 14.0, *)
|
||||
extension TemplateUserProfileServiceProtocol {
|
||||
var mxContentUri: String? {
|
||||
|
||||
@@ -41,13 +41,13 @@ class TemplateUserProfileUITests: MockScreenTest {
|
||||
func verifyTemplateUserProfilePresence(presence: TemplateUserProfilePresence) {
|
||||
let presenceText = app.staticTexts["presenceText"]
|
||||
XCTAssert(presenceText.exists)
|
||||
XCTAssert(presenceText.label == presence.title)
|
||||
XCTAssertEqual(presenceText.label, presence.title)
|
||||
}
|
||||
|
||||
func verifyTemplateUserProfileLongName(name: String) {
|
||||
let displayNameText = app.staticTexts["displayNameText"]
|
||||
XCTAssert(displayNameText.exists)
|
||||
XCTAssert(displayNameText.label == name)
|
||||
XCTAssertEqual(displayNameText.label, name)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -38,10 +38,16 @@ struct TemplateUserProfile: View {
|
||||
presence: viewModel.viewState.presence
|
||||
)
|
||||
Divider()
|
||||
VStack{
|
||||
Text("More great user content!")
|
||||
HStack{
|
||||
Text("Counter: \(viewModel.viewState.count)")
|
||||
.font(theme.fonts.title2)
|
||||
.foregroundColor(theme.colors.secondaryContent)
|
||||
Button("-") {
|
||||
viewModel.send(viewAction: .decrementCount)
|
||||
}
|
||||
Button("+") {
|
||||
viewModel.send(viewAction: .incrementCount)
|
||||
}
|
||||
}
|
||||
.frame(maxHeight: .infinity)
|
||||
}
|
||||
|
||||
@@ -52,7 +52,8 @@ class TemplateUserProfileViewModel: TemplateUserProfileViewModelType, TemplateUs
|
||||
return TemplateUserProfileViewState(
|
||||
avatar: templateUserProfileService.avatarData,
|
||||
displayName: templateUserProfileService.displayName,
|
||||
presence: templateUserProfileService.presenceSubject.value
|
||||
presence: templateUserProfileService.presenceSubject.value,
|
||||
count: 0
|
||||
)
|
||||
}
|
||||
|
||||
@@ -71,6 +72,10 @@ class TemplateUserProfileViewModel: TemplateUserProfileViewModelType, TemplateUs
|
||||
cancel()
|
||||
case .done:
|
||||
done()
|
||||
case .incrementCount:
|
||||
dispatch(action: .incrementCount)
|
||||
case .decrementCount:
|
||||
dispatch(action: .decrementCount)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,6 +83,10 @@ class TemplateUserProfileViewModel: TemplateUserProfileViewModelType, TemplateUs
|
||||
switch action {
|
||||
case .updatePresence(let presence):
|
||||
state.presence = presence
|
||||
case .incrementCount:
|
||||
state.count += 1
|
||||
case .decrementCount:
|
||||
state.count -= 1
|
||||
}
|
||||
UILog.debug("[TemplateUserProfileViewModel] reducer with action \(action) produced state: \(state)")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user