Skip to content

Instantly share code, notes, and snippets.

@EvolverSwiftUI
Forked from pallavtrivedi03/SplashVC.swift
Created November 22, 2022 04:33
Show Gist options
  • Select an option

  • Save EvolverSwiftUI/a15436227a34909392cd97eeb97a45a8 to your computer and use it in GitHub Desktop.

Select an option

Save EvolverSwiftUI/a15436227a34909392cd97eeb97a45a8 to your computer and use it in GitHub Desktop.

Revisions

  1. @pallavtrivedi03 pallavtrivedi03 created this gist Jul 19, 2022.
    82 changes: 82 additions & 0 deletions SplashVC.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,82 @@
    import UIKit
    import Combine

    class SplashViewController: UIViewController {

    @IBOutlet weak var activityIndicator: UIActivityIndicatorView!
    private var cancellables = Set<AnyCancellable>()

    var launchDataDispatchGroup: DispatchGroup = DispatchGroup()

    override func viewDidLoad() {
    super.viewDidLoad()

    DispatchQueue.global().async { [weak self] in
    self?.getAppLaunchData()
    }
    }

    func getAppLaunchData() {

    launchDataDispatchGroup.enter()

    NetworkManager.shared.getData(endpoint: .userPreferences, type: UserPreference.self)
    .sink { [weak self] completion in
    self?.launchDataDispatchGroup.leave()

    switch completion {
    case .failure(let err):
    print("Error is \(err.localizedDescription)")
    case .finished:
    break
    }
    } receiveValue: { userPreferences in
    print("Watchlists -> \(userPreferences.watchlist?.first ?? "")")
    }
    .store(in: &self.cancellables)

    launchDataDispatchGroup.enter()

    NetworkManager.shared.getData(endpoint: .appConfig, type: AppConfig.self)
    .sink { [weak self] completion in
    self?.launchDataDispatchGroup.leave()

    switch completion {
    case .failure(let err):
    print("Error is \(err.localizedDescription)")
    case .finished:
    break
    }
    } receiveValue: { config in
    print("Base URL -> \(config.baseURL ?? "")")
    }
    .store(in: &self.cancellables)

    let waitResult: DispatchTimeoutResult = launchDataDispatchGroup.wait(timeout: .now() + .seconds(3))
    DispatchQueue.main.async { [weak self] in

    switch waitResult {
    case .success:
    print("API calls completed before timeout")
    case .timedOut:
    print("APIs timed out")
    }
    self?.activityIndicator.stopAnimating()
    self?.navigateToSignupVC()
    }

    // launchDataDispatchGroup.notify(queue: .main) { [weak self] in
    // print("Launch calls complete, navigate to next screen")
    // self?.activityIndicator.stopAnimating()
    // self?.navigateToSignupVC()
    // }
    }

    func navigateToSignupVC() {
    guard let signupVC: SignupViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SignupViewController") as? SignupViewController else { return }
    UIApplication.shared.windows.first?.rootViewController = signupVC
    UIApplication.shared.windows.first?.makeKeyAndVisible()

    }
    }