<commit_before>//
//  DKPhotoGalleryInteractiveTransition.swift
//  DKPhotoGallery
//
//  Created by ZhangAo on 09/09/2017.
//  Copyright © 2017 ZhangAo. All rights reserved.
//

import UIKit

class DKPhotoGalleryInteractiveTransition: UIPercentDrivenInteractiveTransition {

    private var gallery: DKPhotoGallery!
    
    private var fromImageView: UIImageView?
    private var fromRect: CGRect!
    
    internal var isInteracting = false
    private var percent: CGFloat = 0
    private var toImageView: UIImageView?
    
    convenience init(gallery: DKPhotoGallery) {
        self.init()
        
        self.gallery = gallery
        self.setupGesture()
    }
    
    private func setupGesture() {
        let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handleGesture))
        self.gallery.visibleViewController?.view.addGestureRecognizer(panGesture)
    }
    
    @objc
    private func handleGesture(_ recognizer: UIPanGestureRecognizer) {
        let offset = recognizer.translation(in: recognizer.view?.superview)
        
        switch recognizer.state {
        case .began:
            self.isInteracting = true
            self.fromImageView = self.gallery.currentImageView()
            self.fromRect = self.fromImageView?.frame
            
            self.gallery.setNavigationBarHidden(true, animated: true)
            
            self.toImageView = self.gallery.dismissImageViewBlock?(self.gallery.currentIndex())
            self.toImageView?.isHidden = true
        case .changed:
            let fraction = CGFloat(fabsf(Float(offset.y / 200)))
            self.percent = fmin(fraction, 1.0)
            
            if let fromImageView = self.fromImageView {
                let currentLocation = recognizer.location(in: nil)
                let originalLocation = CGPoint(x: currentLocation.x - offset.x, y: currentLocation.y - offset.y)
                var percent = CGFloat(1.0)
                percent = fmax(offset.y > 0 ? 1 - self.percent : CGFloat(1), 0.5)
                let currentWidth = self.fromRect.width * percent
                let currentHeight = self.fromRect.height * percent
                
                let result = CGRect(x: currentLocation.x - (originalLocation.x - self.fromRect.origin.x) * percent,
                                    y: currentLocation.y - (originalLocation.y - self.fromRect.origin.y) * percent,
                                    width: currentWidth,
                                    height: currentHeight)
                fromImageView.frame = (fromImageView.superview?.convert(result, from: nil))!
                
                if offset.y < 0 {
                    self.percent = -self.percent
                }
                
                if !self.gallery.isStatusBarHidden {
                    if UIApplication.shared.isStatusBarHidden && self.percent > 0.04 {
                        UIApplication.shared.isStatusBarHidden = false
                    } else if !UIApplication.shared.isStatusBarHidden && self.percent < 0.04 {
                        UIApplication.shared.isStatusBarHidden = true
                    }
                }
                
                self.colorAnimation()
            }
        case .ended,
             .cancelled:
            self.isInteracting = false
            let shouldComplete = self.percent > 0.5
            if !shouldComplete || recognizer.state == .cancelled {
                if let fromImageView = self.fromImageView {
                    UIView.animate(withDuration: 0.3, animations: { 
                        fromImageView.frame = self.fromRect
                        fromImageView.superview?.superview?.backgroundColor = UIColor.black
                        self.gallery.view.backgroundColor = UIColor.black
                        
                        if !self.gallery.isStatusBarHidden && !UIApplication.shared.isStatusBarHidden {
                            UIApplication.shared.isStatusBarHidden = true
                        }
                        
                    }) { (finished) in
                        self.toImageView?.isHidden = false
                    }
                }
            } else {
                self.gallery.dismissGallery()
                self.finish()
            }
            self.fromImageView = nil
            self.percent = 0<commit_msg>Improved handling of StatusBar.<commit_after>//
//  DKPhotoGalleryInteractiveTransition.swift
//  DKPhotoGallery
//
//  Created by ZhangAo on 09/09/2017.
//  Copyright © 2017 ZhangAo. All rights reserved.
//

import UIKit

class DKPhotoGalleryInteractiveTransition: UIPercentDrivenInteractiveTransition {

    private var gallery: DKPhotoGallery!
    
    private var fromImageView: UIImageView?
    private var fromRect: CGRect!
    
    internal var isInteracting = false
    private var percent: CGFloat = 0
    private var toImageView: UIImageView?
    
    convenience init(gallery: DKPhotoGallery) {
        self.init()
        
        self.gallery = gallery
        self.setupGesture()
    }
    
    private func setupGesture() {
        let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handleGesture))
        self.gallery.visibleViewController?.view.addGestureRecognizer(panGesture)
    }
    
    @objc
    private func handleGesture(_ recognizer: UIPanGestureRecognizer) {
        let offset = recognizer.translation(in: recognizer.view?.superview)
        
        switch recognizer.state {
        case .began:
            self.isInteracting = true
            self.fromImageView = self.gallery.currentImageView()
            self.fromRect = self.fromImageView?.frame
            
            self.gallery.setNavigationBarHidden(true, animated: true)
            
            self.toImageView = self.gallery.dismissImageViewBlock?(self.gallery.currentIndex())
            self.toImageView?.isHidden = true
        case .changed:
            let fraction = CGFloat(fabsf(Float(offset.y / 200)))
            self.percent = fmin(fraction, 1.0)
            
            if let fromImageView = self.fromImageView {
                let currentLocation = recognizer.location(in: nil)
                let originalLocation = CGPoint(x: currentLocation.x - offset.x, y: currentLocation.y - offset.y)
                var percent = CGFloat(1.0)
                percent = fmax(offset.y > 0 ? 1 - self.percent : CGFloat(1), 0.5)
                let currentWidth = self.fromRect.width * percent
                let currentHeight = self.fromRect.height * percent
                
                let result = CGRect(x: currentLocation.x - (originalLocation.x - self.fromRect.origin.x) * percent,
                                    y: currentLocation.y - (originalLocation.y - self.fromRect.origin.y) * percent,
                                    width: currentWidth,
                                    height: currentHeight)
                fromImageView.frame = (fromImageView.superview?.convert(result, from: nil))!
                
                if offset.y < 0 {
                    self.percent = -self.percent
                }
                
                if !self.gallery.isStatusBarHidden {
                    if UIApplication.shared.isStatusBarHidden && self.percent > 0.04 {
                        UIApplication.shared.isStatusBarHidden = false
                    } else if !UIApplication.shared.isStatusBarHidden && self.percent < 0.04 {
                        UIApplication.shared.isStatusBarHidden = true
                    }
                }
                
                self.colorAnimation()
            }
        case .ended,
             .cancelled:
            self.isInteracting = false
            let shouldComplete = self.percent > 0.5
            if !shouldComplete || recognizer.state == .cancelled {
                if let fromImageView = self.fromImageView {
                    UIView.animate(withDuration: 0.3, animations: { 
                        fromImageView.frame = self.fromRect
                        fromImageView.superview?.superview?.backgroundColor = UIColor.black
                        self.gallery.view.backgroundColor = UIColor.black
                        
                        if !self.gallery.isStatusBarHidden && !UIApplication.shared.isStatusBarHidden {
                            UIApplication.shared.setStatusBarHidden(true, with: .fade)
                        }
                        
                    }) { (finished) in
                        self.toImageView?.isHidden = false
                    }
                }
            } else {
                self.gallery.dismissGallery()
                self.finish()
            }
            self.fromImageView = nil
            self.percent = 0
{'fraction_non_alphanumeric': 0.07988603988603989, 'fraction_numerical': 0.008433048433048432, 'mean_word_length': 1.5189437428243397, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '533066', 'n_tokens_mistral': 2364, 'n_tokens_neox': 2125, 'n_words': 433}