// // LauncherScreenWireframe.swift // GME Remit // // Created by InKwon James Kim on 22/07/2019. //Copyright © 2019 Gobal Money Express Co. Ltd. All rights reserved. // import UIKit import LGSideMenuController class LauncherScreenWireframe { weak var view: UIViewController! private var biometricViewController: UIViewController? } extension LauncherScreenWireframe: LauncherScreenWireframeInput { var storyboardName: String {return "LauncherScreen"} func getMainView() -> UIViewController { let service = LauncherScreenService() let interactor = LauncherScreenInteractor(service: service) let presenter = LauncherScreenPresenter() let viewController = viewControllerFromStoryboard(of: LauncherScreenViewController.self) viewController.presenter = presenter interactor.output = presenter presenter.interactor = interactor presenter.wireframe = self presenter.view = viewController view = viewController return viewController } func goSplashScreen() { let splashWireframe = SplashScreenWireframe() let splashViewController = splashWireframe.getMainView() let nav = UINavigationController(rootViewController: splashViewController) window?.rootViewController = nav } func openAuthenticationScreen() { let biometricAuthenticationWireframe = BiometricAuthenticationWireframe() biometricAuthenticationWireframe.openWithDelegate( on: view, delegate: self ) } } // MARK: - BiometricAuthenticationViewControllerDelegate extension LauncherScreenWireframe: BiometricAuthenticationViewControllerDelegate { func viewController( _ viewController: BiometricAuthenticationViewController, informationTitleLabel titleLabel: UILabel, authenticationButton button: UIButton ) { biometricViewController = viewController titleLabel.text = "bio_login_intro_text".localized() button.setTitle("login_text".localized(), for: .normal) } func didComplete(_ viewController: BiometricAuthenticationViewController) { self.doLogin() } func viewController( _ viewController: BiometricAuthenticationViewController, didFailWithError error: BiometricAuthenticationError, errorMessage: String? ) { print("BiometricAuthenticationWireframe Error: \(errorMessage ?? "")") // if don't use biometric authentication or user press password DispatchQueue.main.async { switch error { case .biometryNotAvailable, .userFallback: // viewController.openPasswordInput { (value) in // KeyChain.shared.save(data: value, key: .password) // // } self.doLogin() case .notBeConfigured, .biometryNotEnrolled: return //viewController.alert(type: .error, message: error.message) default: break } } } func doSelectLocalAuthenticationPolicy( _ viewController: BiometricAuthenticationViewController ) -> BiometricAuthenticationPolicy { return .deviceOwnerAuthenticationWithBiometrics } } // MARK: - Other Methods extension LauncherScreenWireframe { private func doLogin() { biometricViewController?.showProgressHud(backgroundColor: .clear, loadingColor: .white, textColor: .white) let loginService = LoginService() guard let userID = KeyChain.shared.get(key: .id), let password = KeyChain.shared.get(key: .password) else { (view as? LauncherScreenViewController)?.presenter?.goSplashScreen() return } GMEDB.shared.getFcmToken { (token, error) in loginService.accessCode( userId: userID, password: password, firebaseToken: token ?? "", customerType: "", success: { (user) in let accessCode = user?.accessCode ?? "" let accessCodeBase64 = accessCode Utility.save(user: user, accessCodeBase64: accessCodeBase64, password: password, login: true) let mainWireFrame = MainWireframe.shared let tabBarViewController = mainWireFrame?.getMainView() guard let sidemenuVc = UIStoryboard(name: "SideMenu", bundle: nil).instantiateViewController( withIdentifier: "SideMenuViewController" ) as? SideMenuViewController else { return } let navWithTab = UINavigationController(rootViewController: tabBarViewController!) let sideMenuController = LGSideMenuController( rootViewController: navWithTab, leftViewController: nil, rightViewController: nil ) sideMenuController.rootViewLayerShadowColor = UIColor(white: 0.9, alpha: 0.6) sideMenuController.rootViewLayerShadowRadius = 8.0 sideMenuController.leftViewPresentationStyle = .slideAbove sideMenuController.leftViewWidth = UIScreen.main.bounds.width - 70.0 sideMenuController.leftViewBackgroundBlurEffect = UIBlurEffect(style: .regular) self.biometricViewController?.hideProgressHud() self.biometricViewController?.dismiss(animated: true) { self.window?.backgroundColor = sidemenuVc.view.backgroundColor self.window?.rootViewController = sideMenuController } }, failure: { (error) in self.biometricViewController?.hideProgressHud() self.biometricViewController?.alert( type: .error, message: error.localizedDescription, title: "Warning" ) { // if failed login, remove all in keychain, change logout status and then go splash screen KeyChain.shared.removeAll() self.biometricViewController?.dismiss(animated: true) { (self.view as? LauncherScreenViewController)?.presenter?.goSplashScreen() } } } )} } }