66//
77
88import UIKit
9+
910import SnapKit
1011import Then
12+ import Moya
1113
1214final class UploadedCourseInfoVC : UIViewController {
1315
1416 // MARK: - Properties
1517
16- var UploadedCourseList : [ UploadedCourseInfoModel ] = [
17- UploadedCourseInfoModel ( title: " 행복한 날들 " , place: " 수원시 장안구 " ) ,
18- UploadedCourseInfoModel ( title: " 몽이랑 산책 " , place: " 수원시 장안구 " ) ,
19- UploadedCourseInfoModel ( title: " 패스트파이브 " , place: " 수원시 장안구 " ) ,
20- UploadedCourseInfoModel ( title: " 행복한 날들 " , place: " 수원시 장안구 " ) ,
21- UploadedCourseInfoModel ( title: " 몽이랑 산책 " , place: " 수원시 장안구 " ) ,
22- UploadedCourseInfoModel ( title: " 패스트파이브 " , place: " 수원시 장안구 " ) ,
23- UploadedCourseInfoModel ( title: " 행복한 날들 " , place: " 수원시 장안구 " ) ,
24- UploadedCourseInfoModel ( title: " 몽이랑 산책 " , place: " 수원시 장안구 " ) ,
25- UploadedCourseInfoModel ( title: " 패스트파이브 " , place: " 수원시 장안구 " )
26- ]
18+ private var uploadedCourseProvider = MoyaProvider < MyPageRouter > (
19+ plugins: [ NetworkLoggerPlugin ( verbose: true ) ]
20+ )
21+
22+ private var uploadedCourseList = [ PublicCourse] ( )
2723
2824 // MARK: - Constants
2925
@@ -43,8 +39,6 @@ final class UploadedCourseInfoVC: UIViewController {
4339 let collectionView = UICollectionView ( frame: . zero, collectionViewLayout: layout)
4440 collectionView. isScrollEnabled = true
4541 collectionView. showsVerticalScrollIndicator = false
46- collectionView. delegate = self
47- collectionView. dataSource = self
4842
4943 return collectionView
5044 } ( )
@@ -57,6 +51,27 @@ final class UploadedCourseInfoVC: UIViewController {
5751 setUI ( )
5852 setLayout ( )
5953 register ( )
54+ setDelegate ( )
55+ getUploadedCourseInfo ( )
56+ }
57+ }
58+
59+ // MARK: - Methods
60+
61+ extension UploadedCourseInfoVC {
62+ private func setData( courseList: [ PublicCourse ] ) {
63+ self . uploadedCourseList = courseList
64+ UploadedCourseInfoCollectionView . reloadData ( )
65+ }
66+
67+ private func setDelegate( ) {
68+ self . UploadedCourseInfoCollectionView. delegate = self
69+ self . UploadedCourseInfoCollectionView. dataSource = self
70+ }
71+
72+ private func register( ) {
73+ UploadedCourseInfoCollectionView . register ( UploadedCourseInfoCVC . self,
74+ forCellWithReuseIdentifier: UploadedCourseInfoCVC . className)
6075 }
6176}
6277
@@ -87,13 +102,6 @@ extension UploadedCourseInfoVC {
87102 make. bottom. equalTo ( view. safeAreaLayoutGuide)
88103 }
89104 }
90-
91- // MARK: - General Helpers
92-
93- private func register( ) {
94- UploadedCourseInfoCollectionView . register ( UploadedCourseInfoCVC . self,
95- forCellWithReuseIdentifier: UploadedCourseInfoCVC . className)
96- }
97105}
98106
99107// MARK: - UICollectionViewDelegateFlowLayout
@@ -119,12 +127,44 @@ extension UploadedCourseInfoVC: UICollectionViewDelegateFlowLayout {
119127
120128extension UploadedCourseInfoVC : UICollectionViewDataSource {
121129 func collectionView( _ collectionView: UICollectionView , numberOfItemsInSection section: Int ) -> Int {
122- return UploadedCourseList . count
130+ return uploadedCourseList . count
123131 }
124132
125133 func collectionView( _ collectionView: UICollectionView , cellForItemAt indexPath: IndexPath ) -> UICollectionViewCell {
126134 guard let uploadedCourseCell = collectionView. dequeueReusableCell ( withReuseIdentifier: UploadedCourseInfoCVC . className, for: indexPath) as? UploadedCourseInfoCVC else { return UICollectionViewCell ( ) }
127- uploadedCourseCell. dataBind ( model: UploadedCourseList [ indexPath. item] )
135+ uploadedCourseCell. setData ( model: uploadedCourseList [ indexPath. item] )
128136 return uploadedCourseCell
129137 }
130138}
139+
140+ // MARK: - Network
141+
142+ extension UploadedCourseInfoVC {
143+ func getUploadedCourseInfo( ) {
144+ LoadingIndicator . showLoading ( )
145+ uploadedCourseProvider. request ( . getUploadedCourseInfo) { [ weak self] response in
146+ LoadingIndicator . hideLoading ( )
147+ guard let self = self else { return }
148+ switch response {
149+ case . success( let result) :
150+ let status = result. statusCode
151+ if 200 ..< 300 ~= status {
152+ do {
153+ let responseDto = try result. map ( BaseResponse< PickedMapListResponseDto> . self )
154+ guard let data = responseDto. data else { return }
155+ self . setData ( courseList: data. publicCourses)
156+ } catch {
157+ print ( error. localizedDescription)
158+ }
159+ }
160+ if status >= 400 {
161+ print ( " 400 error " )
162+ self . showNetworkFailureToast ( )
163+ }
164+ case . failure( let error) :
165+ print ( error. localizedDescription)
166+ self . showNetworkFailureToast ( )
167+ }
168+ }
169+ }
170+ }
0 commit comments