diff --git a/Signal/src/ViewControllers/MediaGallery/MediaGalleryAccessoriesHelper.swift b/Signal/src/ViewControllers/MediaGallery/MediaGalleryAccessoriesHelper.swift index d1c92f7d02..96ea281b4f 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,37 @@ 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 { + viewController?.selectNone() + } else { + viewController?.selectAll() + } + 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 7db61abd22..e999934563 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 e58de2d8f8..27464f4743 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 abf3676de6..b8af4aa08b 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.")