<commit_before>//
//  GalleryViewController.swift
//  My-Simple-Instagram
//
//  Created by <NAME> on 01/11/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import UIKit
import Kingfisher
import Cards

class GalleryViewController: UIViewController {
    
    //MARK:- Outlets
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var refreshImageButton: UIBarButtonItem!
    
    //MARK:- Variables
    var images = [Image]()
    var card: CardHighlight!
    var refresher: UIRefreshControl!

    //MARK:- Override
    override func viewDidLoad() {
        super.viewDidLoad()
        setup()
        setupCollectionView()
        downloadProfile()
        downloadRecentMedia()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    //MARK:- Setup
    private func setup() {
        if #available(iOS 11.0, *) {
            self.navigationController?.navigationBar.prefersLargeTitles = true
            self.navigationItem.largeTitleDisplayMode = .automatic
            UINavigationBar.appearance().largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
        }
    }
    private func setupCollectionView() {
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        
        self.refresher = UIRefreshControl()
        self.collectionView.alwaysBounceVertical = true
        self.refresher.addTarget(self, action: #selector(RefreshDidTap(_:)), for: .valueChanged)
        
        if #available(iOS 10.0, *) {
            setRefresherWith(text: "Common" ~> "pull to refresh", andColor: .white)
            self.collectionView.refreshControl = refresher
        } else {
            setRefresherWith(text: "Common" ~> "pull to refresh", andColor: .black)
            self.collectionView.addSubview(refresher)
        }
    }
    private func myProfileButton() {
        let profileButton: LAButton = LAButton(type: .custom)
        profileButton.frame = CGRect(x: 0, y: 100, width: 34, height: 34)
        profileButton.isCircle = true
        profileButton.layer.masksToBounds = true
        profileButton.widthAnchor.constraint(equalToConstant: 34.0).isActive = true
        profileButton.heightAnchor.constraint(equalToConstant: 34.0).isActive = true
        profileButton.addTarget(self, action: #selector(openMyProfile), for: .touchUpInside)
        var stringUrl = ""
        if let myProfile = User.getMyProfile() {
            stringUrl = myProfile.profilePicture
        }
        let imageView: UIImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 34, height: 34))
        imageView.layer.masksToBounds = true
        imageView.kf.setImage(with: URL(string: stringUrl), placeholder: #imageLiteral(resourceName: "ic_account")) { (image, error, cache, url) in
            if let img = image, error == nil {
                profileButton.setImage(img, for: .normal)
                let barButtonItem = UIBarButtonItem(customView: profileButton)
                self.navigationItem.setLeftBarButton(barButtonItem, animated: true)
            }
        }
    }
    private func setRefresherWith(text: String = "", andColor c: UIColor) {
        self.refresher.tintColor = c
        self.refresher.attributedTitle = NSAttributedString(string: text, attributes: [NSAttributedStringKey.foregroundColor : c])
    }
    
    //MARK:- Helpers
    @objc private func openMyProfile() {
        let mainStoryboard = UIStoryboard(name: "Profile", bundle: Bundle.main)
        let controller: ProfileViewController = mainStoryboard.instantiateViewController(withIdentifier: "ProfileViewController") as! ProfileViewController
        self.navigationController?.pushViewController(controller, animated: true)
    }
    
    //MARK:- APIs
    private func downloadProfile() {
        API.UserClass.getMyProfile { (success) in
            guard success else {
                print("Sorry I have no profile")
                return
            }
            //Add my profile bar button
            self.myProfileButton()
        }
    }
    @objc func downloadRecentMedia(_ maxId: String = "", _ minId: String = "", _ count: String = "") {
        //ADD activity indicator
        API.UserClass.getMyMedia(maxId: maxId, minId: minId, count: count) { (success) in
            self.refresher.endRefreshing()
            guard let id = Config.id(), success else {
                print("Errore")
                return
            }
            self.images = Image.getAllImages(withUserID: id)
            self.collectionView.reloadData()
        }
    }<commit_msg>Set gallery images improvement<commit_after>//
//  GalleryViewController.swift
//  My-Simple-Instagram
//
//  Created by <NAME> on 01/11/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import UIKit
import Kingfisher
import Cards

