Introduce StateStore with bindings example for Chat example.

This commit is contained in:
David Langley
2021-09-15 11:47:59 +01:00
parent f3dbd2a056
commit 7aad7c51ba
8 changed files with 37 additions and 39 deletions
@@ -16,6 +16,6 @@
import Foundation
struct TemplateRoomChatViewModelInput {
struct TemplateRoomChatViewModelBindings {
var messageInput: String
}
@@ -16,6 +16,7 @@
import Foundation
struct TemplateRoomChatViewState {
struct TemplateRoomChatViewState: BindableState {
var bubbles: [TemplateRoomChatBubble]
var bindings: TemplateRoomChatViewModelBindings
}
@@ -46,7 +46,7 @@ enum MockTemplateRoomChatScreenState: MockScreenState, CaseIterable {
// can simulate service and viewModel actions here if needs be.
return AnyView(TemplateRoomChat(viewModel: viewModel)
return AnyView(TemplateRoomChat(viewModel: viewModel.context)
.addDependency(MockAvatarService.example))
}
}
@@ -27,7 +27,7 @@ struct TemplateRoomChat: View {
// MARK: Public
@ObservedObject var viewModel: TemplateRoomChatViewModel
@ObservedObject var viewModel: TemplateRoomChatViewModel.Context
var body: some View {
VStack {
@@ -46,7 +46,7 @@ struct TemplateRoomChat: View {
}
HStack {
TextField(VectorL10n.roomMessageShortPlaceholder, text: $viewModel.input.messageInput)
TextField(VectorL10n.roomMessageShortPlaceholder, text: $viewModel.bindings.messageInput)
.textFieldStyle(BorderedInputFieldStyle())
Button(action: {
@@ -61,12 +61,12 @@ struct TemplateRoomChat: View {
.toolbar {
ToolbarItem(placement: .primaryAction) {
Button(VectorL10n.done) {
viewModel.process(viewAction: .cancel)
viewModel.send(viewAction: .cancel)
}
}
ToolbarItem(placement: .cancellationAction) {
Button(VectorL10n.cancel) {
viewModel.process(viewAction: .cancel)
viewModel.send(viewAction: .cancel)
}
}
}
@@ -17,27 +17,26 @@
import SwiftUI
import Combine
@available(iOS 14.0, *)
class TemplateRoomChatViewModel: ObservableObject, TemplateRoomChatViewModelProtocol {
@available(iOS 14, *)
typealias TemplateRoomChatViewModelType = StateStoreViewModel<TemplateRoomChatViewState,
TemplateRoomChatStateAction,
TemplateRoomChatViewAction>
@available(iOS 14, *)
class TemplateRoomChatViewModel: TemplateRoomChatViewModelType, TemplateRoomChatViewModelProtocol {
// MARK: - Properties
// MARK: Private
private let templateRoomChatService: TemplateRoomChatServiceProtocol
private var cancellables = Set<AnyCancellable>()
// MARK: Public
@Published var input: TemplateRoomChatViewModelInput
@Published private(set) var viewState: TemplateRoomChatViewState
var completion: ((TemplateRoomChatViewModelResult) -> Void)?
// MARK: - Setup
init(templateRoomChatService: TemplateRoomChatServiceProtocol, initialState: TemplateRoomChatViewState? = nil) {
self.input = TemplateRoomChatViewModelInput(messageInput: "")
init(templateRoomChatService: TemplateRoomChatServiceProtocol) {
self.templateRoomChatService = templateRoomChatService
self.viewState = initialState ?? Self.defaultState(templateRoomChatService: templateRoomChatService)
super.init(initialViewState: Self.defaultState(templateRoomChatService: templateRoomChatService))
templateRoomChatService.chatMessagesSubject
.map(Self.makeBubbles(messages:))
.map(TemplateRoomChatStateAction.updateBubbles)
@@ -50,7 +49,8 @@ class TemplateRoomChatViewModel: ObservableObject, TemplateRoomChatViewModelProt
private static func defaultState(templateRoomChatService: TemplateRoomChatServiceProtocol) -> TemplateRoomChatViewState {
let bubbles = makeBubbles(messages: templateRoomChatService.chatMessagesSubject.value)
return TemplateRoomChatViewState(bubbles: bubbles)
let bindings = TemplateRoomChatViewModelBindings(messageInput: "")
return TemplateRoomChatViewState(bubbles: bubbles, bindings: bindings)
}
private static func makeBubbles(messages: [TemplateRoomChatMessage]) -> [TemplateRoomChatBubble] {
@@ -86,7 +86,7 @@ class TemplateRoomChatViewModel: ObservableObject, TemplateRoomChatViewModelProt
}
// MARK: - Public
func process(viewAction: TemplateRoomChatViewAction) {
override func process(viewAction: TemplateRoomChatViewAction) {
switch viewAction {
case .cancel:
cancel()
@@ -95,18 +95,10 @@ class TemplateRoomChatViewModel: ObservableObject, TemplateRoomChatViewModelProt
}
}
// MARK: - Private
/**
Send state actions to mutate the state.
*/
private func dispatch(action: TemplateRoomChatStateAction) {
Self.reducer(state: &self.viewState, action: action)
}
/**
A redux style reducer, all modifications to state happen here. Receives a state and a state action and produces a new state.
*/
private static func reducer(state: inout TemplateRoomChatViewState, action: TemplateRoomChatStateAction) {
override class func reducer(state: inout TemplateRoomChatViewState, action: TemplateRoomChatStateAction) {
switch action {
case .updateBubbles(let bubbles):
state.bubbles = bubbles