<commit_before>//
//  ImageSlideshow.swift
//  ImageSlideshow
//
//  Created by <NAME> on 30.07.15.
//

import UIKit

public enum PageControlPosition {
    case Hidden
    case InsideScrollView
    case UnderScrollView
    case Custom(padding: CGFloat)
    
    var bottomPadding: CGFloat {
        switch self {
        case .Hidden, .InsideScrollView:
            return 0.0
        case .UnderScrollView:
            return 30.0
        case .Custom(let padding):
            return padding
        }
    }
}

public class ImageSlideshow: UIView, UIScrollViewDelegate {
    
    public let scrollView = UIScrollView()
    public let pageControl = UIPageControl()

    public var singleTap:(()->())?
    public var doubleTap:(()->())?
    var singleTapGestureRecognizer : UITapGestureRecognizer?
    var doubleTapGestureRecognizer : UITapGestureRecognizer?

    // MARK: - State properties
    
    /// Page control position
    public var pageControlPosition = PageControlPosition.InsideScrollView {
        didSet {
            setNeedsLayout()
            layoutScrollView()
        }
    }
    
    /// Current item index
    public private(set) var currentItemIndex: Int = 0 {
        didSet {
            pageControl.currentPage = currentItemIndex;
        }
    }
    
    /// Currenlty displayed slideshow item
    public var currentSlideshowItem: ImageSlideshowItem? {
        if slideshowItems.count > scrollViewPage {
            return slideshowItems[scrollViewPage]
        } else {
            return nil
        }
    }
    
    public private(set) var scrollViewPage: Int = 0
    public private(set) var images = [InputSource]()
    public private(set) var slideshowItems = [ImageSlideshowItem]()
    
    // MARK: - Preferences
    
    /// Enables/disables infinite scrolling between images
    public var circular = true
    
    /// Enables/disables user interactions
    public var draggingEnabled = true {
        didSet {
            self.scrollView.userInteractionEnabled = draggingEnabled
        }
    }
    
    /// Enables/disables zoom
    public var zoomEnabled = false
    
    /// Image change interval, zero stops the auto-scrolling
    public var slideshowInterval = 0.0 {
        didSet {
            self.slideshowTimer?.invalidate()
            self.slideshowTimer = nil
            setTimerIfNeeded()
        }
    }
    
    /// Content mode of each image in the slideshow
    public var contentScaleMode: UIViewContentMode = UIViewContentMode.ScaleAspectFit {
        didSet {
            for view in slideshowItems {
                view.imageView.contentMode = contentScaleMode
            }
        }
    }
    
    private var slideshowTimer: NSTimer?
    private var scrollViewImages = [InputSource]()

    // MARK: - Life cycle
    
    override public init(frame: CGRect) {
        super.init(frame: frame)
        initialize()
    }
    
