Made StateStoreViewModel state mutable and removed the reducer for all the features using it.

This commit is contained in:
Stefan Ceriu
2022-01-28 12:58:31 +02:00
committed by Stefan Ceriu
parent fc9e95aee8
commit 313b05485a
42 changed files with 324 additions and 431 deletions

View File

@@ -31,12 +31,7 @@ final class PollEditFormCoordinator: Coordinator, Presentable {
private let parameters: PollEditFormCoordinatorParameters
private let pollEditFormHostingController: UIViewController
private var _pollEditFormViewModel: Any? = nil
@available(iOS 14.0, *)
fileprivate var pollEditFormViewModel: PollEditFormViewModel {
return _pollEditFormViewModel as! PollEditFormViewModel
}
private var pollEditFormViewModel: PollEditFormViewModelProtocol
// MARK: Public
@@ -64,7 +59,7 @@ final class PollEditFormCoordinator: Coordinator, Presentable {
let view = PollEditForm(viewModel: viewModel.context)
_pollEditFormViewModel = viewModel
pollEditFormViewModel = viewModel
pollEditFormHostingController = VectorHostingController(rootView: view)
}
@@ -84,18 +79,18 @@ final class PollEditFormCoordinator: Coordinator, Presentable {
let pollStartContent = self.buildPollContentWithDetails(details)
self.pollEditFormViewModel.dispatch(action: .startLoading)
self.pollEditFormViewModel.startLoading()
self.parameters.room.sendPollStart(withContent: pollStartContent, threadId: nil, localEcho: nil) { [weak self] result in
guard let self = self else { return }
self.pollEditFormViewModel.dispatch(action: .stopLoading(nil))
self.pollEditFormViewModel.stopLoading()
self.completion?()
} failure: { [weak self] error in
guard let self = self else { return }
MXLog.error("Failed creating poll with error: \(String(describing: error))")
self.pollEditFormViewModel.dispatch(action: .stopLoading(.failedCreatingPoll))
self.pollEditFormViewModel.stopLoading(errorAlertType: .failedCreatingPoll)
}
case .update(let details):
@@ -103,10 +98,10 @@ final class PollEditFormCoordinator: Coordinator, Presentable {
fatalError()
}
self.pollEditFormViewModel.dispatch(action: .startLoading)
self.pollEditFormViewModel.startLoading()
guard let oldPollContent = MXEventContentPollStart(fromJSON: pollStartEvent.content) else {
self.pollEditFormViewModel.dispatch(action: .stopLoading(.failedUpdatingPoll))
self.pollEditFormViewModel.stopLoading(errorAlertType: .failedUpdatingPoll)
return
}
@@ -117,13 +112,13 @@ final class PollEditFormCoordinator: Coordinator, Presentable {
newContent: newPollContent, localEcho: nil) { [weak self] result in
guard let self = self else { return }
self.pollEditFormViewModel.dispatch(action: .stopLoading(nil))
self.pollEditFormViewModel.stopLoading()
self.completion?()
} failure: { [weak self] error in
guard let self = self else { return }
MXLog.error("Failed updating poll with error: \(String(describing: error))")
self.pollEditFormViewModel.dispatch(action: .stopLoading(.failedUpdatingPoll))
self.pollEditFormViewModel.stopLoading(errorAlertType: .failedUpdatingPoll)
}
}
}

View File

