<commit_before>//
//  FolderCollectionViewLayout.swift
//  SwiftBoard
//
//  Created by <NAME> on 2014-09-22.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import UIKit

class FolderCollectionViewLayout: UICollectionViewLayout {

    var itemFrames: [CGRect] = []
    var previousItemFrames: [CGRect] = []
    var numberOfItems = 0
    var hideIndexPath: NSIndexPath?
    
    override func collectionViewContentSize() -> CGSize {
        if let myCollectionView = collectionView {
            return myCollectionView.bounds.size
        } else {
            return CGSizeZero
        }
    }
    
    override func prepareLayout() {
        if collectionView == nil {
            return
        }
        
        previousItemFrames = itemFrames
        
        let myCollectionView = collectionView!
        numberOfItems = myCollectionView.numberOfItemsInSection(0)
        
        let itemsToLayout = numberOfItems > 8 ? 8 : numberOfItems
        let availableWidth = myCollectionView.bounds.width
        let itemSize = availableWidth / 3
        
        itemFrames = []
        
        for i in 0..<itemsToLayout {
            var row = CGFloat(i / 3)
            var column = CGFloat(i % 3)
            
            var rect = CGRect(x: column*itemSize, y: row*itemSize, width: itemSize, height: itemSize)
            itemFrames.append(rect)
        }
    }
    
    override func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]? {
        var attributes: [UICollectionViewLayoutAttributes] = []
        
        if let myCollectionView = collectionView {
            for itemIndex in 0..<numberOfItems {
                let indexPath = NSIndexPath(forItem: itemIndex, inSection: 0)
                attributes.append(layoutAttributesForItemAtIndexPath(indexPath))
            }
        }
        
        return attributes;
    }
    
    override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes! {
        let itemAttributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
        
        if indexPath.item < 9 {
            itemAttributes.frame = itemFrames[indexPath.item]
            
            if let hideIndex = hideIndexPath {
                if hideIndex == indexPath {
                    // I was using hidden = true before, but it had a weird interaction with the jiggle animation. The docs
                    // mention that it may not create the view when the item is hidden for optimization, so that's a possible
                    // cause?
                    itemAttributes.alpha = 0
                }
            }
        } else {
            itemAttributes.hidden = true
        }
        
        return itemAttributes
    }
    
    // When the bounds change, all items are "removed" from view then re-"added" if they're still visible. Provide their
    // original frame so the change will be animated.
    override func initialLayoutAttributesForAppearingItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
        let itemAttributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
        
        if indexPath.item < 9 {
            itemAttributes.frame = previousItemFrames[indexPath.item]
        } else {
            itemAttributes.hidden = true
        }
        
        return itemAttributes
    }
    
    override func finalLayoutAttributesForDisappearingItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
        return nil
    }
    
    override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool {
        return true
    }
    
    func indexPathToMoveSourceIndexPathLeftOfDestIndexPath(sourceIndexPath:NSIndexPath, destIndexPath:NSIndexPath) -> NSIndexPath {
        let column = destIndexPath.item % 3
        var offset = 0
        if sourceIndexPath.item < destIndexPath.item && column != 0 {
            offset = -1
        }
        
        return NSIndexPath(forItem: destIndexPath.item + offset, inSection: destIndexPath.section)
    }
    
    func indexPathToMoveSourceIndexPathRightOfDestIndexPath(sourceIndexPath:NSIndexPath, destIndexPath:NSIndexPath) -> NSIndexPath {
        var offset = 1
        if sourceIndexPath.item < destIndexPath.item {
            offset = 0
        }
        
        return NSIndexPath(forItem: destIndexPath.item + offset, inSection: destIndexPath.section)
    }
}<commit_msg>Animation fixes in folder layout<commit_after>//
//  FolderCollectionViewLayout.swift
//  SwiftBoard
//
//  Created by <NAME> on 2014-09-22.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import UIKit

class FolderCollectionViewLayout: UICollectionViewLayout {

    var itemFrames: [CGRect] = []
    var previousItemFrames: [CGRect] = []
    var numberOfItems = 0
    var hideIndexPath: NSIndexPath?
    var updating = false
    
