Report Content v2 (#659)

* created the empty files

* set up the view content

* connected the Room Coordinator to the ReportContent Coordinator

* added the loading indicators and the dismiss behaviour

* almost completed but I need to display the success indicator when the report is sent succesfully

* completed

* added an untranslated string

* tests

* Update ElementX/Sources/Screens/RoomScreen/RoomScreenCoordinator.swift

Co-authored-by: Doug <6060466+pixlwave@users.noreply.github.com>

* Update ElementX/Sources/Screens/ReportContent/View/ReportContentScreen.swift

Co-authored-by: Doug <6060466+pixlwave@users.noreply.github.com>

* pr comment

* pr suggestion

* removing unused identifiers

* fixing compilation error

* added a form text editor view

* changelog

---------

Co-authored-by: Doug <6060466+pixlwave@users.noreply.github.com>
This commit is contained in:
Mauro
2023-03-02 19:24:10 +01:00
committed by GitHub
parent 1c09a7eace
commit eed031c9cc
27 changed files with 472 additions and 85 deletions
@@ -0,0 +1,90 @@
//
// 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 SwiftUI
struct ReportContentCoordinatorParameters {
let itemID: String
let roomProxy: RoomProxyProtocol
weak var userIndicatorController: UserIndicatorControllerProtocol?
}
enum ReportContentCoordinatorAction {
case cancel
case finish
}
final class ReportContentCoordinator: CoordinatorProtocol {
private let parameters: ReportContentCoordinatorParameters
private var viewModel: ReportContentViewModelProtocol
var callback: ((ReportContentCoordinatorAction) -> Void)?
init(parameters: ReportContentCoordinatorParameters) {
self.parameters = parameters
viewModel = ReportContentViewModel(itemID: parameters.itemID, roomProxy: parameters.roomProxy)
}
// MARK: - Public
func start() {
viewModel.callback = { [weak self] action in
guard let self else { return }
switch action {
case .submitStarted:
self.startLoading()
case let .submitFailed(error):
self.stopLoading()
self.showError(description: error.localizedDescription)
case .submitFinished:
self.stopLoading()
self.callback?(.finish)
case .cancel:
self.callback?(.cancel)
}
}
}
func stop() {
stopLoading()
}
func toPresentable() -> AnyView {
AnyView(ReportContentScreen(context: viewModel.context))
}
// MARK: - Private
private static let loadingIndicatorIdentifier = "ReportContentLoading"
private func startLoading() {
parameters.userIndicatorController?.submitIndicator(
UserIndicator(id: Self.loadingIndicatorIdentifier,
type: .modal,
title: ElementL10n.sending,
persistent: true)
)
}
private func stopLoading() {
parameters.userIndicatorController?.retractIndicatorWithId(Self.loadingIndicatorIdentifier)
}
private func showError(description: String) {
parameters.userIndicatorController?.submitIndicator(UserIndicator(title: description))
}
}
@@ -0,0 +1,37 @@
//
// 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 ReportContentViewModelAction {
case cancel
case submitStarted
case submitFinished
case submitFailed(error: Error)
}
struct ReportContentViewState: BindableState {
var bindings: ReportContentViewStateBindings
}
struct ReportContentViewStateBindings {
var reasonText: String
}
enum ReportContentViewAction {
case cancel
case submit
}
@@ -0,0 +1,57 @@
//
// 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 SwiftUI
typealias ReportContentViewModelType = StateStoreViewModel<ReportContentViewState, ReportContentViewAction>
class ReportContentViewModel: ReportContentViewModelType, ReportContentViewModelProtocol {
var callback: ((ReportContentViewModelAction) -> Void)?
private let itemID: String
private let roomProxy: RoomProxyProtocol
init(itemID: String, roomProxy: RoomProxyProtocol) {
self.itemID = itemID
self.roomProxy = roomProxy
super.init(initialViewState: ReportContentViewState(bindings: ReportContentViewStateBindings(reasonText: "")))
}
// MARK: - Public
override func process(viewAction: ReportContentViewAction) async {
switch viewAction {
case .cancel:
callback?(.cancel)
case .submit:
await submitReport()
}
}
// MARK: Private
private func submitReport() async {
callback?(.submitStarted)
switch await roomProxy.reportContent(itemID, reason: state.bindings.reasonText) {
case .success:
MXLog.info("Submit Report Content succeeded")
callback?(.submitFinished)
case let .failure(error):
MXLog.error("Submit Report Content failed: \(error)")
callback?(.submitFailed(error: error))
}
}
}
@@ -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 Foundation
@MainActor
protocol ReportContentViewModelProtocol {
var callback: ((ReportContentViewModelAction) -> Void)? { get set }
var context: ReportContentViewModelType.Context { get }
}
@@ -0,0 +1,84 @@
//
// 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 SwiftUI
struct ReportContentScreen: View {
@Environment(\.horizontalSizeClass) private var horizontalSizeClass
@ObservedObject var context: ReportContentViewModel.Context
private var horizontalPadding: CGFloat {
horizontalSizeClass == .regular ? 50 : 16
}
var body: some View {
ScrollView {
mainContent
.padding(.top, 50)
.padding(.horizontal, horizontalPadding)
}
.scrollDismissesKeyboard(.immediately)
.background(Color.element.formBackground.ignoresSafeArea())
.navigationTitle(ElementL10n.reportContent)
.navigationBarTitleDisplayMode(.inline)
.toolbar { toolbar }
.interactiveDismissDisabled()
}
/// The main content of the view to be shown in a scroll view.
var mainContent: some View {
VStack(alignment: .leading, spacing: 24) {
infoText
reasonTextEditor
}
}
private var infoText: some View {
Text(ElementL10n.reportContentInfo)
.font(.element.body)
.foregroundColor(Color.element.primaryContent)
}
private var reasonTextEditor: some View {
FormTextEditor(text: $context.reasonText, placeholder: ElementL10n.reportContentCustomHint)
}
@ToolbarContentBuilder
private var toolbar: some ToolbarContent {
ToolbarItem(placement: .cancellationAction) {
Button(ElementL10n.actionCancel) {
context.send(viewAction: .cancel)
}
}
ToolbarItem(placement: .confirmationAction) {
Button(ElementL10n.actionSend) {
context.send(viewAction: .submit)
}
}
}
}
// MARK: - Previews
struct ReportContent_Previews: PreviewProvider {
static let viewModel = ReportContentViewModel(itemID: "", roomProxy: MockRoomProxy(displayName: nil))
static var previews: some View {
ReportContentScreen(context: viewModel.context)
}
}