<commit_before>//
//  LPRTableView.swift
//  LPRTableView
//
//  Objective-C code Copyright (c) 2013 <NAME>. All rights reserved.
//  Swift adaptation Copyright (c) 2014 <NAME>. All rights reserved.
//

import Foundation
import QuartzCore
import UIKit

/** The delegate of a LPRTableView object can adopt the LPRTableViewDelegate protocol. Optional methods of the protocol allow the delegate to modify a cell visually before dragging occurs, or to be notified when a cell is about to be dragged or about to be dropped. */
@objc
public protocol LPRTableViewDelegate: NSObjectProtocol {
	
	/** Provides the delegate a chance to modify the cell visually before dragging occurs. Defaults to using the cell as-is if not implemented. */
	@objc optional func tableView(_ tableView: UITableView, draggingCell cell: UITableViewCell, at indexPath: IndexPath) -> UITableViewCell
	
	/** Called within an animation block when the dragging view is about to show. */
	@objc optional func tableView(_ tableView: UITableView, showDraggingView view: UIView, at indexPath: IndexPath)
	
	/** Called within an animation block when the dragging view is about to hide. */
	@objc optional func tableView(_ tableView: UITableView, hideDraggingView view: UIView, at indexPath: IndexPath)

	/** Called when the dragging gesture's vertical location changes. */
	@objc optional func tableView(_ tableView: UITableView, draggingGestureChanged gesture: UILongPressGestureRecognizer)
	
}

open class LPRTableView: UITableView {
	
	/** The object that acts as the delegate of the receiving table view. */
	weak open var longPressReorderDelegate: LPRTableViewDelegate?
	
	fileprivate var longPressGestureRecognizer: UILongPressGestureRecognizer!
	
	fileprivate var initialIndexPath: IndexPath?
	
	fileprivate var currentLocationIndexPath: IndexPath?
	
	fileprivate var draggingView: UIView?
	
	fileprivate var scrollRate: Double = 0.0
	
	fileprivate var scrollDisplayLink: CADisplayLink?
	
	fileprivate var feedbackGenerator: AnyObject?

	fileprivate var previousGestureVerticalPosition: CGFloat?
	
	/** A Bool property that indicates whether long press to reorder is enabled. */
	open var longPressReorderEnabled: Bool {
		get {
			return longPressGestureRecognizer.isEnabled
		}
		set {
			longPressGestureRecognizer.isEnabled = newValue
		}
	}
	
	/**
	The minimum period a finger must press on a cell for the reordering to begin.
	
	The time interval is in seconds. The default duration is is 0.5 seconds.
	*/
	open var minimumPressDuration: CFTimeInterval {
		get {
			return longPressGestureRecognizer.minimumPressDuration
		}
		set {
			longPressGestureRecognizer.minimumPressDuration = newValue
		}
	}
	
	public convenience init() {
		self.init(frame: .zero)
	}
	
	public override init(frame: CGRect, style: UITableView.Style) {
		super.init(frame: frame, style: style)
		initialize()
	}
	
	public required init?(coder aDecoder: NSCoder) {
		super.init(coder: aDecoder)
		initialize()
	}
	
	fileprivate func initialize() {
		longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(LPRTableView._longPress(_:)))
		longPressGestureRecognizer.delegate = self
		addGestureRecognizer(longPressGestureRecognizer)
		
		estimatedRowHeight = 0.0
		estimatedSectionHeaderHeight = 0.0
		estimatedSectionFooterHeight = 0.0
	}
	
}

extension LPRTableView: UIGestureRecognizerDelegate {
	
	open override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
		let location: CGPoint = gestureRecognizer.location(in: self)
		let indexPath: IndexPath? = indexPathForRow(at: location)
		let sections: Int = numberOfSections
		var rows: Int = 0
		for i in 0..<sections {
			rows += numberOfRows(inSection: i)
		}
		// Long press gesture should not begin if it was not on a valid row or our table is empty
		// or the `dataSource.tableView(_:canMoveRowAt:)` doesn't allow moving the row.
		return (rows > 0)
			&& (indexPath != nil)
			&& canMoveRowAt(indexPath: indexPath!)
	}
	
}

extension LPRTableView {
	
