Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] #476 - 습관 인증 스톱워치 백그라운드에서 멈추는 문제 해결 #477

Merged
merged 5 commits into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Spark-iOS/Spark-iOS/Resource/Constants/UserDefaultsKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
9 changes: 9 additions & 0 deletions Spark-iOS/Spark-iOS/Source/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

와아..이렇게 할수 있군여 굳아이디어...!

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}
}
Comment on lines +173 to +180
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

포그라운드에서 백그라운드로 갈때는 notification 에 time 이 없어서 if let 같은 구문보다 위와 같이 옵셔널을 바인딩한 부분 좋은 거 같아요
그런데 애초에 포그라운드에서 백그라운드로 갈때 UserDefaults 를 쓰는것 의미가 있는데 notification 을 보내는 건 의미가 있을까 싶어요! 다른 역할이 있을까요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아 저 이것저것 해볼때 포그라운드에서 백그라운드로 갈 때 타이머를 멈춰주는 함수를 넣느라 그때 썼던건데
지금은 안사용하고 있어서 지우는게 맞겠네요.. 감사합니다..

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

굳굳 덕분에 좋은 노티 알고가요~

}

@objc
Expand Down