<commit_before>//
//  IndexView.swift
//  TableViewIndex
//
//  Created by <NAME> on 02/05/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import UIKit

class IndexView : UIView {
        
    var items = [UIView]() {
        willSet {
            for item in items {
                if newValue.contains(item) {
                    continue
                }
                CATransaction.setCompletionBlock({
                    item.alpha = 1
                    
                    if (!self.items.contains(item)) {
                        item.removeFromSuperview()
                    }
                })
                item.alpha = 0
            }
        }
        
        didSet {
            updateLayout()
            
            guard var layout = layout else {
                return
            }
            layout.layoutInRect(bounds)
            
            for (index, item) in items.enumerate() {
                if oldValue.contains(item) {
                    continue
                }
                addSubview(item)
                
                UIView.performWithoutAnimation({
                    if (layout.itemFrames.count > index) {
                        item.frame = layout.itemFrames[index]
                        item.alpha = 0
                    }
                })
                item.alpha = 1
            }
        }
    }
    
    var style: Style? {
        didSet {
            updateLayout()
        }
    }
    
    private var layout: Layout?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }
    
    private func commonInit() {
        userInteractionEnabled = false
        backgroundColor = UIColor.clearColor()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        guard var layout = layout else {
            return
        }
        layout.layoutInRect(bounds)
        
        for (index, item) in items.enumerate() {
            item.frame = layout.itemFrames[index]
        }
    }
    
    private func updateLayout() {
        if style != nil && items.count > 0 {
            layout = Layout(items: items, style: style!)
            setNeedsLayout()
        }
    }
    
    override func intrinsicContentSize() -> CGSize {
        if let layout = layout {
            return layout.metrics.size
        } else {
            return CGSize()
        }
    }
    
    override func sizeThatFits(size: CGSize) -> CGSize {<commit_msg>Refactor index view update<commit_after>//
//  IndexView.swift
//  TableViewIndex
//
//  Created by <NAME> on 02/05/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import UIKit

class IndexView : UIView {
        
    var items = [UIView]() {
        didSet {
            updateWithItems(items, oldItems: oldValue)
        }
    }
    
    var style: Style? {
        didSet {
            updateLayout()
        }
    }
    
    private var layout: Layout?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }
    
    private func commonInit() {
        userInteractionEnabled = false
        backgroundColor = UIColor.clearColor()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        guard var layout = layout else {
            return
        }
        layout.layoutInRect(bounds)
        
        for (index, item) in items.enumerate() {
            item.frame = layout.itemFrames[index]
        }
    }
    
    private func updateLayout() {
        if style != nil && items.count > 0 {
            layout = Layout(items: items, style: style!)
            setNeedsLayout()
        }
    }
    
    private func updateWithItems(items: [UIView], oldItems: [UIView]) {
        for item in oldItems where !items.contains(item) {
            removeItem(item)
        }
        
        updateLayout()
        
        guard var layout = layout else {
            return
        }
        layout.layoutInRect(bounds)
        
        for (index, item) in items.enumerate() where !oldItems.contains(item) {
            addItem(item, withFrame: layout.itemFrames[index])
        }
    }
    
    private func removeItem(item: UIView) {
        // A little trickery to make item removal look nice when performed inside animation block
        CATransaction.setCompletionBlock {
            item.alpha = 1
            
            if (!self.items.contains(item)) {
                item.removeFromSuperview()
            }
        }
        item.alpha = 0
    }
    
    private func addItem(item: UIView, withFrame frame: CGRect) {
        addSubview(item)
        
        // Make item appear with nice fade in animation when layout is called inside animation block
        UIView.performWithoutAnimation {
            item.frame = frame
            item.alpha = 0
        }
        item.alpha = 1
    }
    
    override func intrinsicContentSize() -> CGSize {
        if let layout = layout {
            return layout.metrics.size
        } else {
            return CGSize()
        }
    }
    
    override func sizeThatFits(size: CGSize) -> CGSize {
{'fraction_non_alphanumeric': 0.08695652173913043, 'fraction_numerical': 0.005574136008918618, 'mean_word_length': 1.3033804022250748, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 14, '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': '398366', 'n_tokens_mistral': 1478, 'n_tokens_neox': 1411, 'n_words': 342}