From dca11031d4ac5b7806740b7934d002cdc188dc92 Mon Sep 17 00:00:00 2001 From: LeeMyeongJin Date: Sat, 23 Sep 2023 15:01:24 +0900 Subject: [PATCH 1/6] =?UTF-8?q?[Fix]=20#183=20-=20getCourseData=20?= =?UTF-8?q?=EB=9D=BC=EC=9A=B0=ED=84=B0=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Runnect-iOS/Network/Router/PublicCourseRouter.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Runnect-iOS/Runnect-iOS/Network/Router/PublicCourseRouter.swift b/Runnect-iOS/Runnect-iOS/Network/Router/PublicCourseRouter.swift index 90df9a48..3c336277 100644 --- a/Runnect-iOS/Runnect-iOS/Network/Router/PublicCourseRouter.swift +++ b/Runnect-iOS/Runnect-iOS/Network/Router/PublicCourseRouter.swift @@ -9,7 +9,7 @@ import Foundation import Moya enum PublicCourseRouter { - case getCourseData + case getCourseData(pageNo: Int) case getCourseSearchData(keyword: String) case courseUploadingData(param: CourseUploadingRequestDto) case getUploadedCourseDetail(publicCourseId: Int) @@ -59,6 +59,8 @@ extension PublicCourseRouter: TargetType { var task: Moya.Task { switch self { + case .getCourseData(let pageNo): + return .requestParameters(parameters: ["pageNo": pageNo], encoding: URLEncoding.default) case .getCourseSearchData(let keyword): return .requestParameters(parameters: ["keyword": keyword], encoding: URLEncoding.default) case .courseUploadingData(param: let param): @@ -73,7 +75,7 @@ extension PublicCourseRouter: TargetType { fatalError("Encoding 실패")} case .deleteUploadedCourse(let publicCourseIdList): return .requestParameters(parameters: ["publicCourseIdList": publicCourseIdList], encoding: JSONEncoding.default) - case .getCourseData, .getUploadedCourseDetail, .getUploadedCourseInfo: + case .getUploadedCourseDetail, .getUploadedCourseInfo: return .requestPlain } } From a75ce4490eadd6a3d74f4655c5913315d1fdeb24 Mon Sep 17 00:00:00 2001 From: LeeMyeongJin Date: Sat, 23 Sep 2023 15:07:50 +0900 Subject: [PATCH 2/6] =?UTF-8?q?[Feat]=20#183=20-=20=EC=BD=94=EC=8A=A4=20?= =?UTF-8?q?=EB=B0=9C=EA=B2=AC=20pagination?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 서버에서 24개씩 받아오는 데이터를 처리하기 위함 입니다. --- .../Views/VC/CourseDiscoveryVC.swift | 44 +++++++++++++++++-- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/Runnect-iOS/Runnect-iOS/Presentation/CourseDiscovery/Views/VC/CourseDiscoveryVC.swift b/Runnect-iOS/Runnect-iOS/Presentation/CourseDiscovery/Views/VC/CourseDiscoveryVC.swift index a26fce98..6a7786dd 100644 --- a/Runnect-iOS/Runnect-iOS/Presentation/CourseDiscovery/Views/VC/CourseDiscoveryVC.swift +++ b/Runnect-iOS/Runnect-iOS/Presentation/CourseDiscovery/Views/VC/CourseDiscoveryVC.swift @@ -21,6 +21,9 @@ final class CourseDiscoveryVC: UIViewController { private var courseList = [PublicCourse]() + // pagenation 을 위한 변수 입니다. + private var pageNo = 1 + // MARK: - UIComponents private lazy var naviBar = CustomNavigationBar(self, type: .title).setTitle("코스 발견") @@ -66,6 +69,13 @@ final class CourseDiscoveryVC: UIViewController { override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) + + // 데이터 초기화 + courseList.removeAll() + pageNo = 1 + + // 컬렉션 뷰를 리로드하여 초기화된 데이터를 화면에 표시 + mapCollectionView.reloadData() self.hideTabBar(wantsToHide: false) self.getCourseData() } @@ -212,6 +222,28 @@ extension CourseDiscoveryVC: UICollectionViewDelegate, UICollectionViewDataSourc return cell } } + + func scrollViewDidScroll(_ scrollView: UIScrollView) { + let contentOffsetY = scrollView.contentOffset.y + let collectionViewHeight = scrollView.contentSize.height + let paginationY = collectionViewHeight * 0.2 + + // 스크롤이 맨 아래에 도달하면 다음 페이지 데이터를 불러옵니다. + if contentOffsetY >= collectionViewHeight - paginationY { + if courseList.count < pageNo * 12 { // 페이지 끝에 도달하면 현재 페이지에 더 이상 데이터가 없음을 의미합니다. + // 페이지네이션 중단 코드 + return + } + + // 다음 페이지 번호를 증가시킵니다. + pageNo += 1 + print("🔥다음 페이지 로드: \(pageNo)🔥") + + // 여기에서 다음 페이지 데이터를 불러오는 함수를 호출하세요. + getCourseData() + } + } + } // MARK: - UICollectionViewDelegateFlowLayout @@ -288,7 +320,7 @@ extension CourseDiscoveryVC: CourseListCVCDeleagte { extension CourseDiscoveryVC { private func getCourseData() { LoadingIndicator.showLoading() - PublicCourseProvider.request(.getCourseData) { response in + PublicCourseProvider.request(.getCourseData(pageNo: pageNo)) { response in LoadingIndicator.hideLoading() switch response { case .success(let result): @@ -297,7 +329,13 @@ extension CourseDiscoveryVC { do { let responseDto = try result.map(BaseResponse.self) guard let data = responseDto.data else { return } - self.setData(courseList: data.publicCourses) + + // 새로 받은 데이터를 기존 리스트에 추가 (쌓기 위함) + self.courseList.append(contentsOf: data.publicCourses) + + // UI를 업데이트하여 추가된 데이터를 반영합니다. + self.mapCollectionView.reloadData() + } catch { print(error.localizedDescription) } @@ -312,7 +350,7 @@ extension CourseDiscoveryVC { } } } - + private func scrapCourse(publicCourseId: Int, scrapTF: Bool) { LoadingIndicator.showLoading() scrapProvider.request(.createAndDeleteScrap(publicCourseId: publicCourseId, scrapTF: scrapTF)) { [weak self] response in From 25cdb94769b98406b570b3a31ef4f17cfb8f33ec Mon Sep 17 00:00:00 2001 From: LeeMyeongJin Date: Sun, 24 Sep 2023 17:49:47 +0900 Subject: [PATCH 3/6] =?UTF-8?q?[Feat]=20#183=20-=20pagination=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CourseDiscovery/Views/VC/CourseDiscoveryVC.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Runnect-iOS/Runnect-iOS/Presentation/CourseDiscovery/Views/VC/CourseDiscoveryVC.swift b/Runnect-iOS/Runnect-iOS/Presentation/CourseDiscovery/Views/VC/CourseDiscoveryVC.swift index 6a7786dd..fc2147d8 100644 --- a/Runnect-iOS/Runnect-iOS/Presentation/CourseDiscovery/Views/VC/CourseDiscoveryVC.swift +++ b/Runnect-iOS/Runnect-iOS/Presentation/CourseDiscovery/Views/VC/CourseDiscoveryVC.swift @@ -230,7 +230,7 @@ extension CourseDiscoveryVC: UICollectionViewDelegate, UICollectionViewDataSourc // 스크롤이 맨 아래에 도달하면 다음 페이지 데이터를 불러옵니다. if contentOffsetY >= collectionViewHeight - paginationY { - if courseList.count < pageNo * 12 { // 페이지 끝에 도달하면 현재 페이지에 더 이상 데이터가 없음을 의미합니다. + if courseList.count < pageNo * 24 { // 페이지 끝에 도달하면 현재 페이지에 더 이상 데이터가 없음을 의미합니다. // 페이지네이션 중단 코드 return } From b66c1f1bad1484a69cd8e59ad47d5648171ed38d Mon Sep 17 00:00:00 2001 From: LeeMyeongJin Date: Sun, 24 Sep 2023 20:33:43 +0900 Subject: [PATCH 4/6] =?UTF-8?q?[Feat]=20#183=20-=20=ED=8E=98=EC=9D=B4?= =?UTF-8?q?=EC=A7=95=20=EA=B8=B0=EC=A4=80=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CourseDiscovery/Views/VC/CourseDiscoveryVC.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Runnect-iOS/Runnect-iOS/Presentation/CourseDiscovery/Views/VC/CourseDiscoveryVC.swift b/Runnect-iOS/Runnect-iOS/Presentation/CourseDiscovery/Views/VC/CourseDiscoveryVC.swift index fc2147d8..ee17732e 100644 --- a/Runnect-iOS/Runnect-iOS/Presentation/CourseDiscovery/Views/VC/CourseDiscoveryVC.swift +++ b/Runnect-iOS/Runnect-iOS/Presentation/CourseDiscovery/Views/VC/CourseDiscoveryVC.swift @@ -225,10 +225,10 @@ extension CourseDiscoveryVC: UICollectionViewDelegate, UICollectionViewDataSourc func scrollViewDidScroll(_ scrollView: UIScrollView) { let contentOffsetY = scrollView.contentOffset.y - let collectionViewHeight = scrollView.contentSize.height + let collectionViewHeight = mapCollectionView.contentSize.height let paginationY = collectionViewHeight * 0.2 - // 스크롤이 맨 아래에 도달하면 다음 페이지 데이터를 불러옵니다. + // 스크롤이 80% (0.2) 까지 도달하면 다음 페이지 데이터를 불러옵니다. if contentOffsetY >= collectionViewHeight - paginationY { if courseList.count < pageNo * 24 { // 페이지 끝에 도달하면 현재 페이지에 더 이상 데이터가 없음을 의미합니다. // 페이지네이션 중단 코드 From 5bc00be71ce45c10147ee8646f7b48042e7229b8 Mon Sep 17 00:00:00 2001 From: LeeMyeongJin Date: Sun, 24 Sep 2023 20:33:43 +0900 Subject: [PATCH 5/6] =?UTF-8?q?[Feat]=20#183=20-=20pagination=20=EA=B8=B0?= =?UTF-8?q?=EC=A4=80=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CourseDiscovery/Views/VC/CourseDiscoveryVC.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Runnect-iOS/Runnect-iOS/Presentation/CourseDiscovery/Views/VC/CourseDiscoveryVC.swift b/Runnect-iOS/Runnect-iOS/Presentation/CourseDiscovery/Views/VC/CourseDiscoveryVC.swift index fc2147d8..ee17732e 100644 --- a/Runnect-iOS/Runnect-iOS/Presentation/CourseDiscovery/Views/VC/CourseDiscoveryVC.swift +++ b/Runnect-iOS/Runnect-iOS/Presentation/CourseDiscovery/Views/VC/CourseDiscoveryVC.swift @@ -225,10 +225,10 @@ extension CourseDiscoveryVC: UICollectionViewDelegate, UICollectionViewDataSourc func scrollViewDidScroll(_ scrollView: UIScrollView) { let contentOffsetY = scrollView.contentOffset.y - let collectionViewHeight = scrollView.contentSize.height + let collectionViewHeight = mapCollectionView.contentSize.height let paginationY = collectionViewHeight * 0.2 - // 스크롤이 맨 아래에 도달하면 다음 페이지 데이터를 불러옵니다. + // 스크롤이 80% (0.2) 까지 도달하면 다음 페이지 데이터를 불러옵니다. if contentOffsetY >= collectionViewHeight - paginationY { if courseList.count < pageNo * 24 { // 페이지 끝에 도달하면 현재 페이지에 더 이상 데이터가 없음을 의미합니다. // 페이지네이션 중단 코드 From c671de9e54b8dc23d6c2db145c0e6437c9f1fb43 Mon Sep 17 00:00:00 2001 From: LeeMyeongJin Date: Mon, 25 Sep 2023 17:36:06 +0900 Subject: [PATCH 6/6] =?UTF-8?q?[Fix]=20#183=20-=20pagination=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 다른 View 를 다녀와도 초기화가 되지 않게 수정 했습니다. --- .../Views/VC/CourseDiscoveryVC.swift | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/Runnect-iOS/Runnect-iOS/Presentation/CourseDiscovery/Views/VC/CourseDiscoveryVC.swift b/Runnect-iOS/Runnect-iOS/Presentation/CourseDiscovery/Views/VC/CourseDiscoveryVC.swift index ee17732e..488528fe 100644 --- a/Runnect-iOS/Runnect-iOS/Presentation/CourseDiscovery/Views/VC/CourseDiscoveryVC.swift +++ b/Runnect-iOS/Runnect-iOS/Presentation/CourseDiscovery/Views/VC/CourseDiscoveryVC.swift @@ -21,9 +21,11 @@ final class CourseDiscoveryVC: UIViewController { private var courseList = [PublicCourse]() - // pagenation 을 위한 변수 입니다. + // pagination 에 꼭 필요한 위한 변수들 입니다. private var pageNo = 1 + private var isDataLoaded = false + // MARK: - UIComponents private lazy var naviBar = CustomNavigationBar(self, type: .title).setTitle("코스 발견") @@ -69,15 +71,8 @@ final class CourseDiscoveryVC: UIViewController { override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) - - // 데이터 초기화 - courseList.removeAll() - pageNo = 1 - - // 컬렉션 뷰를 리로드하여 초기화된 데이터를 화면에 표시 - mapCollectionView.reloadData() self.hideTabBar(wantsToHide: false) - self.getCourseData() + setDataLoadIfNeeded() } } @@ -107,6 +102,22 @@ extension CourseDiscoveryVC { self.searchButton.addTarget(self, action: #selector(pushToSearchVC), for: .touchUpInside) self.uploadButton.addTarget(self, action: #selector(pushToDiscoveryVC), for: .touchUpInside) } + + private func setDataLoadIfNeeded() { /// 데이터를 받고 다른 뷰를 갔다가 와도 데이터가 유지되게끔 하기 위한 함수 입니다. (한번만 호출되면 되는 함수!) + if !isDataLoaded { + // 앱이 실행 될때 처음에만 데이터 초기화 + courseList.removeAll() + pageNo = 1 + + // 컬렉션 뷰를 리로드하여 초기화된 데이터를 화면에 표시 + mapCollectionView.reloadData() + self.getCourseData() + + isDataLoaded = true // 데이터가 로드되었음을 표시 + } else { + return + } + } } // MARK: - @objc Function