mirror of
https://gitlab.opencode.de/bwi/bundesmessenger/clients/bundesmessenger-ios.git
synced 2026-04-29 20:56:57 +02:00
Use dynamicMembers for StoreState for single source of truth. Fix bubble grouping and add send message function.
This commit is contained in:
+15
-18
@@ -17,24 +17,29 @@
|
||||
import SwiftUI
|
||||
import Combine
|
||||
|
||||
@available(iOS 14, *)
|
||||
typealias TemplateRoomListViewModelType = StateStoreViewModel<TemplateRoomListViewState,
|
||||
TemplateRoomListStateAction,
|
||||
TemplateRoomListViewAction>
|
||||
@available(iOS 14.0, *)
|
||||
class TemplateRoomListViewModel: ObservableObject, TemplateRoomListViewModelProtocol {
|
||||
class TemplateRoomListViewModel: TemplateRoomListViewModelType, TemplateRoomListViewModelProtocol {
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
// MARK: Private
|
||||
|
||||
private let templateRoomListService: TemplateRoomListServiceProtocol
|
||||
private var cancellables = Set<AnyCancellable>()
|
||||
|
||||
// MARK: Public
|
||||
@Published private(set) var viewState: TemplateRoomListViewState
|
||||
|
||||
var completion: ((TemplateRoomListViewModelResult) -> Void)?
|
||||
|
||||
// MARK: - Setup
|
||||
init(templateRoomListService: TemplateRoomListServiceProtocol, initialState: TemplateRoomListViewState? = nil) {
|
||||
|
||||
init(templateRoomListService: TemplateRoomListServiceProtocol) {
|
||||
self.templateRoomListService = templateRoomListService
|
||||
self.viewState = initialState ?? Self.defaultState(templateRoomListService: templateRoomListService)
|
||||
|
||||
super.init(initialViewState: Self.defaultState(templateRoomListService: templateRoomListService))
|
||||
|
||||
templateRoomListService.roomsSubject
|
||||
.map(TemplateRoomListStateAction.updateRooms)
|
||||
@@ -50,7 +55,8 @@ class TemplateRoomListViewModel: ObservableObject, TemplateRoomListViewModelProt
|
||||
}
|
||||
|
||||
// MARK: - Public
|
||||
func process(viewAction: TemplateRoomListViewAction) {
|
||||
|
||||
override func process(viewAction: TemplateRoomListViewAction) {
|
||||
switch viewAction {
|
||||
case .cancel:
|
||||
cancel()
|
||||
@@ -59,18 +65,7 @@ class TemplateRoomListViewModel: ObservableObject, TemplateRoomListViewModelProt
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
/**
|
||||
Send state actions to mutate the state.
|
||||
*/
|
||||
private func dispatch(action: TemplateRoomListStateAction) {
|
||||
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 TemplateRoomListViewState, action: TemplateRoomListStateAction) {
|
||||
override class func reducer(state: inout TemplateRoomListViewState, action: TemplateRoomListStateAction) {
|
||||
switch action {
|
||||
case .updateRooms(let rooms):
|
||||
state.rooms = rooms
|
||||
@@ -78,6 +73,8 @@ class TemplateRoomListViewModel: ObservableObject, TemplateRoomListViewModelProt
|
||||
UILog.debug("[TemplateRoomListViewModel] reducer with action \(action) produced state: \(state)")
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
private func done() {
|
||||
completion?(.done)
|
||||
}
|
||||
|
||||
+2
@@ -18,4 +18,6 @@ import Foundation
|
||||
|
||||
protocol TemplateRoomListViewModelProtocol {
|
||||
var completion: ((TemplateRoomListViewModelResult) -> Void)? { get set }
|
||||
@available(iOS 14, *)
|
||||
var context: TemplateRoomListViewModelType.Context { get }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user