88import Foundation
99import GenericCollectionViewKit
1010import ICTMDBViewKit
11+ import UIKit
1112
1213//MARK: CellItemType
1314
@@ -18,6 +19,8 @@ final class HomePresenter {
1819 weak var view : PresenterToViewHomeProtocol ?
1920 private var interactor : PresenterToInteractorHomeProtocol
2021 private var router : PresenterToRouterHomeProtocol
22+ private var popularTvShows : [ PopularTVShowPresentation ] = [ ]
23+ private var airingTodayShows : [ AiringTodayPresentation ] = [ ]
2124 init ( view: PresenterToViewHomeProtocol ? ,
2225 interactor: PresenterToInteractorHomeProtocol ,
2326 router : PresenterToRouterHomeProtocol
@@ -31,9 +34,10 @@ final class HomePresenter {
3134//MARK: HomePresenter : ViewToPresenterHomeProtocol
3235extension HomePresenter : ViewToPresenterHomeProtocol {
3336
34-
3537 func viewDidLoad( ) {
3638 view? . setBackColorAble ( color: " backColor " )
39+ interactor. loadPopularMovies ( )
40+ interactor. loadAiringMovies ( )
3741 }
3842}
3943
@@ -55,9 +59,9 @@ extension HomePresenter {
5559 guard let sectionType = SectionType ( rawValue: section) else { return 0 }
5660 switch sectionType {
5761 case . popular:
58- return 5
62+ return popularTvShows . count
5963 case . airingToday:
60- return 3
64+ return airingTodayShows . count
6165 }
6266 }
6367
@@ -66,23 +70,31 @@ extension HomePresenter {
6670 }
6771
6872 func cellForItem( section: Int , item: Int ) -> CellItemType {
73+
6974 guard let sectionType = SectionType ( rawValue: section) else { return . none }
7075 switch sectionType {
7176 case . popular:
72-
73- return . popular( " popular " )
77+ let tvShow = popularTvShows [ item ]
78+ return . popular( tvShow )
7479 case . airingToday:
75-
76- return . airing( " airing " )
80+ let tvShow = airingTodayShows [ item ]
81+ return . airing( tvShow )
7782 }
78-
7983
8084 }
8185
82- func didSelectItem( at indexPath: IndexPath ) {
83-
86+ func didSelectItem( section: Int , item: Int ) {
87+ guard let sectionType = SectionType ( rawValue: section) else { return }
88+ switch sectionType {
89+ case . popular:
90+ let id = popularTvShows [ item] . id
91+ router. toDetailPage ( view: view, id: id)
92+ case . airingToday:
93+ let id = airingTodayShows [ item] . id
94+ router. toDetailPage ( view: view, id: id)
95+ }
8496 }
85-
97+
8698 func titleForSection( at section: Int ) -> (
8799 title: String , sizeType: SectionSizeType ,
88100 buttonType: [ TitleForSectionButtonType ] ? ) {
@@ -143,15 +155,29 @@ extension HomePresenter {
143155
144156//MARK: HomePresenter: InteractorToPresenterHomeProtocol
145157extension HomePresenter : InteractorToPresenterHomeProtocol {
146- func sendAiringTvShows( ) {
147-
158+ func sendAiringTvShows( _ data: [ AiringToday ] ) {
159+ view? . startLoading ( )
160+ airingTodayShows = data. map {
161+ AiringTodayPresentation (
162+ tvShow: $0) }
163+ view? . sendError ( errorState: ( isHidden: false , message: " " ) )
164+ view? . relaodCollectionView ( )
165+ view? . finishLoading ( )
148166 }
149167
150- func sendPopularTvShows( ) {
151-
168+ func sendPopularTvShows( _ data: [ PopularTvShows ] ) {
169+ view? . startLoading ( )
170+ popularTvShows = data. map { PopularTVShowPresentation (
171+ tvShow: $0) } . sorted { $0. rating > $1. rating }
172+ view? . sendError ( errorState: ( isHidden: false , message: " " ) )
173+ view? . relaodCollectionView ( )
174+ view? . finishLoading ( )
152175 }
153176
154- func sendError( ) {
155-
177+ func sendError( _ type: HomePageErrorType ) {
178+ view? . startLoading ( )
179+ view? . sendError ( errorState: ( isHidden: true , message: LocalizableUI . somethingWentWrong. localized) )
180+ view? . relaodCollectionView ( )
181+ view? . finishLoading ( )
156182 }
157183}
0 commit comments