Pan horizontal to bulk select images

This commit is contained in:
Michael Kirk 2019-01-18 18:01:07 -07:00
parent 3ac9732b65
commit 599a57e3a4
4 changed files with 174 additions and 71 deletions

View file

@ -336,7 +336,6 @@
452037D11EE84975004E4CDF /* DebugUISessionState.m in Sources */ = {isa = PBXBuildFile; fileRef = 452037D01EE84975004E4CDF /* DebugUISessionState.m */; };
4520D8D51D417D8E00123472 /* Photos.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4520D8D41D417D8E00123472 /* Photos.framework */; };
4521C3C01F59F3BA00B4C582 /* TextFieldHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4521C3BF1F59F3BA00B4C582 /* TextFieldHelper.swift */; };
452314A01F7E9E18003A428C /* DirectionalPanGestureRecognizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4523149F1F7E9E18003A428C /* DirectionalPanGestureRecognizer.swift */; };
4523D016206EDC2B00A2AB51 /* LRUCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4523D015206EDC2B00A2AB51 /* LRUCache.swift */; };
452B999020A34B6B006F2F9E /* AddContactShareToExistingContactViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 452B998F20A34B6B006F2F9E /* AddContactShareToExistingContactViewController.swift */; };
452C468F1E427E200087B011 /* OutboundCallInitiator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 452C468E1E427E200087B011 /* OutboundCallInitiator.swift */; };
@ -450,6 +449,7 @@
4C23A5F2215C4ADE00534937 /* SheetViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C23A5F1215C4ADE00534937 /* SheetViewController.swift */; };
4C2F454F214C00E1004871FF /* AvatarTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C2F454E214C00E1004871FF /* AvatarTableViewCell.swift */; };
4C3E245C21F29FCE000AE092 /* Toast.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CA5F792211E1F06008C2708 /* Toast.swift */; };
4C3E245D21F2B395000AE092 /* DirectionalPanGestureRecognizer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4523149F1F7E9E18003A428C /* DirectionalPanGestureRecognizer.swift */; };
4C3EF7FD2107DDEE0007EBF7 /* ParamParserTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C3EF7FC2107DDEE0007EBF7 /* ParamParserTest.swift */; };
4C3EF802210918740007EBF7 /* SSKProtoEnvelopeTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C3EF801210918740007EBF7 /* SSKProtoEnvelopeTest.swift */; };
4C4AEC4520EC343B0020E72B /* DismissableTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C4AEC4420EC343B0020E72B /* DismissableTextField.swift */; };
@ -1718,6 +1718,7 @@
346129CE1FD207F200532771 /* Views */ = {
isa = PBXGroup;
children = (
4523149F1F7E9E18003A428C /* DirectionalPanGestureRecognizer.swift */,
34AC0A0C211B39EA00997B47 /* AvatarImageView.swift */,
34AC0A07211B39E900997B47 /* CommonStrings.swift */,
34AC0A0A211B39EA00997B47 /* ContactCellView.h */,
@ -2332,7 +2333,6 @@
34E3E5671EC4B19400495BAC /* AudioProgressView.swift */,
4C2F454E214C00E1004871FF /* AvatarTableViewCell.swift */,
451764291DE939FD00EDB8B9 /* ContactCell.swift */,
4523149F1F7E9E18003A428C /* DirectionalPanGestureRecognizer.swift */,
4C4AEC4420EC343B0020E72B /* DismissableTextField.swift */,
45A663C41F92EC760027B59E /* GroupTableViewCell.swift */,
45E5A6981F61E6DD001E4A8A /* MarqueeLabel.swift */,
@ -3311,6 +3311,7 @@
34074F61203D0CBE004596AE /* OWSSounds.m in Sources */,
34BEDB1721C80BCA007B0EAE /* OWSAnyTouchGestureRecognizer.m in Sources */,
34B6A909218B8824007C4606 /* OWS112TypingIndicatorsMigration.swift in Sources */,
4C3E245D21F2B395000AE092 /* DirectionalPanGestureRecognizer.swift in Sources */,
346129B51FD1F7E800532771 /* OWSProfileManager.m in Sources */,
4CBBCA6321714B4500EEB37D /* OWS110SortIdMigration.swift in Sources */,
342950832124C9750000B063 /* OWSTextView.m in Sources */,
@ -3509,7 +3510,6 @@
4556FA681F54AA9500AF40DD /* DebugUIProfile.swift in Sources */,
45A6DAD61EBBF85500893231 /* ReminderView.swift in Sources */,
34D1F0881F8678AA0066283D /* ConversationViewLayout.m in Sources */,
452314A01F7E9E18003A428C /* DirectionalPanGestureRecognizer.swift in Sources */,
34330AA31E79686200DF2FB9 /* OWSProgressView.m in Sources */,
344825C6211390C800DB4BD8 /* OWSOrphanDataCleaner.m in Sources */,
45D2AC02204885170033C692 /* OWS2FAReminderViewController.swift in Sources */,

View file

@ -92,6 +92,69 @@ class ImagePickerGridController: UICollectionViewController, PhotoLibraryDelegat
}
collectionView.backgroundColor = .ows_gray95
let selectionPanGesture = DirectionalPanGestureRecognizer(direction: [.horizontal], target: self, action: #selector(didPanSelection))
selectionPanGesture.delegate = self
self.selectionPanGesture = selectionPanGesture
collectionView.addGestureRecognizer(selectionPanGesture)
}
var selectionPanGesture: UIPanGestureRecognizer?
@objc
func didPanSelection(_ selectionPanGesture: UIPanGestureRecognizer) {
guard isInBatchSelectMode else {
return
}
guard let collectionView = collectionView else {
owsFailDebug("collectionView was unexpectedly nil")
return
}
switch selectionPanGesture.state {
case .possible:
break
case .began:
collectionView.isUserInteractionEnabled = false
collectionView.isScrollEnabled = false
case .changed:
let location = selectionPanGesture.location(in: collectionView)
guard let indexPath = collectionView.indexPathForItem(at: location) else {
return
}
tryToBatchSelectItem(at: indexPath)
case .cancelled, .ended, .failed:
collectionView.isUserInteractionEnabled = true
collectionView.isScrollEnabled = true
}
}
func tryToBatchSelectItem(at indexPath: IndexPath) {
guard isInBatchSelectMode else {
owsFailDebug("isInBatchSelectMode was unexpectedly false")
return
}
guard let collectionView = collectionView else {
owsFailDebug("collectionView was unexpectedly nil")
return
}
guard canSelectAdditionalItems else {
showTooManySelectedToast()
return
}
let asset = photoCollectionContents.asset(at: indexPath.item)
selectedIds.add(asset.localIdentifier)
updateDoneButton()
collectionView.selectItem(at: indexPath, animated: true, scrollPosition: [])
}
var canSelectAdditionalItems: Bool {
return selectedIds.count <= SignalAttachment.maxAttachmentsAllowed
}
override func viewWillLayoutSubviews() {
@ -628,3 +691,20 @@ class ImagePickerGridController: UICollectionViewController, PhotoLibraryDelegat
assetIdToCommentMap[assetId] = captionText
}
}
extension ImagePickerGridController: UIGestureRecognizerDelegate {
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
// Ensure we can still scroll the collectionView by allowing other gestures to
// take precedence.
guard otherGestureRecognizer == selectionPanGesture else {
return true
}
// Once we've startd the selectionPanGesture, don't allow scrolling
if otherGestureRecognizer.state == .began || otherGestureRecognizer.state == .changed {
return false
}
return true
}
}