@@ -38,12 +38,6 @@ enum PollEditFormMode {
case editing
}
enum PollEditFormStateAction {
case viewAction(PollEditFormViewAction)
case startLoading
case stopLoading(PollEditFormErrorAlertInfo.AlertType?)
}
enum PollEditFormViewAction {
case addAnswerOption
case deleteAnswerOption(PollEditFormAnswerOption)

View File

@@ -23,11 +23,11 @@ struct PollEditFormViewModelParameters {
}
@available(iOS 14, *)
typealias PollEditFormViewModelType = StateStoreViewModel< PollEditFormViewState,
PollEditFormStateAction,
PollEditFormViewAction >
typealias PollEditFormViewModelType = StateStoreViewModel <PollEditFormViewState,
Never,
PollEditFormViewAction>
@available(iOS 14, *)
class PollEditFormViewModel: PollEditFormViewModelType {
class PollEditFormViewModel: PollEditFormViewModelType, PollEditFormViewModelProtocol {
private struct Constants {
static let minAnswerOptionsCount = 2
@@ -71,40 +71,32 @@ class PollEditFormViewModel: PollEditFormViewModelType {
completion?(.create(buildPollDetails()))
case .update:
completion?(.update(buildPollDetails()))
default:
dispatch(action: .viewAction(viewAction))
case .addAnswerOption:
state.bindings.answerOptions.append(PollEditFormAnswerOption(text: "", maxLength: Constants.maxAnswerOptionLength))
case .deleteAnswerOption(let answerOption):
state.bindings.answerOptions.removeAll { $0 == answerOption }
}
}
override class func reducer(state: inout PollEditFormViewState, action: PollEditFormStateAction) {
switch action {
case .viewAction(let viewAction):
switch viewAction {
case .deleteAnswerOption(let answerOption):
state.bindings.answerOptions.removeAll { $0 == answerOption }
case .addAnswerOption:
state.bindings.answerOptions.append(PollEditFormAnswerOption(text: "", maxLength: Constants.maxAnswerOptionLength))
default:
break
}
case .startLoading:
state.showLoadingIndicator = true
break
case .stopLoading(let error):
state.showLoadingIndicator = false
switch error {
case .failedCreatingPoll:
state.bindings.alertInfo = PollEditFormErrorAlertInfo(id: .failedCreatingPoll,
title: VectorL10n.pollEditFormPostFailureTitle,
subtitle: VectorL10n.pollEditFormPostFailureSubtitle)
case .failedUpdatingPoll:
state.bindings.alertInfo = PollEditFormErrorAlertInfo(id: .failedUpdatingPoll,
title: VectorL10n.pollEditFormUpdateFailureTitle,
subtitle: VectorL10n.pollEditFormUpdateFailureSubtitle)
case .none:
break
}
// MARK: - PollEditFormViewModelProtocol
func startLoading() {
state.showLoadingIndicator = true
}
func stopLoading(errorAlertType: PollEditFormErrorAlertInfo.AlertType?) {
state.showLoadingIndicator = false
switch errorAlertType {
case .failedCreatingPoll:
state.bindings.alertInfo = PollEditFormErrorAlertInfo(id: .failedCreatingPoll,
title: VectorL10n.pollEditFormPostFailureTitle,
subtitle: VectorL10n.pollEditFormPostFailureSubtitle)
case .failedUpdatingPoll:
state.bindings.alertInfo = PollEditFormErrorAlertInfo(id: .failedUpdatingPoll,
title: VectorL10n.pollEditFormUpdateFailureTitle,
subtitle: VectorL10n.pollEditFormUpdateFailureSubtitle)
case .none:
break
}
}
@@ -115,8 +107,8 @@ class PollEditFormViewModel: PollEditFormViewModelType {
return EditFormPollDetails(type: state.bindings.type,
question: state.bindings.question.text.trimmingCharacters(in: .whitespacesAndNewlines),
answerOptions: state.bindings.answerOptions.compactMap({ answerOption in
let text = answerOption.text.trimmingCharacters(in: .whitespacesAndNewlines)
return text.isEmpty ? nil : text
}))
let text = answerOption.text.trimmingCharacters(in: .whitespacesAndNewlines)
return text.isEmpty ? nil : text
}))
}
}

View File

@@ -0,0 +1,30 @@
//
// Copyright 2021 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
protocol PollEditFormViewModelProtocol {
var completion: ((PollEditFormViewModelResult) -> Void)? { get set }
func startLoading()
func stopLoading(errorAlertType: PollEditFormErrorAlertInfo.AlertType?)
}
extension PollEditFormViewModelProtocol {
func stopLoading() {
stopLoading(errorAlertType: nil)
}
}