From d634995a20c030444dc7eea63114d694368fb7ac Mon Sep 17 00:00:00 2001 From: Stefan Ceriu Date: Tue, 22 Feb 2022 13:53:34 +0200 Subject: [PATCH] Added error toasts on the login screen. --- ElementX/Sources/AppCoordinator.swift | 17 ++++++++++++++++- .../AuthenticationCoordinator.swift | 5 ++--- .../ToastActivityPresenter.swift | 7 ++++--- .../Activity/Toasts/RoundedToastView.swift | 4 ++++ 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/ElementX/Sources/AppCoordinator.swift b/ElementX/Sources/AppCoordinator.swift index 93ad752ec..a4c972345 100644 --- a/ElementX/Sources/AppCoordinator.swift +++ b/ElementX/Sources/AppCoordinator.swift @@ -20,6 +20,7 @@ class AppCoordinator: AuthenticationCoordinatorDelegate, Coordinator { private let authenticationCoordinator: AuthenticationCoordinator! private var loadingActivity: Activity? + private var errorActivity: Activity? var childCoordinators: [Coordinator] = [] @@ -55,6 +56,7 @@ class AppCoordinator: AuthenticationCoordinatorDelegate, Coordinator { func authenticationCoordinator(_ authenticationCoordinator: AuthenticationCoordinator, didFailWithError error: AuthenticationCoordinatorError) { hideLoadingIndicator() + showLoginErrorToast() } func authenticationCoordinatorDidSetupUserSession(_ authenticationCoordinator: AuthenticationCoordinator) { @@ -91,7 +93,8 @@ class AppCoordinator: AuthenticationCoordinatorDelegate, Coordinator { } private func showLoadingIndicator() { - let presenter = FullscreenLoadingActivityPresenter(label: "Loading", on: self.mainNavigationController) + let presenter = FullscreenLoadingActivityPresenter(label: "Loading", + on: mainNavigationController) let request = ActivityRequest( presenter: presenter, @@ -104,4 +107,16 @@ class AppCoordinator: AuthenticationCoordinatorDelegate, Coordinator { private func hideLoadingIndicator() { loadingActivity = nil } + + private func showLoginErrorToast() { + let presenter = ToastActivityPresenter(viewState: .init(style: .success, label: "Failed logging in"), + navigationController: mainNavigationController) + + let request = ActivityRequest( + presenter: presenter, + dismissal: .timeout(3.0) + ) + + errorActivity = ActivityCenter.shared.add(request) + } } diff --git a/ElementX/Sources/Modules/Authentication/AuthenticationCoordinator.swift b/ElementX/Sources/Modules/Authentication/AuthenticationCoordinator.swift index 2a9e86e69..0399697f3 100644 --- a/ElementX/Sources/Modules/Authentication/AuthenticationCoordinator.swift +++ b/ElementX/Sources/Modules/Authentication/AuthenticationCoordinator.swift @@ -97,12 +97,11 @@ class AuthenticationCoordinator: Coordinator { switch result { case .success: completion(.success(())) + self.remove(childCoordinator: coordinator) + self.navigationRouter.dismissModule() case .failure(let error): completion(.failure(error)) } - - self.remove(childCoordinator: coordinator) - self.navigationRouter.dismissModule() } } } diff --git a/ElementX/Sources/Modules/Other/Activity/ActivityPresenters/ToastActivityPresenter.swift b/ElementX/Sources/Modules/Other/Activity/ActivityPresenters/ToastActivityPresenter.swift index 7d8720af6..df77fb314 100644 --- a/ElementX/Sources/Modules/Other/Activity/ActivityPresenters/ToastActivityPresenter.swift +++ b/ElementX/Sources/Modules/Other/Activity/ActivityPresenters/ToastActivityPresenter.swift @@ -28,9 +28,10 @@ class ToastActivityPresenter: ActivityPresentable { self.viewState = viewState self.navigationController = navigationController } - + func present() { - guard let navigationController = navigationController else { + guard let navigationController = navigationController, + let window = navigationController.view.window else { return } @@ -38,7 +39,7 @@ class ToastActivityPresenter: ActivityPresentable { self.view = view view.translatesAutoresizingMaskIntoConstraints = false - navigationController.view.addSubview(view) + window.addSubview(view) NSLayoutConstraint.activate([ view.centerXAnchor.constraint(equalTo: navigationController.navigationBar.centerXAnchor), view.topAnchor.constraint(equalTo: navigationController.navigationBar.bottomAnchor) diff --git a/ElementX/Sources/Modules/Other/Activity/Toasts/RoundedToastView.swift b/ElementX/Sources/Modules/Other/Activity/Toasts/RoundedToastView.swift index 1c348921b..23fdb9eb5 100644 --- a/ElementX/Sources/Modules/Other/Activity/Toasts/RoundedToastView.swift +++ b/ElementX/Sources/Modules/Other/Activity/Toasts/RoundedToastView.swift @@ -48,6 +48,7 @@ class RoundedToastView: UIView { private let stackView: UIStackView = { let stack = UIStackView() stack.axis = .horizontal + stack.alignment = .center stack.spacing = 5 return stack }() @@ -66,6 +67,9 @@ class RoundedToastView: UIView { } private func setup(viewState: ViewState) { + + backgroundColor = .gray.withAlphaComponent(0.75) + setupLayer() setupStackView() stackView.addArrangedSubview(toastView(for: viewState.style))