	fileprivate func canMoveRowAt(indexPath: IndexPath) -> Bool {
		return (dataSource?.responds(to: #selector(UITableViewDataSource.tableView(_:canMoveRowAt:))) == false) || (dataSource?.tableView?(self, canMoveRowAt: indexPath) == true)
	}
	
	@objc internal func _longPress(_ gesture: UILongPressGestureRecognizer) {
		let location: CGPoint = gesture.location(in: self)
		let indexPath: IndexPath? = indexPathForRow(at: location)
		
		switch gesture.state {<commit_msg>Fixed bug where system recognizer was canceled.<commit_after>//
//  LPRTableView.swift
//  LPRTableView
//
//  Objective-C code Copyright (c) 2013 <NAME>. All rights reserved.
//  Swift adaptation Copyright (c) 2014 <NAME>. All rights reserved.
//

import Foundation
import QuartzCore
import UIKit

/** The delegate of a LPRTableView object can adopt the LPRTableViewDelegate protocol. Optional methods of the protocol allow the delegate to modify a cell visually before dragging occurs, or to be notified when a cell is about to be dragged or about to be dropped. */
@objc
public protocol LPRTableViewDelegate: NSObjectProtocol {
	
	/** Provides the delegate a chance to modify the cell visually before dragging occurs. Defaults to using the cell as-is if not implemented. */
	@objc optional func tableView(_ tableView: UITableView, draggingCell cell: UITableViewCell, at indexPath: IndexPath) -> UITableViewCell
	
	/** Called within an animation block when the dragging view is about to show. */
	@objc optional func tableView(_ tableView: UITableView, showDraggingView view: UIView, at indexPath: IndexPath)
	
	/** Called within an animation block when the dragging view is about to hide. */
	@objc optional func tableView(_ tableView: UITableView, hideDraggingView view: UIView, at indexPath: IndexPath)

	/** Called when the dragging gesture's vertical location changes. */
	@objc optional func tableView(_ tableView: UITableView, draggingGestureChanged gesture: UILongPressGestureRecognizer)
	
}

open class LPRTableView: UITableView {
	
	/** The object that acts as the delegate of the receiving table view. */
	weak open var longPressReorderDelegate: LPRTableViewDelegate?
	
	fileprivate var longPressGestureRecognizer: UILongPressGestureRecognizer!
	
	fileprivate var initialIndexPath: IndexPath?
	
	fileprivate var currentLocationIndexPath: IndexPath?
	
	fileprivate var draggingView: UIView?
	
	fileprivate var scrollRate: Double = 0.0
	
	fileprivate var scrollDisplayLink: CADisplayLink?
	
	fileprivate var feedbackGenerator: AnyObject?

	fileprivate var previousGestureVerticalPosition: CGFloat?
	
	/** A Bool property that indicates whether long press to reorder is enabled. */
	open var longPressReorderEnabled: Bool {
		get {
			return longPressGestureRecognizer.isEnabled
		}
		set {
			longPressGestureRecognizer.isEnabled = newValue
		}
	}
	
	/**
	The minimum period a finger must press on a cell for the reordering to begin.
	
	The time interval is in seconds. The default duration is is 0.5 seconds.
	*/
	open var minimumPressDuration: CFTimeInterval {
		get {
			return longPressGestureRecognizer.minimumPressDuration
		}
		set {
			longPressGestureRecognizer.minimumPressDuration = newValue
		}
	}
	
	public convenience init() {
		self.init(frame: .zero)
	}
	
	public override init(frame: CGRect, style: UITableView.Style) {
		super.init(frame: frame, style: style)
		initialize()
	}
	
	public required init?(coder aDecoder: NSCoder) {
		super.init(coder: aDecoder)
		initialize()
	}
	
	fileprivate func initialize() {
		longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(LPRTableView._longPress(_:)))
		longPressGestureRecognizer.delegate = self
		addGestureRecognizer(longPressGestureRecognizer)
		
		estimatedRowHeight = 0.0
		estimatedSectionHeaderHeight = 0.0
		estimatedSectionFooterHeight = 0.0
	}
	
}

extension LPRTableView: UIGestureRecognizerDelegate {
	
	open override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
		guard gestureRecognizer == longPressGestureRecognizer else { return true }
		let location: CGPoint = gestureRecognizer.location(in: self)
		let indexPath: IndexPath? = indexPathForRow(at: location)
		let sections: Int = numberOfSections
		var rows: Int = 0
		for i in 0..<sections {
			rows += numberOfRows(inSection: i)
		}
		// Long press gesture should not begin if it was not on a valid row or our table is empty
		// or the `dataSource.tableView(_:canMoveRowAt:)` doesn't allow moving the row.
		return (rows > 0)
			&& (indexPath != nil)
			&& canMoveRowAt(indexPath: indexPath!)
	}
	
}

extension LPRTableView {
	
	fileprivate func canMoveRowAt(indexPath: IndexPath) -> Bool {
		return (dataSource?.responds(to: #selector(UITableViewDataSource.tableView(_:canMoveRowAt:))) == false) || (dataSource?.tableView?(self, canMoveRowAt: indexPath) == true)
	}
	
	@objc internal func _longPress(_ gesture: UILongPressGestureRecognizer) {
		let location: CGPoint = gesture.location(in: self)
		let indexPath: IndexPath? = indexPathForRow(at: location)
		
		switch gesture.state {
{'fraction_non_alphanumeric': 0.07742998352553541, 'fraction_numerical': 0.004612850082372323, 'mean_word_length': 4.6948092557848655, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '762147', 'n_tokens_mistral': 2949, 'n_tokens_neox': 2553, 'n_words': 882}