<commit_before>//
//  PhotoCollectionView.swift
//  PhotoCollectionView
//
//  Created by <NAME> on 7/6/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import UIKit

@objc public protocol PhotoCollectionViewDataSource: NSObjectProtocol {
    func numPhotos(in photoCollectionView: PhotoCollectionView) -> Int
    
    @objc optional func photoColletionView(_ photoCollectionView: PhotoCollectionView, imageAt index: Int) -> UIImage?
    @objc optional func photoCollectionView(_ photoCollectionView: PhotoCollectionView, urlImageAt index: Int) -> URL?
}

@objc public protocol PhotoCollectionViewDelegate: NSObjectProtocol {
    @objc optional func photoCollectionView(_ photoCollectionView: PhotoCollectionView, didSelectImageAt index: Int)
    @objc optional func photoCollectionView(_ photoCollectionView: PhotoCollectionView, didCreated photoView: PhotoView, at index: Int) -> Void
}

@IBDesignable
open class PhotoCollectionView: UIView {
    var margin: CGFloat = 1
    var maxImage = 4
    var photoViews: [PhotoView] = []
    
    weak open var dataSource: PhotoCollectionViewDataSource?
    weak open var delegate: PhotoCollectionViewDelegate?
    
    @IBInspectable open var moreTextColor: UIColor! = UIColor.white
    @IBInspectable open var moreTextBackgroundColor: UIColor! = UIColor(white: 0.2, alpha: 0.6)
    open var moreTextFont: UIFont! = UIFont.systemFont(ofSize: 17)
    
    override open var bounds: CGRect {
        didSet {
            reloadData()
        }
    }
    
    override open func removeFromSuperview() {
        super.removeFromSuperview()
        clear()
    }
    
    func clear() {
        while photoViews.count > 0 {
            let view = photoViews.removeFirst()
            view.removeFromSuperview()
        }
    }
    
    open func reloadData() {
        clear()
        guard let dataSource = dataSource else {
            return
        }
        let numImage = dataSource.numPhotos(in: self)
        guard numImage > 0 else {
            return
        }
        
        let numShow = min(maxImage, numImage)
        
        let size = bounds.size
        var isVertical = false
        var nextOffset = CGPoint.zero
        
        for i in 0..<numShow {
            let image = dataSource.photoColletionView?(self, imageAt: i)
            if let image = image {
                if i == 0 {
                    isVertical = image.size.width < image.size.height
                }
            }
            var itemSize = CGSize.zero
            let offset = nextOffset
            
            if numShow == 1 {
                itemSize.width = size.width
                itemSize.height = size.height
            } else {
                let remainCount = CGFloat(numShow - 1)
                if isVertical {
                    if i == 0 {<commit_msg>Add images to caches image in photoView<commit_after>//
//  PhotoCollectionView.swift
//  PhotoCollectionView
//
//  Created by <NAME> on 7/6/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import UIKit

@objc public protocol PhotoCollectionViewDataSource: NSObjectProtocol {
    func numPhotos(in photoCollectionView: PhotoCollectionView) -> Int
    
    @objc optional func photoColletionView(_ photoCollectionView: PhotoCollectionView, imageAt index: Int) -> UIImage?
    @objc optional func photoCollectionView(_ photoCollectionView: PhotoCollectionView, urlImageAt index: Int) -> URL?
}

@objc public protocol PhotoCollectionViewDelegate: NSObjectProtocol {
    @objc optional func photoCollectionView(_ photoCollectionView: PhotoCollectionView, didSelectImageAt index: Int)
    @objc optional func photoCollectionView(_ photoCollectionView: PhotoCollectionView, didCreated photoView: PhotoView, at index: Int) -> Void
}

@IBDesignable
open class PhotoCollectionView: UIView {
    var margin: CGFloat = 1
    var maxImage = 4
    var photoViews: [PhotoView] = []
    var images: [UIImage] = []
    
    weak open var dataSource: PhotoCollectionViewDataSource?
    weak open var delegate: PhotoCollectionViewDelegate?
    
    @IBInspectable open var moreTextColor: UIColor! = UIColor.white
    @IBInspectable open var moreTextBackgroundColor: UIColor! = UIColor(white: 0.2, alpha: 0.6)
    open var moreTextFont: UIFont! = UIFont.systemFont(ofSize: 17)
    
    override open var bounds: CGRect {
        didSet {
            reloadData()
        }
    }
    
    override open func removeFromSuperview() {
        super.removeFromSuperview()
        clear()
    }
    
    func clear() {
        while photoViews.count > 0 {
            let view = photoViews.removeFirst()
            view.removeFromSuperview()
        }
        images.removeAll()
    }
    
    open func reloadData() {
        clear()
        guard let dataSource = dataSource else {
            return
        }
        let numImage = dataSource.numPhotos(in: self)
        guard numImage > 0 else {
            return
        }
        
        let numShow = min(maxImage, numImage)
        
        let size = bounds.size
        var isVertical = false
        var nextOffset = CGPoint.zero
        
        for i in 0..<numShow {
            let image = dataSource.photoColletionView?(self, imageAt: i)
            if let image = image {
                images.append(image)
                if i == 0 {
                    isVertical = image.size.width < image.size.height
                }
            }
            var itemSize = CGSize.zero
            let offset = nextOffset
            
            if numShow == 1 {
                itemSize.width = size.width
                itemSize.height = size.height
            } else {
                let remainCount = CGFloat(numShow - 1)
                if isVertical {
                    if i == 0 {
{'fraction_non_alphanumeric': 0.07704320666319625, 'fraction_numerical': 0.007981953843484296, 'mean_word_length': 2.0889603429796355, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, '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': '1763841', 'n_tokens_mistral': 1600, 'n_tokens_neox': 1532, 'n_words': 443}