2021-12-16 11:47:02 +00:00
|
|
|
// File created from FlowTemplate
|
|
|
|
// $ createRootCoordinator.sh Onboarding/SplashScreen Onboarding OnboardingSplashScreen
|
|
|
|
/*
|
|
|
|
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 UIKit
|
|
|
|
|
|
|
|
/// OnboardingCoordinator input parameters
|
|
|
|
struct OnboardingCoordinatorParameters {
|
|
|
|
|
|
|
|
/// The navigation router that manage physical navigation
|
|
|
|
let router: NavigationRouterType
|
2022-01-12 17:11:29 +00:00
|
|
|
/// The credentials to use if a soft logout has taken place.
|
|
|
|
let softLogoutCredentials: MXCredentials?
|
2021-12-16 11:47:02 +00:00
|
|
|
|
2022-01-12 17:11:29 +00:00
|
|
|
init(router: NavigationRouterType? = nil,
|
|
|
|
softLogoutCredentials: MXCredentials? = nil) {
|
2022-01-17 17:37:59 +00:00
|
|
|
self.router = router ?? NavigationRouter(navigationController: RiotNavigationController(isLockedToPortraitOnPhone: true))
|
2022-01-12 17:11:29 +00:00
|
|
|
self.softLogoutCredentials = softLogoutCredentials
|
2021-12-16 11:47:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@objcMembers
|
2022-01-18 16:48:52 +00:00
|
|
|
/// A coordinator to manage the full onboarding flow with pre-auth screens, authentication and setup screens once signed in.
|
2022-01-27 14:44:10 +00:00
|
|
|
final class OnboardingCoordinator: NSObject, OnboardingCoordinatorProtocol {
|
2022-01-18 16:48:52 +00:00
|
|
|
|
2021-12-16 11:47:02 +00:00
|
|
|
// MARK: - Properties
|
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
|
|
|
|
private let parameters: OnboardingCoordinatorParameters
|
2022-01-18 16:48:52 +00:00
|
|
|
// TODO: these can likely be consolidated using an additional authType.
|
|
|
|
/// The any registration parameters for AuthenticationViewController from a server provisioning link.
|
2022-01-12 17:11:29 +00:00
|
|
|
private var externalRegistrationParameters: [AnyHashable: Any]?
|
2022-01-18 16:48:52 +00:00
|
|
|
/// A custom homeserver to be shown when logging in.
|
2022-01-12 17:11:29 +00:00
|
|
|
private var customHomeserver: String?
|
2022-01-18 16:48:52 +00:00
|
|
|
/// A custom identity server to be used once logged in.
|
2022-01-12 17:11:29 +00:00
|
|
|
private var customIdentityServer: String?
|
2021-12-16 11:47:02 +00:00
|
|
|
|
2022-01-28 11:53:50 +00:00
|
|
|
// MARK: Navigation State
|
2021-12-16 11:47:02 +00:00
|
|
|
private var navigationRouter: NavigationRouterType {
|
|
|
|
parameters.router
|
|
|
|
}
|
2022-05-24 11:26:39 +00:00
|
|
|
/// A strong ref to the legacy authVC as we need to init early to preload its view.
|
|
|
|
private let legacyAuthenticationCoordinator: LegacyAuthenticationCoordinator
|
|
|
|
/// The currently active authentication coordinator, otherwise `nil`.
|
|
|
|
private weak var authenticationCoordinator: AuthenticationCoordinatorProtocol?
|
2022-04-14 10:06:12 +00:00
|
|
|
#warning("This might be removable when SSO comes through the AuthenticationService?")
|
2022-02-28 14:27:32 +00:00
|
|
|
/// A boolean to prevent authentication being shown when already in progress.
|
2022-04-14 10:06:12 +00:00
|
|
|
private var isShowingLegacyAuthentication = false
|
2022-01-28 11:53:50 +00:00
|
|
|
|
|
|
|
// MARK: Screen results
|
2022-01-12 17:11:29 +00:00
|
|
|
private var splashScreenResult: OnboardingSplashScreenViewModelResult?
|
2022-02-02 12:24:22 +00:00
|
|
|
private var useCaseResult: OnboardingUseCaseViewModelResult?
|
2022-05-06 12:08:54 +00:00
|
|
|
/// The flow being used for authentication.
|
|
|
|
private var authenticationFlow: AuthenticationFlow?
|
|
|
|
/// The type of authentication used to login/register.
|
|
|
|
private var authenticationType: AuthenticationType?
|
2022-02-24 17:24:04 +00:00
|
|
|
private var session: MXSession?
|
2022-03-25 11:58:35 +00:00
|
|
|
/// A place to store the image selected in the avatar screen until it has been saved.
|
|
|
|
private var selectedAvatar: UIImage?
|
2022-02-24 17:24:04 +00:00
|
|
|
|
2022-03-15 11:51:17 +00:00
|
|
|
private var shouldShowDisplayNameScreen = false
|
|
|
|
private var shouldShowAvatarScreen = false
|
|
|
|
|
2022-02-24 17:24:04 +00:00
|
|
|
/// Whether all of the onboarding steps have been completed or not. `false` if there are more screens to be shown.
|
2022-02-28 14:27:32 +00:00
|
|
|
private var onboardingFinished = false
|
|
|
|
/// Whether authentication is complete. `true` once authenticated, verified and the app is ready to be shown.
|
|
|
|
private var authenticationFinished = false
|
2021-12-16 11:47:02 +00:00
|
|
|
|
|
|
|
// MARK: Public
|
|
|
|
|
|
|
|
// Must be used only internally
|
|
|
|
var childCoordinators: [Coordinator] = []
|
2022-01-12 17:11:29 +00:00
|
|
|
var completion: (() -> Void)?
|
2021-12-16 11:47:02 +00:00
|
|
|
|
|
|
|
// MARK: - Setup
|
|
|
|
|
|
|
|
init(parameters: OnboardingCoordinatorParameters) {
|
|
|
|
self.parameters = parameters
|
2022-02-17 16:50:02 +00:00
|
|
|
|
2022-05-24 11:26:39 +00:00
|
|
|
// Preload the legacy authVC (it is *really* slow to load in realtime)
|
2022-04-14 10:06:12 +00:00
|
|
|
let authenticationParameters = LegacyAuthenticationCoordinatorParameters(navigationRouter: parameters.router, canPresentAdditionalScreens: false)
|
2022-05-24 11:26:39 +00:00
|
|
|
legacyAuthenticationCoordinator = LegacyAuthenticationCoordinator(parameters: authenticationParameters)
|
2022-02-17 16:50:02 +00:00
|
|
|
|
2022-01-12 17:11:29 +00:00
|
|
|
super.init()
|
|
|
|
}
|
2021-12-16 11:47:02 +00:00
|
|
|
|
|
|
|
// MARK: - Public
|
|
|
|
|
|
|
|
func start() {
|
2022-01-18 16:48:52 +00:00
|
|
|
// TODO: Manage a separate flow for soft logout that just uses AuthenticationCoordinator
|
2022-05-10 10:23:42 +00:00
|
|
|
if parameters.softLogoutCredentials == nil, BuildSettings.authScreenShowRegister {
|
2022-01-12 17:11:29 +00:00
|
|
|
showSplashScreen()
|
2021-12-16 11:47:02 +00:00
|
|
|
} else {
|
2022-04-14 10:06:12 +00:00
|
|
|
showLegacyAuthenticationScreen()
|
2021-12-16 11:47:02 +00:00
|
|
|
}
|
2022-01-12 17:11:29 +00:00
|
|
|
}
|
2021-12-16 11:47:02 +00:00
|
|
|
|
|
|
|
func toPresentable() -> UIViewController {
|
|
|
|
navigationRouter.toPresentable()
|
|
|
|
}
|
|
|
|
|
2022-01-18 16:48:52 +00:00
|
|
|
/// Force a registration process based on a predefined set of parameters from a server provisioning link.
|
|
|
|
/// For more information see `AuthenticationViewController.externalRegistrationParameters`.
|
2022-01-12 17:11:29 +00:00
|
|
|
func update(externalRegistrationParameters: [AnyHashable: Any]) {
|
|
|
|
self.externalRegistrationParameters = externalRegistrationParameters
|
2022-05-24 11:26:39 +00:00
|
|
|
legacyAuthenticationCoordinator.update(externalRegistrationParameters: externalRegistrationParameters)
|
2022-01-12 17:11:29 +00:00
|
|
|
}
|
|
|
|
|
2022-01-18 16:48:52 +00:00
|
|
|
/// Set up the authentication screen with the specified homeserver and/or identity server.
|
2022-01-27 14:44:10 +00:00
|
|
|
func updateHomeserver(_ homeserver: String?, andIdentityServer identityServer: String?) {
|
2022-01-12 17:11:29 +00:00
|
|
|
self.customHomeserver = homeserver
|
|
|
|
self.customIdentityServer = identityServer
|
2022-05-24 11:26:39 +00:00
|
|
|
legacyAuthenticationCoordinator.updateHomeserver(homeserver, andIdentityServer: identityServer)
|
2022-01-12 17:11:29 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 17:24:04 +00:00
|
|
|
// MARK: - Pre-Authentication
|
2022-01-12 17:11:29 +00:00
|
|
|
|
2022-01-18 16:48:52 +00:00
|
|
|
/// Show the onboarding splash screen as the root module in the flow.
|
2022-01-12 17:11:29 +00:00
|
|
|
private func showSplashScreen() {
|
2022-02-24 17:24:04 +00:00
|
|
|
MXLog.debug("[OnboardingCoordinator] showSplashScreen")
|
|
|
|
|
2022-01-27 14:44:10 +00:00
|
|
|
let coordinator = OnboardingSplashScreenCoordinator()
|
2022-01-12 17:11:29 +00:00
|
|
|
coordinator.completion = { [weak self, weak coordinator] result in
|
|
|
|
guard let self = self, let coordinator = coordinator else { return }
|
|
|
|
self.splashScreenCoordinator(coordinator, didCompleteWith: result)
|
2021-12-16 11:47:02 +00:00
|
|
|
}
|
2022-01-12 17:11:29 +00:00
|
|
|
|
|
|
|
coordinator.start()
|
|
|
|
add(childCoordinator: coordinator)
|
|
|
|
|
2022-05-12 16:40:36 +00:00
|
|
|
navigationRouter.setRootModule(coordinator) { [weak self] in
|
|
|
|
self?.remove(childCoordinator: coordinator)
|
|
|
|
}
|
2022-01-12 17:11:29 +00:00
|
|
|
}
|
|
|
|
|
2022-01-18 16:48:52 +00:00
|
|
|
/// Displays the next view in the flow after the splash screen.
|
2022-01-12 17:11:29 +00:00
|
|
|
private func splashScreenCoordinator(_ coordinator: OnboardingSplashScreenCoordinator, didCompleteWith result: OnboardingSplashScreenViewModelResult) {
|
|
|
|
splashScreenResult = result
|
2022-02-02 12:24:22 +00:00
|
|
|
|
2022-05-24 11:26:39 +00:00
|
|
|
// Set the auth type early on the legacy auth to allow network requests to finish during display of the use case screen.
|
|
|
|
legacyAuthenticationCoordinator.update(authenticationFlow: result.flow)
|
2022-02-04 16:10:03 +00:00
|
|
|
|
2022-02-02 12:24:22 +00:00
|
|
|
switch result {
|
|
|
|
case .register:
|
2022-02-10 12:59:13 +00:00
|
|
|
showUseCaseSelectionScreen()
|
2022-02-02 12:24:22 +00:00
|
|
|
case .login:
|
2022-05-24 11:26:39 +00:00
|
|
|
if BuildSettings.onboardingEnableNewAuthenticationFlow {
|
|
|
|
beginAuthentication(with: .login, onStart: coordinator.stop)
|
|
|
|
} else {
|
|
|
|
coordinator.stop()
|
|
|
|
showLegacyAuthenticationScreen()
|
|
|
|
}
|
2022-02-02 12:24:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Show the use case screen for new users.
|
2022-05-12 16:40:36 +00:00
|
|
|
private func showUseCaseSelectionScreen(animated: Bool = true) {
|
2022-02-24 17:24:04 +00:00
|
|
|
MXLog.debug("[OnboardingCoordinator] showUseCaseSelectionScreen")
|
|
|
|
|
2022-02-17 11:19:32 +00:00
|
|
|
let coordinator = OnboardingUseCaseSelectionCoordinator()
|
2022-02-02 12:24:22 +00:00
|
|
|
coordinator.completion = { [weak self, weak coordinator] result in
|
|
|
|
guard let self = self, let coordinator = coordinator else { return }
|
2022-02-17 11:19:32 +00:00
|
|
|
self.useCaseSelectionCoordinator(coordinator, didCompleteWith: result)
|
2022-02-02 12:24:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
coordinator.start()
|
|
|
|
add(childCoordinator: coordinator)
|
|
|
|
|
2022-02-24 17:24:04 +00:00
|
|
|
if navigationRouter.modules.isEmpty {
|
2022-05-12 16:40:36 +00:00
|
|
|
navigationRouter.setRootModule(coordinator) { [weak self] in
|
|
|
|
self?.remove(childCoordinator: coordinator)
|
|
|
|
}
|
2022-02-02 12:24:22 +00:00
|
|
|
} else {
|
2022-05-12 16:40:36 +00:00
|
|
|
navigationRouter.push(coordinator, animated: animated) { [weak self] in
|
2022-02-02 12:24:22 +00:00
|
|
|
self?.remove(childCoordinator: coordinator)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Displays the next view in the flow after the use case screen.
|
2022-02-17 11:19:32 +00:00
|
|
|
private func useCaseSelectionCoordinator(_ coordinator: OnboardingUseCaseSelectionCoordinator, didCompleteWith result: OnboardingUseCaseViewModelResult) {
|
2022-02-02 12:24:22 +00:00
|
|
|
useCaseResult = result
|
2022-04-14 10:06:12 +00:00
|
|
|
|
|
|
|
guard BuildSettings.onboardingEnableNewAuthenticationFlow else {
|
|
|
|
showLegacyAuthenticationScreen()
|
2022-05-12 16:40:36 +00:00
|
|
|
coordinator.stop()
|
2022-04-14 10:06:12 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-18 10:29:53 +00:00
|
|
|
if result == .customServer {
|
|
|
|
beginAuthentication(with: .selectServerForRegistration, onStart: coordinator.stop)
|
|
|
|
} else {
|
|
|
|
beginAuthentication(with: .registration, onStart: coordinator.stop)
|
2022-04-14 10:06:12 +00:00
|
|
|
}
|
2022-01-12 17:11:29 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 17:24:04 +00:00
|
|
|
// MARK: - Authentication
|
|
|
|
|
2022-04-14 10:06:12 +00:00
|
|
|
/// Show the authentication flow, starting at the specified initial screen.
|
2022-05-18 10:29:53 +00:00
|
|
|
private func beginAuthentication(with initialScreen: AuthenticationCoordinator.EntryPoint, onStart: @escaping () -> Void) {
|
2022-04-14 10:06:12 +00:00
|
|
|
MXLog.debug("[OnboardingCoordinator] beginAuthentication")
|
|
|
|
|
|
|
|
let parameters = AuthenticationCoordinatorParameters(navigationRouter: navigationRouter,
|
|
|
|
initialScreen: initialScreen,
|
|
|
|
canPresentAdditionalScreens: false)
|
|
|
|
let coordinator = AuthenticationCoordinator(parameters: parameters)
|
2022-05-12 16:40:36 +00:00
|
|
|
coordinator.callback = { [weak self, weak coordinator] result in
|
2022-04-14 10:06:12 +00:00
|
|
|
guard let self = self, let coordinator = coordinator else { return }
|
|
|
|
|
|
|
|
switch result {
|
2022-05-18 10:29:53 +00:00
|
|
|
case .didStart:
|
|
|
|
onStart()
|
2022-05-06 12:08:54 +00:00
|
|
|
case .didLogin(let session, let authenticationFlow, let authenticationType):
|
|
|
|
self.authenticationCoordinator(coordinator, didLoginWith: session, and: authenticationFlow, using: authenticationType)
|
2022-04-14 10:06:12 +00:00
|
|
|
case .didComplete:
|
|
|
|
self.authenticationCoordinatorDidComplete(coordinator)
|
2022-05-12 16:40:36 +00:00
|
|
|
case .cancel(let flow):
|
|
|
|
self.cancelAuthentication(flow: flow)
|
2022-04-14 10:06:12 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-20 10:14:17 +00:00
|
|
|
authenticationCoordinator = coordinator
|
2022-04-14 10:06:12 +00:00
|
|
|
|
|
|
|
add(childCoordinator: coordinator)
|
2022-05-18 10:29:53 +00:00
|
|
|
coordinator.start()
|
2022-04-14 10:06:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Show the legacy authentication screen. Any parameters that have been set in previous screens are be applied.
|
|
|
|
private func showLegacyAuthenticationScreen() {
|
|
|
|
guard !isShowingLegacyAuthentication else { return }
|
2022-01-12 17:11:29 +00:00
|
|
|
|
2022-04-14 10:06:12 +00:00
|
|
|
MXLog.debug("[OnboardingCoordinator] showLegacyAuthenticationScreen")
|
2022-01-12 17:11:29 +00:00
|
|
|
|
2022-05-24 11:26:39 +00:00
|
|
|
let coordinator = legacyAuthenticationCoordinator
|
2022-05-12 16:40:36 +00:00
|
|
|
coordinator.callback = { [weak self, weak coordinator] result in
|
2022-01-12 17:11:29 +00:00
|
|
|
guard let self = self, let coordinator = coordinator else { return }
|
2022-02-17 16:50:02 +00:00
|
|
|
|
|
|
|
switch result {
|
2022-05-06 12:08:54 +00:00
|
|
|
case .didLogin(let session, let authenticationFlow, let authenticationType):
|
|
|
|
self.authenticationCoordinator(coordinator, didLoginWith: session, and: authenticationFlow, using: authenticationType)
|
2022-02-24 17:24:04 +00:00
|
|
|
case .didComplete:
|
|
|
|
self.authenticationCoordinatorDidComplete(coordinator)
|
2022-05-18 10:29:53 +00:00
|
|
|
case .didStart, .cancel:
|
|
|
|
// These results are only sent by the new flow.
|
2022-05-12 16:40:36 +00:00
|
|
|
break
|
2022-02-17 16:50:02 +00:00
|
|
|
}
|
2022-01-12 17:11:29 +00:00
|
|
|
}
|
|
|
|
|
2022-01-28 11:53:50 +00:00
|
|
|
// Due to needing to preload the authVC, this breaks the Coordinator init/start pattern.
|
|
|
|
// This can be re-assessed once we re-write a native flow for authentication.
|
|
|
|
|
|
|
|
if let externalRegistrationParameters = externalRegistrationParameters {
|
|
|
|
coordinator.update(externalRegistrationParameters: externalRegistrationParameters)
|
|
|
|
}
|
2022-02-04 16:10:03 +00:00
|
|
|
|
2022-03-10 11:28:03 +00:00
|
|
|
coordinator.customServerFieldsVisible = useCaseResult == .customServer
|
2022-02-04 16:10:03 +00:00
|
|
|
|
2022-01-28 11:53:50 +00:00
|
|
|
if let softLogoutCredentials = parameters.softLogoutCredentials {
|
|
|
|
coordinator.update(softLogoutCredentials: softLogoutCredentials)
|
|
|
|
}
|
|
|
|
|
2022-05-24 11:26:39 +00:00
|
|
|
authenticationCoordinator = coordinator
|
|
|
|
|
2022-01-12 17:11:29 +00:00
|
|
|
coordinator.start()
|
|
|
|
add(childCoordinator: coordinator)
|
|
|
|
|
|
|
|
if customHomeserver != nil || customIdentityServer != nil {
|
2022-01-27 14:44:10 +00:00
|
|
|
coordinator.updateHomeserver(customHomeserver, andIdentityServer: customIdentityServer)
|
2022-01-12 17:11:29 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 17:24:04 +00:00
|
|
|
if navigationRouter.modules.isEmpty {
|
|
|
|
navigationRouter.setRootModule(coordinator, popCompletion: nil)
|
2022-01-12 17:11:29 +00:00
|
|
|
} else {
|
2022-02-24 17:24:04 +00:00
|
|
|
navigationRouter.push(coordinator, animated: true) { [weak self] in
|
2022-01-18 16:48:52 +00:00
|
|
|
self?.remove(childCoordinator: coordinator)
|
2022-04-14 10:06:12 +00:00
|
|
|
self?.isShowingLegacyAuthentication = false
|
2022-01-18 16:48:52 +00:00
|
|
|
}
|
2022-01-12 17:11:29 +00:00
|
|
|
}
|
2022-04-14 10:06:12 +00:00
|
|
|
isShowingLegacyAuthentication = true
|
2022-01-12 17:11:29 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 16:40:36 +00:00
|
|
|
/// Cancels the registration flow, returning to the Use Case screen.
|
|
|
|
private func cancelAuthentication(flow: AuthenticationFlow) {
|
|
|
|
switch flow {
|
|
|
|
case .register:
|
|
|
|
navigationRouter.popAllModules(animated: false)
|
|
|
|
|
|
|
|
showSplashScreen()
|
|
|
|
showUseCaseSelectionScreen(animated: false)
|
|
|
|
case .login:
|
|
|
|
// Probably not needed, error for now until the new login flow is implemented.
|
|
|
|
MXLog.failure("[OnboardingCoordinator] cancelAuthentication: Not implemented for the login flow")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-14 10:06:12 +00:00
|
|
|
/// Displays the next view in the flow after the authentication screens,
|
2022-02-24 17:24:04 +00:00
|
|
|
/// whilst crypto and the rest of the app is launching in the background.
|
|
|
|
private func authenticationCoordinator(_ coordinator: AuthenticationCoordinatorProtocol,
|
|
|
|
didLoginWith session: MXSession,
|
2022-05-06 12:08:54 +00:00
|
|
|
and authenticationFlow: AuthenticationFlow,
|
|
|
|
using authenticationType: AuthenticationType) {
|
2022-02-24 17:24:04 +00:00
|
|
|
self.session = session
|
2022-05-06 12:08:54 +00:00
|
|
|
self.authenticationFlow = authenticationFlow
|
2022-02-24 17:24:04 +00:00
|
|
|
self.authenticationType = authenticationType
|
|
|
|
|
|
|
|
// Check whether another screen should be shown.
|
2022-05-10 10:23:42 +00:00
|
|
|
if authenticationFlow == .register,
|
|
|
|
let userId = session.credentials.userId,
|
|
|
|
let userSession = UserSessionsService.shared.userSession(withUserId: userId) {
|
|
|
|
// If personalisation is to be shown, check that the homeserver supports it otherwise show the congratulations screen
|
|
|
|
if BuildSettings.onboardingShowAccountPersonalization {
|
|
|
|
checkHomeserverCapabilities(for: userSession)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
showCongratulationsScreen(for: userSession)
|
2022-02-24 17:24:04 +00:00
|
|
|
return
|
|
|
|
}
|
2022-05-10 10:23:42 +00:00
|
|
|
} else if Analytics.shared.shouldShowAnalyticsPrompt {
|
|
|
|
showAnalyticsPrompt(for: session)
|
|
|
|
return
|
2022-02-24 17:24:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise onboarding is finished.
|
2022-02-28 14:27:32 +00:00
|
|
|
onboardingFinished = true
|
2022-02-24 17:24:04 +00:00
|
|
|
completeIfReady()
|
2022-02-17 16:50:02 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 19:08:25 +00:00
|
|
|
/// Checks the capabilities of the user's homeserver in order to determine
|
|
|
|
/// whether or not the display name and avatar can be updated.
|
|
|
|
///
|
|
|
|
/// Once complete this method will start the post authentication flow automatically.
|
2022-03-15 11:51:17 +00:00
|
|
|
private func checkHomeserverCapabilities(for userSession: UserSession) {
|
|
|
|
userSession.matrixSession.matrixRestClient.capabilities { [weak self] capabilities in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.shouldShowDisplayNameScreen = capabilities?.setDisplayName?.isEnabled == true
|
|
|
|
self.shouldShowAvatarScreen = capabilities?.setAvatarUrl?.isEnabled == true
|
|
|
|
|
|
|
|
self.beginPostAuthentication(for: userSession)
|
|
|
|
} failure: { [weak self] _ in
|
|
|
|
MXLog.warning("[OnboardingCoordinator] Homeserver capabilities not returned. Skipping personalisation")
|
|
|
|
self?.beginPostAuthentication(for: userSession)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-14 10:06:12 +00:00
|
|
|
/// Completes the onboarding flow if possible, otherwise waits for any remaining screens.
|
2022-02-24 17:24:04 +00:00
|
|
|
private func authenticationCoordinatorDidComplete(_ coordinator: AuthenticationCoordinatorProtocol) {
|
2022-04-14 10:06:12 +00:00
|
|
|
isShowingLegacyAuthentication = false
|
2022-02-02 12:24:22 +00:00
|
|
|
|
2022-02-15 17:54:17 +00:00
|
|
|
// Handle the chosen use case where applicable
|
2022-05-06 12:08:54 +00:00
|
|
|
if authenticationFlow == .register,
|
2022-02-15 17:54:17 +00:00
|
|
|
let useCase = useCaseResult?.userSessionPropertyValue,
|
2022-02-02 12:24:22 +00:00
|
|
|
let userSession = UserSessionsService.shared.mainUserSession {
|
2022-02-04 16:10:03 +00:00
|
|
|
// Store the value in the user's session
|
2022-02-15 17:54:17 +00:00
|
|
|
userSession.userProperties.useCase = useCase
|
|
|
|
|
2022-02-17 12:39:11 +00:00
|
|
|
// Update the analytics user properties with the use case
|
|
|
|
Analytics.shared.updateUserProperties(ftueUseCase: useCase)
|
2022-02-02 12:24:22 +00:00
|
|
|
}
|
2022-02-24 17:24:04 +00:00
|
|
|
|
|
|
|
// This method is only called when the app is ready so we can complete if finished
|
2022-02-28 14:27:32 +00:00
|
|
|
authenticationFinished = true
|
2022-02-24 17:24:04 +00:00
|
|
|
completeIfReady()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Post-Authentication
|
|
|
|
|
2022-03-17 19:08:25 +00:00
|
|
|
/// Starts the part of the flow that comes after authentication for new users.
|
2022-03-15 11:51:17 +00:00
|
|
|
private func beginPostAuthentication(for userSession: UserSession) {
|
|
|
|
showCongratulationsScreen(for: userSession)
|
|
|
|
}
|
|
|
|
|
2022-03-17 19:08:25 +00:00
|
|
|
/// Show the congratulations screen for new users. The screen will be configured based on the homeserver's capabilities.
|
2022-03-15 11:51:17 +00:00
|
|
|
private func showCongratulationsScreen(for userSession: UserSession) {
|
2022-02-24 17:24:04 +00:00
|
|
|
MXLog.debug("[OnboardingCoordinator] showCongratulationsScreen")
|
|
|
|
|
2022-03-15 11:51:17 +00:00
|
|
|
let parameters = OnboardingCongratulationsCoordinatorParameters(userSession: userSession,
|
|
|
|
personalizationDisabled: !shouldShowDisplayNameScreen && !shouldShowAvatarScreen)
|
2022-02-24 17:24:04 +00:00
|
|
|
let coordinator = OnboardingCongratulationsCoordinator(parameters: parameters)
|
|
|
|
|
|
|
|
coordinator.completion = { [weak self, weak coordinator] result in
|
|
|
|
guard let self = self, let coordinator = coordinator else { return }
|
|
|
|
self.congratulationsCoordinator(coordinator, didCompleteWith: result)
|
|
|
|
}
|
|
|
|
|
|
|
|
add(childCoordinator: coordinator)
|
|
|
|
coordinator.start()
|
|
|
|
|
|
|
|
// Navigating back doesn't make any sense now, so replace the whole stack.
|
|
|
|
navigationRouter.setRootModule(coordinator, hideNavigationBar: true, animated: true) { [weak self] in
|
|
|
|
self?.remove(childCoordinator: coordinator)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-17 19:08:25 +00:00
|
|
|
/// Displays the next view in the flow after the congratulations screen.
|
2022-03-07 16:30:26 +00:00
|
|
|
private func congratulationsCoordinator(_ coordinator: OnboardingCongratulationsCoordinator, didCompleteWith result: OnboardingCongratulationsCoordinatorResult) {
|
|
|
|
switch result {
|
2022-03-21 11:42:58 +00:00
|
|
|
case .personalizeProfile(let userSession):
|
2022-03-15 11:51:17 +00:00
|
|
|
if shouldShowDisplayNameScreen {
|
|
|
|
showDisplayNameScreen(for: userSession)
|
|
|
|
return
|
|
|
|
} else if shouldShowAvatarScreen {
|
|
|
|
showAvatarScreen(for: userSession)
|
|
|
|
return
|
|
|
|
} else if Analytics.shared.shouldShowAnalyticsPrompt {
|
|
|
|
showAnalyticsPrompt(for: userSession.matrixSession)
|
|
|
|
return
|
|
|
|
}
|
2022-03-07 16:30:26 +00:00
|
|
|
case .takeMeHome(let userSession):
|
|
|
|
if Analytics.shared.shouldShowAnalyticsPrompt {
|
|
|
|
showAnalyticsPrompt(for: userSession.matrixSession)
|
|
|
|
return
|
2022-02-24 17:24:04 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-15 11:51:17 +00:00
|
|
|
|
|
|
|
onboardingFinished = true
|
|
|
|
completeIfReady()
|
2022-03-07 16:30:26 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 19:08:25 +00:00
|
|
|
/// Show the display name personalization screen for new users using the supplied user session.
|
2022-03-07 16:30:26 +00:00
|
|
|
private func showDisplayNameScreen(for userSession: UserSession) {
|
|
|
|
MXLog.debug("[OnboardingCoordinator]: showDisplayNameScreen")
|
|
|
|
|
|
|
|
let parameters = OnboardingDisplayNameCoordinatorParameters(userSession: userSession)
|
|
|
|
let coordinator = OnboardingDisplayNameCoordinator(parameters: parameters)
|
|
|
|
|
2022-03-25 11:58:35 +00:00
|
|
|
coordinator.completion = { [weak self, weak coordinator] userSession in
|
2022-03-07 16:30:26 +00:00
|
|
|
guard let self = self, let coordinator = coordinator else { return }
|
2022-03-25 11:58:35 +00:00
|
|
|
self.displayNameCoordinator(coordinator, didCompleteWith: userSession)
|
2022-03-07 16:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
add(childCoordinator: coordinator)
|
|
|
|
coordinator.start()
|
|
|
|
|
|
|
|
navigationRouter.setRootModule(coordinator, hideNavigationBar: false, animated: true) { [weak self] in
|
|
|
|
self?.remove(childCoordinator: coordinator)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-17 19:08:25 +00:00
|
|
|
/// Displays the next view in the flow after the display name screen.
|
2022-03-07 16:30:26 +00:00
|
|
|
private func displayNameCoordinator(_ coordinator: OnboardingDisplayNameCoordinator, didCompleteWith userSession: UserSession) {
|
2022-03-15 11:51:17 +00:00
|
|
|
if shouldShowAvatarScreen {
|
|
|
|
showAvatarScreen(for: userSession)
|
2022-03-21 17:08:54 +00:00
|
|
|
} else {
|
|
|
|
showCelebrationScreen(for: userSession)
|
2022-03-15 11:51:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-17 19:08:25 +00:00
|
|
|
/// Show the avatar personalization screen for new users using the supplied user session.
|
2022-03-15 11:51:17 +00:00
|
|
|
private func showAvatarScreen(for userSession: UserSession) {
|
|
|
|
MXLog.debug("[OnboardingCoordinator]: showAvatarScreen")
|
|
|
|
|
2022-03-25 11:58:35 +00:00
|
|
|
let parameters = OnboardingAvatarCoordinatorParameters(userSession: userSession, avatar: selectedAvatar)
|
2022-03-15 11:51:17 +00:00
|
|
|
let coordinator = OnboardingAvatarCoordinator(parameters: parameters)
|
|
|
|
|
2022-05-11 12:18:26 +00:00
|
|
|
coordinator.callback = { [weak self, weak coordinator] result in
|
2022-03-15 11:51:17 +00:00
|
|
|
guard let self = self, let coordinator = coordinator else { return }
|
2022-03-25 11:58:35 +00:00
|
|
|
|
|
|
|
switch result {
|
|
|
|
case .selectedAvatar(let image):
|
|
|
|
// Store the avatar so that if the user navigates back to the display name
|
|
|
|
// screen we can show the chosen image again when the avatar screen is pushed.
|
|
|
|
self.selectedAvatar = image
|
|
|
|
case .complete(let userSession):
|
|
|
|
self.avatarCoordinator(coordinator, didCompleteWith: userSession)
|
|
|
|
}
|
2022-03-15 11:51:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
add(childCoordinator: coordinator)
|
|
|
|
coordinator.start()
|
|
|
|
|
2022-03-17 19:08:25 +00:00
|
|
|
if navigationRouter.modules.isEmpty || !shouldShowDisplayNameScreen {
|
2022-03-15 11:51:17 +00:00
|
|
|
navigationRouter.setRootModule(coordinator, hideNavigationBar: false, animated: true) { [weak self] in
|
|
|
|
self?.remove(childCoordinator: coordinator)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
navigationRouter.push(coordinator, animated: true) { [weak self] in
|
|
|
|
self?.remove(childCoordinator: coordinator)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-17 19:08:25 +00:00
|
|
|
/// Displays the next view in the flow after the avatar screen.
|
2022-03-15 11:51:17 +00:00
|
|
|
private func avatarCoordinator(_ coordinator: OnboardingAvatarCoordinator, didCompleteWith userSession: UserSession) {
|
2022-03-21 17:08:54 +00:00
|
|
|
showCelebrationScreen(for: userSession)
|
2022-03-25 11:58:35 +00:00
|
|
|
|
|
|
|
// It is no longer possible to navigate backwards so forget the selected avatar
|
|
|
|
selectedAvatar = nil
|
2022-03-21 17:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private func showCelebrationScreen(for userSession: UserSession) {
|
|
|
|
MXLog.debug("[OnboardingCoordinator] showCelebrationScreen")
|
|
|
|
|
|
|
|
let parameters = OnboardingCelebrationCoordinatorParameters(userSession: userSession)
|
|
|
|
let coordinator = OnboardingCelebrationCoordinator(parameters: parameters)
|
|
|
|
|
|
|
|
coordinator.completion = { [weak self, weak coordinator] userSession in
|
|
|
|
guard let self = self, let coordinator = coordinator else { return }
|
|
|
|
self.celebrationCoordinator(coordinator, didCompleteWith: userSession)
|
|
|
|
}
|
|
|
|
|
|
|
|
add(childCoordinator: coordinator)
|
|
|
|
coordinator.start()
|
|
|
|
|
|
|
|
navigationRouter.setRootModule(coordinator, hideNavigationBar: true, animated: true) { [weak self] in
|
|
|
|
self?.remove(childCoordinator: coordinator)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func celebrationCoordinator(_ coordinator: OnboardingCelebrationCoordinator, didCompleteWith userSession: UserSession) {
|
2022-03-07 16:30:26 +00:00
|
|
|
if Analytics.shared.shouldShowAnalyticsPrompt {
|
|
|
|
showAnalyticsPrompt(for: userSession.matrixSession)
|
|
|
|
return
|
|
|
|
}
|
2022-02-24 17:24:04 +00:00
|
|
|
|
2022-02-28 14:27:32 +00:00
|
|
|
onboardingFinished = true
|
2022-02-24 17:24:04 +00:00
|
|
|
completeIfReady()
|
|
|
|
}
|
|
|
|
|
2022-03-17 19:08:25 +00:00
|
|
|
/// Shows the analytics prompt for the supplied session.
|
|
|
|
///
|
|
|
|
/// Check `Analytics.shared.shouldShowAnalyticsPrompt` before calling this method.
|
2022-02-24 17:24:04 +00:00
|
|
|
private func showAnalyticsPrompt(for session: MXSession) {
|
|
|
|
MXLog.debug("[OnboardingCoordinator]: Invite the user to send analytics")
|
|
|
|
|
|
|
|
let parameters = AnalyticsPromptCoordinatorParameters(session: session)
|
|
|
|
let coordinator = AnalyticsPromptCoordinator(parameters: parameters)
|
|
|
|
|
|
|
|
coordinator.completion = { [weak self, weak coordinator] in
|
|
|
|
guard let self = self, let coordinator = coordinator else { return }
|
|
|
|
self.analyticsPromptCoordinatorDidComplete(coordinator)
|
|
|
|
}
|
|
|
|
|
|
|
|
add(childCoordinator: coordinator)
|
|
|
|
coordinator.start()
|
|
|
|
|
|
|
|
// TODO: Re-asses replacing the stack based on the previous screen once the whole flow is implemented
|
|
|
|
navigationRouter.setRootModule(coordinator, hideNavigationBar: true, animated: true) { [weak self] in
|
|
|
|
self?.remove(childCoordinator: coordinator)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-17 19:08:25 +00:00
|
|
|
/// Displays the next view in the flow after the analytics screen.
|
2022-02-24 17:24:04 +00:00
|
|
|
private func analyticsPromptCoordinatorDidComplete(_ coordinator: AnalyticsPromptCoordinator) {
|
2022-02-28 14:27:32 +00:00
|
|
|
onboardingFinished = true
|
2022-02-24 17:24:04 +00:00
|
|
|
completeIfReady()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Finished
|
|
|
|
|
2022-03-17 19:08:25 +00:00
|
|
|
/// Calls the coordinator's completion handler if both `onboardingFinished` and `authenticationFinished`
|
|
|
|
/// are true. Otherwise displays any pending screens and waits to be called again.
|
2022-02-24 17:24:04 +00:00
|
|
|
private func completeIfReady() {
|
2022-02-28 14:27:32 +00:00
|
|
|
guard onboardingFinished else {
|
2022-02-24 17:24:04 +00:00
|
|
|
MXLog.debug("[OnboardingCoordinator] Delaying onboarding completion until all screens have been shown.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-28 14:27:32 +00:00
|
|
|
guard authenticationFinished else {
|
2022-05-24 11:26:39 +00:00
|
|
|
guard let authenticationCoordinator = authenticationCoordinator else {
|
|
|
|
MXLog.failure("[OnboardingCoordinator] completeIfReady: authenticationCoordinator is missing.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-20 10:14:17 +00:00
|
|
|
MXLog.debug("[OnboardingCoordinator] Allowing AuthenticationCoordinator to display any remaining screens.")
|
2022-02-28 14:27:32 +00:00
|
|
|
authenticationCoordinator.presentPendingScreensIfNecessary()
|
2022-02-24 17:24:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-06 12:08:54 +00:00
|
|
|
trackSignup()
|
|
|
|
|
2022-02-24 17:24:04 +00:00
|
|
|
completion?()
|
|
|
|
}
|
2022-05-06 12:08:54 +00:00
|
|
|
|
|
|
|
/// Sends a signup event to the Analytics class if onboarding has completed via the register flow.
|
|
|
|
private func trackSignup() {
|
|
|
|
guard authenticationFlow == .register else { return }
|
|
|
|
guard let authenticationType = authenticationType else {
|
|
|
|
MXLog.warning("[OnboardingCoordinator] sendSignedEvent: Registration finished without collecting an authentication type.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
Analytics.shared.trackSignup(authenticationType: authenticationType.analyticsType)
|
|
|
|
}
|
2022-02-24 17:24:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Helpers
|
|
|
|
|
|
|
|
extension OnboardingSplashScreenViewModelResult {
|
2022-05-06 12:08:54 +00:00
|
|
|
/// The result converted into an authentication flow.
|
|
|
|
var flow: AuthenticationFlow {
|
2022-02-24 17:24:04 +00:00
|
|
|
switch self {
|
|
|
|
case .login:
|
|
|
|
return .login
|
|
|
|
case .register:
|
|
|
|
return .register
|
|
|
|
}
|
2022-02-02 12:24:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension OnboardingUseCaseViewModelResult {
|
|
|
|
/// The result converted into the type stored in the user session.
|
|
|
|
var userSessionPropertyValue: UserSessionProperties.UseCase? {
|
|
|
|
switch self {
|
|
|
|
case .personalMessaging:
|
|
|
|
return .personalMessaging
|
|
|
|
case .workMessaging:
|
|
|
|
return .workMessaging
|
|
|
|
case .communityMessaging:
|
|
|
|
return .communityMessaging
|
|
|
|
case .skipped:
|
|
|
|
return .skipped
|
|
|
|
case .customServer:
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-16 11:47:02 +00:00
|
|
|
}
|
|
|
|
}
|