<commit_before>//
// AEImageScrollView.swift
//
// Copyright (c) 2016 <NAME> <<EMAIL>> http://tadija.net
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//

import UIKit

/**
 This is base class which consists from `UIImageView` inside `UIScrollView`.
 It will update current zoom scale on `imageView` whenever its `frame` changes.
 
 It may be used directly from code or storyboard with auto layout,
 just set its `image` property and it will do the rest.
 */
open class AEImageScrollView: UIScrollView, UIScrollViewDelegate {
    
    // MARK: - Types
    
    /// Modes for calculating zoom scale.
    public enum DisplayMode {
        /// switches between `fit` and `fill` depending of the image ratio.
        case automatic
        
        /// Fits entire image.
        case fit
        
        /// Fills entire `imageView`.
        case fill
        
        /// Fills width of the `imageView`.
        case fillWidth
        
        /// Fills height of the `imageView`.
        case fillHeight
    }
    
    // MARK: - Outlets
    
    private let stackView = UIStackView()
    
    /// Image view which displays the image.
    public let imageView = UIImageView()
    
    private let leftImageView = UIImageView()
    private let rightImageView = UIImageView()
    
    // MARK: - Properties
    
    /// Image to be displayed. UI will be updated whenever you set this property.
    @IBInspectable open var image: UIImage? {
        didSet {
            updateUI()
        }
    }
    
    /// Mode to be used when calculating zoom scale. Default value is `.automatic`.
    open var displayMode: DisplayMode = .automatic {
        didSet {
            if displayMode != oldValue {
                configureZoomScales()
            }
        }
    }
    
    /// Whenever frame property is changed zoom scales are gonna be re-calculated.
    override open var frame: CGRect {
        willSet {
            if !frame.size.equalTo(newValue.size) {
                prepareToResize()
            }
        }
        didSet {
            if !frame.size.equalTo(oldValue.size) {
                configureZoomScales()
                recoverFromResizing()
            }
        }
    }
    
    // MARK: - Init
    
    override public init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }
    
    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }
    
    public init() {
        super.init(frame: CGRect.zero)
        commonInit()
    }
    
    private func commonInit() {
        configureSelf()
        updateUI()
    }
    
    // MARK: - UIScrollViewDelegate
    
    /// View used for zooming is `imageView`, be sure to keep that logic.
    public func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return stackView
    }
    
    public func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if !scrollView.isDecelerating {
            updateContentOffsetRelative()
        }
    }
    
    public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        updateContentOffsetRelative()
    }
    
    private func updateContentOffsetRelative() {
        let xOffset = contentOffset.x
        let width = contentSize.width / 3
        
        let maxOffset = width * 2
        if xOffset > maxOffset {
            let diff = xOffset - maxOffset
            let newOffset = CGPoint(x: width + diff, y: 0)
            setContentOffset(newOffset, animated: false)
        }
        
        let minOffset = width - bounds.width
        if xOffset < minOffset {
            let diff = minOffset - xOffset
            let newOffset = CGPoint(x: width + minOffset - diff, y: 0)
            setContentOffset(newOffset, animated: false)
        }
    }
    
    // MARK: - Helpers
    
    private func configureSelf() {
        backgroundColor = UIColor.black
        showsVerticalScrollIndicator = false
        showsHorizontalScrollIndicator = false
        bouncesZoom = true
        delegate = self
        
        stackView.addArrangedSubview(leftImageView)
        stackView.addArrangedSubview(imageView)
        stackView.addArrangedSubview(rightImageView)
        
        addSubview(stackView)
    }<commit_msg>Improved logic to work when content offset is animated with gyro motion<commit_after>//
// AEImageScrollView.swift
//
// Copyright (c) 2016 <NAME> <<EMAIL>> http://tadija.net
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//

import UIKit

/**
 This is base class which consists from `UIImageView` inside `UIScrollView`.
 It will update current zoom scale on `imageView` whenever its `frame` changes.
 
 It may be used directly from code or storyboard with auto layout,
 just set its `image` property and it will do the rest.
 */
open class AEImageScrollView: UIScrollView, UIScrollViewDelegate {
    
    // MARK: - Types
    
    /// Modes for calculating zoom scale.
    public enum DisplayMode {
        /// switches between `fit` and `fill` depending of the image ratio.
        case automatic
        
        /// Fits entire image.
        case fit
        
        /// Fills entire `imageView`.
        case fill
        
        /// Fills width of the `imageView`.
        case fillWidth
        
        /// Fills height of the `imageView`.
        case fillHeight
    }
    
    // MARK: - Outlets
    
    private let stackView = UIStackView()
    
    /// Image view which displays the image.
    public let imageView = UIImageView()
    
    private let leftImageView = UIImageView()
    private let rightImageView = UIImageView()
    
    // MARK: - Properties
    
    /// Image to be displayed. UI will be updated whenever you set this property.
    @IBInspectable open var image: UIImage? {
        didSet {
            updateUI()
        }
    }
    
    /// Mode to be used when calculating zoom scale. Default value is `.automatic`.
    open var displayMode: DisplayMode = .automatic {
        didSet {
            if displayMode != oldValue {
                configureZoomScales()
            }
        }
    }
    
    /// Whenever frame property is changed zoom scales are gonna be re-calculated.
    override open var frame: CGRect {
        willSet {
            if !frame.size.equalTo(newValue.size) {
                prepareToResize()
            }
        }
        didSet {
            if !frame.size.equalTo(oldValue.size) {
                configureZoomScales()
                recoverFromResizing()
            }
        }
    }
    
    // MARK: - Init
    
    override public init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }
    
    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }
    
    public init() {
        super.init(frame: CGRect.zero)
        commonInit()
    }
    
    private func commonInit() {
        configureSelf()
        updateUI()
    }
    
    // MARK: - UIScrollViewDelegate
    
    /// View used for zooming is `imageView`, be sure to keep that logic.
    public func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return stackView
    }
    
    public func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if !scrollView.isDecelerating {
            updateContentOffsetRelative()
        }
    }
    
    public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        updateContentOffsetRelative()
    }
    
    private func updateContentOffsetRelative() {
        let xOffset = contentOffset.x
        let width = contentSize.width / 3
        
        let maxOffset = width * 2
        if xOffset > maxOffset {
            let diff = xOffset - maxOffset
            let newOffset = CGPoint(x: width + diff, y: 0)
            UIView.performWithoutAnimation {
                setContentOffset(newOffset, animated: false)
            }
        }
        
        let minOffset = width - bounds.width
        if xOffset < minOffset {
            let diff = minOffset - xOffset
            let newOffset = CGPoint(x: width + minOffset - diff, y: 0)
            UIView.performWithoutAnimation {
                setContentOffset(newOffset, animated: false)
            }
        }
    }
    
    // MARK: - Helpers
    
    private func configureSelf() {
        backgroundColor = UIColor.black
        showsVerticalScrollIndicator = false
        showsHorizontalScrollIndicator = false
        bouncesZoom = true
        delegate = self
        
        stackView.addArrangedSubview(leftImageView)
        stackView.addArrangedSubview(imageView)
        stackView.addArrangedSubview(rightImageView)
        
        addSubview(stackView)
    }
{'fraction_non_alphanumeric': 0.07582233785541721, 'fraction_numerical': 0.001486712506968965, 'mean_word_length': 2.033540022547914, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, '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': '7224894', 'n_tokens_mistral': 2889, 'n_tokens_neox': 2790, 'n_words': 1007}