2021-11-01 14:52:00 +00:00
|
|
|
//
|
|
|
|
// 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 SwiftUI
|
|
|
|
import Combine
|
|
|
|
|
2022-01-12 06:44:53 +00:00
|
|
|
struct PollEditFormViewModelParameters {
|
|
|
|
let mode: PollEditFormMode
|
|
|
|
let pollDetails: PollDetails
|
|
|
|
}
|
|
|
|
|
2021-11-01 14:52:00 +00:00
|
|
|
@available(iOS 14, *)
|
|
|
|
typealias PollEditFormViewModelType = StateStoreViewModel< PollEditFormViewState,
|
|
|
|
PollEditFormStateAction,
|
|
|
|
PollEditFormViewAction >
|
|
|
|
@available(iOS 14, *)
|
|
|
|
class PollEditFormViewModel: PollEditFormViewModelType {
|
|
|
|
|
|
|
|
private struct Constants {
|
|
|
|
static let maxAnswerOptionsCount = 20
|
2021-11-19 15:40:52 +00:00
|
|
|
static let maxQuestionLength = 340
|
|
|
|
static let maxAnswerOptionLength = 340
|
2021-11-01 14:52:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Properties
|
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
|
|
|
|
// MARK: Public
|
|
|
|
|
|
|
|
var completion: ((PollEditFormViewModelResult) -> Void)?
|
|
|
|
|
|
|
|
// MARK: - Setup
|
|
|
|
|
2022-01-12 06:44:53 +00:00
|
|
|
init(parameters: PollEditFormViewModelParameters) {
|
|
|
|
let state = PollEditFormViewState(
|
2021-11-01 14:52:00 +00:00
|
|
|
maxAnswerOptionsCount: Constants.maxAnswerOptionsCount,
|
2022-01-12 06:44:53 +00:00
|
|
|
mode: parameters.mode,
|
2021-11-01 14:52:00 +00:00
|
|
|
bindings: PollEditFormViewStateBindings(
|
2022-01-12 06:44:53 +00:00
|
|
|
question: PollEditFormQuestion(text: parameters.pollDetails.question, maxLength: Constants.maxQuestionLength),
|
2022-01-13 13:57:26 +00:00
|
|
|
answerOptions: parameters.pollDetails.answerOptions.map { PollEditFormAnswerOption(text: $0, maxLength: Constants.maxAnswerOptionLength) },
|
|
|
|
type: parameters.pollDetails.type
|
2021-11-01 14:52:00 +00:00
|
|
|
)
|
|
|
|
)
|
2022-01-12 06:44:53 +00:00
|
|
|
|
|
|
|
super.init(initialViewState: state)
|
2021-11-01 14:52:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Public
|
|
|
|
|
|
|
|
override func process(viewAction: PollEditFormViewAction) {
|
|
|
|
switch viewAction {
|
|
|
|
case .cancel:
|
|
|
|
completion?(.cancel)
|
|
|
|
case .create:
|
2022-01-12 06:44:53 +00:00
|
|
|
completion?(.create(buildPollDetails()))
|
|
|
|
case .update:
|
|
|
|
completion?(.update(buildPollDetails()))
|
2021-11-01 14:52:00 +00:00
|
|
|
default:
|
|
|
|
dispatch(action: .viewAction(viewAction))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2021-11-19 15:40:52 +00:00
|
|
|
case .startLoading:
|
|
|
|
state.showLoadingIndicator = true
|
|
|
|
break
|
|
|
|
case .stopLoading(let error):
|
|
|
|
state.showLoadingIndicator = false
|
|
|
|
|
2022-01-12 06:44:53 +00:00
|
|
|
switch error {
|
|
|
|
case .failedCreatingPoll:
|
|
|
|
state.bindings.alertInfo = PollEditFormErrorAlertInfo(id: .failedCreatingPoll,
|
|
|
|
title: VectorL10n.pollEditFormPostFailureTitle,
|
|
|
|
subtitle: VectorL10n.pollEditFormPostFailureSubtitle)
|
|
|
|
case .failedUpdatingPoll:
|
|
|
|
state.bindings.alertInfo = PollEditFormErrorAlertInfo(id: .failedCreatingPoll,
|
|
|
|
title: VectorL10n.pollEditFormUpdateFailureTitle,
|
|
|
|
subtitle: VectorL10n.pollEditFormUpdateFailureSubtitle)
|
|
|
|
case .none:
|
|
|
|
break
|
2021-11-19 15:40:52 +00:00
|
|
|
}
|
|
|
|
break
|
2021-11-01 14:52:00 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-12 06:44:53 +00:00
|
|
|
|
|
|
|
// MARK: - Private
|
|
|
|
|
|
|
|
private func buildPollDetails() -> PollDetails {
|
2022-01-13 13:57:26 +00:00
|
|
|
return PollDetails(type: state.bindings.type,
|
|
|
|
question: state.bindings.question.text.trimmingCharacters(in: .whitespacesAndNewlines),
|
2022-01-12 06:44:53 +00:00
|
|
|
answerOptions: state.bindings.answerOptions.compactMap({ answerOption in
|
|
|
|
let text = answerOption.text.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
|
|
return text.isEmpty ? nil : text
|
|
|
|
}))
|
|
|
|
}
|
2021-11-01 14:52:00 +00:00
|
|
|
}
|