Skip to content

Commit 8e85f17

Browse files
authored
Merge pull request #326 from Hi-lingual/refactor/#324
[refactor] 리퀴드 글라스 대응 + 피드 api 중복 호출 문제 해결
2 parents 7d01f66 + d9e3927 commit 8e85f17

File tree

3 files changed

+41
-19
lines changed

3 files changed

+41
-19
lines changed

HilingualNetwork/Sources/HilingualNetwork/Foundation/AuthInterceptor.swift

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@ public final class AuthInterceptor: RequestInterceptor {
1414
public static let shared = AuthInterceptor()
1515
private init() {}
1616

17+
// MARK: - Properties
1718
private var cancellables = Set<AnyCancellable>()
19+
private var isRefreshing = false
20+
private var requestsToRetry: [(RetryResult) -> Void] = []
1821

1922
// MARK: - 요청 시 토큰 헤더 주입
23+
2024
public func adapt(_ urlRequest: URLRequest,
2125
for session: Session,
2226
completion: @escaping (Result<URLRequest, Error>) -> Void) {
@@ -37,6 +41,7 @@ public final class AuthInterceptor: RequestInterceptor {
3741
}
3842

3943
// MARK: - 401 발생 시 토큰 재발급 후 재시도
44+
4045
public func retry(_ request: Request,
4146
for session: Session,
4247
dueTo error: Error,
@@ -49,35 +54,56 @@ public final class AuthInterceptor: RequestInterceptor {
4954

5055
print("[AuthInterceptor] 🔄 401 감지 → 토큰 리프레시 시도")
5156

57+
requestsToRetry.append(completion)
58+
59+
guard !isRefreshing else { return }
60+
isRefreshing = true
61+
5262
let refreshToken = UserDefaultHandler.refreshToken
5363
if refreshToken.isEmpty {
64+
completeAllWithFailure(NetworkError.refreshFailed)
5465
notifySessionExpired()
55-
completion(.doNotRetryWithError(NetworkError.refreshFailed))
5666
return
5767
}
5868

59-
let authService = DefaultAuthService()
60-
authService.refreshToken(token: refreshToken)
69+
DefaultAuthService().refreshToken(token: refreshToken)
6170
.sink { [weak self] result in
6271
switch result {
6372
case .failure(let err):
6473
print("[AuthInterceptor] ❌ 토큰 재발급 실패: \(err)")
74+
self?.completeAllWithFailure(err)
6575
self?.notifySessionExpired()
66-
completion(.doNotRetryWithError(err))
6776
case .finished:
6877
break
6978
}
70-
} receiveValue: { dto in
79+
} receiveValue: { [weak self] dto in
80+
guard let self else { return }
7181
UserDefaultHandler.accessToken = dto.accessToken
7282
UserDefaultHandler.refreshToken = dto.refreshToken
7383
print("[AuthInterceptor] ✅ 토큰 재발급 성공 → 요청 재시도")
74-
completion(.retry)
84+
self.completeAllWithRetry()
7585
}
7686
.store(in: &cancellables)
7787
}
7888

89+
// MARK: - Pending 요청 처리
90+
91+
private func completeAllWithRetry() {
92+
isRefreshing = false
93+
let completions = requestsToRetry
94+
requestsToRetry.removeAll()
95+
completions.forEach { $0(.retry) }
96+
}
97+
98+
private func completeAllWithFailure(_ error: Error) {
99+
isRefreshing = false
100+
let completions = requestsToRetry
101+
requestsToRetry.removeAll()
102+
completions.forEach { $0(.doNotRetryWithError(error)) }
103+
}
104+
79105
// MARK: - 세션 만료 이벤트 발행
80-
106+
81107
private func notifySessionExpired() {
82108
NotificationCenter.default.post(
83109
name: Notification.Name("SessionExpired"),

HilingualPresentation/Sources/Presentation/Common/Extensions/BaseUIViewController+Navigation.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ public extension BaseUIViewController {
9292
button.setImage(image, for: .normal)
9393
button.tintColor = .black
9494
button.addTarget(self, action: action, for: .touchUpInside)
95-
button.frame = CGRect(x: 0, y: 0, width: 24, height: 24)
96-
button.contentHorizontalAlignment = .fill
97-
button.contentVerticalAlignment = .fill
95+
// button.frame = CGRect(x: 0, y: 0, width: 24, height: 24)
96+
// button.contentHorizontalAlignment = .fill
97+
// button.contentVerticalAlignment = .fill
9898

9999
return UIBarButtonItem(customView: button)
100100
}

HilingualPresentation/Sources/Presentation/Feed/FeedViewController.swift

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,14 @@ public final class FeedViewController: BaseUIViewController<FeedViewModel> {
8282
navigationController?.setNavigationBarHidden(true, animated: false)
8383

8484
input.reload.send()
85-
85+
}
86+
87+
public override func viewDidAppear(_ animated: Bool) {
88+
super.viewDidAppear(animated)
8689
recommendFeedVC.refresh()
8790
followingFeedVC.refresh()
8891
}
89-
92+
9093
// MARK: - Binding
9194

9295
public override func bind(viewModel: FeedViewModel) {
@@ -99,13 +102,6 @@ public final class FeedViewController: BaseUIViewController<FeedViewModel> {
99102
}
100103
.store(in: &viewModel.cancellables)
101104
}
102-
103-
public override func viewDidAppear(_ animated: Bool) {
104-
super.viewDidAppear(animated)
105-
106-
recommendFeedVC.refresh()
107-
followingFeedVC.refresh()
108-
}
109105

110106
//MARK: - Action
111107

0 commit comments

Comments
 (0)