Skip to content

Commit

Permalink
[Feature] background상태에서 foreground진입시 일정시간 이후 앱 초기화 로직 생성 (#626)
Browse files Browse the repository at this point in the history
* [Feature] background상태에서 foreground진입시 일정시간 이후 앱 초기화 로직 생성

* feat: timeout 시간 5초 -> 10분으로 수정
  • Loading branch information
MaraMincho authored Oct 14, 2024
1 parent ebec0f0 commit 9d01c76
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 6 deletions.
41 changes: 41 additions & 0 deletions Projects/App/Sources/App/SSTimeOut.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
12 changes: 10 additions & 2 deletions Projects/App/Sources/App/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions Projects/App/Sources/App/SusuMain.swift
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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)
}
}
}
2 changes: 1 addition & 1 deletion Projects/App/Sources/App/UINavigationController+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

0 comments on commit 9d01c76

Please sign in to comment.