From dc1f55e1737bae126063a751c73a2091e4793d55 Mon Sep 17 00:00:00 2001 From: James Swift Date: Mon, 16 Oct 2023 22:56:56 +0200 Subject: [PATCH 1/2] Ability to select all media in selection mode --- .../MediaGalleryAccessoriesHelper.swift | 33 ++++++++++++++++++- .../MediaTileViewController.swift | 17 ++++++++++ .../translations/en.lproj/Localizable.strings | 6 ++++ SignalMessaging/utils/CommonStrings.swift | 8 +++++ 4 files changed, 63 insertions(+), 1 deletion(-) diff --git a/Signal/src/ViewControllers/MediaGallery/MediaGalleryAccessoriesHelper.swift b/Signal/src/ViewControllers/MediaGallery/MediaGalleryAccessoriesHelper.swift index d1c92f7d026..c8975d1c1c6 100644 --- a/Signal/src/ViewControllers/MediaGallery/MediaGalleryAccessoriesHelper.swift +++ b/Signal/src/ViewControllers/MediaGallery/MediaGalleryAccessoriesHelper.swift @@ -27,6 +27,8 @@ protocol MediaGalleryPrimaryViewController: UIViewController { var isEmpty: Bool { get } var hasSelection: Bool { get } func selectionInfo() -> (count: Int, totalSize: Int64)? + func selectAll() + func selectNone() func disableFiltering() func batchSelectionModeDidChange(isInBatchSelectMode: Bool) func didEndSelectMode() @@ -172,6 +174,7 @@ public class MediaGalleryAccessoriesHelper { updateFooterBarState() updateSelectionInfoLabel() updateSelectionModeControls() + updateSelectAllButton() updateDeleteButton() updateShareButton() } @@ -183,6 +186,7 @@ public class MediaGalleryAccessoriesHelper { return } updateSelectionInfoLabel() + updateSelectAllButton() updateDeleteButton() updateShareButton() } @@ -438,7 +442,7 @@ public class MediaGalleryAccessoriesHelper { case .hidden: return nil case .selection: - return [ shareButton, flexibleSpace(), selectionInfoButton, flexibleSpace(), deleteButton ] + return [ shareButton, selectAllButton, flexibleSpace(), selectionInfoButton, flexibleSpace(), deleteButton ] case .regular: let firstItem: UIBarButtonItem if isGridViewAllowed { @@ -569,6 +573,33 @@ public class MediaGalleryAccessoriesHelper { Logger.debug("") viewController?.shareSelectedItems(sender) } + + // MARK: - Select All/None + + private lazy var selectAllButton = UIBarButtonItem( + title: CommonStrings.selectAllButton, + style: .plain, + target: self, + action: #selector(didPressSelectAll) + ) + + @objc + private func didPressSelectAll(_ sender: Any) { + Logger.debug("") + if viewController?.hasSelection == false { + viewController?.selectAll() + } else { + viewController?.selectNone() + } + + didModifySelection() + } + + private func updateSelectAllButton() { + guard let viewController else { return } + selectAllButton.title = + viewController.hasSelection ? CommonStrings.selectNoneButton : CommonStrings.selectAllButton + } // MARK: - Selection Info diff --git a/Signal/src/ViewControllers/MediaGallery/MediaTileViewController.swift b/Signal/src/ViewControllers/MediaGallery/MediaTileViewController.swift index 7db61abd22e..e9999345636 100644 --- a/Signal/src/ViewControllers/MediaGallery/MediaTileViewController.swift +++ b/Signal/src/ViewControllers/MediaGallery/MediaTileViewController.swift @@ -1490,6 +1490,23 @@ extension MediaTileViewController: MediaGalleryPrimaryViewController { } return false } + + func selectAll() { + (0.. [IndexPath]? in + return (0.. IndexPath? in + return IndexPath(item: item, section: section) + }) + }.flatMap { $0 }.forEach { (indexPath) in + collectionView.selectItem(at: indexPath, animated: true, scrollPosition: []) + } + } + + func selectNone() { + collectionView.indexPathsForSelectedItems?.forEach({ (indexPath) in + collectionView.deselectItem(at: indexPath, animated: true) + }) + } func selectionInfo() -> (count: Int, totalSize: Int64)? { guard diff --git a/Signal/translations/en.lproj/Localizable.strings b/Signal/translations/en.lproj/Localizable.strings index e58de2d8f87..27464f4743b 100644 --- a/Signal/translations/en.lproj/Localizable.strings +++ b/Signal/translations/en.lproj/Localizable.strings @@ -646,6 +646,12 @@ /* Button text to enable batch selection mode */ "BUTTON_SELECT" = "Select"; +/* Button text to select all in batch selection mode */ +"BUTTON_SELECT_ALL" = "Select All"; + +/* Button text to select none in batch selection mode */ +"BUTTON_SELECT_NONE" = "Select None"; + /* Label for the 'set' button. */ "BUTTON_SET" = "Set"; diff --git a/SignalMessaging/utils/CommonStrings.swift b/SignalMessaging/utils/CommonStrings.swift index abf3676de60..b8af4aa08bf 100644 --- a/SignalMessaging/utils/CommonStrings.swift +++ b/SignalMessaging/utils/CommonStrings.swift @@ -37,6 +37,14 @@ public class CommonStrings: NSObject { static public var selectButton: String { OWSLocalizedString("BUTTON_SELECT", comment: "Button text to enable batch selection mode") } + + static public var selectAllButton: String { + OWSLocalizedString("BUTTON_SELECT_ALL", comment: "Button text to select all in batch selection mode") + } + + static public var selectNoneButton: String { + OWSLocalizedString("BUTTON_SELECT_NONE", comment: "Button text to select none in batch selection mode") + } static public var doneButton: String { OWSLocalizedString("BUTTON_DONE", comment: "Label for generic done button.") From 8480dcc17656dd3c6f39211dd6b95ed24e3be3de Mon Sep 17 00:00:00 2001 From: James Swift Date: Sat, 18 Nov 2023 18:02:36 +0100 Subject: [PATCH 2/2] More readable condition Co-authored-by: pawisoon --- .../MediaGallery/MediaGalleryAccessoriesHelper.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Signal/src/ViewControllers/MediaGallery/MediaGalleryAccessoriesHelper.swift b/Signal/src/ViewControllers/MediaGallery/MediaGalleryAccessoriesHelper.swift index c8975d1c1c6..96ea281b4f5 100644 --- a/Signal/src/ViewControllers/MediaGallery/MediaGalleryAccessoriesHelper.swift +++ b/Signal/src/ViewControllers/MediaGallery/MediaGalleryAccessoriesHelper.swift @@ -586,7 +586,11 @@ public class MediaGalleryAccessoriesHelper { @objc private func didPressSelectAll(_ sender: Any) { Logger.debug("") - if viewController?.hasSelection == false { + if viewController?.hasSelection { + viewController?.selectNone() + } else { + viewController?.selectAll() + } viewController?.selectAll() } else { viewController?.selectNone()