Skip to content

Commit

Permalink
�[Fix] #287 - Interceptor retry 오류 수정
Browse files Browse the repository at this point in the history
�[Fix] #287 - Interceptor retry 오류 수정
  • Loading branch information
yungu0010 authored Aug 6, 2024
2 parents 605f49e + f447e1c commit 2c4ac36
Show file tree
Hide file tree
Showing 13 changed files with 122 additions and 86 deletions.
19 changes: 9 additions & 10 deletions pophory-iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -759,15 +759,6 @@
path = ViewController;
sourceTree = "<group>";
};
3B218EA52B833F85006959E3 /* Interceptor */ = {
isa = PBXGroup;
children = (
3B218EA62B833FA0006959E3 /* AuthInterceptor.swift */,
);
name = Interceptor;
path = "Intercep색";
sourceTree = "<group>";
};
3B3A090F2A56A4F200C8A740 /* PhotoDetail */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -816,6 +807,14 @@
path = ViewModel;
sourceTree = "<group>";
};
3BC15D842C6255BC0056A87F /* Interceptor */ = {
isa = PBXGroup;
children = (
3B218EA62B833FA0006959E3 /* AuthInterceptor.swift */,
);
path = Interceptor;
sourceTree = "<group>";
};
3BE178932C4FF1A600FC755B /* View */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -1006,7 +1005,7 @@
91F3A77C2A52AD8300C06D1B /* Network */ = {
isa = PBXGroup;
children = (
3B218EA52B833F85006959E3 /* Interceptor */,
3BC15D842C6255BC0056A87F /* Interceptor */,
91F3A77D2A52ADCA00C06D1B /* Common */,
B1631F5E2A175FD00050974F /* Models */,
91F3A78A2A52B1B300C06D1B /* API */,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"images" : [
{
"filename" : "Frame 514631.pdf",
"filename" : "img_networkError.svg",
"idiom" : "universal"
}
],
Expand Down
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions pophory-iOS/Global/Resources/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ extension SceneDelegate {

let window = UIWindow(windowScene: windowScene)
window.overrideUserInterfaceStyle = .light
RootViewSwitcher.shared.setWindow(window)
self.window = window
window.makeKeyAndVisible()

RootViewSwitcher.shared.setRootView(.splash)

self.window = window
}
}
25 changes: 12 additions & 13 deletions pophory-iOS/Global/Utilities/RootViewSwitcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,20 @@

import UIKit

enum RootView {
case splash
case onboarding
case home
case albumFull
case addPhoto(image: UIImage, imageType: PhotoCellType)
case share(shareId: String)
}

enum UpdateType {
case force
case optional
}

final class RootViewSwitcher {
enum RootView {
case splash
case onboarding
case home
case albumFull
case addPhoto(image: UIImage, imageType: PhotoCellType)
case share(shareId: String)
}

static let shared = RootViewSwitcher()

Expand All @@ -41,8 +40,9 @@ final class RootViewSwitcher {
extension RootViewSwitcher {

func setRootView(_ rootView: RootView) {
guard let window = self.window else { return }

let sceneDelegate = UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate
guard let delegate = sceneDelegate else { return }

var rootViewController: UIViewController
switch rootView {
case .splash:
Expand Down Expand Up @@ -74,7 +74,6 @@ extension RootViewSwitcher {
navigationController.pushViewController(addPhotoViewController, animated: false)
return
}
window.rootViewController = PophoryNavigationController(rootViewController: rootViewController)
window.makeKeyAndVisible()
delegate.window?.rootViewController = PophoryNavigationController(rootViewController: rootViewController)
}
}
65 changes: 65 additions & 0 deletions pophory-iOS/Network/Interceptor/AuthInterceptor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//
// File.swift
// pophory-iOS
//
// Created by 강윤서 on 2/19/24.
//

import Foundation
import Alamofire

final class AuthInterceptor: RequestInterceptor {

static let shared = AuthInterceptor()

private init() {}

func adapt(_ urlRequest: URLRequest, for session: Session, completion: @escaping (Result<URLRequest, Error>) -> Void) {
guard let urlString = Bundle.main.infoDictionary?["BASE_URL"] as? String,
let url = URL(string: urlString) else {
fatalError("🚨Base URL을 찾을 수 없습니다🚨")
}

guard urlRequest.url?.absoluteString.hasPrefix(urlString) == true,
let accessToken = PophoryTokenManager.shared.fetchAccessToken(),
let refreshToken = PophoryTokenManager.shared.fetchRefreshToken() else {
completion(.success(urlRequest))
return
}

var urlRequest = urlRequest
urlRequest.setValue("Bearer \(PophoryTokenManager.shared.fetchAccessToken()!)", forHTTPHeaderField: "Authorization")
// urlRequest.headers.update(name: "Authorization", value: "Bearer \(PophoryTokenManager.shared.fetchAccessToken()!)")
print("🍥🍥🍥adator 적용 \(urlRequest.headers)")
completion(.success(urlRequest))
}

func retry(_ request: Request, for session: Session, dueTo error: Error, completion: @escaping (RetryResult) -> Void) {
print("🍥🍥🍥retry 진입")
guard let response = request.task?.response as? HTTPURLResponse, response.statusCode == 401 else {
completion(.doNotRetryWithError(error))
return
}

NetworkService.shared.authRepostiory.updateRefreshToken { result in
switch result {
case .success(let response):
guard let data = response as? UpdatedAccessTokenDTO else {
DispatchQueue.main.async {
RootViewSwitcher.shared.setRootView(.onboarding)
}
completion(.doNotRetryWithError(error))
return
}
PophoryTokenManager.shared.saveAccessToken(data.accessToken)
PophoryTokenManager.shared.saveRefreshToken(data.refreshToken)
completion(.retry)
default:
DispatchQueue.main.async {
RootViewSwitcher.shared.setRootView(.onboarding)
}
completion(.doNotRetryWithError(error))
}
}
}
}
53 changes: 0 additions & 53 deletions pophory-iOS/Network/Intercep색/AuthInterceptor.swift

This file was deleted.

4 changes: 1 addition & 3 deletions pophory-iOS/Network/Repository/DefaultAuthRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ final class DefaultAuthRepository: BaseRepository, AuthRepository {
if response.statusCode < 300 {
do {
let loginResponse = try response.map(UpdatedAccessTokenDTO.self)
completion(.success((loginResponse)))
PophoryTokenManager.shared.saveAccessToken(loginResponse.accessToken)
PophoryTokenManager.shared.saveRefreshToken(loginResponse.refreshToken)
completion(.success((loginResponse)))
print("Successfully refreshed access token")
} catch {
print("Error decoding the login response: \(error)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import UIKit

class StartPophoryViewController: BaseViewController {
final class StartPophoryViewController: BaseViewController {

lazy var startPophoryView = StartPophoryView()

Expand Down
4 changes: 4 additions & 0 deletions pophory-iOS/Screen/HomeAlbum/HomeAlbumView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,8 @@ extension HomeAlbumView {
func setMaxPhotoCount(_ maxPhotoCount: Int) {
self.maxPhotoLimit = maxPhotoCount
}

func setPhotoCount(_ photoCount: String) {
self.statusLabelText = photoCount
}
}
2 changes: 1 addition & 1 deletion pophory-iOS/Screen/HomeAlbum/HomeAlbumViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ extension HomeAlbumViewController {
homeAlbumView.albumImageView.image = ImageLiterals.albumCoverList[coverIndex]
}

homeAlbumView.statusLabelText = String(album.photoCount ?? 0)
homeAlbumView.setPhotoCount(String(album.photoCount ?? 0))

homeAlbumView.updateProgressBarWidth(updateWidth: progressValueUnwrapped)

Expand Down
2 changes: 1 addition & 1 deletion pophory-iOS/Screen/Mypage/View/MyPage/MyPageRootView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protocol MyPageRootViewDelegate: NSObject {
func handleOnAd()
}

class MyPageRootView: UIView {
final class MyPageRootView: UIView {

// MARK: - Properties

Expand Down

0 comments on commit 2c4ac36

Please sign in to comment.