Skip to content

Commit

Permalink
[Fix] LifecycleAdapter 위치 이동 : SOPT-iOS 모듈로 이동 (sopt-makers#319)
Browse files Browse the repository at this point in the history
* [Fix] LifecycleAdapter 위치 이동 : SOPT-iOS 모듈로 이동

* [Etc] Demo App target에도 Adapter 추가
  • Loading branch information
elesahich authored Dec 3, 2023
1 parent 9d1a938 commit 120052a
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//
// AppLifecycleAdapter.swift
// BaseFeatureDependency
// SOPT-iOS-Demo
//
// Created by Ian on 12/2/23.
// Created by Ian on 12/3/23.
// Copyright © 2023 SOPT-iOS. All rights reserved.
//

Expand All @@ -12,24 +12,18 @@ import Networks
import UIKit

final public class AppLifecycleAdapter {
private let authService: AuthService
private let cancelBag = CancelBag()

public init() {
self.authService = DefaultAuthService()
}
private let authService = DefaultAuthService()
}

// MARK: - Public functions
// MARK: - Private functions
extension AppLifecycleAdapter {
public func prepare() {
self.onWillEnterForeground()
self.onWillEnterBackground()
}
}

// MARK: - Lifecycle Usecases
extension AppLifecycleAdapter {
//MARK: - Usecases
private func onWillEnterForeground() {
NotificationCenter.default
.publisher(for: UIApplication.willEnterForegroundNotification)
Expand All @@ -39,16 +33,15 @@ extension AppLifecycleAdapter {
self?.reissureTokens()
}).store(in: self.cancelBag)
}

private func onWillEnterBackground() { }
}

// MARK: - Private functions
extension AppLifecycleAdapter {
private func reissureTokens() {
guard UserDefaultKeyList.Auth.appAccessToken != nil else { return }

self.authService.reissuance { _ in }
}
}

8 changes: 6 additions & 2 deletions SOPT-iOS/Projects/Demo/Sources/Application/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ import BaseFeatureDependency
class AppDelegate: UIResponder, UIApplicationDelegate {

private var appLifecycleAdapter = AppLifecycleAdapter()

func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
configureSentry()
configureAppLifecycleAdapter()
registerDependencies()
application.registerForRemoteNotifications()

appLifecycleAdapter.prepare()
return true
}

Expand Down Expand Up @@ -63,6 +63,10 @@ extension AppDelegate {
}
}

private func configureAppLifecycleAdapter() {
self.appLifecycleAdapter.prepare()
}

/// APNs 등록 실패할 경우 호출되는 메서드
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("APNs Failed to register for notifications: \(error.localizedDescription)")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// AppLifecycleAdapter.swift
// SOPT-iOS
//
// Created by Ian on 12/3/23.
// Copyright © 2023 SOPT-iOS. All rights reserved.
//

import Core
import Networks

import UIKit

final public class AppLifecycleAdapter {
private let cancelBag = CancelBag()
private let authService = DefaultAuthService()
}

// MARK: - Private functions
extension AppLifecycleAdapter {
public func prepare() {
self.onWillEnterForeground()
self.onWillEnterBackground()
}

//MARK: - Usecases
private func onWillEnterForeground() {
NotificationCenter.default
.publisher(for: UIApplication.willEnterForegroundNotification)
.subscribe(on: DispatchQueue.global())
.receive(on: DispatchQueue.main)
.sink(receiveValue: { [weak self] _ in
self?.reissureTokens()
}).store(in: self.cancelBag)
}

private func onWillEnterBackground() { }
}

// MARK: - Private functions
extension AppLifecycleAdapter {
private func reissureTokens() {
guard UserDefaultKeyList.Auth.appAccessToken != nil else { return }

self.authService.reissuance { _ in }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
configureSentry()
configureAppLifecycleAdapter()
registerDependencies()

appLifecycleAdapter.prepare()

application.registerForRemoteNotifications()
return true
Expand Down Expand Up @@ -63,6 +62,10 @@ extension AppDelegate {
options.enableAutoBreadcrumbTracking = true
}
}

private func configureAppLifecycleAdapter() {
self.appLifecycleAdapter.prepare()
}
}

// MARK: - APNs
Expand Down

0 comments on commit 120052a

Please sign in to comment.