<commit_before>//
//  ImageSlideShowViewController.swift
//
//  Created by <NAME> on 02/11/15.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import UIKit

@objc protocol ImageSlideShowProtocol
{
	func slideIdentifier() -> String
	func image(completion: @escaping (_ image:UIImage?, _ error:Error?) -> Void)
}

class ImageSlideShowCache: NSCache<AnyObject, AnyObject>
{
	override init()
	{
		super.init()
		
		NotificationCenter.default.addObserver(self, selector:#selector(NSMutableArray.removeAllObjects), name: Notification.Name.UIApplicationDidReceiveMemoryWarning, object: nil)
	}
	
	deinit
	{
		NotificationCenter.default.removeObserver(self);
	}
}

class ImageSlideShowViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate
{
	static var imageSlideShowStoryboard:UIStoryboard = UIStoryboard(name: "ImageSlideShow", bundle: nil)
	
	var slides:[ImageSlideShowProtocol]?
	var initialIndex:Int = 0
	var pageSpacing:CGFloat = 10.0
	var panDismissTolerance:CGFloat = 30.0
	var dismissOnPanGesture:Bool = false
	var enableZoom:Bool = false
	var statusBarStyle:UIStatusBarStyle = .lightContent
	var navigationBarTintColor:UIColor = .white
	
	var controllerDidDismiss:() -> Void = {}
	var stepAnimate:((_ offset:CGFloat, _ viewController:UIViewController) -> Void) = { _,_ in }
	var restoreAnimation:((_ viewController:UIViewController) -> Void) = { _ in }
	var dismissAnimation:((_ viewController:UIViewController, _ panDirection:CGPoint, _ completion: @escaping ()->()) -> Void) = { _,_,_ in }
	
	fileprivate var originPanViewCenter:CGPoint = .zero
	fileprivate var panViewCenter:CGPoint = .zero
	fileprivate var navigationBarHidden = false
	fileprivate var toggleBarButtonItem:UIBarButtonItem?
	fileprivate var currentIndex = 0
	fileprivate let slidesViewControllerCache = ImageSlideShowCache()
	
	override var preferredStatusBarStyle:UIStatusBarStyle
	{
		return statusBarStyle
	}
	
	override var prefersStatusBarHidden:Bool
	{
		return navigationBarHidden
	}
	
	override var shouldAutorotate:Bool
	{
		return true
	}
	
	override var supportedInterfaceOrientations:UIInterfaceOrientationMask
	{
		return .all
	}
	
	//	MARK: - Class methods
	
	class func imageSlideShowNavigationController() -> ImageSlideShowNavigationController
	{
		let controller = ImageSlideShowViewController.imageSlideShowStoryboard.instantiateViewController(withIdentifier: "ImageSlideShowNavigationController") as! ImageSlideShowNavigationController
		controller.modalPresentationStyle = .overCurrentContext
		controller.modalPresentationCapturesStatusBarAppearance = true
		
		return controller
	}
	
	class func imageSlideShowViewController() -> ImageSlideShowViewController
	{
		let controller = ImageSlideShowViewController.imageSlideShowStoryboard.instantiateViewController(withIdentifier: "ImageSlideShowViewController") as! ImageSlideShowViewController
		controller.modalPresentationStyle = .overCurrentContext
		controller.modalPresentationCapturesStatusBarAppearance = true
		
		return controller
	}
	
	class open func presentFrom(_ viewController:UIViewController, configure:((_ controller: ImageSlideShowViewController) -> ())?)
	{
		let navController = self.imageSlideShowNavigationController()
		if let issViewController = navController.visibleViewController as? ImageSlideShowViewController
		{
			configure?(issViewController)
			
			viewController.present(navController, animated: true, completion: nil)
		}
	}
	
	required public init?(coder: NSCoder)
	{
		super.init(coder: coder)
		
		prepareAnimations()
	}
	
	//	MARK: - Instance methods
	
	override func viewDidLoad()
	{
		super.viewDidLoad()
		
		delegate = self
		dataSource = self
		
		hidesBottomBarWhenPushed = true
		
		navigationController?.navigationBar.tintColor = navigationBarTintColor
		navigationController?.view.backgroundColor = .black
		navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(dismiss(sender:)))
		
		//	Manage Gestures
		
