<commit_before>//
//  ViewController.swift
//  beauties
//
//  Created by <NAME> on 15/6/27.
//  Copyright (c) 2015年 <NAME>. All rights reserved.
//

import Foundation
import UIKit
import Alamofire
import Kingfisher

class TodayViewController: UIViewController {

    var beautyImageView: UIImageView!
    
    var todayBeauty: BeautyImageEntity?
    var canBeClosed: Bool
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        canBeClosed = false
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }
    
    required init(coder aDecoder: NSCoder) {
        canBeClosed = false
        super.init(coder: aDecoder)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        beautyImageView = UIImageView()
        beautyImageView.userInteractionEnabled = true
        beautyImageView.layer.borderColor = UIColor.whiteColor().CGColor
        beautyImageView.layer.borderWidth = 10
        beautyImageView.layer.shadowOpacity = 0.5
        beautyImageView.layer.shadowColor = UIColor(red: 187 / 255.0, green: 187 / 255.0, blue: 187 / 255.0, alpha: 1).CGColor
        beautyImageView.layer.shadowOffset = CGSizeMake(2, 6)
        self.view.addSubview(beautyImageView)
        
        if canBeClosed {
            var swipeGesture = UISwipeGestureRecognizer(target: self, action: "onSwipe:")
            swipeGesture.direction = UISwipeGestureRecognizerDirection.Down
            beautyImageView.addGestureRecognizer(swipeGesture)
        }
        
        var setImage: BeautyImageEntity -> Void = {
            if let imageURLString = $0.imageUrl {
                if let imageURL = NSURL(string: imageURLString) {
                    self.beautyImageView.kf_setImageWithURL(imageURL, placeholderImage: nil, optionsInfo: nil) {
                        (image, error, cacheType, imageURL) -> () in
                        if image != nil {
                            var bgi = UIImageView(image: image!)
                            bgi.contentMode = .ScaleToFill
                            bgi.frame = self.view.bounds
                            self.view.addSubview(bgi)
                            self.view.sendSubviewToBack(bgi)
                            bgi.applyBlurEffect()
                        }
                    }
                    self.view.setNeedsLayout()
                }
            }
        };
        
        if todayBeauty != nil {
            setImage(todayBeauty!)
            return
        }
        
        NetworkUtil.getTodayImage() {
            beautyEntity in
            self.todayBeauty = beautyEntity
            if beautyEntity != nil {
                setImage(beautyEntity!)
            }
        }
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        let maxHeight = Int(self.view.bounds.height) - 100
        let maxWidth = Int(self.view.bounds.width) - 40
        
        if self.todayBeauty != nil {
            var preferWidth = maxWidth
            var preferHeight = Int(preferWidth * self.todayBeauty!.imageHeight! / self.todayBeauty!.imageWidth!)
            
            if preferHeight > maxHeight {
                preferHeight = maxHeight
                preferWidth = Int(preferHeight * self.todayBeauty!.imageWidth! / self.todayBeauty!.imageHeight!)
            }
            
            self.beautyImageView.frame = CGRect(origin: CGPointZero, size: CGSize(width: preferWidth, height: preferHeight))
        }
        self.beautyImageView.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds) - 50)
    }

    func onSwipe(sender: UISwipeGestureRecognizer) {
        if canBeClosed {
            self.dismissViewControllerAnimated(true, completion: nil)
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}
<commit_msg>Save image to album<commit_after>//
//  ViewController.swift
//  beauties
//
//  Created by <NAME> on 15/6/27.
//  Copyright (c) 2015年 <NAME>. All rights reserved.
//

import Foundation
import UIKit
import Alamofire
import Kingfisher

class TodayViewController: UIViewController {

    var beautyImageView: UIImageView!
    
    var todayBeauty: BeautyImageEntity?
    var canBeClosed: Bool
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        canBeClosed = false
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }
    
    required init(coder aDecoder: NSCoder) {
        canBeClosed = false
        super.init(coder: aDecoder)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        beautyImageView = UIImageView()
        beautyImageView.userInteractionEnabled = true
        beautyImageView.layer.borderColor = UIColor.whiteColor().CGColor
        beautyImageView.layer.borderWidth = 10
        beautyImageView.layer.shadowOpacity = 0.5
        beautyImageView.layer.shadowColor = UIColor(red: 187 / 255.0, green: 187 / 255.0, blue: 187 / 255.0, alpha: 1).CGColor
        beautyImageView.layer.shadowOffset = CGSizeMake(2, 6)
        self.view.addSubview(beautyImageView)
        
        if canBeClosed {
            var swipeGesture = UISwipeGestureRecognizer(target: self, action: "onSwipe:")
            swipeGesture.direction = UISwipeGestureRecognizerDirection.Down
            beautyImageView.addGestureRecognizer(swipeGesture)
        }
        
        var longPressGenture = UILongPressGestureRecognizer(target: self, action: "onLongPress:")
        beautyImageView.addGestureRecognizer(longPressGenture)
        
        var setImage: BeautyImageEntity -> Void = {
            if let imageURLString = $0.imageUrl {
                if let imageURL = NSURL(string: imageURLString) {
                    self.beautyImageView.kf_setImageWithURL(imageURL, placeholderImage: nil, optionsInfo: nil) {
                        (image, error, cacheType, imageURL) -> () in
                        if image != nil {
                            var bgi = UIImageView(image: image!)
                            bgi.contentMode = .ScaleToFill
                            bgi.frame = self.view.bounds
                            self.view.addSubview(bgi)
                            self.view.sendSubviewToBack(bgi)
                            bgi.applyBlurEffect()
                        }
                    }
                    self.view.setNeedsLayout()
                }
            }
        };
        
        if todayBeauty != nil {
            setImage(todayBeauty!)
            return
        }
        
        NetworkUtil.getTodayImage() {
            beautyEntity in
            self.todayBeauty = beautyEntity
            if beautyEntity != nil {
                setImage(beautyEntity!)
            }
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        let maxHeight = Int(self.view.bounds.height) - 100
        let maxWidth = Int(self.view.bounds.width) - 40
        
        if self.todayBeauty != nil {
            var preferWidth = maxWidth
            var preferHeight = Int(preferWidth * self.todayBeauty!.imageHeight! / self.todayBeauty!.imageWidth!)
            
            if preferHeight > maxHeight {
                preferHeight = maxHeight
                preferWidth = Int(preferHeight * self.todayBeauty!.imageWidth! / self.todayBeauty!.imageHeight!)
            }
            
            self.beautyImageView.frame = CGRect(origin: CGPointZero, size: CGSize(width: preferWidth, height: preferHeight))
        }
        self.beautyImageView.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds) - 50)
    }

    func onSwipe(sender: UISwipeGestureRecognizer) {
        if canBeClosed {
            self.dismissViewControllerAnimated(true, completion: nil)
        }
    }
    
    func onLongPress(sender: UILongPressGestureRecognizer) {
        if sender.state == .Began {
            let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
            
            let cancelAction = UIAlertAction(title: "取消", style: .Cancel, handler: nil)
            alertController.addAction(cancelAction)
            
            let saveAction = UIAlertAction(title: "保存", style: .Default, handler: {
                (action) -> Void in
                if let image = self.beautyImageView.image {
                    UIImageWriteToSavedPhotosAlbum(image, self, "saveImageFinished:error:contextInfo:", nil)
                }
            })
            alertController.addAction(saveAction)
            
            let shareAction = UIAlertAction(title: "分享", style: .Default, handler: {
                (action) -> Void in
                
            })
            alertController.addAction(shareAction)
            
            self.presentViewController(alertController, animated: true, completion: nil)
        }
    }
    
    func saveImageFinished(image: UIImage, error: NSErrorPointer, contextInfo: UnsafePointer<()>) {
        var message = "保存成功 (ฅ´ω`ฅ)"
        var OKTitle = "好的"
        if error != nil {
            println(error.memory)
            message = "保存失败 (´◔ ‸◔')"
            OKTitle = "好吧"
        }
        let alertController = UIAlertController(title: nil, message: message, preferredStyle: .Alert)
        
        let OKAction = UIAlertAction(title: OKTitle, style: .Default, handler: nil)
        alertController.addAction(OKAction)
        
        self.presentViewController(alertController, animated: true, completion: nil)
    }
}

{'fraction_non_alphanumeric': 0.08553575087072321, 'fraction_numerical': 0.00921942224953903, 'mean_word_length': 1.847185768445611, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1345572', 'n_tokens_mistral': 2905, 'n_tokens_neox': 2697, 'n_words': 524}