Rename screens (#826)

* Rename: AnalyticsPrompt -> AnalyticsPromptScreen

* Rename: AnalyticsSettings -> AnalyticsSettingsScreen

* Rename: Settings -> SettingsScreen

* Rename: SessionVerification -> SessionVerificationScreen

* Rename: BugReport -> BugReportScreen

* Rename: FilePreview -> FilePreviewScreen

* Rename: AnalyticsPrompt -> AnalyticsPromptScreen

* Rename: Invites -> InvitesScreen

* Rename: InviteUsers -> InviteUsersScreen

* Rename: MediaPicker -> MediaPickerScreen

* Delete unused UIActivityViewControllerWrapper

* Rename: ReportContent -> ReportContentScreen

* Rename: RoomDetails -> RoomDetailsScreen

* Rename: RoomMemberDetails -> RoomMemberDetailsScreen

* Rename: RoomMemberList -> RoomMemberListScreen

* Rename: Cleanup

* Renaming missed items

* Low effort attempt at fixing the flakey unit tests
This commit is contained in:
Stefan Ceriu
2023-04-24 18:48:07 +03:00
committed by GitHub
parent 9911ad2b9b
commit 24572c6582
88 changed files with 492 additions and 520 deletions
@@ -0,0 +1,51 @@
//
// 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 SessionVerificationScreenCoordinatorParameters {
let sessionVerificationControllerProxy: SessionVerificationControllerProxyProtocol
}
final class SessionVerificationScreenCoordinator: CoordinatorProtocol {
private let parameters: SessionVerificationScreenCoordinatorParameters
private var viewModel: SessionVerificationScreenViewModelProtocol
var callback: (() -> Void)?
init(parameters: SessionVerificationScreenCoordinatorParameters) {
self.parameters = parameters
viewModel = SessionVerificationScreenViewModel(sessionVerificationControllerProxy: parameters.sessionVerificationControllerProxy)
}
// MARK: - Public
func start() {
viewModel.callback = { [weak self] action in
guard let self else { return }
switch action {
case .finished:
self.callback?()
}
}
}
func toPresentable() -> AnyView {
AnyView(SessionVerificationScreen(context: viewModel.context))
}
}
@@ -0,0 +1,88 @@
//
// 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 SessionVerificationScreenViewModelAction {
case finished
}
struct SessionVerificationScreenViewState: BindableState {
var verificationState: SessionVerificationScreenStateMachine.State = .initial
var title: String? {
switch verificationState {
case .initial:
return L10n.screenSessionVerificationOpenExistingSessionTitle
case .requestingVerification:
return L10n.screenSessionVerificationWaitingToAcceptTitle
case .verificationRequestAccepted:
return L10n.screenSessionVerificationCompareEmojisTitle
case .startingSasVerification:
return nil
case .sasVerificationStarted:
return nil
case .showingChallenge:
return L10n.screenSessionVerificationCompareEmojisTitle
case .acceptingChallenge:
return L10n.screenSessionVerificationCompareEmojisTitle
case .decliningChallenge:
return nil
case .verified:
return L10n.commonVerificationComplete
case .cancelling:
return nil
case .cancelled:
return L10n.commonVerificationCancelled
}
}
var message: String {
switch verificationState {
case .initial:
return L10n.screenSessionVerificationOpenExistingSessionSubtitle
case .requestingVerification:
return L10n.screenSessionVerificationWaitingToAcceptSubtitle
case .verificationRequestAccepted:
return L10n.screenSessionVerificationRequestAcceptedSubtitle
case .startingSasVerification:
return L10n.commonWaiting
case .sasVerificationStarted:
return L10n.commonWaiting
case .acceptingChallenge:
return L10n.screenSessionVerificationCompareEmojisSubtitle
case .decliningChallenge:
return L10n.commonWaiting
case .cancelling:
return L10n.commonWaiting
case .showingChallenge:
return L10n.screenSessionVerificationCompareEmojisSubtitle
case .verified:
return L10n.screenSessionVerificationCompleteSubtitle
case .cancelled:
return L10n.screenSessionVerificationCancelledSubtitle
}
}
}
enum SessionVerificationScreenViewAction {
case requestVerification
case startSasVerification
case restart
case accept
case decline
case close
}
@@ -0,0 +1,149 @@
//
// 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
import SwiftState
class SessionVerificationScreenStateMachine {
/// States the SessionVerificationViewModel can find itself in
enum State: StateType {
/// The initial state, before verification started
case initial
/// Waiting for verification acceptance
case requestingVerification
/// Verification request accepted. Waiting for start
case verificationRequestAccepted
/// Waiting for SaS verification start
case startingSasVerification
/// A SaS verification flow has been started
case sasVerificationStarted
/// Verification accepted and emojis received
case showingChallenge(emojis: [SessionVerificationEmoji])
/// Emojis match locally
case acceptingChallenge(emojis: [SessionVerificationEmoji])
/// Emojis do not match locally
case decliningChallenge(emojis: [SessionVerificationEmoji])
/// Verification successful
case verified
/// User requested verification cancellation
case cancelling
/// The verification has been cancelled, remotely or locally
case cancelled
}
/// Events that can be triggered on the SessionVerification state machine
enum Event: EventType {
/// Request verification
case requestVerification
/// The current verification request has been accepted
case didAcceptVerificationRequest
/// Start a SaS verification flow
case startSasVerification
/// Started a SaS verification flow
case didStartSasVerification
/// Has received emojis
case didReceiveChallenge(emojis: [SessionVerificationEmoji])
/// Emojis match
case acceptChallenge
/// Emojis do not match
case declineChallenge
/// Remote accepted challenge
case didAcceptChallenge
/// Request cancellation
case cancel
/// Verification cancelled
case didCancel
/// Request failed
case didFail
/// Restart the verification flow
case restart
}
private let stateMachine: StateMachine<State, Event>
var state: State {
stateMachine.state
}
init() {
stateMachine = StateMachine(state: .initial)
configure()
}
// swiftlint:disable:next cyclomatic_complexity
private func configure() {
stateMachine.addRoutes(event: .requestVerification, transitions: [.initial => .requestingVerification])
stateMachine.addRoutes(event: .didAcceptVerificationRequest, transitions: [.requestingVerification => .verificationRequestAccepted])
stateMachine.addRoutes(event: .startSasVerification, transitions: [.verificationRequestAccepted => .startingSasVerification])
stateMachine.addRoutes(event: .didFail, transitions: [.requestingVerification => .initial])
stateMachine.addRoutes(event: .restart, transitions: [.cancelled => .initial])
// Transitions with associated values need to be handled through `addRouteMapping`
stateMachine.addRouteMapping { event, fromState, _ in
switch (event, fromState) {
case (.didStartSasVerification, _):
return .sasVerificationStarted
case (.didReceiveChallenge(let emojis), .sasVerificationStarted):
return .showingChallenge(emojis: emojis)
case (.acceptChallenge, .showingChallenge(let emojis)):
return .acceptingChallenge(emojis: emojis)
case (.didFail, .acceptingChallenge(let emojis)):
return .showingChallenge(emojis: emojis)
case (.didAcceptChallenge, .acceptingChallenge):
return .verified
case (.declineChallenge, .showingChallenge(let emojis)):
return .decliningChallenge(emojis: emojis)
case (.didFail, .decliningChallenge(let emojis)):
return .showingChallenge(emojis: emojis)
case (.cancel, _):
return .cancelling
case (.didCancel, _):
return .cancelled
default:
return nil
}
}
addTransitionHandler { context in
if let event = context.event {
MXLog.info("Transitioning from `\(context.fromState)` to `\(context.toState)` with event `\(event)`")
} else {
MXLog.info("Transitioning from \(context.fromState)` to `\(context.toState)`")
}
}
}
/// Attempt to move the state machine to another state through an event
/// It will either invoke the `transitionHandler` or the `errorHandler` depending on its current state
func processEvent(_ event: Event) {
stateMachine.tryEvent(event)
}
/// Registers a callback for processing state machine transitions
func addTransitionHandler(_ handler: @escaping StateMachine<State, Event>.Handler) {
stateMachine.addAnyHandler(.any => .any, handler: handler)
}
/// Registers a callback for processing state machine errors
func addErrorHandler(_ handler: @escaping StateMachine<State, Event>.Handler) {
stateMachine.addErrorHandler(handler: handler)
}
}
@@ -0,0 +1,182 @@
//
// 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 SessionVerificationViewModelType = StateStoreViewModel<SessionVerificationScreenViewState, SessionVerificationScreenViewAction>
class SessionVerificationScreenViewModel: SessionVerificationViewModelType, SessionVerificationScreenViewModelProtocol {
private let sessionVerificationControllerProxy: SessionVerificationControllerProxyProtocol
private var stateMachine: SessionVerificationScreenStateMachine
var callback: ((SessionVerificationScreenViewModelAction) -> Void)?
init(sessionVerificationControllerProxy: SessionVerificationControllerProxyProtocol,
initialState: SessionVerificationScreenViewState = SessionVerificationScreenViewState()) {
self.sessionVerificationControllerProxy = sessionVerificationControllerProxy
stateMachine = SessionVerificationScreenStateMachine()
super.init(initialViewState: initialState)
setupStateMachine()
sessionVerificationControllerProxy.callbacks
.receive(on: DispatchQueue.main)
.sink { [weak self] callback in
guard let self else { return }
switch callback {
case .acceptedVerificationRequest:
self.stateMachine.processEvent(.didAcceptVerificationRequest)
case .startedSasVerification:
self.stateMachine.processEvent(.didStartSasVerification)
case .receivedVerificationData(let emojis):
guard self.stateMachine.state == .sasVerificationStarted else {
MXLog.warning("Callbacks: Ignoring receivedVerificationData due to invalid state.")
return
}
self.stateMachine.processEvent(.didReceiveChallenge(emojis: emojis))
case .finished:
self.stateMachine.processEvent(.didAcceptChallenge)
case .cancelled:
self.stateMachine.processEvent(.didCancel)
case .failed:
self.stateMachine.processEvent(.didFail)
}
}
.store(in: &cancellables)
}
override func process(viewAction: SessionVerificationScreenViewAction) {
switch viewAction {
case .requestVerification:
stateMachine.processEvent(.requestVerification)
case .startSasVerification:
stateMachine.processEvent(.startSasVerification)
case .restart:
stateMachine.processEvent(.restart)
case .close:
guard stateMachine.state == .initial ||
stateMachine.state == .verified ||
stateMachine.state == .cancelled else {
stateMachine.processEvent(.cancel)
return
}
callback?(.finished)
case .accept:
stateMachine.processEvent(.acceptChallenge)
case .decline:
stateMachine.processEvent(.declineChallenge)
}
}
// MARK: - Private
private func setupStateMachine() {
stateMachine.addTransitionHandler { [weak self] context in
guard let self else { return }
self.state.verificationState = context.toState
switch (context.fromState, context.event, context.toState) {
case (.initial, .requestVerification, .requestingVerification):
self.requestVerification()
case (.verificationRequestAccepted, .startSasVerification, .startingSasVerification):
self.startSasVerification()
case (.showingChallenge, .acceptChallenge, .acceptingChallenge):
self.acceptChallenge()
case (.showingChallenge, .declineChallenge, .decliningChallenge):
self.declineChallenge()
case (_, .cancel, .cancelling):
self.cancelVerification()
case (_, _, .verified):
// Dismiss the success screen automatically.
Task {
try await Task.sleep(for: .seconds(2))
self.callback?(.finished)
}
default:
break
}
}
stateMachine.addErrorHandler { context in
MXLog.error("Failed transition with context: \(context)")
}
}
private func requestVerification() {
Task {
switch await sessionVerificationControllerProxy.requestVerification() {
case .success:
// Need to wait for the callback from the remote
break
case .failure:
stateMachine.processEvent(.didFail)
}
}
}
private func cancelVerification() {
Task {
switch await sessionVerificationControllerProxy.cancelVerification() {
case .success:
stateMachine.processEvent(.didCancel)
case .failure:
stateMachine.processEvent(.didFail)
}
}
}
private func startSasVerification() {
Task {
switch await sessionVerificationControllerProxy.startSasVerification() {
case .success:
// Need to wait for the callback from the remote
break
case .failure:
stateMachine.processEvent(.didFail)
}
}
}
private func acceptChallenge() {
Task {
switch await sessionVerificationControllerProxy.approveVerification() {
case .success:
// Need to wait for the callback from the remote
break
case .failure:
stateMachine.processEvent(.didFail)
}
}
}
private func declineChallenge() {
Task {
switch await sessionVerificationControllerProxy.declineVerification() {
case .success:
stateMachine.processEvent(.didCancel)
case .failure:
stateMachine.processEvent(.didFail)
}
}
}
}
@@ -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 SessionVerificationScreenViewModelProtocol {
var callback: ((SessionVerificationScreenViewModelAction) -> Void)? { get set }
var context: SessionVerificationViewModelType.Context { get }
}
@@ -0,0 +1,254 @@
//
// 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 SessionVerificationScreen: View {
@ObservedObject var context: SessionVerificationScreenViewModel.Context
var body: some View {
NavigationStack {
ScrollView {
VStack(spacing: 32) {
screenHeader
Spacer()
mainContent
}
.padding(.horizontal, 16)
.padding(.top, 24)
.frame(maxWidth: .infinity)
.navigationBarTitleDisplayMode(.inline)
.toolbar { toolbarContent }
}
.background(Color.element.background.ignoresSafeArea())
.safeAreaInset(edge: .bottom) { actionButtons.padding() }
}
.interactiveDismissDisabled() // Make sure dismissal goes through the state machine(s).
}
// MARK: - Private
private var headerImageName: String {
switch context.viewState.verificationState {
case .initial:
return "macbook.and.iphone"
case .cancelled:
return "exclamationmark.shield"
case .requestingVerification:
return "hourglass"
case .verificationRequestAccepted:
return "face.smiling"
case .startingSasVerification:
return "hourglass"
case .sasVerificationStarted:
return "hourglass"
case .cancelling:
return "hourglass"
case .acceptingChallenge:
return "hourglass"
case .decliningChallenge:
return "hourglass"
case .showingChallenge:
return "face.smiling"
case .verified:
return "checkmark.shield"
}
}
@ViewBuilder
private var screenHeader: some View {
VStack(spacing: 0) {
AuthenticationIconImage(image: Image(systemName: headerImageName))
.padding(.bottom, 16)
Text(context.viewState.title ?? "")
.font(.title2.bold())
.multilineTextAlignment(.center)
.foregroundColor(.element.primaryContent)
.padding(.bottom, 8)
Text(context.viewState.message)
.font(.subheadline)
.multilineTextAlignment(.center)
.foregroundColor(.element.tertiaryContent)
}
}
@ViewBuilder
private var mainContent: some View {
switch context.viewState.verificationState {
case .showingChallenge(let emojis):
emojisPanel(with: emojis)
.accessibilityIdentifier(A11yIdentifiers.sessionVerificationScreen.emojiWrapper)
case .acceptingChallenge(let emojis):
emojisPanel(with: emojis)
.accessibilityIdentifier(A11yIdentifiers.sessionVerificationScreen.emojiWrapper)
case .requestingVerification:
ProgressView()
.tint(.element.secondaryContent)
.scaleEffect(2)
default:
// In All other cases, we just want an empty view
EmptyView()
}
}
@ViewBuilder
private func emojisPanel(with emojis: [SessionVerificationEmoji]) -> some View {
VStack(spacing: 32) {
HStack(spacing: 16) {
ForEach(emojis.prefix(4), id: \.self) { emoji in
EmojiView(emoji: emoji)
}
}
HStack(spacing: 16) {
ForEach(emojis.suffix(from: 4), id: \.self) { emoji in
EmojiView(emoji: emoji)
}
}
}
}
@ViewBuilder
private var actionButtons: some View {
switch context.viewState.verificationState {
case .initial:
Button(L10n.actionStartVerification) {
context.send(viewAction: .requestVerification)
}
.buttonStyle(.elementAction(.xLarge))
.accessibilityIdentifier(A11yIdentifiers.sessionVerificationScreen.requestVerification)
case .cancelled:
Button(L10n.actionRetry) {
context.send(viewAction: .restart)
}
.buttonStyle(.elementAction(.xLarge))
case .verificationRequestAccepted:
Button(L10n.actionStart) {
context.send(viewAction: .startSasVerification)
}
.buttonStyle(.elementAction(.xLarge))
.accessibilityIdentifier(A11yIdentifiers.sessionVerificationScreen.startSasVerification)
case .showingChallenge:
VStack(spacing: 30) {
Button { context.send(viewAction: .accept) } label: {
Label(L10n.screenSessionVerificationTheyMatch, systemImage: "checkmark")
}
.buttonStyle(.elementAction(.xLarge))
.accessibilityIdentifier(A11yIdentifiers.sessionVerificationScreen.acceptChallenge)
Button(L10n.screenSessionVerificationTheyDontMatch) {
context.send(viewAction: .decline)
}
.font(.element.bodyBold)
.accessibilityIdentifier(A11yIdentifiers.sessionVerificationScreen.declineChallenge)
}
case .acceptingChallenge:
VStack(spacing: 30) {
Button { context.send(viewAction: .accept) } label: {
HStack(spacing: 16) {
ProgressView()
.tint(.element.background)
Label(L10n.screenSessionVerificationTheyMatch, systemImage: "checkmark")
}
}
.buttonStyle(.elementAction(.xLarge))
.accessibilityIdentifier(A11yIdentifiers.sessionVerificationScreen.acceptChallenge)
.disabled(true)
Button(L10n.screenSessionVerificationTheyDontMatch) {
context.send(viewAction: .decline)
}
.font(.element.bodyBold)
.accessibilityIdentifier(A11yIdentifiers.sessionVerificationScreen.declineChallenge)
.disabled(true)
}
default:
EmptyView()
}
}
@ToolbarContentBuilder
private var toolbarContent: some ToolbarContent {
ToolbarItem(placement: .cancellationAction) {
Button(L10n.actionCancel) {
context.send(viewAction: .close)
}
.foregroundColor(.element.accent)
.accessibilityIdentifier(A11yIdentifiers.sessionVerificationScreen.close)
}
}
struct EmojiView: View {
let emoji: SessionVerificationEmoji
var body: some View {
VStack(spacing: 16.0) {
Text(emoji.symbol)
.font(.element.largeTitleBold)
Text(emoji.description)
.font(.element.subheadline)
.foregroundColor(.element.secondaryContent)
}
.padding(8.0)
}
}
struct StateIcon: View {
let systemName: String
var body: some View {
Image(systemName: systemName)
.resizable()
.font(.element.body.weight(.light))
.scaledToFit()
.foregroundColor(.element.accent)
.frame(width: 100, height: 100)
}
}
}
// MARK: - Previews
struct SessionVerification_Previews: PreviewProvider {
static var previews: some View {
sessionVerificationScreen(state: .initial)
.previewDisplayName("Initial")
sessionVerificationScreen(state: .requestingVerification)
.previewDisplayName("Requesting Verification")
sessionVerificationScreen(state: .verificationRequestAccepted)
.previewDisplayName("Request Accepted")
sessionVerificationScreen(state: .cancelled)
.previewDisplayName("Cancelled")
sessionVerificationScreen(state: .showingChallenge(emojis: SessionVerificationControllerProxyMock.emojis))
.previewDisplayName("Showing Challenge")
sessionVerificationScreen(state: .verified)
.previewDisplayName("Verified")
}
static func sessionVerificationScreen(state: SessionVerificationScreenStateMachine.State) -> some View {
let viewModel = SessionVerificationScreenViewModel(sessionVerificationControllerProxy: SessionVerificationControllerProxyMock.configureMock(),
initialState: SessionVerificationScreenViewState(verificationState: state))
return SessionVerificationScreen(context: viewModel.context)
}
}