Skip to content

Commit

Permalink
fix : PhotoPicker RIB 에 사진 선택 넘버링 내부로직 구현 #7
Browse files Browse the repository at this point in the history
- 선태된 순서를 기반으로 중간에 선택한 사진을 해제할 경우 나머지 사진들이 선택된 순서에 따라 넘버링을 동적으로 업데이트
  • Loading branch information
wongbingg committed Aug 8, 2023
1 parent 9c02b29 commit 8cbc854
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ protocol PhotoPickerPresentableListener: AnyObject {
func nextButtonTapped(with images: [PHAsset])
}

final class PhotoPickerViewController: UIViewController, PhotoPickerPresentable, PhotoPickerViewControllable {
final class PhotoPickerViewController: UIViewController,
PhotoPickerPresentable,
PhotoPickerViewControllable {

enum Metric {
enum NavigationBar {
Expand All @@ -31,7 +33,7 @@ final class PhotoPickerViewController: UIViewController, PhotoPickerPresentable,

weak var listener: PhotoPickerPresentableListener?
private var disposeBag: DisposeBag = .init()
private var selectedItems: [PHAsset] = []
private var selectedIndexPaths: [IndexPath] = []

private let navigationBar: UIView = {
let view = UIView()
Expand Down Expand Up @@ -74,6 +76,7 @@ final class PhotoPickerViewController: UIViewController, PhotoPickerPresentable,
return collectionView
}()

// MARK: - Initializers
init() {
super.init(nibName: nil, bundle: nil)
modalPresentationStyle = .fullScreen
Expand All @@ -83,6 +86,7 @@ final class PhotoPickerViewController: UIViewController, PhotoPickerPresentable,
fatalError("init(coder:) has not been implemented")
}

// MARK: - View LifeCycles
override func viewDidLoad() {
super.viewDidLoad()
setupInitialView()
Expand Down Expand Up @@ -120,49 +124,81 @@ final class PhotoPickerViewController: UIViewController, PhotoPickerPresentable,

nextButton.rx.tap
.bind { [weak self] in
self?.listener?.nextButtonTapped(with: self?.selectedItems ?? [])
guard let self = self else { return }
self.listener?.nextButtonTapped(with: self.getSelectedPhotoes())
}
.disposed(by: disposeBag)
}

private func bind() {
bindCollectionViewCell()
bindItemSelected()
}

private func bindCollectionViewCell() {
Observable.of(Array(0...(photoCount - 1)))
.bind(to: photoCollectionView.rx.items(
cellIdentifier: PhotoCell.defaultReuseIdentifier,
cellType: PhotoCell.self)
) { index, item, cell in

guard let allPhotos = allPhotos else {
Console.error("allPhotos 값이 존재하지 않습니다.")
Console.error("\(#function) allPhotos 값이 존재하지 않습니다.")
return
}
cell.setImage(with: allPhotos.object(at: item))
}
.disposed(by: disposeBag)

}

private func bindItemSelected() {
photoCollectionView
.rx
.itemSelected
.subscribe { [weak self] indexPath in
guard let self = self else { return }
guard let self = self,
let indexPath = indexPath.element else { return }

guard let allPhotos = allPhotos else {
Console.error("allPhotos 값이 존재하지 않습니다.")
return
}
let selectedPhoto = allPhotos.object(at: indexPath.element?.row ?? 0)
if self.selectedItems.contains(selectedPhoto) {
self.selectedItems.remove(at: self.selectedItems.firstIndex(of: selectedPhoto) ?? .zero)
if self.selectedIndexPaths.contains(indexPath) {
self.selectedIndexPaths.remove(
at: self.selectedIndexPaths.firstIndex(of: indexPath) ?? .zero
)
guard let cell = self.getCell(at: indexPath) else { return }
cell.uncheck()
} else {
self.selectedItems.append(selectedPhoto)
self.selectedIndexPaths.append(indexPath)
}
guard let cell = self.photoCollectionView.cellForItem(at: indexPath.element ?? .init(item: 0, section: 0)) as? PhotoCell else {
return

for (order, indexPath) in self.selectedIndexPaths.enumerated() {
guard let cell = self.getCell(at: indexPath) else { return }
cell.check(number: order + 1)
}
cell.check(number: self.selectedItems.count)
}
.disposed(by: disposeBag)
}

private func getSelectedPhotoes() -> [PHAsset] {
guard let allPhotos = allPhotos else {
Console.error("\(#function) allPhotos 값이 존재하지 않습니다.")
return []
}
let selectedPhotoes = selectedIndexPaths.map {
allPhotos.object(at: $0.row)
}

return selectedPhotoes
}

private func getCell(at indexPath: IndexPath?) -> PhotoCell? {
guard let indexPath = indexPath else {
Console.error("\(#function) indexPath가 존재하지 않습니다.")
return nil
}
guard let cell = self.photoCollectionView.cellForItem(at: indexPath) as? PhotoCell else {
return nil
}
return cell
}
}

// MARK: - Layout Settting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@ final class PhotoCell: UICollectionViewCell {
}

func check(number: Int) {
if checkBox_checked.alpha == 0 {
dimmingView.alpha = 0.3
checkBox_checked.alpha = 1
numberLabel.alpha = 1
numberLabel.text = "\(number)"
} else {
dimmingView.alpha = 0
checkBox_checked.alpha = 0
numberLabel.alpha = 0
}
dimmingView.alpha = 0.3
checkBox_checked.alpha = 1
numberLabel.alpha = 1
numberLabel.text = "\(number)"
}

func uncheck() {
dimmingView.alpha = 0
checkBox_checked.alpha = 0
numberLabel.alpha = 0
}
}

Expand Down

0 comments on commit 8cbc854

Please sign in to comment.