Skip to content

Commit 378d7d7

Browse files
authored
[NL-79]: 미비점 수정 (#63)
* [NL-79]: 신규 번호 추천 시 홈에서 추천 번호 조회 API 만 호출하도록 수정 * [NL-79]: 당첨 결과 확인 후 홈 화면 번호 재조회 * [NL-79]: 웹뷰 로딩 인디케이터 추가
1 parent a5b04c5 commit 378d7d7

File tree

6 files changed

+101
-33
lines changed

6 files changed

+101
-33
lines changed

Feature/History/Sources/HistoryWebViewController.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,41 @@ public final class HistoryWebViewController: BaseViewController {
5151
viewModel.output.loadURL
5252
.receive(on: DispatchQueue.main)
5353
.sink { [weak self] request in
54+
self?.showLoading()
5455
self?.webView.load(request)
5556
}
5657
.store(in: &cancellables)
5758
}
5859
}
5960

6061
extension HistoryWebViewController: WKNavigationDelegate {
62+
public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
63+
// TODO: 뷰모델로 분리
64+
hideLoading()
65+
}
66+
6167
public func webView(
6268
_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: any Error
6369
) {
64-
print(error)
70+
// TODO: 뷰모델로 분리
71+
hideLoading()
6572
// TODO: 에러 처리
73+
print(error)
6674
}
6775

6876
public func webView(
6977
_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!,
7078
withError error: any Error
7179
) {
80+
// TODO: 뷰모델로 분리
81+
hideLoading()
82+
// TODO: 에러 처리
7283
print(error)
84+
}
85+
86+
public func webViewWebContentProcessDidTerminate(_ webView: WKWebView) {
87+
// TODO: 뷰모델로 분리
88+
hideLoading()
7389
// TODO: 에러 처리
7490
}
7591
}

Feature/Home/Sources/Home/HomeService.swift

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77

