<commit_before>//
//  PhotoFullScreenViewController.swift
//  localview
//
//  Created by <NAME> on 8/14/15.
//  Copyright (c) 2015 sparkwing. All rights reserved.
//

import UIKit

class PhotoFullScreenViewController: UIViewController, UIScrollViewDelegate {
    var containerScrollView: UIScrollView!
    @IBOutlet var fullImageView: UIImageView!
    @IBOutlet weak var commentTextView: UITextView!
    @IBOutlet weak var imageDownloadProgressView: UIProgressView!
    @IBOutlet weak var doneButton: UIButton!
    var fullImage: UIImage?
    var flickrPhoto: FlickrPhoto?
    var photoFetchState: FlickrApiUtils.PhotoFetchState?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.photoFetchState = .photosNotFetched
        if Utils.isPhone() {
            UIDevice.current.beginGeneratingDeviceOrientationNotifications()
            NotificationCenter.default.addObserver(
                self,
                selector: #selector(PhotoFullScreenViewController.orientationChanged(_:)),
                name: NSNotification.Name.UIDeviceOrientationDidChange,
                object: UIDevice.current)
            self.createViews()
        }
        // Do any additional setup after loading the view.
    }
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return UIStatusBarStyle.lightContent
    }
    func orientationChanged(_ note: Notification) {
        // we have to remove the image and scroll views and re-add them because the
        // navigation bar size changes when the orientation changes
        self.fullImageView.removeFromSuperview()
        self.containerScrollView.removeFromSuperview()
        self.createViews()
        self.setupImageInScrollView()
    }
    func createViews() -> Void {
        let viewBounds: CGRect = self.viewBounds()
        self.createScrollView(viewBounds)
        self.createImageView(viewBounds)
        self.view.bringSubview(toFront: doneButton)
    }
    func viewBounds() -> CGRect {
        var viewFrame: CGRect = CGRect.zero
        viewFrame = self.view.bounds
        return viewFrame
    }
    func createScrollView(_ viewBounds: CGRect) {
        // Create the scroll view
        self.containerScrollView = UIScrollView(frame: viewBounds)
        self.containerScrollView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        self.containerScrollView.showsHorizontalScrollIndicator = false
        self.containerScrollView.showsVerticalScrollIndicator = false
        self.containerScrollView.delegate = self
        let MaxScale: CGFloat = 1.5
        self.containerScrollView.maximumZoomScale = MaxScale
        self.view.addSubview(self.containerScrollView)
    }
    func createImageView(_ viewBounds: CGRect) {
        self.fullImageView = UIImageView(frame:viewBounds)
        self.fullImageView.contentMode = UIViewContentMode.scaleAspectFill
        self.fullImageView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        self.fullImageView.clipsToBounds = true
        self.containerScrollView.addSubview(self.fullImageView)
    }
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        if self.photoFetchState == .photosNotFetched {
            self.showPhotoAfterDownload()
        }
    }
    @IBAction func doneButtonTapped(_ sender: AnyObject) {
        presentingViewController?.dismiss(animated: true, completion: nil)
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.imageDownloadProgressView.isHidden = true
        self.doneButton.isHidden = true
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    // MARK: - Scroll View Delegate
    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return self.fullImageView
    }
    func scrollViewDidZoom(_ scrollView: UIScrollView) {
        self.centerScrollViewContent()
    }
    func showPhotoAfterDownload() {
        let sdWebImageManager: SDWebImageManager = SDWebImageManager.shared()
        /*let progressBlock: SDWebImageDownloaderProgressBlock! = {
            (receivedSize: Int, expectedSize: Int, targetUrl: NSURL) -> () in
            self.imageDownloadProgressView.isHidden = false
            let receivedSizeFloat: Float = NSNumber(value: receivedSize as Int).floatValue
            let expectedSizeFloat: Float = NSNumber(value: expectedSize as Int).floatValue
            let progress: Float = receivedSizeFloat/expectedSizeFloat
            self.imageDownloadProgressView.setProgress(progress, animated: true)
        }*/
        let progress: SDWebImageDownloaderProgressBlock = { [weak self] (receivedSize, expectedSize, targetURL) in
            guard self != nil else {
                return
            }
            self?.imageDownloadProgressView.isHidden = false
            let receivedSizeFloat: Float = NSNumber(value: receivedSize as Int).floatValue
            let expectedSizeFloat: Float = NSNumber(value: expectedSize as Int).floatValue
            let progress: Float = receivedSizeFloat/expectedSizeFloat
            self?.imageDownloadProgressView.setProgress(progress, animated: true)
        }
        /*let completionBlock: SDWebImageCompletionWithFinishedBlock! = {
            (image, error, cacheType, finished, url) -> Void in
            DispatchQueue.main.async {
                self.photoFetchState = .photosFetched
                if Utils.isPad() {
                    self.fullImageView.image = image
                } else if Utils.isPhone() {
                    self.fullImage = UIImage()
                    self.fullImage = image
                    self.setupImageInScrollView()
                }
                self.imageDownloadProgressView.isHidden = true
                self.doneButton.isHidden = false
            }
        }*/

        let completion: SDInternalCompletionBlock = { [weak self] (image, data, error, cacheType, finished, imageURL) in
            guard self != nil else {
                return
            }
            self?.photoFetchState = .photosFetched
            if Utils.isPad() {
                self?.fullImageView.image = image
            } else if Utils.isPhone() {
                self?.fullImage = UIImage()
                self?.fullImage = image
                self?.setupImageInScrollView()
            }
            self?.imageDownloadProgressView.isHidden = true
            self?.doneButton.isHidden = false

        }
        /*sdWebImageManager.download(with: self.flickrPhoto?.bigImageUrl as URL!,
                                        options: [],
                                        progress: progress,
                                        completed: completion)*/
        sdWebImageManager.loadImage(with: self.flickrPhoto?.bigImageUrl as URL?, options: [], progress: progress, completed: completion)<commit_msg>Remove commented out code in PhotoFullScreenViewController<commit_after>//
//  PhotoFullScreenViewController.swift
//  localview
//
//  Created by <NAME> on 8/14/15.
//  Copyright (c) 2015 sparkwing. All rights reserved.
//

import UIKit

class PhotoFullScreenViewController: UIViewController, UIScrollViewDelegate {
    var containerScrollView: UIScrollView!
    @IBOutlet var fullImageView: UIImageView!
    @IBOutlet weak var commentTextView: UITextView!
    @IBOutlet weak var imageDownloadProgressView: UIProgressView!
    @IBOutlet weak var doneButton: UIButton!
    var fullImage: UIImage?
    var flickrPhoto: FlickrPhoto?
    var photoFetchState: FlickrApiUtils.PhotoFetchState?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.photoFetchState = .photosNotFetched
        if Utils.isPhone() {
            UIDevice.current.beginGeneratingDeviceOrientationNotifications()
            NotificationCenter.default.addObserver(
                self,
                selector: #selector(PhotoFullScreenViewController.orientationChanged(_:)),
                name: NSNotification.Name.UIDeviceOrientationDidChange,
                object: UIDevice.current)
            self.createViews()
        }
        // Do any additional setup after loading the view.
    }
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return UIStatusBarStyle.lightContent
    }
    func orientationChanged(_ note: Notification) {
        // we have to remove the image and scroll views and re-add them because the
        // navigation bar size changes when the orientation changes
        self.fullImageView.removeFromSuperview()
        self.containerScrollView.removeFromSuperview()
        self.createViews()
        self.setupImageInScrollView()
    }
    func createViews() -> Void {
        let viewBounds: CGRect = self.viewBounds()
        self.createScrollView(viewBounds)
        self.createImageView(viewBounds)
        self.view.bringSubview(toFront: doneButton)
    }
    func viewBounds() -> CGRect {
        var viewFrame: CGRect = CGRect.zero
        viewFrame = self.view.bounds
        return viewFrame
    }
    func createScrollView(_ viewBounds: CGRect) {
        // Create the scroll view
        self.containerScrollView = UIScrollView(frame: viewBounds)
        self.containerScrollView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        self.containerScrollView.showsHorizontalScrollIndicator = false
        self.containerScrollView.showsVerticalScrollIndicator = false
        self.containerScrollView.delegate = self
        let MaxScale: CGFloat = 1.5
        self.containerScrollView.maximumZoomScale = MaxScale
        self.view.addSubview(self.containerScrollView)
    }
    func createImageView(_ viewBounds: CGRect) {
        self.fullImageView = UIImageView(frame:viewBounds)
        self.fullImageView.contentMode = UIViewContentMode.scaleAspectFill
        self.fullImageView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        self.fullImageView.clipsToBounds = true
        self.containerScrollView.addSubview(self.fullImageView)
    }
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        if self.photoFetchState == .photosNotFetched {
            self.showPhotoAfterDownload()
        }
    }
    @IBAction func doneButtonTapped(_ sender: AnyObject) {
        presentingViewController?.dismiss(animated: true, completion: nil)
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.imageDownloadProgressView.isHidden = true
        self.doneButton.isHidden = true
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    // MARK: - Scroll View Delegate
    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return self.fullImageView
    }
    func scrollViewDidZoom(_ scrollView: UIScrollView) {
        self.centerScrollViewContent()
    }
    func showPhotoAfterDownload() {
        let sdWebImageManager: SDWebImageManager = SDWebImageManager.shared()
        let progress: SDWebImageDownloaderProgressBlock = { [weak self] (receivedSize, expectedSize, targetURL) in
            guard self != nil else {
                return
            }
            DispatchQueue.main.async {
                self?.imageDownloadProgressView.isHidden = false
                let receivedSizeFloat: Float = NSNumber(value: receivedSize as Int).floatValue
                let expectedSizeFloat: Float = NSNumber(value: expectedSize as Int).floatValue
                let progress: Float = receivedSizeFloat/expectedSizeFloat
                self?.imageDownloadProgressView.setProgress(progress, animated: true)
            }
        }

        let completion: SDInternalCompletionBlock = { [weak self] (image, data, error, cacheType, finished, imageURL) in
            guard self != nil else {
                return
            }
            DispatchQueue.main.async {
                self?.photoFetchState = .photosFetched
                if Utils.isPad() {
                    self?.fullImageView.image = image
                } else if Utils.isPhone() {
                    self?.fullImage = UIImage()
                    self?.fullImage = image
                    self?.setupImageInScrollView()
                }
                self?.imageDownloadProgressView.isHidden = true
                self?.doneButton.isHidden = false
            }

        }
        sdWebImageManager.loadImage(with: self.flickrPhoto?.bigImageUrl as URL?, options: [], progress: progress, completed: completion)
{'fraction_non_alphanumeric': 0.07658833768494343, 'fraction_numerical': 0.0017406440382941688, 'mean_word_length': 2.438520130576714, 'pattern_counts': {'":': 0, '<': 5, '<?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': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3824014', 'n_tokens_mistral': 3322, 'n_tokens_neox': 3159, 'n_words': 719}