@@ -12,7 +12,9 @@ final class TvShowDetailPresenter {
1212 weak var view : PresenterToViewTvShowDetailProtocol ?
1313
1414 private var interactor : PresenterToInteractorTvShowDetailProtocol
15-
15+ private let title = TvShowDetailTitlePresentation ( )
16+ private var seasonList : [ SeasonPresentation ] = [ ]
17+ private var castList : [ CastPresentation ] = [ ]
1618
1719 init ( view: PresenterToViewTvShowDetailProtocol ? ,
1820 interactor: PresenterToInteractorTvShowDetailProtocol ) {
@@ -22,52 +24,122 @@ final class TvShowDetailPresenter {
2224
2325 func viewDidLoad( ) {
2426 view? . setBackColorAble ( color: " backColor " )
25-
27+ // view?.prepareCollectionView()
2628 }
2729}
2830
2931extension TvShowDetailPresenter : ViewToPresenterTvShowDetailProtocol {
3032 func layout( for sectionIndex: Int ) -> LayoutSource {
31-
33+ guard let sectionType = SectionType ( rawValue: sectionIndex) else {
34+ return LayoutSourceTeamplate . none. template
35+ }
36+
37+ switch sectionType {
38+ case . cast:
39+ return LayoutSource (
40+ groupOrientation: . horizontal,
41+ itemSize: . init(
42+ width: ( type: . fractional, value: 1.0 ) ,
43+ height: ( type: . fractional, value: 0.7 ) ) ,
44+ groupSize: . init(
45+ width: ( type: . fractional, value: 0.4 ) ,
46+ height: ( type: . fractional, value: 0.45 ) ) ,
47+ sectionInsets: ( top: 0 , leading: 5 , bottom: 0 , trailing: 5 ) ,
48+ interItemSpacing: 0 ,
49+ interGroupSpacing: 0 ,
50+ scrollDirection: . horizontal
51+ )
52+ case . season:
53+ return LayoutSourceTeamplate . horizontalSingleRow. template
54+ }
3255 }
3356
3457 func getID( id: Int ? ) {
3558 guard let id = id else { return }
36-
59+ interactor. loadTvShowDetail ( id: id)
60+ interactor. loadTvShowCasts ( id: id)
3761
3862 }
3963
4064 func numberOfRowsInSection( in section: Int ) -> Int {
41-
65+ guard let sectionType = SectionType ( rawValue: section) else { return 0 }
66+ switch sectionType {
67+ case . cast:
68+ return castList. count
69+ case . season:
70+ return seasonList. count
71+ }
4272 }
4373
4474 func numberOfSections( ) -> Int {
45-
75+ SectionType . allCases . count
4676 }
4777
4878 func cellForItem( section: Int , item: Int ) -> CellItemType {
49-
79+ guard let sectionType = SectionType ( rawValue: section) else { return . none }
80+ switch sectionType {
81+ case . cast:
82+ let data = castList [ item]
83+ return . cast( data)
84+ case . season:
85+ let data = seasonList [ item]
86+ return . season( data)
87+ }
5088 }
5189
5290
5391
5492 func titleForSection( at section: Int ) -> (
5593 title: String , sizeType: SectionSizeType ,
5694 buttonType: [ TitleForSectionButtonType ] ? ) {
57-
95+ guard let sectionType = SectionType ( rawValue: section) else { return ( title: " " , sizeType: . small, buttonType: [ ] ) }
96+ var item : ( title: String , sizeType: SectionSizeType , buttonType: [ TitleForSectionButtonType ] ? )
97+ switch sectionType {
98+ case . cast:
99+ item = ( title: LocalizableUI . cast. localized, sizeType: . large, buttonType: [ ] )
100+ case . season:
101+ item = ( title: LocalizableUI . season. localized, sizeType: . large, buttonType: [ ] )
102+ }
103+ return item
58104 }
59105
60106 func sectionType( at section: Int ) -> SectionType {
61-
107+ return SectionType ( rawValue : section ) ?? . cast
62108 }
63109
64110 func cellIdentifier( at section: Int ) -> String {
65-
111+ guard let section = SectionType ( rawValue: section) else { return " " }
112+ switch section {
113+ case . cast: return CastCell . identifier
114+ case . season: return SeasonCell . identifier
115+
116+ }
66117 }
67118}
68119
69120extension TvShowDetailPresenter : InteractorToPresenterTvShowDetailProtocol {
70121 func onHandle( handle: TvShowInteractorResult ) {
122+ switch handle {
123+ case . sendData( let detail) :
124+ let detailPresentation = TvShowDetailPresentation ( tvShowDetail: detail)
125+ view? . sendData ( detail: detailPresentation, title: title)
126+ let seasons = detail. seasons
127+ guard let seasons = seasons else { return }
128+ seasonList = seasons. map { SeasonPresentation ( season: $0) }
129+
130+ case . sendError( let error) :
131+ switch error {
132+ case . detailError:
133+ break
134+ case . castError:
135+ castList = [ ]
136+ }
137+ case . sendCast( let casts) :
138+ castList = casts. map { CastPresentation ( cast: $0) }
139+
140+ }
71141
142+ view? . prepareCollectionView ( )
143+ view? . relaodCollectionView ( )
72144 }
73145}
0 commit comments