mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-04 10:33:45 +00:00
Create poll UX (#1571)
* Add poll attachment button * Add poll creation feature flag * Setup navigation to CreatePollScreen * Add create/cancel actions * Add create poll screen ui skeleton * Add bindings in CreatePollScreen * Add logics in CreatePollScreen * Cleanup code * Fix option deletion crash * Fix conflicts * Add create poll logic * Add localisations * Fix test build errors * Fix crash * Add UTs * Add accessibility IDs * Add ui tests * Add 240 char limit * Fix addOption hide behavior * Add maxNumberOfOptions * Cleanup code * Move delete workaround in the view model * Use compound delete icon
This commit is contained in:
@@ -41,6 +41,7 @@ final class AppSettings {
|
||||
case swiftUITimelineEnabled
|
||||
case pollsInTimeline
|
||||
case richTextEditorEnabled
|
||||
case pollsCreationEnabled
|
||||
}
|
||||
|
||||
private static var suiteName: String = InfoPlistReader.main.appGroupIdentifier
|
||||
@@ -241,4 +242,7 @@ final class AppSettings {
|
||||
|
||||
@UserPreference(key: UserDefaultsKeys.richTextEditorEnabled, defaultValue: false, storageType: .userDefaults(store))
|
||||
var richTextEditorEnabled
|
||||
|
||||
@UserPreference(key: UserDefaultsKeys.pollsCreationEnabled, defaultValue: false, storageType: .userDefaults(store))
|
||||
var pollsCreationEnabled
|
||||
}
|
||||
|
||||
@@ -146,6 +146,7 @@ class RoomFlowCoordinator: FlowCoordinatorProtocol {
|
||||
return .messageForwarding(roomID: roomID, itemID: itemID)
|
||||
case (.dismissMessageForwarding, .messageForwarding(let roomID, _)):
|
||||
return .room(roomID: roomID)
|
||||
|
||||
case (.presentMapNavigator, .room(let roomID)):
|
||||
return .mapNavigator(roomID: roomID)
|
||||
case (.dismissMapNavigator, .mapNavigator(let roomID)):
|
||||
@@ -155,6 +156,12 @@ class RoomFlowCoordinator: FlowCoordinatorProtocol {
|
||||
return .notificationSettingsScreen(roomID: roomID)
|
||||
case (.dismissNotificationSettingsScreen, .notificationSettingsScreen(let roomID)):
|
||||
return .roomDetails(roomID: roomID, isRoot: false)
|
||||
|
||||
case (.presentCreatePollForm, .room(let roomID)):
|
||||
return .createPollForm(roomID: roomID)
|
||||
case (.dismissCreatePollForm, .createPollForm(let roomID)):
|
||||
return .room(roomID: roomID)
|
||||
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
@@ -217,6 +224,7 @@ class RoomFlowCoordinator: FlowCoordinatorProtocol {
|
||||
presentMessageForwarding(for: itemID)
|
||||
case (.messageForwarding, .dismissMessageForwarding, .room):
|
||||
break
|
||||
|
||||
case (.room, .presentMapNavigator(let mode), .mapNavigator):
|
||||
presentMapNavigator(interactionMode: mode)
|
||||
case (.mapNavigator, .dismissMapNavigator, .room):
|
||||
@@ -227,6 +235,11 @@ class RoomFlowCoordinator: FlowCoordinatorProtocol {
|
||||
case (.notificationSettingsScreen, .dismissNotificationSettingsScreen, .roomDetails):
|
||||
break
|
||||
|
||||
case (.room, .presentCreatePollForm, .createPollForm):
|
||||
presentCreatePollForm()
|
||||
case (.createPollForm, .dismissCreatePollForm, .room):
|
||||
break
|
||||
|
||||
default:
|
||||
fatalError("Unknown transition: \(context)")
|
||||
}
|
||||
@@ -324,6 +337,8 @@ class RoomFlowCoordinator: FlowCoordinatorProtocol {
|
||||
stateMachine.tryEvent(.presentEmojiPicker(itemID: itemID, selectedEmojis: selectedEmojis))
|
||||
case .presentLocationPicker:
|
||||
stateMachine.tryEvent(.presentMapNavigator(interactionMode: .picker))
|
||||
case .presentPollForm:
|
||||
stateMachine.tryEvent(.presentCreatePollForm)
|
||||
case .presentLocationViewer(_, let geoURI, let description):
|
||||
stateMachine.tryEvent(.presentMapNavigator(interactionMode: .viewOnly(geoURI: geoURI, description: description)))
|
||||
case .presentRoomMemberDetails(member: let member):
|
||||
@@ -559,7 +574,48 @@ class RoomFlowCoordinator: FlowCoordinatorProtocol {
|
||||
self?.stateMachine.tryEvent(.dismissMapNavigator)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private func presentCreatePollForm() {
|
||||
let navigationStackCoordinator = NavigationStackCoordinator()
|
||||
let coordinator = CreatePollScreenCoordinator(parameters: .init())
|
||||
navigationStackCoordinator.setRootCoordinator(coordinator)
|
||||
|
||||
coordinator.actions
|
||||
.sink { [weak self] action in
|
||||
guard let self else {
|
||||
return
|
||||
}
|
||||
|
||||
self.navigationSplitCoordinator.setSheetCoordinator(nil)
|
||||
|
||||
switch action {
|
||||
case .cancel:
|
||||
break
|
||||
case let .create(question, options, pollKind):
|
||||
Task {
|
||||
guard let roomProxy = self.roomProxy else {
|
||||
self.userIndicatorController.submitIndicator(UserIndicator(title: L10n.errorUnknown))
|
||||
return
|
||||
}
|
||||
|
||||
let result = await roomProxy.createPoll(question: question, answers: options, pollKind: pollKind)
|
||||
|
||||
switch result {
|
||||
case .success:
|
||||
break
|
||||
case .failure:
|
||||
self.userIndicatorController.submitIndicator(UserIndicator(title: L10n.errorUnknown))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
|
||||
navigationSplitCoordinator.setSheetCoordinator(navigationStackCoordinator) { [weak self] in
|
||||
self?.stateMachine.tryEvent(.dismissCreatePollForm)
|
||||
}
|
||||
}
|
||||
|
||||
private func presentRoomMemberDetails(member: RoomMemberProxyProtocol) {
|
||||
guard let roomProxy else {
|
||||
fatalError()
|
||||
@@ -689,6 +745,7 @@ private extension RoomFlowCoordinator {
|
||||
case roomMemberDetails(roomID: String, member: HashableRoomMemberWrapper)
|
||||
case messageForwarding(roomID: String, itemID: TimelineItemIdentifier)
|
||||
case notificationSettingsScreen(roomID: String)
|
||||
case createPollForm(roomID: String)
|
||||
}
|
||||
|
||||
struct EventUserInfo {
|
||||
@@ -726,6 +783,9 @@ private extension RoomFlowCoordinator {
|
||||
|
||||
case presentNotificationSettingsScreen
|
||||
case dismissNotificationSettingsScreen
|
||||
|
||||
case presentCreatePollForm
|
||||
case dismissCreatePollForm
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -42,6 +42,7 @@ internal enum Asset {
|
||||
internal static let locationPointerFull = ImageAsset(name: "images/location-pointer-full")
|
||||
internal static let locationPointer = ImageAsset(name: "images/location-pointer")
|
||||
internal static let timelineComposerSendMessage = ImageAsset(name: "images/timeline-composer-send-message")
|
||||
internal static let timelinePollAttachment = ImageAsset(name: "images/timeline-poll-attachment")
|
||||
internal static let timelinePoll = ImageAsset(name: "images/timeline-poll")
|
||||
internal static let timelineReactionAddMore = ImageAsset(name: "images/timeline-reaction-add-more")
|
||||
internal static let waitingGradient = ImageAsset(name: "images/waiting-gradient")
|
||||
|
||||
@@ -1464,6 +1464,27 @@ class RoomProxyMock: RoomProxyProtocol {
|
||||
return canUserRedactUserIDReturnValue
|
||||
}
|
||||
}
|
||||
//MARK: - createPoll
|
||||
|
||||
var createPollQuestionAnswersPollKindCallsCount = 0
|
||||
var createPollQuestionAnswersPollKindCalled: Bool {
|
||||
return createPollQuestionAnswersPollKindCallsCount > 0
|
||||
}
|
||||
var createPollQuestionAnswersPollKindReceivedArguments: (question: String, answers: [String], pollKind: Poll.Kind)?
|
||||
var createPollQuestionAnswersPollKindReceivedInvocations: [(question: String, answers: [String], pollKind: Poll.Kind)] = []
|
||||
var createPollQuestionAnswersPollKindReturnValue: Result<Void, RoomProxyError>!
|
||||
var createPollQuestionAnswersPollKindClosure: ((String, [String], Poll.Kind) async -> Result<Void, RoomProxyError>)?
|
||||
|
||||
func createPoll(question: String, answers: [String], pollKind: Poll.Kind) async -> Result<Void, RoomProxyError> {
|
||||
createPollQuestionAnswersPollKindCallsCount += 1
|
||||
createPollQuestionAnswersPollKindReceivedArguments = (question: question, answers: answers, pollKind: pollKind)
|
||||
createPollQuestionAnswersPollKindReceivedInvocations.append((question: question, answers: answers, pollKind: pollKind))
|
||||
if let createPollQuestionAnswersPollKindClosure = createPollQuestionAnswersPollKindClosure {
|
||||
return await createPollQuestionAnswersPollKindClosure(question, answers, pollKind)
|
||||
} else {
|
||||
return createPollQuestionAnswersPollKindReturnValue
|
||||
}
|
||||
}
|
||||
}
|
||||
class RoomTimelineProviderMock: RoomTimelineProviderProtocol {
|
||||
var updatePublisher: AnyPublisher<TimelineProviderUpdate, Never> {
|
||||
|
||||
@@ -40,6 +40,7 @@ struct A11yIdentifiers {
|
||||
static let migrationScreen = MigrationScreen()
|
||||
static let notificationSettingsScreen = NotificationSettingsScreen()
|
||||
static let notificationSettingsEditScreen = NotificationSettingsEditScreen()
|
||||
static let createPollScreen = CreatePollScreen()
|
||||
|
||||
struct AnalyticsPromptScreen {
|
||||
let title = "analytics_prompt-title"
|
||||
@@ -181,6 +182,19 @@ struct A11yIdentifiers {
|
||||
let roomTopic = "create_room-room_topic"
|
||||
}
|
||||
|
||||
struct CreatePollScreen {
|
||||
let question = "create_poll-question"
|
||||
let create = "create_poll-create"
|
||||
let addOption = "create_poll-add_option"
|
||||
let pollKind = "create_poll-kind"
|
||||
|
||||
private let optionPrefix = "create_poll-option"
|
||||
|
||||
func optionID(_ index: Int) -> String {
|
||||
"\(optionPrefix)-\(index)"
|
||||
}
|
||||
}
|
||||
|
||||
struct WelcomeScreen {
|
||||
let letsGo = "welcome_screen-lets_go"
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ enum ComposerToolbarViewModelAction {
|
||||
case displayMediaPicker
|
||||
case displayDocumentPicker
|
||||
case displayLocationPicker
|
||||
case displayPollForm
|
||||
|
||||
case handlePasteOrDrop(provider: NSItemProvider)
|
||||
|
||||
@@ -40,6 +41,7 @@ enum ComposerToolbarViewAction {
|
||||
case displayMediaPicker
|
||||
case displayDocumentPicker
|
||||
case displayLocationPicker
|
||||
case displayPollForm
|
||||
case handlePasteOrDrop(provider: NSItemProvider)
|
||||
}
|
||||
|
||||
|
||||
@@ -78,6 +78,8 @@ final class ComposerToolbarViewModel: ComposerToolbarViewModelType, ComposerTool
|
||||
actionsSubject.send(.displayDocumentPicker)
|
||||
case .displayLocationPicker:
|
||||
actionsSubject.send(.displayLocationPicker)
|
||||
case .displayPollForm:
|
||||
actionsSubject.send(.displayPollForm)
|
||||
case .handlePasteOrDrop(let provider):
|
||||
actionsSubject.send(.handlePasteOrDrop(provider: provider))
|
||||
}
|
||||
|
||||
@@ -65,6 +65,15 @@ struct RoomAttachmentPicker: View {
|
||||
PickerLabel(title: L10n.screenRoomAttachmentSourceLocation, icon: Image(asset: Asset.Images.locationPin))
|
||||
}
|
||||
.accessibilityLabel(A11yIdentifiers.roomScreen.attachmentPickerLocation)
|
||||
|
||||
if ServiceLocator.shared.settings.pollsCreationEnabled {
|
||||
Button {
|
||||
context.showAttachmentPopover = false
|
||||
context.send(viewAction: .displayPollForm)
|
||||
} label: {
|
||||
PickerLabel(title: L10n.screenRoomAttachmentSourcePoll, icon: Image(asset: Asset.Images.timelinePollAttachment))
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding(.top, isPresented ? 20 : 0)
|
||||
.background {
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
//
|
||||
// Copyright 2022 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 Combine
|
||||
import SwiftUI
|
||||
|
||||
struct CreatePollScreenCoordinatorParameters { }
|
||||
|
||||
enum CreatePollScreenCoordinatorAction {
|
||||
case cancel
|
||||
case create(question: String, options: [String], pollKind: Poll.Kind)
|
||||
}
|
||||
|
||||
final class CreatePollScreenCoordinator: CoordinatorProtocol {
|
||||
private let parameters: CreatePollScreenCoordinatorParameters
|
||||
private var viewModel: CreatePollScreenViewModelProtocol
|
||||
private let actionsSubject: PassthroughSubject<CreatePollScreenCoordinatorAction, Never> = .init()
|
||||
private var cancellables: Set<AnyCancellable> = .init()
|
||||
|
||||
var actions: AnyPublisher<CreatePollScreenCoordinatorAction, Never> {
|
||||
actionsSubject.eraseToAnyPublisher()
|
||||
}
|
||||
|
||||
init(parameters: CreatePollScreenCoordinatorParameters) {
|
||||
self.parameters = parameters
|
||||
|
||||
viewModel = CreatePollScreenViewModel()
|
||||
}
|
||||
|
||||
func start() {
|
||||
viewModel.actions.sink { [weak self] action in
|
||||
MXLog.info("Coordinator: received view model action: \(action)")
|
||||
|
||||
guard let self else { return }
|
||||
switch action {
|
||||
case let .create(question, options, pollKind):
|
||||
self.actionsSubject.send(.create(question: question, options: options, pollKind: pollKind))
|
||||
case .cancel:
|
||||
self.actionsSubject.send(.cancel)
|
||||
}
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
}
|
||||
|
||||
func toPresentable() -> AnyView {
|
||||
AnyView(CreatePollScreen(context: viewModel.context))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
//
|
||||
// Copyright 2022 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
|
||||
|
||||
enum CreatePollScreenViewModelAction {
|
||||
case create(question: String, options: [String], pollKind: Poll.Kind)
|
||||
case cancel
|
||||
}
|
||||
|
||||
struct CreatePollScreenViewState: BindableState {
|
||||
let maxNumberOfOptions = 20
|
||||
var bindings: CreatePollScreenViewStateBindings = .init()
|
||||
}
|
||||
|
||||
struct CreatePollScreenViewStateBindings {
|
||||
var question = ""
|
||||
var options: [Option] = [.init(), .init()]
|
||||
var isUndisclosed = false
|
||||
|
||||
struct Option: Identifiable, Equatable {
|
||||
let id = UUID()
|
||||
var text = ""
|
||||
}
|
||||
|
||||
var isCreateButtonDisabled: Bool {
|
||||
question.isEmpty || options.count < 2 || options.contains { $0.text.isEmpty }
|
||||
}
|
||||
}
|
||||
|
||||
enum CreatePollScreenViewAction {
|
||||
case cancel
|
||||
case create
|
||||
case deleteOption(index: Int)
|
||||
case addOption
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
//
|
||||
// Copyright 2022 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 Combine
|
||||
import SwiftUI
|
||||
|
||||
typealias CreatePollScreenViewModelType = StateStoreViewModel<CreatePollScreenViewState, CreatePollScreenViewAction>
|
||||
|
||||
class CreatePollScreenViewModel: CreatePollScreenViewModelType, CreatePollScreenViewModelProtocol {
|
||||
private var actionsSubject: PassthroughSubject<CreatePollScreenViewModelAction, Never> = .init()
|
||||
|
||||
var actions: AnyPublisher<CreatePollScreenViewModelAction, Never> {
|
||||
actionsSubject.eraseToAnyPublisher()
|
||||
}
|
||||
|
||||
init() {
|
||||
super.init(initialViewState: .init())
|
||||
}
|
||||
|
||||
// MARK: - Public
|
||||
|
||||
override func process(viewAction: CreatePollScreenViewAction) {
|
||||
switch viewAction {
|
||||
case .create:
|
||||
actionsSubject.send(.create(question: state.bindings.question,
|
||||
options: state.bindings.options.map(\.text),
|
||||
pollKind: state.bindings.isUndisclosed ? .undisclosed : .disclosed))
|
||||
case .cancel:
|
||||
actionsSubject.send(.cancel)
|
||||
case .deleteOption(let index):
|
||||
// fixes a crash that caused an index out of range when an option with the keyboard focus was deleted
|
||||
Task {
|
||||
try await Task.sleep(for: .milliseconds(100))
|
||||
guard state.bindings.options.indices.contains(index) else {
|
||||
return
|
||||
}
|
||||
state.bindings.options.remove(at: index)
|
||||
}
|
||||
case .addOption:
|
||||
guard state.bindings.options.count < state.maxNumberOfOptions else {
|
||||
return
|
||||
}
|
||||
state.bindings.options.append(.init())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
//
|
||||
// Copyright 2022 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 Combine
|
||||
|
||||
@MainActor
|
||||
protocol CreatePollScreenViewModelProtocol {
|
||||
var actions: AnyPublisher<CreatePollScreenViewModelAction, Never> { get }
|
||||
var context: CreatePollScreenViewModelType.Context { get }
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
//
|
||||
// Copyright 2022 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 Compound
|
||||
import SwiftUI
|
||||
|
||||
struct CreatePollScreen: View {
|
||||
@ObservedObject var context: CreatePollScreenViewModel.Context
|
||||
@FocusState var focus: Focus?
|
||||
|
||||
enum Focus: Hashable {
|
||||
case question
|
||||
case option(index: Int)
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
Form {
|
||||
questionSection
|
||||
optionsSection
|
||||
showResultsSection
|
||||
}
|
||||
.compoundForm()
|
||||
.scrollDismissesKeyboard(.immediately)
|
||||
.environment(\.editMode, .constant(.active))
|
||||
.navigationTitle(L10n.screenCreatePollTitle)
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
.toolbar { toolbar }
|
||||
.animation(.elementDefault, value: context.options)
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
private var questionSection: some View {
|
||||
Section(L10n.screenCreatePollQuestionDesc) {
|
||||
TextField(text: $context.question) {
|
||||
Text(L10n.screenCreatePollQuestionHint)
|
||||
.compoundFormTextFieldPlaceholder()
|
||||
}
|
||||
.introspect(.textField, on: .iOS(.v16)) { textField in
|
||||
textField.clearButtonMode = .whileEditing
|
||||
}
|
||||
.textFieldStyle(.compoundForm)
|
||||
.focused($focus, equals: .question)
|
||||
.accessibilityIdentifier(A11yIdentifiers.createPollScreen.question)
|
||||
}
|
||||
.compoundFormSection()
|
||||
}
|
||||
|
||||
private var optionsSection: some View {
|
||||
Section {
|
||||
ForEach(context.options) { option in
|
||||
if let index = context.options.firstIndex(of: option) {
|
||||
CreatePollOptionView(text: $context.options[index].text.limited(to: 240),
|
||||
placeholder: L10n.screenCreatePollAnswerHint(index + 1),
|
||||
canDeleteItem: context.options.count > 2) {
|
||||
if case .option(let focusedIndex) = focus, focusedIndex == index {
|
||||
focus = nil
|
||||
}
|
||||
|
||||
context.send(viewAction: .deleteOption(index: index))
|
||||
}
|
||||
.focused($focus, equals: .option(index: index))
|
||||
.accessibilityIdentifier(A11yIdentifiers.createPollScreen.optionID(index))
|
||||
}
|
||||
}
|
||||
.onMove { offsets, toOffset in
|
||||
context.options.move(fromOffsets: offsets, toOffset: toOffset)
|
||||
}
|
||||
|
||||
if context.options.count < context.viewState.maxNumberOfOptions {
|
||||
Button(L10n.screenCreatePollAddOptionBtn) {
|
||||
context.send(viewAction: .addOption)
|
||||
}
|
||||
.accessibilityIdentifier(A11yIdentifiers.createPollScreen.addOption)
|
||||
}
|
||||
}
|
||||
.compoundFormSection()
|
||||
}
|
||||
|
||||
private var showResultsSection: some View {
|
||||
Section {
|
||||
Toggle(L10n.screenCreatePollAnonymousDesc, isOn: $context.isUndisclosed)
|
||||
.accessibilityIdentifier(A11yIdentifiers.createPollScreen.pollKind)
|
||||
}
|
||||
.compoundFormSection()
|
||||
}
|
||||
|
||||
@ToolbarContentBuilder
|
||||
private var toolbar: some ToolbarContent {
|
||||
ToolbarItem(placement: .navigationBarLeading) {
|
||||
Button(L10n.actionCancel) {
|
||||
context.send(viewAction: .cancel)
|
||||
}
|
||||
}
|
||||
|
||||
ToolbarItem(placement: .confirmationAction) {
|
||||
Button(L10n.actionCreate) {
|
||||
context.send(viewAction: .create)
|
||||
}
|
||||
.disabled(context.viewState.bindings.isCreateButtonDisabled)
|
||||
.accessibilityIdentifier(A11yIdentifiers.createPollScreen.create)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private struct CreatePollOptionView: View {
|
||||
@Environment(\.editMode) var editMode
|
||||
@Binding var text: String
|
||||
let placeholder: String
|
||||
let canDeleteItem: Bool
|
||||
let deleteAction: () -> Void
|
||||
|
||||
var body: some View {
|
||||
HStack {
|
||||
if editMode?.wrappedValue == .active {
|
||||
Button(action: deleteAction) {
|
||||
CompoundIcon(\.delete)
|
||||
.font(.system(size: 24))
|
||||
.foregroundColor(.compound.iconCriticalPrimary)
|
||||
}
|
||||
.disabled(!canDeleteItem)
|
||||
.buttonStyle(PlainButtonStyle())
|
||||
}
|
||||
TextField(text: $text) {
|
||||
Text(placeholder)
|
||||
.compoundFormTextFieldPlaceholder()
|
||||
}
|
||||
.introspect(.textField, on: .iOS(.v16)) { textField in
|
||||
textField.clearButtonMode = .whileEditing
|
||||
}
|
||||
.textFieldStyle(.compoundForm)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Previews
|
||||
|
||||
struct CreatePollScreen_Previews: PreviewProvider {
|
||||
static let viewModel = CreatePollScreenViewModel()
|
||||
static var previews: some View {
|
||||
NavigationStack {
|
||||
CreatePollScreen(context: viewModel.context)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private extension Binding where Value == String {
|
||||
func limited(to limit: Int) -> Self {
|
||||
.init {
|
||||
wrappedValue
|
||||
} set: { newValue in
|
||||
wrappedValue = String(newValue.prefix(limit))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,7 @@ enum RoomScreenCoordinatorAction {
|
||||
case presentMediaUploadPreviewScreen(URL)
|
||||
case presentRoomDetails
|
||||
case presentLocationPicker
|
||||
case presentPollForm
|
||||
case presentLocationViewer(body: String, geoURI: GeoURI, description: String?)
|
||||
case presentEmojiPicker(itemID: TimelineItemIdentifier, selectedEmojis: Set<String>)
|
||||
case presentRoomMemberDetails(member: RoomMemberProxyProtocol)
|
||||
@@ -86,6 +87,8 @@ final class RoomScreenCoordinator: CoordinatorProtocol {
|
||||
actionsSubject.send(.presentMediaUploadPicker(.documents))
|
||||
case .displayLocationPicker:
|
||||
actionsSubject.send(.presentLocationPicker)
|
||||
case .displayPollForm:
|
||||
actionsSubject.send(.presentPollForm)
|
||||
case .displayMediaUploadPreviewScreen(let url):
|
||||
actionsSubject.send(.presentMediaUploadPreviewScreen(url))
|
||||
case .displayRoomMemberDetails(let member):
|
||||
|
||||
@@ -28,6 +28,7 @@ enum RoomScreenViewModelAction {
|
||||
case displayMediaPicker
|
||||
case displayDocumentPicker
|
||||
case displayLocationPicker
|
||||
case displayPollForm
|
||||
case displayMediaUploadPreviewScreen(url: URL)
|
||||
case displayRoomMemberDetails(member: RoomMemberProxyProtocol)
|
||||
case displayMessageForwarding(itemID: TimelineItemIdentifier)
|
||||
|
||||
@@ -158,6 +158,8 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol
|
||||
actionsSubject.send(.displayDocumentPicker)
|
||||
case .displayLocationPicker:
|
||||
actionsSubject.send(.displayLocationPicker)
|
||||
case .displayPollForm:
|
||||
actionsSubject.send(.displayPollForm)
|
||||
case .handlePasteOrDrop(let provider):
|
||||
handlePasteOrDrop(provider)
|
||||
case .composerModeChanged(mode: let mode):
|
||||
|
||||
+1
@@ -52,6 +52,7 @@ protocol DeveloperOptionsProtocol: AnyObject {
|
||||
var swiftUITimelineEnabled: Bool { get set }
|
||||
var pollsInTimelineEnabled: Bool { get set }
|
||||
var richTextEditorEnabled: Bool { get set }
|
||||
var pollsCreationEnabled: Bool { get set }
|
||||
}
|
||||
|
||||
extension AppSettings: DeveloperOptionsProtocol { }
|
||||
|
||||
+4
@@ -70,6 +70,10 @@ struct DeveloperOptionsScreen: View {
|
||||
Toggle(isOn: $context.pollsInTimelineEnabled) {
|
||||
Text("View polls in timeline")
|
||||
}
|
||||
|
||||
Toggle(isOn: $context.pollsCreationEnabled) {
|
||||
Text("View polls creation flow")
|
||||
}
|
||||
}
|
||||
|
||||
Section("Rich Text Editor") {
|
||||
|
||||
@@ -684,6 +684,17 @@ class RoomProxy: RoomProxyProtocol {
|
||||
}
|
||||
}
|
||||
|
||||
func createPoll(question: String, answers: [String], pollKind: Poll.Kind) async -> Result<Void, RoomProxyError> {
|
||||
await Task.dispatch(on: .global()) {
|
||||
do {
|
||||
return try .success(self.room.createPoll(question: question, answers: answers, maxSelections: 1, pollKind: .init(pollKind: pollKind), txnId: genTransactionId()))
|
||||
} catch {
|
||||
MXLog.error("Failed creating a poll: \(error)")
|
||||
return .failure(.failedCreatingPoll)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
/// Force the timeline to load member details so it can populate sender profiles whenever we add a timeline listener
|
||||
@@ -770,3 +781,14 @@ private final class RoomInfoUpdateListener: RoomInfoListener {
|
||||
onUpdateClosure()
|
||||
}
|
||||
}
|
||||
|
||||
private extension MatrixRustSDK.PollKind {
|
||||
init(pollKind: Poll.Kind) {
|
||||
switch pollKind {
|
||||
case .disclosed:
|
||||
self = .disclosed
|
||||
case .undisclosed:
|
||||
self = .undisclosed
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,6 +42,7 @@ enum RoomProxyError: Error, Equatable {
|
||||
case failedRemovingAvatar
|
||||
case failedUploadingAvatar
|
||||
case failedCheckingPermission
|
||||
case failedCreatingPoll
|
||||
}
|
||||
|
||||
// sourcery: AutoMockable
|
||||
@@ -165,6 +166,8 @@ protocol RoomProxyProtocol {
|
||||
func uploadAvatar(media: MediaInfo) async -> Result<Void, RoomProxyError>
|
||||
|
||||
func canUserRedact(userID: String) async -> Result<Bool, RoomProxyError>
|
||||
|
||||
func createPoll(question: String, answers: [String], pollKind: Poll.Kind) async -> Result<Void, RoomProxyError>
|
||||
}
|
||||
|
||||
extension RoomProxyProtocol {
|
||||
|
||||
@@ -606,7 +606,7 @@ private extension LocationRoomTimelineItemContent.AssetType {
|
||||
}
|
||||
}
|
||||
|
||||
private extension Poll.Kind {
|
||||
extension Poll.Kind {
|
||||
init(pollKind: MatrixRustSDK.PollKind) {
|
||||
switch pollKind {
|
||||
case .disclosed:
|
||||
|
||||
@@ -666,6 +666,11 @@ class MockScreen: Identifiable {
|
||||
let coordinator = CreateRoomCoordinator(parameters: parameters)
|
||||
navigationStackCoordinator.setRootCoordinator(coordinator)
|
||||
return navigationStackCoordinator
|
||||
case .createPoll:
|
||||
let navigationStackCoordinator = NavigationStackCoordinator()
|
||||
let coordinator = CreatePollScreenCoordinator(parameters: .init())
|
||||
navigationStackCoordinator.setRootCoordinator(coordinator)
|
||||
return navigationStackCoordinator
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
@@ -75,6 +75,7 @@ enum UITestsScreenIdentifier: String {
|
||||
case inviteUsersInRoomExistingMembers
|
||||
case createRoom
|
||||
case createRoomNoUsers
|
||||
case createPoll
|
||||
}
|
||||
|
||||
extension UITestsScreenIdentifier: CustomStringConvertible {
|
||||
|
||||
Reference in New Issue
Block a user