diff --git a/Spark-iOS/Spark-iOS/Resource/Constants/UserDefaultsKey.swift b/Spark-iOS/Spark-iOS/Resource/Constants/UserDefaultsKey.swift index f27971d7..9f37d03f 100644 --- a/Spark-iOS/Spark-iOS/Resource/Constants/UserDefaultsKey.swift +++ b/Spark-iOS/Spark-iOS/Resource/Constants/UserDefaultsKey.swift @@ -15,5 +15,7 @@ extension Const { static let accessToken = "accessToekn" static let fcmToken = "fcmToken" static let checkHabitRoomGuide = "checkHabitRoomGuide" + static let sceneWillEnterForeground = "sceneWillEnterForeground" + static let sceneDidEnterBackground = "sceneDidEnterBackground" } } diff --git a/Spark-iOS/Spark-iOS/Source/SceneDelegate.swift b/Spark-iOS/Spark-iOS/Source/SceneDelegate.swift index b70f0113..5902b751 100644 --- a/Spark-iOS/Spark-iOS/Source/SceneDelegate.swift +++ b/Spark-iOS/Spark-iOS/Source/SceneDelegate.swift @@ -41,9 +41,18 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { func sceneWillResignActive(_ scene: UIScene) { } + // Foreground에 들어올 때 + // sceneDidEnterBackground라는 키워드로 저장해둔 값을 sceneWillEnterForeground라는 키워드 노티의 userInfo로 전달 func sceneWillEnterForeground(_ scene: UIScene) { + guard let start = UserDefaults.standard.object(forKey: Const.UserDefaultsKey.sceneDidEnterBackground) as? Date else { return } + let interval = Int(Date().timeIntervalSince(start)) + NotificationCenter.default.post(name: NSNotification.Name(Const.UserDefaultsKey.sceneWillEnterForeground), object: nil, userInfo: ["time": interval]) } + // Background에 들어갈때 + // sceneDidEnterBackground라는 키워드의 노티 전달하며 + // sceneDidEnterBackground라는 키워드로 값 저장 func sceneDidEnterBackground(_ scene: UIScene) { + UserDefaults.standard.setValue(Date(), forKey: Const.UserDefaultsKey.sceneDidEnterBackground) } } diff --git a/Spark-iOS/Spark-iOS/Source/ViewControllers/Auth/AuthTimer/AuthTimerVC.swift b/Spark-iOS/Spark-iOS/Source/ViewControllers/Auth/AuthTimer/AuthTimerVC.swift index 33533e84..f3fd42be 100644 --- a/Spark-iOS/Spark-iOS/Source/ViewControllers/Auth/AuthTimer/AuthTimerVC.swift +++ b/Spark-iOS/Spark-iOS/Source/ViewControllers/Auth/AuthTimer/AuthTimerVC.swift @@ -50,6 +50,13 @@ class AuthTimerVC: UIViewController { navigationController?.interactivePopGestureRecognizer?.delegate = self } + override func viewWillDisappear(_ animated: Bool) { + super.viewWillDisappear(animated) + + NotificationCenter.default.removeObserver(self, name: NSNotification.Name(Const.UserDefaultsKey.sceneWillEnterForeground), object: nil) + NotificationCenter.default.removeObserver(self, name: NSNotification.Name(Const.UserDefaultsKey.sceneDidEnterBackground), object: nil) + } + // MARK: - Methods private func setUI() { @@ -93,6 +100,7 @@ class AuthTimerVC: UIViewController { private func setNotification() { NotificationCenter.default.addObserver(self, selector: #selector(resetTimer(_:)), name: .resetStopWatch, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(checkBackgroundTimer), name: NSNotification.Name(Const.UserDefaultsKey.sceneWillEnterForeground), object: nil) } private func setButton(_ button: UIButton, title: String, backgroundColor: UIColor, isEnable: Bool) { @@ -103,7 +111,7 @@ class AuthTimerVC: UIViewController { button.layer.cornerRadius = 2 } - func dismissAuthTimerVC() { + private func dismissAuthTimerVC() { if timeLabel.text != "00:00:00" { guard let dialogVC = UIStoryboard(name: Const.Storyboard.Name.dialogue, bundle: nil).instantiateViewController(withIdentifier: Const.ViewController.Identifier.dialogue) as? DialogueVC else { return } dialogVC.dialogueType = .exitTimer @@ -118,6 +126,18 @@ class AuthTimerVC: UIViewController { } } + private func timeFormatter(_ intTime: Int) -> String { + let hour = intTime / 3600 + let min = (intTime % 3600) / 60 + let sec = (intTime % 3600) % 60 + + let hourStr = hour < 10 ? "0\(hour)" : String(hour) + let minStr = min < 10 ? "0\(min)" : String(min) + let secStr = sec < 10 ? "0\(sec)" : String(sec) + + return "\(hourStr):\(minStr):\(secStr)" + } + // MARK: - @objc @objc @@ -150,16 +170,14 @@ class AuthTimerVC: UIViewController { } } - func timeFormatter(_ intTime: Int) -> String { - let hour = intTime / 3600 - let min = (intTime % 3600) / 60 - let sec = (intTime % 3600) % 60 - - let hourStr = hour < 10 ? "0\(hour)" : String(hour) - let minStr = min < 10 ? "0\(min)" : String(min) - let secStr = sec < 10 ? "0\(sec)" : String(sec) - - return "\(hourStr):\(minStr):\(secStr)" + @objc + func checkBackgroundTimer(_ notification: NSNotification) { + if let isValid = timer?.isValid { + if isTimerOn && isValid { + let time = notification.userInfo?["time"] as? Int ?? 0 + currentTimeCount += time + } + } } @objc