Skip to content

Commit 7021df8

Browse files
committed
Multiple selection gesture YAY!
1 parent a4a86a4 commit 7021df8

File tree

3 files changed

+50
-11
lines changed

3 files changed

+50
-11
lines changed

Sources/ComposedMediaUI/MediaAssetCell.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,25 @@ final class MediaAssetCell: UICollectionViewCell {
1212
return view
1313
}()
1414

15+
private(set) lazy var selectionImageView: UIImageView = {
16+
let view = UIImageView()
17+
view.backgroundColor = .clear
18+
view.translatesAutoresizingMaskIntoConstraints = false
19+
view.clipsToBounds = true
20+
view.contentMode = .scaleAspectFit
21+
view.adjustsImageSizeForAccessibilityContentSizeCategory = true
22+
23+
if #available(iOS 13.0, *) {
24+
let config = UIImage.SymbolConfiguration(pointSize: 20, weight: .medium, scale: .medium)
25+
view.preferredSymbolConfiguration = config
26+
view.tintColor = .label
27+
} else {
28+
view.tintColor = .white
29+
}
30+
31+
return view
32+
}()
33+
1534
private(set) weak var asset: PHAsset?
1635
private(set) var onReuse: (() -> Void)?
1736

@@ -37,11 +56,26 @@ final class MediaAssetCell: UICollectionViewCell {
3756
t.duration = 0.1
3857
imageView.layer.add(t, forKey: nil)
3958
imageView.alpha = isEditing && selected ? 0.5 : 1
59+
60+
guard isEditing else { return }
61+
62+
if #available(iOS 13.0, *) {
63+
selectionImageView.image = selected
64+
? UIImage(systemName: "checkmark.circle.fill")
65+
: nil
66+
} else {
67+
// ✓
68+
// selectionImageView.image = selected
69+
// ? UIImage(systemName: "circle.fill")
70+
// : nil
71+
}
4072
}
4173

4274
private func prepare() {
4375
contentView.layoutMargins = .zero
76+
4477
contentView.addSubview(imageView)
78+
contentView.addSubview(selectionImageView)
4579

4680
NSLayoutConstraint.activate([
4781
imageView.topAnchor.constraint(lessThanOrEqualTo: contentView.layoutMarginsGuide.topAnchor),
@@ -50,6 +84,9 @@ final class MediaAssetCell: UICollectionViewCell {
5084
imageView.bottomAnchor.constraint(lessThanOrEqualTo: contentView.layoutMarginsGuide.bottomAnchor),
5185
imageView.centerXAnchor.constraint(equalTo: contentView.layoutMarginsGuide.centerXAnchor),
5286
imageView.centerYAnchor.constraint(equalTo: contentView.layoutMarginsGuide.centerYAnchor),
87+
88+
selectionImageView.trailingAnchor.constraint(equalTo: imageView.trailingAnchor, constant: -10),
89+
selectionImageView.bottomAnchor.constraint(equalTo: imageView.bottomAnchor, constant: -10)
5390
])
5491
}
5592

Sources/ComposedMediaUI/MediaAssetSection.swift

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ extension MediaAssetSection {
5151
extension MediaAssetSection: CollectionSectionProvider {
5252

5353
public func section(with traitCollection: UITraitCollection) -> CollectionSection {
54-
let cell = CollectionCellElement(section: self, dequeueMethod: .fromClass(MediaAssetCell.self)) { cell, index, section in
54+
let cell = CollectionCellElement(section: self, dequeueMethod: .fromClass(MediaAssetCell.self), configure: { cell, index, section in
5555
let config = section.configuration
5656
let asset = section.element(at: index)
5757

@@ -63,10 +63,10 @@ extension MediaAssetSection: CollectionSectionProvider {
6363
}
6464
}
6565

66-
cell.prepareAsset(asset) {
66+
cell.prepareAsset(asset, onReuse: {
6767
section.imageManager.cancelImageRequest(imageRequestId)
68-
}
69-
}
68+
})
69+
})
7070

7171
return CollectionSection(section: self, cell: cell)
7272
}
@@ -153,10 +153,6 @@ extension MediaAssetSection: CollectionSelectionHandler {
153153

154154
extension MediaAssetSection: CollectionEditingHandler {
155155

156-
public func allowsEditing(at index: Int) -> Bool {
157-
return index != 0
158-
}
159-
160156
public func setEditing(_ editing: Bool) {
161157
allowsMultipleSelection = editing
162158
}

Sources/ComposedMediaUI/MediaViewController.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ open class MediaViewController: UIViewController {
9393

9494
collectionCoordinator?.setEditing(editing, animated: animated)
9595
updateNavigationItems(editing: editing, animated: animated)
96-
navigationController?.setToolbarHidden(!editing, animated: animated)
9796
}
9897

9998
@objc private func open(_ sender: Any?) {
@@ -131,7 +130,11 @@ open class MediaViewController: UIViewController {
131130
navigationItem.rightBarButtonItems?.last?.isEnabled = collectionView.indexPathsForSelectedItems?.isEmpty == false
132131
}
133132

134-
navigationItem.title = editing ? "Select Items" : title
133+
navigationItem.title = editing
134+
? collectionView.indexPathsForSelectedItems?.isEmpty == true
135+
? NSLocalizedString("Select Items", comment: "")
136+
: "\(collectionView.indexPathsForSelectedItems?.count ?? 0) \(NSLocalizedString("Selected", comment: ""))"
137+
: title
135138
}
136139

137140
@objc private func performSelectAll(_ sender: Any?) {
@@ -152,7 +155,6 @@ open class MediaViewController: UIViewController {
152155

153156
open override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
154157
super.viewWillTransition(to: size, with: coordinator)
155-
imageManager.stopCachingImagesForAllAssets()
156158

157159
if layout is UICollectionViewFlowLayout {
158160
collectionCoordinator?.invalidateLayout()
@@ -168,6 +170,10 @@ open class MediaViewController: UIViewController {
168170

169171
extension MediaViewController: UICollectionViewDelegate {
170172

173+
public func collectionView(_ collectionView: UICollectionView, shouldBeginMultipleSelectionInteractionAt indexPath: IndexPath) -> Bool {
174+
return false
175+
}
176+
171177
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
172178
updateNavigationItems(editing: isEditing, animated: false)
173179
}

0 commit comments

Comments
 (0)