mobile-ios/CovidSafe/InitialScreenViewController.swift

130 lines
4.8 KiB
Swift
Raw Permalink Normal View History

2020-05-08 07:49:14 +00:00
//
// InitialScreenViewController.swift
// CovidSafe
//
// Copyright © 2020 Australian Government. All rights reserved.
//
import UIKit
import KeychainSwift
2020-05-26 07:13:26 +00:00
class InitialScreenViewController: UIViewController, EncounterDBMigrationProgress {
2020-05-08 07:49:14 +00:00
2020-05-26 07:13:26 +00:00
let displayTimeSeconds = 4.0
2020-05-15 07:47:40 +00:00
let giveupTimeSeconds = 8.0
2020-05-26 07:13:26 +00:00
var migrationStart: Date?
2020-05-15 07:47:40 +00:00
var isKeychainAvailable = false
var isDisplayTimeElapsed = false
2021-05-13 00:39:38 +00:00
let keychain = KeychainSwift.shared
2020-05-15 07:47:40 +00:00
var giveupTimer: Timer?
2020-05-26 07:13:26 +00:00
var initialDelayTimer: Timer?
var migrationViewController: UIViewController?
2020-05-08 07:49:14 +00:00
override func viewDidLoad() {
super.viewDidLoad()
2020-08-18 00:52:17 +00:00
EncounterDB.shared.setup(migrationDelegate: self)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
2020-05-15 07:47:40 +00:00
switch UIApplication.shared.isProtectedDataAvailable {
case true :
isKeychainAvailable = true
break
case false:
NotificationCenter.default.addObserver(self, selector: #selector(setKeychainAvailable(_:)), name: UIApplication.protectedDataDidBecomeAvailableNotification, object: nil)
break
}
2020-05-26 07:13:26 +00:00
2020-05-08 07:49:14 +00:00
view.window?.tintColor = .covidSafeColor
2020-05-15 07:47:40 +00:00
2020-08-18 00:52:17 +00:00
// if a migration started let the migration delegate handle the timers
if migrationStart == nil {
continueAfterDelay(delay: displayTimeSeconds)
// add give up action in case the keychain notification in not received after 8 seconds
giveupTimer = Timer.scheduledTimer(withTimeInterval: giveupTimeSeconds, repeats: false) { timer in
self.performSegue(withIdentifier: "initialScreenToIWantToHelpSegue", sender: self)
}
2020-05-15 07:47:40 +00:00
}
2020-05-26 07:13:26 +00:00
}
func continueAfterDelay(delay: TimeInterval) {
initialDelayTimer = Timer.scheduledTimer(withTimeInterval: delay,
repeats: false,
block: { (_) in
self.isDisplayTimeElapsed = true
if(self.proceedWithChecks()) {
self.performCheck()
}
})
2020-05-15 07:47:40 +00:00
}
@objc
func setKeychainAvailable(_ notification: Notification) {
NotificationCenter.default.removeObserver(self, name: UIApplication.protectedDataDidBecomeAvailableNotification, object: nil)
isKeychainAvailable = true
if(self.proceedWithChecks()) {
self.performCheck()
}
}
private func proceedWithChecks() -> Bool {
return isDisplayTimeElapsed && isKeychainAvailable
2020-05-08 07:49:14 +00:00
}
private func performCheck() {
2020-05-15 07:47:40 +00:00
giveupTimer?.invalidate()
2020-05-26 07:13:26 +00:00
initialDelayTimer?.invalidate()
if let migrationVc = migrationViewController {
migrationVc.dismiss(animated: true, completion: nil)
}
2020-05-08 07:49:14 +00:00
let isLoggedIn: Bool = (keychain.get("JWT_TOKEN") != nil)
2020-08-03 06:01:39 +00:00
if !UserDefaults.standard.bool(forKey: "completedIWantToHelp") ||
!UserDefaults.standard.bool(forKey: "hasConsented") ||
!isLoggedIn {
2020-05-08 07:49:14 +00:00
keychain.delete("JWT_TOKEN")
self.performSegue(withIdentifier: "initialScreenToIWantToHelpSegue", sender: self)
} else if !UserDefaults.standard.bool(forKey: "allowedPermissions") {
self.performSegue(withIdentifier: "initialScreenToAllowPermissionsSegue", sender: self)
} else {
2020-09-14 01:23:11 +00:00
DispatchQueue.main.async {
2021-02-26 03:41:20 +00:00
let tabVC = MainTabBarViewController()
self.navigationController?.setViewControllers([tabVC], animated: true)
2020-09-14 01:23:11 +00:00
}
2020-05-08 07:49:14 +00:00
}
}
2020-05-26 07:13:26 +00:00
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "presentMigrationSegue" {
migrationViewController = segue.destination
}
}
func migrationBegun() {
DLog("MIGRATION BEGUN")
giveupTimer?.invalidate()
initialDelayTimer?.invalidate()
migrationStart = Date()
performSegue(withIdentifier: "presentMigrationSegue", sender: nil)
}
func migrationComplete() {
DLog("MIGRATION COMPLETE")
if let migrationStart = migrationStart {
let migrationDuration = abs(migrationStart.timeIntervalSinceNow)
if migrationDuration > displayTimeSeconds {
performCheck()
} else {
// migration was quick, still need to delay minimum 4 seconds
DLog("Migration too quick, waiting \(displayTimeSeconds - migrationDuration) seconds")
continueAfterDelay(delay: displayTimeSeconds - migrationDuration)
}
}
}
func migrationFailed(error: Error) {
fatalError("Migration Failed \(error)")
}
2020-05-08 07:49:14 +00:00
}