    convenience init() {
        self.init(frame: CGRectZero)
    }
    
    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        initialize()
    }
    
    private func initialize() {
        autoresizesSubviews = true
        clipsToBounds = true
        
        // scroll view configuration
        scrollView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height - 50.0)
        scrollView.delegate = self
        scrollView.pagingEnabled = true
        scrollView.bounces = true
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.showsVerticalScrollIndicator = false
        scrollView.autoresizingMask = self.autoresizingMask
        addSubview(scrollView)
        
        addSubview(pageControl)


        doubleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(doubleTapActivated))
        doubleTapGestureRecognizer?.numberOfTapsRequired = 2
        self.addGestureRecognizer(doubleTapGestureRecognizer!)

        singleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(singleTapActivated))
        singleTapGestureRecognizer?.numberOfTapsRequired = 1
        self.addGestureRecognizer(singleTapGestureRecognizer!)

        singleTapGestureRecognizer?.requireGestureRecognizerToFail(doubleTapGestureRecognizer!)

        setTimerIfNeeded()
        layoutScrollView()
    }

    func doubleTapActivated() {
        if let _ = doubleTap {
            doubleTap!()
        }
    }

    func singleTapActivated() {
        if let _ = singleTap {
            singleTap!()
        }
    }

    public func presentFullScreenController(fromController:UIViewController) {
        let ctr = FullScreenSlideshowViewController()
        ctr.pageSelected = {(page: Int) in
            self.setScrollViewPage(page, animated: false)
        }

        ctr.slideshow.circular = self.circular
        ctr.initialImageIndex = scrollViewPage
        ctr.inputs = images

        let slideshowTransitioningDelegate = ZoomAnimatedTransitioningDelegate(slideshowView: self, slideshowController: ctr)
        // Uncomment if you want disable the slide-to-dismiss feature on full screen preview
        // self.transitionDelegate?.slideToDismissEnabled = false
        ctr.transitioningDelegate = slideshowTransitioningDelegate

        fromController.presentViewController(ctr, animated: true, completion: nil)
    }
    
    override public func layoutSubviews() {
        super.layoutSubviews()

        if case .Hidden = self.pageControlPosition {
            pageControl.hidden = true
        } else {
            pageControl.hidden = false
        }<commit_msg>Hold reference to transitioning delegate<commit_after>//
//  ImageSlideshow.swift
//  ImageSlideshow
//
//  Created by <NAME> on 30.07.15.
//

import UIKit

public enum PageControlPosition {
    case Hidden
    case InsideScrollView
    case UnderScrollView
    case Custom(padding: CGFloat)
    
    var bottomPadding: CGFloat {
        switch self {
        case .Hidden, .InsideScrollView:
            return 0.0
        case .UnderScrollView:
            return 30.0
        case .Custom(let padding):
            return padding
        }
    }
}

public class ImageSlideshow: UIView, UIScrollViewDelegate {
    
    public let scrollView = UIScrollView()
    public let pageControl = UIPageControl()

    public var singleTap:(()->())?
    public var doubleTap:(()->())?
    var singleTapGestureRecognizer : UITapGestureRecognizer?
    var doubleTapGestureRecognizer : UITapGestureRecognizer?

    var slideshowTransitioningDelegate:ZoomAnimatedTransitioningDelegate?

    // MARK: - State properties
    
    /// Page control position
    public var pageControlPosition = PageControlPosition.InsideScrollView {
        didSet {
            setNeedsLayout()
            layoutScrollView()
        }
    }
    
    /// Current item index
    public private(set) var currentItemIndex: Int = 0 {
        didSet {
            pageControl.currentPage = currentItemIndex;
        }
    }
    
    /// Currenlty displayed slideshow item
    public var currentSlideshowItem: ImageSlideshowItem? {
        if slideshowItems.count > scrollViewPage {
            return slideshowItems[scrollViewPage]
        } else {
            return nil
        }
    }
    
    public private(set) var scrollViewPage: Int = 0
    public private(set) var images = [InputSource]()
    public private(set) var slideshowItems = [ImageSlideshowItem]()
    
    // MARK: - Preferences
    
    /// Enables/disables infinite scrolling between images
    public var circular = true
    
    /// Enables/disables user interactions
    public var draggingEnabled = true {
        didSet {
            self.scrollView.userInteractionEnabled = draggingEnabled
        }
    }
    
    /// Enables/disables zoom
    public var zoomEnabled = false
    
    /// Image change interval, zero stops the auto-scrolling
    public var slideshowInterval = 0.0 {
        didSet {
            self.slideshowTimer?.invalidate()
            self.slideshowTimer = nil
            setTimerIfNeeded()
        }
    }
    
    /// Content mode of each image in the slideshow
    public var contentScaleMode: UIViewContentMode = UIViewContentMode.ScaleAspectFit {
        didSet {
            for view in slideshowItems {
                view.imageView.contentMode = contentScaleMode
            }
        }
    }
    
    private var slideshowTimer: NSTimer?
    private var scrollViewImages = [InputSource]()

    // MARK: - Life cycle
    
    override public init(frame: CGRect) {
        super.init(frame: frame)
        initialize()
    }
    
    convenience init() {
        self.init(frame: CGRectZero)
    }
    
    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        initialize()
    }
    
    private func initialize() {
        autoresizesSubviews = true
        clipsToBounds = true
        
        // scroll view configuration
        scrollView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height - 50.0)
        scrollView.delegate = self
        scrollView.pagingEnabled = true
        scrollView.bounces = true
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.showsVerticalScrollIndicator = false
        scrollView.autoresizingMask = self.autoresizingMask
        addSubview(scrollView)
        
        addSubview(pageControl)


        doubleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(doubleTapActivated))
        doubleTapGestureRecognizer?.numberOfTapsRequired = 2
        self.addGestureRecognizer(doubleTapGestureRecognizer!)

        singleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(singleTapActivated))
        singleTapGestureRecognizer?.numberOfTapsRequired = 1
        self.addGestureRecognizer(singleTapGestureRecognizer!)

        singleTapGestureRecognizer?.requireGestureRecognizerToFail(doubleTapGestureRecognizer!)

        setTimerIfNeeded()
        layoutScrollView()
    }

    func doubleTapActivated() {
        if let _ = doubleTap {
            doubleTap!()
        }
    }

    func singleTapActivated() {
        if let _ = singleTap {
            singleTap!()
        }
    }

    public func presentFullScreenController(fromController:UIViewController) {
        let ctr = FullScreenSlideshowViewController()
        ctr.pageSelected = {(page: Int) in
            self.setScrollViewPage(page, animated: false)
        }

        ctr.slideshow.circular = self.circular
        ctr.initialImageIndex = scrollViewPage
        ctr.inputs = images

        slideshowTransitioningDelegate = ZoomAnimatedTransitioningDelegate(slideshowView: self, slideshowController: ctr)
        // Uncomment if you want disable the slide-to-dismiss feature on full screen preview
        // self.transitionDelegate?.slideToDismissEnabled = false
        ctr.transitioningDelegate = slideshowTransitioningDelegate

        fromController.presentViewController(ctr, animated: true, completion: nil)
    }
    
    override public func layoutSubviews() {
        super.layoutSubviews()

        if case .Hidden = self.pageControlPosition {
            pageControl.hidden = true
        } else {
            pageControl.hidden = false
        }
{'fraction_non_alphanumeric': 0.07347447073474471, 'fraction_numerical': 0.003913894324853229, 'mean_word_length': 2.315541138307284, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '4807179', 'n_tokens_mistral': 3195, 'n_tokens_neox': 3022, 'n_words': 679}