Advertisement
Tark_Wight

AppCoordinator

Jan 1st, 2024 (edited)
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.89 KB | Source Code | 0 0
  1. class AppCoordinator: ObservableObject {
  2.     enum AuthenticationStatus {
  3.         case loggedIn
  4.         case firstTimeUser
  5.     }
  6.  
  7.     @Published var currentScreen: Screen?
  8.  
  9.     // Использую ленивые свойства для создания координаторов
  10.     lazy var loginCoordinator: LoginCoordinator = {
  11.         let coordinator = LoginCoordinator()
  12.         coordinator.parentCoordinator = self
  13.         return coordinator
  14.     }()
  15.  
  16.     lazy var registrationCoordinator: RegistrationCoordinator = {
  17.         let coordinator = RegistrationCoordinator()
  18.         coordinator.parentCoordinator = self
  19.         return coordinator
  20.     }()
  21.  
  22.     lazy var mainCoordinator: MainScreenCoordinator = {
  23.         let coordinator = MainScreenCoordinator()
  24.         coordinator.parentCoordinator = self
  25.         return coordinator
  26.     }()
  27.  
  28.     init() {
  29.         checkAuthenticationStatus()
  30.         NotificationCenter.default.addObserver(self, selector: #selector(handleShowRegistration), name: NSNotification.Name("ShowRegistration"), object: nil)
  31.  
  32.     }
  33.  
  34.     deinit {
  35.             NotificationCenter.default.removeObserver(self)
  36.         }
  37.    
  38.    
  39.     @objc func handleShowRegistration() {
  40.             showRegistration()
  41.         }
  42.     private func checkAuthenticationStatus() {
  43.       // TODO: логика
  44.         let status: AuthenticationStatus = .firstTimeUser
  45.         setCurrentScreen(for: status)
  46.     }
  47.  
  48.     private func setCurrentScreen(for status: AuthenticationStatus) {
  49.         switch status {
  50.         case .loggedIn:
  51.             currentScreen = .main
  52.         case .firstTimeUser:
  53.             currentScreen = .authenticationChoice
  54.         }
  55.     }
  56.  
  57.     func showLogin() {
  58.         currentScreen = .login
  59.     }
  60.  
  61.     func showRegistration() {
  62.         currentScreen = .registration
  63.     }
  64.  
  65.     func showMain() {
  66.         currentScreen = .main
  67.     }
  68. }
  69.  
  70.  
  71.  
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement