Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion Signal/src/ViewControllers/Photos/ImagePickerController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import Photos
import SignalServiceKit
import UIKit
import SignalUI

protocol ImagePickerGridControllerDelegate: AnyObject {
Expand Down Expand Up @@ -90,6 +91,11 @@ class ImagePickerGridController: UICollectionViewController, PhotoLibraryDelegat
}
cancelButton.tintColor = Theme.darkThemePrimaryColor
navigationItem.leftBarButtonItem = cancelButton

let privacyButton: UIBarButtonItem = .button(icon: .settingsPrivacy, children: [createSelectMoreActionForPrivacyButton(), createSettingsActionForPrivacyButton()])
privacyButton.tintColor = Theme.darkThemePrimaryColor
navigationItem.rightBarButtonItem = privacyButton
updatePrivacyButtonVisibility()

view.addSubview(doneButton)
doneButton.autoPinBottomToSuperviewMargin(withInset: UIDevice.current.hasIPhoneXNotch ? 8 : 16)
Expand All @@ -100,7 +106,48 @@ class ImagePickerGridController: UICollectionViewController, PhotoLibraryDelegat
self.selectionPanGesture = selectionPanGesture
collectionView.addGestureRecognizer(selectionPanGesture)
}




private func createSelectMoreActionForPrivacyButton() -> UIAction {
let selectMoreAction = UIAction(
title: OWSLocalizedString(
"ATTACHMENT_KEYBOARD_CONTEXT_MENU_BUTTON_SELECT_MORE",
comment: "Button in a context menu from the 'privacy' button in attachment panel that allows to select more photos/videos to give Signal access to"),
image: UIImage(named: "album-tilt-light")
) { _ in
guard let frontmostVC = CurrentAppContext().frontmostViewController() else { return }
PHPhotoLibrary.shared().presentLimitedLibraryPicker(from: frontmostVC)
}
return selectMoreAction
}

private func createSettingsActionForPrivacyButton() -> UIAction {
let settingsAction = UIAction(
title: OWSLocalizedString(
"ATTACHMENT_KEYBOARD_CONTEXT_MENU_BUTTON_SYSTEM_SETTINGS",
comment: "Button in a context menu from the 'privacy' button in attachment panel that opens the iOS system settings for Signal to update access permissions"),
image: UIImage(named: "settings-light")
) { _ in
if let openSettingsURL = URL(string: UIApplication.openSettingsURLString) {
UIApplication.shared.open(openSettingsURL)
}
}
return settingsAction
}

private func updatePrivacyButtonVisibility() {
if isPhotoPermissionLimited {
navigationItem.rightBarButtonItem?.isEnabled = true
} else {
navigationItem.rightBarButtonItem?.isEnabled = false
}
}

private var isPhotoPermissionLimited: Bool {
return PHPhotoLibrary.authorizationStatus(for: .readWrite) == .limited
}

private var selectionPanGesture: UIPanGestureRecognizer?
private enum BatchSelectionGestureMode {
case select, deselect
Expand Down Expand Up @@ -315,6 +362,8 @@ class ImagePickerGridController: UICollectionViewController, PhotoLibraryDelegat
collectionView.reloadData()
collectionView.layoutIfNeeded()
}



// MARK: - Actions

Expand Down Expand Up @@ -381,6 +430,7 @@ class ImagePickerGridController: UICollectionViewController, PhotoLibraryDelegat
func photoLibraryDidChange(_ photoLibrary: PhotoLibrary) {
photoAlbumContents = photoAlbum.contents()
reloadData()
updatePrivacyButtonVisibility()
}

// MARK: - PhotoCollectionPicker Presentation
Expand Down
14 changes: 13 additions & 1 deletion SignalUI/UIKitExtensions/UIButton+SignalUI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,11 @@ public extension UIBarButtonItem {
self.handler = handler
}
}

/*
Instead of having all of these different kinds of methods for the different kinds of combinatins of buttons, invest in turning
all of this into either a builder or a fluent builder. https://www.geeksforgeeks.org/builder-fluent-builder-and-faceted-builder-method-design-pattern-in-java/
*/

/// Creates a bar button with the given title that performs the action in the provided closure.
static func button(
title: String,
Expand All @@ -223,6 +227,14 @@ public extension UIBarButtonItem {
) -> UIBarButtonItem {
ClosureBarButtonItem(image: Theme.iconImage(icon), style: style, action: action)
}

/// Creates a bar button with the given icon that opens the provided context menu children
static func button(
icon: ThemeIcon,
children: [UIMenuElement]
) -> UIBarButtonItem {
return UIBarButtonItem(image: Theme.iconImage(icon), menu: UIMenu(children: children))
}

// Keep this static function public instead of exposing ClosureBarButtonItem
// because ClosureBarButtonItem will only function properly if using its
Expand Down