Skip to content

Commit

Permalink
fix bug for choose max photo number
Browse files Browse the repository at this point in the history
  • Loading branch information
gaoyuexit committed Sep 14, 2017
1 parent ee20320 commit d0f9c02
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
26 changes: 17 additions & 9 deletions LPAlbum/Controllers/LPAlbum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ extension LPAlbum: UICollectionViewDelegate, UICollectionViewDataSource {
cell.set(model)
cell.iconClickAction = {[weak self] in
guard let `self` = self else { return }
guard self.checkoutMaxCount(willselect: !$0) else { return }
guard self.checkoutMaxCount(willselect: !$0, show: self) else { return }
var newModel = model
newModel.isSelect = !$0
self.albumModels = self.albumModels.change(assetModel: newModel)
Expand All @@ -203,19 +203,27 @@ extension LPAlbum: UICollectionViewDelegate, UICollectionViewDataSource {

public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if config.hasCamera && indexPath.row == 0 {
guard checkoutMaxCount(willselect: true) else { return }
guard checkoutMaxCount(willselect: true, show: self) else { return }
AuthorizationTool.cameraRequestAuthorization{
$0 == .authorized ? self.takePhoto() : self.errorBlock?(self, AlbumError.noCameraPermission)
}
}else{
let previewVc = PhotosPreviewController()
previewVc.assetModels = albumModels[currentAlbumIndex].assetModels
let assetModels = albumModels[currentAlbumIndex].assetModels
previewVc.assetModels = assetModels
previewVc.currentIndex = config.hasCamera ? indexPath.row - 1 : indexPath.row
previewVc.chooseAction = {[weak self] in
previewVc.chooseAction = {[weak self] (index, button, vc) in
guard let `self` = self else { return }
self.albumModels = self.albumModels.change(assetModel: $0)
let currentIndex = self.config.hasCamera ? $1 + 1 : $1
self.collectionView.reloadItems(at: [IndexPath(row: currentIndex, section: 0)])

let willselect = !button.isSelected
guard self.checkoutMaxCount(willselect: willselect, show: vc) else { return }

button.isSelected = willselect
vc.assetModels[index].isSelect = willselect
self.albumModels = self.albumModels.change(assetModel: vc.assetModels[index])

let cellIndex = self.config.hasCamera ? index + 1 : index
self.collectionView.reloadItems(at: [IndexPath(row: cellIndex, section: 0)])
}
navigationController?.pushViewController(previewVc, animated: true)
}
Expand All @@ -231,9 +239,9 @@ extension LPAlbum: UICollectionViewDelegate, UICollectionViewDataSource {
present(picker, animated: true, completion: nil)
}

func checkoutMaxCount(willselect: Bool) -> Bool {
func checkoutMaxCount(willselect: Bool, show vc: UIViewController) -> Bool {
if self.config.maxSelectCount == self.albumModels[0].selectCount && willselect {
self.errorBlock?(self,AlbumError.moreThanLargestChoiceCount)
self.errorBlock?(vc,AlbumError.moreThanLargestChoiceCount)
return false
}
return true
Expand Down
12 changes: 5 additions & 7 deletions LPAlbum/Controllers/PhotosPreviewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class PhotosPreviewController: UIViewController {
var assetModels: [AssetModel]!
var currentIndex: Int!

var chooseAction: ((AssetModel, Int) -> Void)?
var chooseAction: ((Int, UIButton, PhotosPreviewController) -> Void)?

fileprivate var collectionView: UICollectionView!
fileprivate let itemPadding: CGFloat = 20.0
Expand All @@ -23,6 +23,9 @@ class PhotosPreviewController: UIViewController {
super.viewDidLoad()
setupUI()
}
deinit {
print("\(self) deinit")
}
}

extension PhotosPreviewController {
Expand Down Expand Up @@ -57,12 +60,7 @@ extension PhotosPreviewController {
}
}
func chooseClick() {
chooseButton.isSelected = !chooseButton.isSelected
assetModels[currentIndex].isSelect = chooseButton.isSelected

var model = assetModels[currentIndex]
model.isSelect = chooseButton.isSelected
chooseAction?(model, currentIndex)
chooseAction?(currentIndex, chooseButton, self)
}
}

Expand Down
Binary file modified LPAlbum/Others/LPAlbum.bundle/en.lproj/Localizable.strings
Binary file not shown.
Binary file modified LPAlbum/Others/LPAlbum.bundle/zh.lproj/Localizable.strings
Binary file not shown.

0 comments on commit d0f9c02

Please sign in to comment.