@@ -10,29 +10,47 @@ import GenericCollectionViewKit
1010import ICTMDBViewKit
1111import UIKit
1212
13- //MARK: CellItemType
14-
15-
16- //MARK: HomePresenter
13+ // MARK: - HomePresenter
14+ /// Handles the presentation logic for the Home module.
1715final class HomePresenter {
16+
17+ // MARK: - Typealias
18+ /// Typealias for cell items displayed in collection view
1819 typealias CellItem = CellItemType
20+
21+ // MARK: - Properties
22+
23+ /// Reference to the View layer
1924 weak var view : PresenterToViewHomeProtocol ?
20- private var interactor : PresenterToInteractorHomeProtocol
21- private var router : PresenterToRouterHomeProtocol
22- private var popularTvShows : [ PopularTVShowPresentation ] = [ ]
23- private var airingTodayShows : [ AiringTodayPresentation ] = [ ]
25+
26+ /// Reference to the Interactor layer
27+ private var interactor : PresenterToInteractorHomeProtocol
28+
29+ /// Reference to the Router layer
30+ private var router : PresenterToRouterHomeProtocol
31+
32+ /// List of popular TV shows for UI display
33+ private var popularTvShows : [ PopularTVShowPresentation ] = [ ]
34+
35+ /// List of airing today shows for UI display
36+ private var airingTodayShows : [ AiringTodayPresentation ] = [ ]
37+
38+
39+ // MARK: - Init
2440 init ( view: PresenterToViewHomeProtocol ? ,
25- interactor: PresenterToInteractorHomeProtocol ,
26- router : PresenterToRouterHomeProtocol
41+ interactor: PresenterToInteractorHomeProtocol ,
42+ router: PresenterToRouterHomeProtocol
2743 ) {
2844 self . view = view
2945 self . interactor = interactor
3046 self . router = router
3147 }
3248}
3349
34- //MARK: HomePresenter : ViewToPresenterHomeProtocol
35- extension HomePresenter : ViewToPresenterHomeProtocol {
50+
51+ // MARK: - ViewToPresenterHomeProtocol
52+ /// Handles actions triggered by the View layer.
53+ extension HomePresenter : ViewToPresenterHomeProtocol {
3654
3755 func viewDidLoad( ) {
3856 view? . setBackColorAble ( color: " backColor " )
@@ -41,8 +59,11 @@ extension HomePresenter : ViewToPresenterHomeProtocol {
4159 }
4260}
4361
44- //MARK: CollectionViewSources
62+
63+ // MARK: - CollectionViewSources
64+ /// Provides collection view data, layout, and interaction logic.
4565extension HomePresenter {
66+
4667 func layout( for sectionIndex: Int ) -> LayoutSource {
4768 guard let sectionType = SectionType ( rawValue: sectionIndex) else {
4869 return LayoutSourceTeamplate . none. template
@@ -56,21 +77,19 @@ extension HomePresenter {
5677 }
5778
5879 func numberOfRowsInSection( in section: Int ) -> Int {
59- guard let sectionType = SectionType ( rawValue: section) else { return 0 }
80+ guard let sectionType = SectionType ( rawValue: section) else { return 0 }
6081 switch sectionType {
61- case . popular:
62- return popularTvShows. count
63- case . airingToday:
64- return airingTodayShows. count
82+ case . popular: return popularTvShows. count
83+ case . airingToday: return airingTodayShows. count
6584 }
6685 }
6786
87+
6888 func numberOfSections( ) -> Int {
6989 SectionType . allCases. count
7090 }
71-
72- func cellForItem( section: Int , item: Int ) -> CellItemType {
73-
91+
92+ func cellForItem( section: Int , item: Int ) -> CellItemType {
7493 guard let sectionType = SectionType ( rawValue: section) else { return . none }
7594 switch sectionType {
7695 case . popular:
@@ -80,11 +99,11 @@ extension HomePresenter {
8099 let tvShow = airingTodayShows [ item]
81100 return . airing( tvShow)
82101 }
83-
84102 }
85103
104+
86105 func didSelectItem( section: Int , item: Int ) {
87- guard let sectionType = SectionType ( rawValue: section) else { return }
106+ guard let sectionType = SectionType ( rawValue: section) else { return }
88107 switch sectionType {
89108 case . popular:
90109 let id = popularTvShows [ item] . id
@@ -94,28 +113,37 @@ extension HomePresenter {
94113 router. toDetailPage ( view: view, id: id)
95114 }
96115 }
97-
98- func titleForSection( at section: Int ) -> (
99- title: String , sizeType: SectionSizeType ,
100- buttonType: [ TitleForSectionButtonType ] ? ) {
101-
102- guard let sectionType = SectionType ( rawValue: section)
103- else { return ( title: " " , sizeType: . small, buttonType: [ ] ) }
104-
105- var item : (
106- title: String ,
107- sizeType: SectionSizeType ,
108- buttonType: [ TitleForSectionButtonType ] ? )
109-
110- switch sectionType {
111- case . popular:
112- item = ( title: LocalizableUI . popular. localized, sizeType: . large, buttonType: [ . allList] )
113- case . airingToday:
114- item = ( title: LocalizableUI . airingToday. localized, sizeType: . small, buttonType: [ . allList] )
115- }
116- return item
116+
117+
118+ func titleForSection( at section: Int ) -> (
119+ title: String ,
120+ sizeType: SectionSizeType ,
121+ buttonType: [ TitleForSectionButtonType ] ?
122+ ) {
123+ guard let sectionType = SectionType ( rawValue: section) else {
124+ return ( title: " " , sizeType: . small, buttonType: [ ] )
125+ }
126+
127+ var item : ( title: String , sizeType: SectionSizeType , buttonType: [ TitleForSectionButtonType ] ? )
128+
129+ switch sectionType {
130+ case . popular:
131+ item = (
132+ title: LocalizableUI . popular. localized,
133+ sizeType: . large,
134+ buttonType: [ . allList]
135+ )
136+ case . airingToday:
137+ item = (
138+ title: LocalizableUI . airingToday. localized,
139+ sizeType: . small,
140+ buttonType: [ . allList]
141+ )
117142 }
143+ return item
144+ }
118145
146+
119147 func onTappedTitleButton( buttonType: TitleForSectionButtonType , section: Int ) {
120148 guard let sectionType = SectionType ( rawValue: section) else { return }
121149
@@ -124,7 +152,6 @@ extension HomePresenter {
124152 switch buttonType {
125153 case . allList:
126154 router. toAllListPage ( view: view, type: . popular)
127- print ( " Popular All List " )
128155 default :
129156 break
130157 }
@@ -144,40 +171,43 @@ extension HomePresenter {
144171 }
145172
146173 func cellIdentifier( at section: Int ) -> String {
147- guard let section = SectionType ( rawValue: section) else { return " " }
174+ guard let section = SectionType ( rawValue: section) else { return " " }
148175 switch section {
149176 case . popular: return PopularCell . identifier
150177 case . airingToday: return AiringTodayCell . identifier
151-
152178 }
153179 }
154180}
155181
156- //MARK: HomePresenter: InteractorToPresenterHomeProtocol
182+
183+ // MARK: - InteractorToPresenterHomeProtocol
184+ /// Receives data from the Interactor and updates the view.
157185extension HomePresenter : InteractorToPresenterHomeProtocol {
186+
158187 func sendAiringTvShows( _ data: [ AiringToday ] ) {
159188 view? . startLoading ( )
160- airingTodayShows = data. map {
161- AiringTodayPresentation (
162- tvShow: $0) }
189+ airingTodayShows = data. map { AiringTodayPresentation ( tvShow: $0) }
163190 view? . sendError ( errorState: ( isHidden: false , message: " " ) )
164191 view? . relaodCollectionView ( )
165192 view? . finishLoading ( )
166193 }
167194
168195 func sendPopularTvShows( _ data: [ PopularTvShows ] ) {
169196 view? . startLoading ( )
170- popularTvShows = data. map { PopularTVShowPresentation (
171- tvShow: $0) } . sorted { $0. rating > $1. rating }
197+ popularTvShows = data
198+ . map { PopularTVShowPresentation ( tvShow: $0) }
199+ . sorted { $0. rating > $1. rating }
172200 view? . sendError ( errorState: ( isHidden: false , message: " " ) )
173201 view? . relaodCollectionView ( )
174202 view? . finishLoading ( )
175203 }
176204
177205 func sendError( _ type: HomePageErrorType ) {
178206 view? . startLoading ( )
179- view? . sendError ( errorState: ( isHidden: true , message: LocalizableUI . somethingWentWrong. localized) )
207+ view? . sendError ( errorState: ( isHidden: true ,
208+ message: LocalizableUI . somethingWentWrong. localized) )
180209 view? . relaodCollectionView ( )
181210 view? . finishLoading ( )
182211 }
183212}
213+
0 commit comments