implemented the basics of the flow coordinator, the logic and the navigation flow to get to the space settings view

This commit is contained in:
Mauro Romito
2025-10-28 18:40:52 +01:00
committed by Mauro
parent 9cd181ef74
commit dfd757f2b6
19 changed files with 460 additions and 7 deletions
@@ -0,0 +1,45 @@
//
// Copyright 2025 Element Creations Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
// Please see LICENSE files in the repository root for full details.
//
import Combine
import SwiftUI
struct SpaceSettingsScreenCoordinatorParameters { }
enum SpaceSettingsScreenCoordinatorAction { }
final class SpaceSettingsScreenCoordinator: CoordinatorProtocol {
private let parameters: SpaceSettingsScreenCoordinatorParameters
private let viewModel: SpaceSettingsScreenViewModelProtocol
private var cancellables = Set<AnyCancellable>()
private let actionsSubject: PassthroughSubject<SpaceSettingsScreenCoordinatorAction, Never> = .init()
var actionsPublisher: AnyPublisher<SpaceSettingsScreenCoordinatorAction, Never> {
actionsSubject.eraseToAnyPublisher()
}
init(parameters: SpaceSettingsScreenCoordinatorParameters) {
self.parameters = parameters
viewModel = SpaceSettingsScreenViewModel()
}
func start() {
viewModel.actionsPublisher.sink { [weak self] action in
MXLog.info("Coordinator: received view model action: \(action)")
guard let self else { return }
switch action { }
}
.store(in: &cancellables)
}
func toPresentable() -> AnyView {
AnyView(SpaceSettingsScreen(context: viewModel.context))
}
}
@@ -0,0 +1,32 @@
//
// Copyright 2025 Element Creations Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
// Please see LICENSE files in the repository root for full details.
//
import Foundation
enum SpaceSettingsScreenViewModelAction { }
struct SpaceSettingsScreenViewState: BindableState {
var title: String
var placeholder: String
var counter = 0
var bindings: SpaceSettingsScreenViewStateBindings
}
struct SpaceSettingsScreenViewStateBindings {
var composerText: String
}
enum SpaceSettingsScreenViewAction {
case done
case textChanged
case incrementCounter
case decrementCounter
// Consider adding CustomStringConvertible conformance if the actions contain PII
}
@@ -0,0 +1,47 @@
//
// Copyright 2025 Element Creations Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
// Please see LICENSE files in the repository root for full details.
//
import Combine
import SwiftUI
typealias SpaceSettingsScreenViewModelType = StateStoreViewModelV2<SpaceSettingsScreenViewState, SpaceSettingsScreenViewAction>
class SpaceSettingsScreenViewModel: SpaceSettingsScreenViewModelType, SpaceSettingsScreenViewModelProtocol {
private let actionsSubject: PassthroughSubject<SpaceSettingsScreenViewModelAction, Never> = .init()
var actionsPublisher: AnyPublisher<SpaceSettingsScreenViewModelAction, Never> {
actionsSubject.eraseToAnyPublisher()
}
init() {
super.init(initialViewState: SpaceSettingsScreenViewState(title: "SpaceSettings title",
placeholder: "Enter something here",
bindings: .init(composerText: "Initial composer text")))
}
// MARK: - Public
override func process(viewAction: SpaceSettingsScreenViewAction) {
MXLog.info("View model: received view action: \(viewAction)")
switch viewAction {
case .done:
break
case .textChanged:
MXLog.info("View model: composer text changed to: \(state.bindings.composerText)")
case .incrementCounter:
Task {
try await Task.sleep(for: .seconds(.random(in: 1.0...2.0)))
state.counter += 1
}
case .decrementCounter:
Task {
try await Task.sleep(for: .seconds(.random(in: 1.0...2.0)))
state.counter -= 1
}
}
}
}
@@ -0,0 +1,14 @@
//
// Copyright 2025 Element Creations Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
// Please see LICENSE files in the repository root for full details.
//
import Combine
@MainActor
protocol SpaceSettingsScreenViewModelProtocol {
var actionsPublisher: AnyPublisher<SpaceSettingsScreenViewModelAction, Never> { get }
var context: SpaceSettingsScreenViewModelType.Context { get }
}
@@ -0,0 +1,71 @@
//
// Copyright 2025 Element Creations Ltd.
//
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
// Please see LICENSE files in the repository root for full details.
//
import Compound
import SwiftUI
struct SpaceSettingsScreen: View {
@Bindable var context: SpaceSettingsScreenViewModel.Context
var body: some View {
Form {
Section {
ListRow(label: .plain(title: context.viewState.placeholder),
kind: .textField(text: $context.composerText))
ListRow(label: .centeredAction(title: L10n.actionDone,
icon: \.leave),
kind: .button { context.send(viewAction: .done) })
}
Section {
ListRow(label: .default(title: "Counter", icon: \.chart),
details: .counter(context.viewState.counter),
kind: .label)
ListRow(label: .default(title: "Increment", icon: \.plus),
kind: .button { context.send(viewAction: .incrementCounter) })
ListRow(label: .default(title: "Decrement", icon: \.minus),
kind: .button { context.send(viewAction: .decrementCounter) })
}
}
.compoundList()
.navigationTitle(context.viewState.title)
.onChange(of: context.composerText) {
context.send(viewAction: .textChanged)
}
}
}
// MARK: - Previews
struct SpaceSettingsScreen_Previews: PreviewProvider, TestablePreview {
static let viewModel = makeViewModel()
static let incrementedViewModel = makeViewModel(counterValue: 1)
static var previews: some View {
NavigationStack {
SpaceSettingsScreen(context: viewModel.context)
}
.previewDisplayName("Initial")
NavigationStack {
SpaceSettingsScreen(context: incrementedViewModel.context)
}
.previewDisplayName("Incremented")
.snapshotPreferences(expect: incrementedViewModel.context.observe(\.viewState.counter).map { $0 == 1 }.eraseToStream())
}
static func makeViewModel(counterValue: Int = 0) -> SpaceSettingsScreenViewModel {
let viewModel = SpaceSettingsScreenViewModel()
for _ in 0..<counterValue {
viewModel.context.send(viewAction: .incrementCounter)
}
return viewModel
}
}