    override func collectionViewContentSize() -> CGSize {
        if let myCollectionView = collectionView {
            return myCollectionView.bounds.size
        } else {
            return CGSizeZero
        }
    }
    
    override func prepareLayout() {
        if collectionView == nil {
            return
        }
        
        previousItemFrames = itemFrames
        
        let myCollectionView = collectionView!
        numberOfItems = myCollectionView.numberOfItemsInSection(0)
        
        let itemsToLayout = numberOfItems > 8 ? 8 : numberOfItems
        let availableWidth = myCollectionView.bounds.width
        let itemSize = availableWidth / 3
        
        itemFrames = []
        
        for i in 0..<itemsToLayout {
            var row = CGFloat(i / 3)
            var column = CGFloat(i % 3)
            
            var rect = CGRect(x: column*itemSize, y: row*itemSize, width: itemSize, height: itemSize)
            itemFrames.append(rect)
        }
    }
    
    override func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]? {
        var attributes: [UICollectionViewLayoutAttributes] = []
        
        if let myCollectionView = collectionView {
            for itemIndex in 0..<numberOfItems {
                let indexPath = NSIndexPath(forItem: itemIndex, inSection: 0)
                attributes.append(layoutAttributesForItemAtIndexPath(indexPath))
            }
        }
        
        return attributes;
    }
    
    override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes! {
        let itemAttributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
        
        if indexPath.item < 9 {
            itemAttributes.frame = itemFrames[indexPath.item]
            
            if let hideIndex = hideIndexPath {
                if hideIndex == indexPath {
                    // I was using hidden = true before, but it had a weird interaction with the jiggle animation. The docs
                    // mention that it may not create the view when the item is hidden for optimization, so that's a possible
                    // cause?
                    itemAttributes.alpha = 0
                }
            }
        } else {
            itemAttributes.hidden = true
        }
        
        return itemAttributes
    }
    
    // Set a flag to indicate we're adding/removing items. The initial/final layout attribute methods need to use
    // the default behaviour in this case... and has special code for animating a bounds change.
    override func prepareForCollectionViewUpdates(updateItems: [AnyObject]!) {
        updating = true
    }
    
    override func finalizeCollectionViewUpdates() {
        updating = false
    }
    
    // When the bounds change, all items are "removed" from view then re-"added" if they're still visible. Provide their
    // original frame so the change will be animated.
    override func initialLayoutAttributesForAppearingItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
        if (updating) {
            return super.initialLayoutAttributesForAppearingItemAtIndexPath(indexPath)
        } else {
            let itemAttributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
            
            if indexPath.item < 9 {
                itemAttributes.frame = previousItemFrames[indexPath.item]
            } else {
                itemAttributes.hidden = true
            }
            
            return itemAttributes
        }
    }
    
    override func finalLayoutAttributesForDisappearingItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
        if (updating) {
            return super.finalLayoutAttributesForDisappearingItemAtIndexPath(indexPath)
        } else {
            return nil
        }
    }
    
    override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool {
        return true
    }
    
    func indexPathToMoveSourceIndexPathLeftOfDestIndexPath(sourceIndexPath:NSIndexPath, destIndexPath:NSIndexPath) -> NSIndexPath {
        let column = destIndexPath.item % 3
        var offset = 0
        if sourceIndexPath.item < destIndexPath.item && column != 0 {
            offset = -1
        }
        
        return NSIndexPath(forItem: destIndexPath.item + offset, inSection: destIndexPath.section)
    }
    
    func indexPathToMoveSourceIndexPathRightOfDestIndexPath(sourceIndexPath:NSIndexPath, destIndexPath:NSIndexPath) -> NSIndexPath {
        var offset = 1
        if sourceIndexPath.item < destIndexPath.item {
            offset = 0
        }
        
        return NSIndexPath(forItem: destIndexPath.item + offset, inSection: destIndexPath.section)
    }
}
{'fraction_non_alphanumeric': 0.06767614338689741, 'fraction_numerical': 0.00638648537288834, 'mean_word_length': 2.113855035279025, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 25, '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': '1203986', 'n_tokens_mistral': 2555, 'n_tokens_neox': 2455, 'n_words': 635}