vector-im/element-ios/issues/5114 - Allow editing poll start events.

This commit is contained in:
Stefan Ceriu
2022-01-12 08:44:53 +02:00
committed by Stefan Ceriu
parent a8165d23a4
commit 0980804213
15 changed files with 264 additions and 93 deletions

View File

@@ -22,6 +22,7 @@ import SwiftUI
struct PollEditFormCoordinatorParameters {
let room: MXRoom
let pollStartEvent: MXEvent?
}
final class PollEditFormCoordinator: Coordinator, Presentable {
@@ -51,9 +52,21 @@ final class PollEditFormCoordinator: Coordinator, Presentable {
init(parameters: PollEditFormCoordinatorParameters) {
self.parameters = parameters
let viewModel = PollEditFormViewModel()
let view = PollEditForm(viewModel: viewModel.context)
var viewModel: PollEditFormViewModel
if let startEvent = parameters.pollStartEvent,
let pollContent = MXEventContentPollStart(fromJSON: startEvent.content) {
viewModel = PollEditFormViewModel(parameters: PollEditFormViewModelParameters(mode: .editing,
pollDetails: PollDetails(question: pollContent.question,
answerOptions: pollContent.answerOptions.map { $0.text })))
} else {
viewModel = PollEditFormViewModel(parameters: PollEditFormViewModelParameters(mode: .creation,
pollDetails: PollDetails(question: "",
answerOptions: ["", ""])))
}
let view = PollEditForm(viewModel: viewModel.context)
_pollEditFormViewModel = viewModel
pollEditFormHostingController = VectorHostingController(rootView: view)
}
@@ -70,16 +83,9 @@ final class PollEditFormCoordinator: Coordinator, Presentable {
switch result {
case .cancel:
self.completion?()
case .create(let question, let answerOptions):
var options = [MXEventContentPollStartAnswerOption]()
for answerOption in answerOptions {
options.append(MXEventContentPollStartAnswerOption(uuid: UUID().uuidString, text: answerOption))
}
case .create(let details):
let pollStartContent = MXEventContentPollStart(question: question,
kind: kMXMessageContentKeyExtensiblePollKindDisclosed,
maxSelections: 1,
answerOptions: options)
let pollStartContent = self.buildPollContentWithDetails(details)
self.pollEditFormViewModel.dispatch(action: .startLoading)
@@ -92,15 +98,58 @@ final class PollEditFormCoordinator: Coordinator, Presentable {
guard let self = self else { return }
MXLog.error("Failed creating poll with error: \(String(describing: error))")
self.pollEditFormViewModel.dispatch(action: .stopLoading(error))
self.pollEditFormViewModel.dispatch(action: .stopLoading(.failedCreatingPoll))
}
case .update(let details):
guard let pollStartEvent = self.parameters.pollStartEvent else {
fatalError()
}
self.pollEditFormViewModel.dispatch(action: .startLoading)
guard let oldPollContent = MXEventContentPollStart(fromJSON: pollStartEvent.content) else {
self.pollEditFormViewModel.dispatch(action: .stopLoading(.failedUpdatingPoll))
return
}
let newPollContent = self.buildPollContentWithDetails(details)
self.parameters.room.sendPollUpdate(for: pollStartEvent,
oldContent: oldPollContent,
newContent: newPollContent, localEcho: nil) { [weak self] result in
guard let self = self else { return }
self.pollEditFormViewModel.dispatch(action: .stopLoading(nil))
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))
}
}
}
}
// MARK: - Private
// MARK: - Presentable
func toPresentable() -> UIViewController {
return pollEditFormHostingController
}
// MARK: - Private
private func buildPollContentWithDetails(_ details: PollDetails) -> MXEventContentPollStart {
var options = [MXEventContentPollStartAnswerOption]()
for answerOption in details.answerOptions {
options.append(MXEventContentPollStartAnswerOption(uuid: UUID().uuidString, text: answerOption))
}
return MXEventContentPollStart(question: details.question,
kind: (details.disclosed ? kMXMessageContentKeyExtensiblePollKindDisclosed : kMXMessageContentKeyExtensiblePollKindUndisclosed) ,
maxSelections: NSNumber(value: details.maxSelections),
answerOptions: options)
}
}

View File

