Skip to content

Commit

Permalink
Merge pull request #142 from nohana/feature/select_photos_from_earlie…
Browse files Browse the repository at this point in the history
…st_photographing_date

Modified to select photos from earliest photographing date.
  • Loading branch information
a2444mo10 authored Apr 8, 2022
2 parents 6b2bfcb + 3eccc4c commit 801aa0d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
11 changes: 8 additions & 3 deletions NohanaImagePicker/AssetDateSectionHeaderView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ class AssetListSelectableDateSectionController: UICollectionViewController, UICo
// MARK: - AssetDateSectionHeaderViewDelegate
extension AssetListSelectableDateSectionController: AssetDateSectionHeaderViewDelegate {
func didPushPickButton() {
collectionView.reloadItems(at: collectionView.indexPathsForVisibleItems)
collectionView.reloadData()
updateDoneBarButtonColor()
}
}
Expand Down
26 changes: 16 additions & 10 deletions NohanaImagePicker/PickedAssetList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
}

0 comments on commit 801aa0d

Please sign in to comment.