-
Notifications
You must be signed in to change notification settings - Fork 1
[홈] 무한 캐러셀 구현 #123
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
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) | ||
|
||
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 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.