66//
77
88import Auth
9+ import Combine
910import DIInjector
1011import Foundation
1112import 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}
0 commit comments