View file

@ -1,68 +0,0 @@
//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
import UIKit.UIGestureRecognizerSubclass
@objc
enum PanDirection: Int {
case left, right, up, down, any
}
@objc
class PanDirectionGestureRecognizer: UIPanGestureRecognizer {
let direction: PanDirection
@objc init(direction: PanDirection, target: AnyObject, action: Selector) {
self.direction = direction
super.init(target: target, action: action)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
// Only start gesture if it's initially in the specified direction.
if state == .possible {
guard let touch = touches.first else {
return
}
let previousLocation = touch.previousLocation(in: view)
let location = touch.location(in: view)
let deltaY = previousLocation.y - location.y
let deltaX = previousLocation.x - location.x
switch direction {
case .up where deltaY > 0:
return
case .down where deltaY < 0:
return
case .left where deltaX > 0:
return
case .right where deltaX < 0:
return
default:
break
}
}
// Gesture was already started, or in the correct direction.
super.touchesMoved(touches, with: event)
if state == .began {
let vel = velocity(in: view)
switch direction {
case .left, .right:
if fabs(vel.y) > fabs(vel.x) {
state = .cancelled
}
case .up, .down:
if fabs(vel.x) > fabs(vel.y) {
state = .cancelled
}
default:
break
}
}
}
}

View file

@ -0,0 +1,91 @@
//
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
//
import UIKit.UIGestureRecognizerSubclass
public struct PanDirection: OptionSet {
public let rawValue: Int
public init(rawValue: Int) {
self.rawValue = rawValue
}
public static let left = PanDirection(rawValue: 1 << 0)
public static let right = PanDirection(rawValue: 1 << 1)
public static let up = PanDirection(rawValue: 1 << 2)
public static let down = PanDirection(rawValue: 1 << 3)
public static let horizontal: PanDirection = [.left, .right]
public static let vertical: PanDirection = [.up, .down]
public static let any: PanDirection = [.left, .right, .up, .down]
}
public class DirectionalPanGestureRecognizer: UIPanGestureRecognizer {
let direction: PanDirection
public init(direction: PanDirection, target: AnyObject, action: Selector) {
self.direction = direction
super.init(target: target, action: action)
}
override public func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
// Only start gesture if it's initially in the specified direction.
if state == .possible {
guard let touch = touches.first else {
return
}
let previousLocation = touch.previousLocation(in: view)
let location = touch.location(in: view)
let deltaY = previousLocation.y - location.y
let deltaX = previousLocation.x - location.x
let isSatisified: Bool = {
if abs(deltaY) > abs(deltaX) {
if direction.contains(.up) && deltaY < 0 {
return true
}
if direction.contains(.down) && deltaY > 0 {
return true
}
} else {
if direction.contains(.left) && deltaX < 0 {
return true
}
if direction.contains(.right) && deltaX > 0 {
return true
}
}
return false
}()
guard isSatisified else {
return
}
}
// Gesture was already started, or in the correct direction.
super.touchesMoved(touches, with: event)
if state == .began {
let vel = velocity(in: view)
switch direction {
case .left, .right:
if fabs(vel.y) > fabs(vel.x) {
state = .cancelled
}
case .up, .down:
if fabs(vel.x) > fabs(vel.y) {
state = .cancelled
}
default:
break
}
}
}
}