diff --git a/Projects/App/Sources/App/SSTimeOut.swift b/Projects/App/Sources/App/SSTimeOut.swift new file mode 100644 index 00000000..8b97d011 --- /dev/null +++ b/Projects/App/Sources/App/SSTimeOut.swift @@ -0,0 +1,41 @@ +// +// SSTimeOut.swift +// susu +// +// Created by MaraMincho on 10/14/24. +// Copyright © 2024 com.oksusu. All rights reserved. +// + +import Foundation +import OSLog +import SSNotification + +final class SSTimeOut { + private nonisolated(unsafe) static let shared = SSTimeOut() + private var timeoutInterval: Int = 60 * 10 + private var enteredBackgroundDate: Date? + + static func setTimeoutInterval(minute: Int, seconds: Int) { + shared.timeoutInterval = minute * 60 + seconds + } + + static func enterBackground() { + os_log("백그라운드 진입") + shared.enteredBackgroundDate = Date.now + } + + static func enterForegroundScreen() { + os_log("포그라운드 진입") + defer { + shared.enteredBackgroundDate = nil + } + guard let backgroundEnteredDate = shared.enteredBackgroundDate else { + return + } + let interval = Date.now.timeIntervalSince(backgroundEnteredDate) + + if shared.timeoutInterval < Int(interval) { + NotificationCenter.default.post(name: SSNotificationName.resetApp, object: nil) + } + } +} diff --git a/Projects/App/Sources/App/SceneDelegate.swift b/Projects/App/Sources/App/SceneDelegate.swift index 7da77528..abf2485a 100644 --- a/Projects/App/Sources/App/SceneDelegate.swift +++ b/Projects/App/Sources/App/SceneDelegate.swift @@ -25,9 +25,17 @@ class MySceneDelegate: UIResponder, UIWindowSceneDelegate { func sceneWillResignActive(_: UIScene) {} - func sceneWillEnterForeground(_: UIScene) {} + // MARK: - Tells the delegate that the scene is about to begin running in the foreground and become visible to the user. - func sceneDidEnterBackground(_: UIScene) {} + func sceneWillEnterForeground(_: UIScene) { + SSTimeOut.enterForegroundScreen() + } + + // MARK: - Tells the delegate that the scene is running in the background and is no longer onscreen. + + func sceneDidEnterBackground(_: UIScene) { + SSTimeOut.enterBackground() + } } // MARK: - KAKAO OAuth2.0 Extension diff --git a/Projects/App/Sources/App/SusuMain.swift b/Projects/App/Sources/App/SusuMain.swift index e43a888f..d670fe6b 100644 --- a/Projects/App/Sources/App/SusuMain.swift +++ b/Projects/App/Sources/App/SusuMain.swift @@ -1,11 +1,13 @@ import Designsystem import KakaoLogin +import SSNotification import SwiftUI // MARK: - SusuApp @main struct SusuApp: App { + @State private var contentViewID: UUID = .init() init() { #if DEBUG UserDefaults.standard.set(false, forKey: "_UIConstraintBasedLayoutLogUnsatisfiable") @@ -17,6 +19,10 @@ struct SusuApp: App { var body: some Scene { WindowGroup { ContentView(contentViewObject: .init()) + .onReceive(NotificationCenter.default.publisher(for: SSNotificationName.resetApp)) { _ in + contentViewID = .init() + } + .id(contentViewID) } } } diff --git a/Projects/App/Sources/App/UINavigationController+.swift b/Projects/App/Sources/App/UINavigationController+.swift index 1206916f..d3a02c00 100644 --- a/Projects/App/Sources/App/UINavigationController+.swift +++ b/Projects/App/Sources/App/UINavigationController+.swift @@ -8,7 +8,7 @@ import UIKit -extension UINavigationController: ObservableObject, UIGestureRecognizerDelegate { +extension UINavigationController: @retroactive ObservableObject, @retroactive UIGestureRecognizerDelegate { override open func viewDidLoad() { super.viewDidLoad() navigationBar.isHidden = true diff --git a/Projects/Core/SSNotification/Sources/SSNotificationName.swift b/Projects/Core/SSNotification/Sources/SSNotificationName.swift index 24265322..caecfc0a 100644 --- a/Projects/Core/SSNotification/Sources/SSNotificationName.swift +++ b/Projects/Core/SSNotification/Sources/SSNotificationName.swift @@ -15,12 +15,10 @@ public enum SSNotificationName { public static let tappedVote = Notification.Name("tappedVote") public static let tappedMyPage = Notification.Name("tappedMyPage") public static let goMainScene = Notification.Name("goMainScene") - + public static let resetApp = Notification.Name("ResetApp") public static let logout = Notification.Name("logout") - public static let goMyPageEditMyProfile = Notification.Name("goMyPageEditMyProfile") public static let goEditProfile = Notification.Name("goEditProfile") - public static let showDefaultNetworkErrorAlert = Notification.Name("ShowDefaultNetworkErrorAlert") public static let logError = Notification.Name("ErrorLog") }