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 Foundation
|
|
|
|
import SwiftUI
|
|
|
|
|
2022-01-14 15:02:04 +00:00
|
|
|
enum EditFormPollType {
|
|
|
|
case disclosed
|
|
|
|
case undisclosed
|
|
|
|
}
|
|
|
|
|
|
|
|
struct EditFormPollDetails {
|
|
|
|
let type: EditFormPollType
|
2022-01-12 06:44:53 +00:00
|
|
|
let question: String
|
|
|
|
let answerOptions: [String]
|
|
|
|
let maxSelections: UInt = 1
|
2022-01-14 15:02:04 +00:00
|
|
|
|
|
|
|
static var `default`: EditFormPollDetails {
|
|
|
|
EditFormPollDetails(type: .disclosed, question: "", answerOptions: ["", ""])
|
|
|
|
}
|
2022-01-12 06:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum PollEditFormMode {
|
|
|
|
case creation
|
|
|
|
case editing
|
|
|
|
}
|
|
|
|
|
2021-11-01 14:52:00 +00:00
|
|
|
enum PollEditFormViewAction {
|
|
|
|
case addAnswerOption
|
|
|
|
case deleteAnswerOption(PollEditFormAnswerOption)
|
|
|
|
case cancel
|
|
|
|
case create
|
2022-01-12 06:44:53 +00:00
|
|
|
case update
|
2021-11-01 14:52:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum PollEditFormViewModelResult {
|
|
|
|
case cancel
|
2022-01-14 15:02:04 +00:00
|
|
|
case create(EditFormPollDetails)
|
|
|
|
case update(EditFormPollDetails)
|
2022-01-13 13:57:26 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 14:52:00 +00:00
|
|
|
struct PollEditFormQuestion {
|
|
|
|
var text: String {
|
|
|
|
didSet {
|
|
|
|
text = String(text.prefix(maxLength))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let maxLength: Int
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PollEditFormAnswerOption: Identifiable, Equatable {
|
|
|
|
let id = UUID()
|
|
|
|
|
|
|
|
var text: String {
|
|
|
|
didSet {
|
|
|
|
text = String(text.prefix(maxLength))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let maxLength: Int
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PollEditFormViewState: BindableState {
|
2022-01-18 11:57:50 +00:00
|
|
|
var minAnswerOptionsCount: Int
|
2021-11-19 15:40:52 +00:00
|
|
|
var maxAnswerOptionsCount: Int
|
2022-01-12 06:44:53 +00:00
|
|
|
var mode: PollEditFormMode
|
2021-11-01 14:52:00 +00:00
|
|
|
var bindings: PollEditFormViewStateBindings
|
|
|
|
|
|
|
|
var confirmationButtonEnabled: Bool {
|
|
|
|
!bindings.question.text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty &&
|
2022-01-18 11:57:50 +00:00
|
|
|
bindings.answerOptions.filter({ !$0.text.isEmpty }).count >= minAnswerOptionsCount
|
2021-11-01 14:52:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var addAnswerOptionButtonEnabled: Bool {
|
|
|
|
bindings.answerOptions.count < maxAnswerOptionsCount
|
|
|
|
}
|
2021-11-19 15:40:52 +00:00
|
|
|
|
|
|
|
var showLoadingIndicator: Bool = false
|
2021-11-01 14:52:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct PollEditFormViewStateBindings {
|
|
|
|
var question: PollEditFormQuestion
|
|
|
|
var answerOptions: [PollEditFormAnswerOption]
|
2022-01-14 15:02:04 +00:00
|
|
|
var type: EditFormPollType
|
2021-11-19 15:40:52 +00:00
|
|
|
|
2022-01-12 06:44:53 +00:00
|
|
|
var alertInfo: PollEditFormErrorAlertInfo?
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PollEditFormErrorAlertInfo: Identifiable {
|
|
|
|
enum AlertType {
|
|
|
|
case failedCreatingPoll
|
|
|
|
case failedUpdatingPoll
|
|
|
|
}
|
|
|
|
|
|
|
|
let id: AlertType
|
|
|
|
let title: String
|
|
|
|
let subtitle: String
|
2021-11-01 14:52:00 +00:00
|
|
|
}
|