diff --git a/NohanaImagePicker/AssetDateSectionHeaderView.swift b/NohanaImagePicker/AssetDateSectionHeaderView.swift index d6d2c08..bbbc44c 100644 --- a/NohanaImagePicker/AssetDateSectionHeaderView.swift +++ b/NohanaImagePicker/AssetDateSectionHeaderView.swift @@ -42,6 +42,7 @@ class AssetDateSectionHeaderView: UICollectionReusableView { @IBAction func didPushPickButton(_ sender: UIButton) { guard let nohanaImagePickerController = nohanaImagePickerController else { return } let firstButtonState = sender.isSelected + var addAssets = [Asset]() for asset in assets { guard nohanaImagePickerController.canPickAsset(asset) else { continue } if firstButtonState { @@ -50,14 +51,18 @@ class AssetDateSectionHeaderView: UICollectionReusableView { } else { if nohanaImagePickerController.pickedAssetList.isPicked(asset) { continue - } else if nohanaImagePickerController.pickedAssetList.pick(asset: asset) { - sender.isSelected = true - } else if nohanaImagePickerController.pickedAssetList.count == nohanaImagePickerController.maximumNumberOfSelection { + } else if nohanaImagePickerController.pickedAssetList.count + addAssets.count == nohanaImagePickerController.maximumNumberOfSelection { sender.isSelected = false break + } else if nohanaImagePickerController.pickedAssetList.canPick(asset: asset) { + addAssets.append(asset) + sender.isSelected = true } } } + addAssets.reversed().forEach { + _ = nohanaImagePickerController.pickedAssetList.pick(asset: $0) + } delegate?.didPushPickButton() nohanaImagePickerController.delegate?.nohanaImagePicker?(nohanaImagePickerController, didSelectAssetDateSectionAssets: assets.compactMap { ($0 as? PhotoKitAsset)?.originalAsset }, date: date) } diff --git a/NohanaImagePicker/PickedAssetList.swift b/NohanaImagePicker/PickedAssetList.swift index 0cdf7ad..b7f0516 100644 --- a/NohanaImagePicker/PickedAssetList.swift +++ b/NohanaImagePicker/PickedAssetList.swift @@ -50,16 +50,7 @@ class PickedAssetList: ItemList { // MARK: - Manage assetlist func pick(asset: Asset) -> Bool { - guard !isPicked(asset) else { - return false - } - let assetsCountBeforePicking = self.count - if asset is PhotoKitAsset { - if let canPick = nohanaImagePickerController!.delegate?.nohanaImagePicker?(nohanaImagePickerController!, willPickPhotoKitAsset: (asset as! PhotoKitAsset).originalAsset, pickedAssetsCount: assetsCountBeforePicking), !canPick { - return false - } - } - guard nohanaImagePickerController!.maximumNumberOfSelection == 0 || assetsCountBeforePicking < nohanaImagePickerController!.maximumNumberOfSelection else { + guard canPick(asset: asset) else { return false } assetlist.append(asset) @@ -112,4 +103,19 @@ class PickedAssetList: ItemList { return assetlist.contains { $0.identifier == asset.identifier } } + func canPick(asset: Asset) -> Bool { + guard !isPicked(asset) else { + return false + } + let assetsCountBeforePicking = self.count + if asset is PhotoKitAsset { + if let canPick = nohanaImagePickerController!.delegate?.nohanaImagePicker?(nohanaImagePickerController!, willPickPhotoKitAsset: (asset as! PhotoKitAsset).originalAsset, pickedAssetsCount: assetsCountBeforePicking), !canPick { + return false + } + } + guard nohanaImagePickerController!.maximumNumberOfSelection == 0 || assetsCountBeforePicking < nohanaImagePickerController!.maximumNumberOfSelection else { + return false + } + return true + } }