From e5d2ce91374551aacc9f12f81271c9647c458abe Mon Sep 17 00:00:00 2001 From: "atsushi.yoshimoto" Date: Thu, 31 Mar 2022 11:30:01 +0900 Subject: [PATCH 1/2] Modified to select photos from earliest photographing date. --- .../AssetDateSectionHeaderView.swift | 11 +++++--- NohanaImagePicker/PickedAssetList.swift | 26 ++++++++++++------- 2 files changed, 24 insertions(+), 13 deletions(-) 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 + } } From 5fe3e904a9353e5ff38d5778908cb93c22477f78 Mon Sep 17 00:00:00 2001 From: "atsushi.yoshimoto" Date: Wed, 6 Apr 2022 14:59:39 +0900 Subject: [PATCH 2/2] Fixed some cells not reloading. --- .../AssetListSelectableDateSectionController.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NohanaImagePicker/AssetListSelectableDateSectionController.swift b/NohanaImagePicker/AssetListSelectableDateSectionController.swift index 269fd99..bb619ec 100644 --- a/NohanaImagePicker/AssetListSelectableDateSectionController.swift +++ b/NohanaImagePicker/AssetListSelectableDateSectionController.swift @@ -249,7 +249,7 @@ class AssetListSelectableDateSectionController: UICollectionViewController, UICo // MARK: - AssetDateSectionHeaderViewDelegate extension AssetListSelectableDateSectionController: AssetDateSectionHeaderViewDelegate { func didPushPickButton() { - collectionView.reloadItems(at: collectionView.indexPathsForVisibleItems) + collectionView.reloadData() updateDoneBarButtonColor() } }