		var gestures = gestureRecognizers
		let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tapGesture(gesture:)))
		gestures.append(tapGesture)
		
		if (dismissOnPanGesture)
		{
			let panGesture = UIPanGestureRecognizer(target: self, action: #selector(panGesture(gesture:)))
			gestures.append(panGesture)
			
			//	If dismiss on pan lock horizontal direction and disable vertical pan to avoid strange behaviours
			
			scrollView()?.isDirectionalLockEnabled = true
			scrollView()?.alwaysBounceVertical = false
		}
		
		view.gestureRecognizers = gestures
	}
	
	override func viewWillAppear(_ animated: Bool)
	{
		super.viewWillAppear(animated)
		
		setPage(withIndex: initialIndex)
	}
	
	//	MARK: Actions
	
	open func dismiss(sender:AnyObject?)
	{
		dismiss(animated: true, completion: nil)
		
		controllerDidDismiss()
	}
	
	open func goToPage(withIndex index:Int)
	{
		if index != currentIndex
		{
			setPage(withIndex: index)
		}
	}
	
	open func goToNextPage()
	{
		let index = currentIndex + 1
		if index < (slides?.count)!
		{<commit_msg>Working on control access.<commit_after>//
//  ImageSlideShowViewController.swift
//
//  Created by <NAME> on 02/11/15.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import UIKit

@objc protocol ImageSlideShowProtocol
{
	func slideIdentifier() -> String
	func image(completion: @escaping (_ image:UIImage?, _ error:Error?) -> Void)
}

class ImageSlideShowCache: NSCache<AnyObject, AnyObject>
{
	override init()
	{
		super.init()
		
		NotificationCenter.default.addObserver(self, selector:#selector(NSMutableArray.removeAllObjects), name: Notification.Name.UIApplicationDidReceiveMemoryWarning, object: nil)
	}
	
	deinit
	{
		NotificationCenter.default.removeObserver(self);
	}
}

open class ImageSlideShowViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate
{
	static var imageSlideShowStoryboard:UIStoryboard = UIStoryboard(name: "ImageSlideShow", bundle: nil)
	
	var slides:[ImageSlideShowProtocol]?
	var initialIndex:Int = 0
	var pageSpacing:CGFloat = 10.0
	var panDismissTolerance:CGFloat = 30.0
	var dismissOnPanGesture:Bool = false
	var enableZoom:Bool = false
	var statusBarStyle:UIStatusBarStyle = .lightContent
	var navigationBarTintColor:UIColor = .white
	
	var controllerDidDismiss:() -> Void = {}
	var stepAnimate:((_ offset:CGFloat, _ viewController:UIViewController) -> Void) = { _,_ in }
	var restoreAnimation:((_ viewController:UIViewController) -> Void) = { _ in }
	var dismissAnimation:((_ viewController:UIViewController, _ panDirection:CGPoint, _ completion: @escaping ()->()) -> Void) = { _,_,_ in }
	
	fileprivate var originPanViewCenter:CGPoint = .zero
	fileprivate var panViewCenter:CGPoint = .zero
	fileprivate var navigationBarHidden = false
	fileprivate var toggleBarButtonItem:UIBarButtonItem?
	fileprivate var currentIndex = 0
	fileprivate let slidesViewControllerCache = ImageSlideShowCache()
	
	override open var preferredStatusBarStyle:UIStatusBarStyle
	{
		return statusBarStyle
	}
	
	override open var prefersStatusBarHidden:Bool
	{
		return navigationBarHidden
	}
	
	override open var shouldAutorotate:Bool
	{
		return true
	}
	
	override open var supportedInterfaceOrientations:UIInterfaceOrientationMask
	{
		return .all
	}
	
	//	MARK: - Class methods
	
	class func imageSlideShowNavigationController() -> ImageSlideShowNavigationController
	{
		let controller = ImageSlideShowViewController.imageSlideShowStoryboard.instantiateViewController(withIdentifier: "ImageSlideShowNavigationController") as! ImageSlideShowNavigationController
		controller.modalPresentationStyle = .overCurrentContext
		controller.modalPresentationCapturesStatusBarAppearance = true
		
		return controller
	}
	
	class func imageSlideShowViewController() -> ImageSlideShowViewController
	{
		let controller = ImageSlideShowViewController.imageSlideShowStoryboard.instantiateViewController(withIdentifier: "ImageSlideShowViewController") as! ImageSlideShowViewController
		controller.modalPresentationStyle = .overCurrentContext
		controller.modalPresentationCapturesStatusBarAppearance = true
		
		return controller
	}
	
	class open func presentFrom(_ viewController:UIViewController, configure:((_ controller: ImageSlideShowViewController) -> ())?)
	{
		let navController = self.imageSlideShowNavigationController()
		if let issViewController = navController.visibleViewController as? ImageSlideShowViewController
		{
			configure?(issViewController)
			
			viewController.present(navController, animated: true, completion: nil)
		}
	}
	
	required public init?(coder: NSCoder)
	{
		super.init(coder: coder)
		
		prepareAnimations()
	}
	
	//	MARK: - Instance methods
	
	override open func viewDidLoad()
	{
		super.viewDidLoad()
		
		delegate = self
		dataSource = self
		
		hidesBottomBarWhenPushed = true
		
		navigationController?.navigationBar.tintColor = navigationBarTintColor
		navigationController?.view.backgroundColor = .black
		navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(dismiss(sender:)))
		
		//	Manage Gestures
		
		var gestures = gestureRecognizers
		let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tapGesture(gesture:)))
		gestures.append(tapGesture)
		
		if (dismissOnPanGesture)
		{
			let panGesture = UIPanGestureRecognizer(target: self, action: #selector(panGesture(gesture:)))
			gestures.append(panGesture)
			
			//	If dismiss on pan lock horizontal direction and disable vertical pan to avoid strange behaviours
			
			scrollView()?.isDirectionalLockEnabled = true
			scrollView()?.alwaysBounceVertical = false
		}
		
		view.gestureRecognizers = gestures
	}
	
	override open func viewWillAppear(_ animated: Bool)
	{
		super.viewWillAppear(animated)
		
		setPage(withIndex: initialIndex)
	}
	
	//	MARK: Actions
	
	open func dismiss(sender:AnyObject?)
	{
		dismiss(animated: true, completion: nil)
		
		controllerDidDismiss()
	}
	
	open func goToPage(withIndex index:Int)
	{
		if index != currentIndex
		{
			setPage(withIndex: index)
		}
	}
	
	open func goToNextPage()
	{
		let index = currentIndex + 1
		if index < (slides?.count)!
		{
{'fraction_non_alphanumeric': 0.08510011778563015, 'fraction_numerical': 0.003729878288182175, 'mean_word_length': 5.352244389027431, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 29, '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': '981205', 'n_tokens_mistral': 3348, 'n_tokens_neox': 2892, 'n_words': 632}