@@ -19,10 +19,22 @@
import Foundation
import SwiftUI
struct PollDetails {
let question: String
let answerOptions: [String]
let maxSelections: UInt = 1
let disclosed: Bool = true
}
enum PollEditFormMode {
case creation
case editing
}
enum PollEditFormStateAction {
case viewAction(PollEditFormViewAction)
case startLoading
case stopLoading(Error?)
case stopLoading(PollEditFormErrorAlertInfo.AlertType?)
}
enum PollEditFormViewAction {
@@ -30,11 +42,13 @@ enum PollEditFormViewAction {
case deleteAnswerOption(PollEditFormAnswerOption)
case cancel
case create
case update
}
enum PollEditFormViewModelResult {
case cancel
case create(String, [String])
case create(PollDetails)
case update(PollDetails)
}
struct PollEditFormQuestion {
@@ -61,6 +75,7 @@ struct PollEditFormAnswerOption: Identifiable, Equatable {
struct PollEditFormViewState: BindableState {
var maxAnswerOptionsCount: Int
var mode: PollEditFormMode
var bindings: PollEditFormViewStateBindings
var confirmationButtonEnabled: Bool {
@@ -79,5 +94,16 @@ struct PollEditFormViewStateBindings {
var question: PollEditFormQuestion
var answerOptions: [PollEditFormAnswerOption]
var showsFailureAlert: Bool = false
var alertInfo: PollEditFormErrorAlertInfo?
}
struct PollEditFormErrorAlertInfo: Identifiable {
enum AlertType {
case failedCreatingPoll
case failedUpdatingPoll
}
let id: AlertType
let title: String
let subtitle: String
}

View File

@@ -28,7 +28,8 @@ enum MockPollEditFormScreenState: MockScreenState, CaseIterable {
}
var screenView: ([Any], AnyView) {
let viewModel = PollEditFormViewModel()
let viewModel = PollEditFormViewModel(parameters: PollEditFormViewModelParameters(mode: .editing,
pollDetails: PollDetails(question: "", answerOptions: ["", ""])))
return ([viewModel], AnyView(PollEditForm(viewModel: viewModel.context)))
}
}

View File

@@ -19,6 +19,11 @@
import SwiftUI
import Combine
struct PollEditFormViewModelParameters {
let mode: PollEditFormMode
let pollDetails: PollDetails
}
@available(iOS 14, *)
typealias PollEditFormViewModelType = StateStoreViewModel< PollEditFormViewState,
PollEditFormStateAction,
@@ -42,20 +47,17 @@ class PollEditFormViewModel: PollEditFormViewModelType {
// MARK: - Setup
init() {
super.init(initialViewState: Self.defaultState())
}
private static func defaultState() -> PollEditFormViewState {
return PollEditFormViewState(
init(parameters: PollEditFormViewModelParameters) {
let state = PollEditFormViewState(
maxAnswerOptionsCount: Constants.maxAnswerOptionsCount,
mode: parameters.mode,
bindings: PollEditFormViewStateBindings(
question: PollEditFormQuestion(text: "", maxLength: Constants.maxQuestionLength),
answerOptions: [PollEditFormAnswerOption(text: "", maxLength: Constants.maxAnswerOptionLength),
PollEditFormAnswerOption(text: "", maxLength: Constants.maxAnswerOptionLength)
]
question: PollEditFormQuestion(text: parameters.pollDetails.question, maxLength: Constants.maxQuestionLength),
answerOptions: parameters.pollDetails.answerOptions.map { PollEditFormAnswerOption(text: $0, maxLength: Constants.maxAnswerOptionLength) }
)
)
super.init(initialViewState: state)
}
// MARK: - Public
@@ -65,11 +67,9 @@ class PollEditFormViewModel: PollEditFormViewModelType {
case .cancel:
completion?(.cancel)
case .create:
completion?(.create(state.bindings.question.text.trimmingCharacters(in: .whitespacesAndNewlines),
state.bindings.answerOptions.compactMap({ answerOption in
let text = answerOption.text.trimmingCharacters(in: .whitespacesAndNewlines)
return text.isEmpty ? nil : text
})))
completion?(.create(buildPollDetails()))
case .update:
completion?(.update(buildPollDetails()))
default:
dispatch(action: .viewAction(viewAction))
}
@@ -92,10 +92,29 @@ class PollEditFormViewModel: PollEditFormViewModelType {
case .stopLoading(let error):
state.showLoadingIndicator = false
if error != nil {
state.bindings.showsFailureAlert = true
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
}
break
}
}
// MARK: - Private
private func buildPollDetails() -> PollDetails {
return PollDetails(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
}))
}
}

View File

@@ -76,17 +76,19 @@ struct PollEditForm: View {
Spacer()
Button(VectorL10n.pollEditFormCreatePoll) {
viewModel.send(viewAction: .create)
if viewModel.viewState.mode == .creation {
Button(VectorL10n.pollEditFormCreatePoll) {
viewModel.send(viewAction: .create)
}
.buttonStyle(PrimaryActionButtonStyle())
.disabled(!viewModel.viewState.confirmationButtonEnabled)
}
.buttonStyle(PrimaryActionButtonStyle())
.disabled(!viewModel.viewState.confirmationButtonEnabled)
}
.padding()
.activityIndicator(show: viewModel.viewState.showLoadingIndicator)
.alert(isPresented: $viewModel.showsFailureAlert) {
Alert(title: Text(VectorL10n.pollEditFormPostFailureTitle),
message: Text(VectorL10n.pollEditFormPostFailureSubtitle),
.alert(item: $viewModel.alertInfo) { info in
Alert(title: Text(info.title),
message: Text(info.subtitle),
dismissButton: .default(Text(VectorL10n.ok)))
}
.frame(minHeight: proxy.size.height) // Make the VStack fill the ScrollView's parent
@@ -101,6 +103,15 @@ struct PollEditForm: View {
.font(.headline)
.foregroundColor(theme.colors.primaryContent)
}
ToolbarItem(placement: .navigationBarTrailing) {
if viewModel.viewState.mode == .editing {
Button(VectorL10n.save, action: {
viewModel.send(viewAction: .update)
})
.disabled(!viewModel.viewState.confirmationButtonEnabled)
}
}
}
.navigationBarTitleDisplayMode(.inline)
}