class GalleryViewController: UIViewController {
    
    //MARK:- Outlets
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var refreshImageButton: UIBarButtonItem!
    
    //MARK:- Variables
    var images = [Image]()
    var card: CardHighlight!
    var refresher: UIRefreshControl!

    //MARK:- Override
    override func viewDidLoad() {
        super.viewDidLoad()
        setup()
        setupCollectionView()
        downloadProfile()
        downloadRecentMedia()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    //MARK:- Setup
    private func setup() {
        if #available(iOS 11.0, *) {
            self.navigationController?.navigationBar.prefersLargeTitles = true
            self.navigationItem.largeTitleDisplayMode = .automatic
            UINavigationBar.appearance().largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
        }
    }
    private func setupImages() {
        guard let id = Config.id() else {
            print("Errore")
            return
        }
        self.images = Image.getAllImages(withUserID: id)
        self.collectionView.reloadData()
    }
    private func setupCollectionView() {
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        
        self.refresher = UIRefreshControl()
        self.collectionView.alwaysBounceVertical = true
        self.refresher.addTarget(self, action: #selector(RefreshDidTap(_:)), for: .valueChanged)
        
        if #available(iOS 10.0, *) {
            setRefresherWith(text: "Common" ~> "pull to refresh", andColor: .white)
            self.collectionView.refreshControl = refresher
        } else {
            setRefresherWith(text: "Common" ~> "pull to refresh", andColor: .black)
            self.collectionView.addSubview(refresher)
        }
    }
    private func myProfileButton() {
        let profileButton: LAButton = LAButton(type: .custom)
        profileButton.frame = CGRect(x: 0, y: 100, width: 34, height: 34)
        profileButton.isCircle = true
        profileButton.layer.masksToBounds = true
        profileButton.widthAnchor.constraint(equalToConstant: 34.0).isActive = true
        profileButton.heightAnchor.constraint(equalToConstant: 34.0).isActive = true
        profileButton.addTarget(self, action: #selector(openMyProfile), for: .touchUpInside)
        var stringUrl = ""
        if let myProfile = User.getMyProfile() {
            stringUrl = myProfile.profilePicture
        }
        let imageView: UIImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 34, height: 34))
        imageView.layer.masksToBounds = true
        imageView.kf.setImage(with: URL(string: stringUrl), placeholder: #imageLiteral(resourceName: "ic_account")) { (image, error, cache, url) in
            if let img = image, error == nil {
                profileButton.setImage(img, for: .normal)
                let barButtonItem = UIBarButtonItem(customView: profileButton)
                self.navigationItem.setLeftBarButton(barButtonItem, animated: true)
            }
        }
    }
    private func setRefresherWith(text: String = "", andColor c: UIColor) {
        self.refresher.tintColor = c
        self.refresher.attributedTitle = NSAttributedString(string: text, attributes: [NSAttributedStringKey.foregroundColor : c])
    }
    
    //MARK:- Helpers
    @objc private func openMyProfile() {
        let mainStoryboard = UIStoryboard(name: "Profile", bundle: Bundle.main)
        let controller: ProfileViewController = mainStoryboard.instantiateViewController(withIdentifier: "ProfileViewController") as! ProfileViewController
        self.navigationController?.pushViewController(controller, animated: true)
    }
    
    //MARK:- APIs
    private func downloadProfile() {
        API.UserClass.getMyProfile { (success) in
            guard success else {
                print("Sorry I have no profile")
                return
            }
            //Add my profile bar button
            self.myProfileButton()
        }
    }
    @objc func downloadRecentMedia(_ maxId: String = "", _ minId: String = "", _ count: String = "") {
        //ADD activity indicator
        API.UserClass.getMyMedia(maxId: maxId, minId: minId, count: count) { (success) in
            self.refresher.endRefreshing()
            guard success else {
                print("Errore")
                return
            }
            self.setupImages()
        }
    }
{'fraction_non_alphanumeric': 0.0985797827903091, 'fraction_numerical': 0.007518796992481203, 'mean_word_length': 2.4661599710459647, 'pattern_counts': {'":': 0, '<': 7, '<?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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7487885', 'n_tokens_mistral': 2733, 'n_tokens_neox': 2609, 'n_words': 597}