-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[홈] 무한 캐러셀 구현 #123
base: develop
Are you sure you want to change the base?
[홈] 무한 캐러셀 구현 #123
Changes from 5 commits
03f94e0
9607f60
74e36af
c1dabb6
7322925
6bb2b4b
d4ef142
624491e
75905eb
c19e450
5adf45c
8302f47
cafef0e
3681bd5
139c0af
213b460
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,9 @@ final class HomeViewController: UIViewController, BaseViewController { | |
|
||
var viewModel: HomeViewModel? | ||
var disposeBag = DisposeBag() | ||
|
||
|
||
var datasource: [HomeCapsuleCellItem] = [] | ||
|
||
var centerIndex: CGFloat { | ||
return homeView.capsuleCollectionView.contentOffset.x / (FrameResource.homeCapsuleCellWidth * 0.75 + 10) | ||
} | ||
|
@@ -30,13 +32,16 @@ final class HomeViewController: UIViewController, BaseViewController { | |
title = "홈" | ||
configureView() | ||
bind() | ||
homeView.capsuleCollectionView.dataSource = self | ||
} | ||
|
||
override func viewWillAppear(_ animated: Bool) { | ||
super.viewWillAppear(animated) | ||
viewModel?.input.viewWillAppear.onNext(()) | ||
} | ||
|
||
// MARK: - Functions | ||
|
||
private func configureView() { | ||
view.backgroundColor = .themeBackground | ||
|
||
|
@@ -49,6 +54,12 @@ final class HomeViewController: UIViewController, BaseViewController { | |
} else { | ||
owner.emptyView = nil | ||
owner.showHomeView() | ||
owner.datasource = capsuleCellItems | ||
owner.homeView.capsuleCollectionView.reloadData() | ||
DispatchQueue.main.async { | ||
Thread.sleep(forTimeInterval: 0.01) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기서 메인스레드를 재우는 이유가 있나요?! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 앱을 처음 실행 시 간혹 캡슐이 짤려서 두줄로 나오는 버그가 발생하였습니다. |
||
owner.homeView.capsuleCollectionView.scrollToItem(at: IndexPath(item: 10000000, section: 0), at: .centeredHorizontally, animated: false) | ||
} | ||
} | ||
} | ||
.disposed(by: disposeBag) | ||
|
@@ -88,18 +99,6 @@ final class HomeViewController: UIViewController, BaseViewController { | |
return | ||
} | ||
|
||
viewModel.output.featuredCapsuleCellItems | ||
.bind(to: homeView.capsuleCollectionView.rx.items) { collectionView, index, element in | ||
guard let cell = collectionView.dequeueReusableCell( | ||
withReuseIdentifier: HomeCapsuleCell.identifier, | ||
for: IndexPath(item: index, section: 0) | ||
) as? HomeCapsuleCell else { | ||
return UICollectionViewCell() | ||
} | ||
cell.configure(capsuleCellModel: element) | ||
return cell | ||
}.disposed(by: disposeBag) | ||
|
||
viewModel.output.userCapsuleStatus | ||
.subscribe(onNext: { [weak self] status in | ||
self?.homeView.mainStatusLabel.updateUserCapsuleStatus( | ||
|
@@ -151,6 +150,7 @@ final class HomeViewController: UIViewController, BaseViewController { | |
.withUnretained(self) | ||
.subscribe( | ||
onNext: { owner, indexPath in | ||
print(owner.centerIndex) | ||
if owner.getIndexRange(index: indexPath.item) ~= owner.centerIndex { | ||
if let cell = owner.homeView.capsuleCollectionView.cellForItem(at: indexPath) as? HomeCapsuleCell { | ||
guard let uuid = cell.uuid else { | ||
|
@@ -164,3 +164,20 @@ final class HomeViewController: UIViewController, BaseViewController { | |
}).disposed(by: disposeBag) | ||
} | ||
} | ||
|
||
extension HomeViewController: UICollectionViewDataSource { | ||
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | ||
return datasource.isEmpty ? 0 : Int.max | ||
} | ||
|
||
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { | ||
guard let cell = collectionView.dequeueReusableCell( | ||
withReuseIdentifier: HomeCapsuleCell.identifier, | ||
for: indexPath | ||
) as? HomeCapsuleCell else { | ||
return UICollectionViewCell() | ||
} | ||
cell.configure(capsuleCellModel: datasource[indexPath.item % datasource.count]) | ||
return cell | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,7 +56,7 @@ final class HomeViewModel: BaseViewModel, CapsuleCellNeedable { | |
owner.output.featuredCapsuleCellItems.accept( | ||
CapsuleType.allCases | ||
.map { owner.getHomeCapsuleCellItem(capsules: capsuleList, type: $0) } | ||
.compactMap({ $0 }) | ||
.compactMap({ $0 }) // + Array(repeating: nil, count: Int.max) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 큰 상관은 없지만 소괄호를 제외해도 될것 같아요 그리고 59번째 라인을 없애고 58번째 라인의 map 메소드를 compactMap 으로 바꿔도 같은 기능을 하지 않을까 싶네요~ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. compactMap으로 변경하였습니다! |
||
) | ||
|
||
}) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.