88
import Auth
9+
import Combine
910
import DIInjector
1011
import Foundation
1112
import NetworkCore
@@ -17,10 +18,15 @@ final class HomeService {
1718
private var appVersion: String? {
1819
Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
1920
}
20-
var round: Int?
21+
private var cachedDailyFortunes: DailyFortunesDTO?
22+
private(set) var round: Int?
23+
var recommendationStateChanged: AnyPublisher<Void, Never> {
24+
NotificationCenter.default.publisher(for: .recommendationStateChanged)
25+
.map { _ in Void() }
26+
.eraseToAnyPublisher()
27+
}
2128

2229
func fetch() async throws -> [any HomeCellModel] {
23-
let name = self.userDataManager.user?.name ?? "" // TODO: 확인 필요
2430
let getLottoRecommendationTarget = HomeTarget.GetLottoRecommendation(
2531
userID: userDataManager.userID)
2632
async let lottoRecommendationRequest = networkProvider.request(
@@ -31,8 +37,39 @@ final class HomeService {
3137
lottoRecommendationRequest, dailyFortunesRequest
3238
)
3339
self.round = lottoRecommendation.round
40+
self.cachedDailyFortunes = dailyFortunes
3441

35-
let recommendationCollectionViewCellModel: HomeRecommendationCollectionViewCellModel
42+
return [
43+
makeHeader(round: lottoRecommendation.round, message: dailyFortunes.title),
44+
makeRecommendation(lottoRecommendation),
45+
makeTodayFortune(dailyFortunes),
46+
]
47+
}
48+
49+
func fetchLottoRecommendation() async throws -> [any HomeCellModel] {
50+
guard let cachedDailyFortunes else {
51+
return try await fetch()
52+
}
53+
let getLottoRecommendationTarget = HomeTarget.GetLottoRecommendation(
54+
userID: userDataManager.userID)
55+
let lottoRecommendation = try await networkProvider.request(target: getLottoRecommendationTarget)
56+
round = lottoRecommendation.round
57+
return [
58+
makeHeader(round: lottoRecommendation.round, message: cachedDailyFortunes.title),
59+
makeRecommendation(lottoRecommendation),
60+
makeTodayFortune(cachedDailyFortunes),
61+
]
62+
}
63+
64+
private func makeHeader(round: Int, message: String?) -> HomeHeaderCollectionViewCellModel {
65+
return HomeHeaderCollectionViewCellModel(
66+
roundText: "\(round)",
67+
message: message ?? "잘 되면 꼭 기억해 주시오"
68+
)
69+
}
70+
71+
private func makeRecommendation(_ lottoRecommendation: LottoRecommendationDTO) -> HomeRecommendationCollectionViewCellModel {
72+
let name = self.userDataManager.user?.name ?? "" // TODO: 확인 필요
3673
if let recommendationContent = lottoRecommendation.content {
3774
let numbers = [
3875
recommendationContent.num1,
@@ -42,33 +79,28 @@ final class HomeService {
4279
recommendationContent.num5,
4380
recommendationContent.num6,
4481
].sorted()
45-
recommendationCollectionViewCellModel = HomeRecommendationCollectionViewCellModel(
82+
return HomeRecommendationCollectionViewCellModel(
4683
title: "\(name)님을 위한 로또 번호 추천",
47-
state: lottoRecommendation.isFinished
48-
? .needsResultCheck(numbers: numbers) : .recommended(numbers: numbers)
84+
// state: lottoRecommendation.isFinished
85+
// ? .needsResultCheck(numbers: numbers) : .recommended(numbers: numbers)
86+
state: .needsResultCheck(numbers: numbers)
4987
)
5088
} else {
51-
recommendationCollectionViewCellModel = HomeRecommendationCollectionViewCellModel(
89+
return HomeRecommendationCollectionViewCellModel(
5290
title: "\(name)님을 위한 로또 번호 추천",
5391
state: .needsRecommendation
5492
)
5593
}
56-
94+
}
95+
96+
private func makeTodayFortune(_ dailyFortunes: DailyFortunesDTO) -> HomeTodayFortuneCollectionViewCellModel {
5797
let homeTodayFortuneCollectionViewCellModels = dailyFortunes.content.map { item in
5898
FortuneItemCollectionViewCellModel(
5999
title: item.fortuneType,
60100
imageURL: item.imageURL,
61101
message: item.description
62102
)
63103
}
64-
65-
return [
66-
HomeHeaderCollectionViewCellModel(
67-
roundText: "\(lottoRecommendation.round)",
68-
message: dailyFortunes.title ?? "잘 되면 꼭 기억해 주시오"
69-
),
70-
recommendationCollectionViewCellModel,
71-
HomeTodayFortuneCollectionViewCellModel(items: homeTodayFortuneCollectionViewCellModels),
72-
]
104+
return HomeTodayFortuneCollectionViewCellModel(items: homeTodayFortuneCollectionViewCellModels)
73105
}
74106
}

Feature/Home/Sources/Home/HomeViewModel.swift

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,23 @@ public final class HomeViewModel {
3636
.getPublisher()
3737
.sink { [weak self] _ in
3838
guard let self = self else { return }
39-
fetchUser()
39+
fetch()
4040
}
4141
.store(in: &cancellables)
4242

43-
RecommendationDetailService
44-
.getPublisher()
43+
homeService
44+
.recommendationStateChanged
4545
.sink { [weak self] _ in
4646
guard let self = self else { return }
47-
fetchUser()
47+
fetchRecommendation()
4848
}
4949
.store(in: &cancellables)
5050
}
5151

5252
func send(input: Input) {
5353
switch input {
5454
case .viewDidLoad:
55-
fetchUser()
55+
fetch()
5656

5757
case .recommendationButtonTapped(let state):
5858
switch state {
@@ -81,8 +81,8 @@ public final class HomeViewModel {
8181
}
8282
}
8383

84-
extension HomeViewModel {
85-
func fetchUser() {
84+
private extension HomeViewModel {
85+
func fetch() {
8686
output.isLoading.send(true)
8787
Task {
8888
do {
@@ -95,4 +95,18 @@ extension HomeViewModel {
9595
output.isLoading.send(false)
9696
}
9797
}
98+
99+
func fetchRecommendation() {
100+
output.isLoading.send(true)
101+
Task {
102+
do {
103+
let sections = try await homeService.fetchLottoRecommendation()
104+
output.sections.send(sections)
105+
} catch {
106+
// TODO: 에러 처리
107+
print(error)
108+
}
109+
output.isLoading.send(false)
110+
}
111+
}
98112
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//
2+
// HomeNotifications.swift
3+
// Home
4+
//
5+
// Created by ttozzi on 8/20/25.
6+
//
7+
8+
import Foundation
9+
10+
extension Notification.Name {
11+
static let recommendationStateChanged = Notification.Name("recommendationStateChanged")
12+
}

Feature/Home/Sources/LottoResult/LottoResultService.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ struct LottoResultService {
2525
func fetch(round: Int) async throws -> Response {
2626
let target = HomeTarget.CheckLottoResult(userID: userDataManager.userID, round: round)
2727
let lottoResult = try await networkProvider.request(target: target)
28+
NotificationCenter.default.post(name: .recommendationStateChanged, object: nil)
2829

2930
let rankTitle =
3031
if let rank = lottoResult.rank {

Feature/Home/Sources/RecommendationDetail/RecommendationDetailService.swift

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ final class RecommendationDetailService {
1616
@Injected private var userDataManager: UserDataManager
1717
@Injected private var networkProvider: NetworkProvider
1818

19-
private static var recommentPublisher = PassthroughSubject<Void, Never>()
20-
2119
private var username: String? { userDataManager.user?.name }
2220
var navigationTitle: String {
2321
guard let username else {
@@ -30,7 +28,7 @@ final class RecommendationDetailService {
3028
func createRecommendation() async throws -> [any RecommendationDetailCellModel] {
3129
let target = HomeTarget.CreateLottoRecommendation(userID: userDataManager.userID)
3230
let lottoRecommendation = try await networkProvider.request(target: target)
33-
RecommendationDetailService.recommentPublisher.send()
31+
NotificationCenter.default.post(name: .recommendationStateChanged, object: nil)
3432
return try makeSections(from: lottoRecommendation)
3533
}
3634

@@ -93,9 +91,4 @@ final class RecommendationDetailService {
9391
),
9492
]
9593
}
96-
97-
public static func getPublisher() -> AnyPublisher<Void, Never> {
98-
RecommendationDetailService.recommentPublisher
99-
.eraseToAnyPublisher()
100-
}
10194
}

0 commit comments

Comments
 (0)