<commit_before>//
//  EpgGridLayout.swift
//  EPG
//
//  Created by <NAME> on 2/14/15.
//  Copyright (c) 2015 CX. All rights reserved.
//

import UIKit

class EpgGridLayout: UICollectionViewLayout
{
    let cellIdentifier = "EPGCollectionViewCell"
    let RELATIVE_HOUR : NSTimeInterval = (240.0)
    let ACTUAL_HOUR : NSTimeInterval = 3600.0

    var epgStartTime : NSDate!
    var epgEndTime : NSDate!
    var xPos:CGFloat = 0
    var yPos:CGFloat = 0
    var layoutInfo : NSMutableDictionary?
    var framesInfo : NSMutableDictionary?
    
    let weekTimeInterval : NSTimeInterval = (60 * 60 * 24 * 7)
    let TILE_WIDTH : CGFloat = 200
    let TILE_HEIGHT : CGFloat = 70
    
    var channels : [Channel]?
    
    override init()
    {
        super.init()
        setup()
    }

    required init(coder aDecoder: NSCoder)
    {
        super.init(coder: aDecoder)
        setup()
    }
    func setup()
    {
        var cal = NSCalendar.currentCalendar()
        var date = NSDate()
        date = cal.startOfDayForDate(date)
        date = cal.dateByAddingUnit(.DayCalendarUnit, value: -1, toDate: date, options: nil)!
        epgStartTime = date
        epgEndTime = epgStartTime.dateByAddingTimeInterval(weekTimeInterval)
        layoutInfo = NSMutableDictionary()
        framesInfo = NSMutableDictionary()
        channels = Channel.channels()
        calculateFramesForAllPrograms()
    }
    override func prepareLayout()
    {
        

        var newLayoutInfo = NSMutableDictionary()
        var cellLayoutInfo = NSMutableDictionary()
        if let chs = channels
        {
            var sections = chs.count
            for section in 0..<sections
            {
                var ch = chs[section]
                if let programs = ch.programs
                {
                    var numberOfItems = programs.count
                    for index in 0..<numberOfItems
                    {
                        var indexPath = NSIndexPath(forItem: index, inSection: section)
                        var itemAttributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
                        itemAttributes.frame = frameForItemAtIndexPath(indexPath)
                        cellLayoutInfo[indexPath] = itemAttributes
                    }
                }
                
            }
            newLayoutInfo[cellIdentifier] = cellLayoutInfo
            layoutInfo = newLayoutInfo
        }
        
    }

    override func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]?
    {
        var layoutAttributes  = NSMutableArray()
        layoutInfo?.enumerateKeysAndObjectsUsingBlock({ (object: AnyObject!, elementInfo: AnyObject!, stop: UnsafeMutablePointer<ObjCBool>) -> Void in
            var infoDic = elementInfo as NSDictionary!
            infoDic.enumerateKeysAndObjectsUsingBlock( { (object: AnyObject!, attributes: AnyObject!, stop: UnsafeMutablePointer<ObjCBool>) -> Void in
                var attr = attributes as UICollectionViewLayoutAttributes!
                if (CGRectIntersectsRect(rect, attributes.frame))
                {
                    layoutAttributes.addObject(attributes)
                }
                
            })
        })

        return layoutAttributes
    }
    override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes!
    {
        var attributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
        attributes.frame = self.frameForItemAtIndexPath(indexPath);
        return attributes
    }

    func sizeForProgramTile(program : Program) -> CGSize
    {
        var duartionFactor = program.duration / ACTUAL_HOUR
        var width :CGFloat = CGFloat(duartionFactor * RELATIVE_HOUR)
        return CGSizeMake(width, TILE_HEIGHT)
    }
    func calculateFramesForAllPrograms()
    {
        if let chs = channels
        {
            for i in 0..<chs.count
            {
                xPos = 0
                var ch = chs[i]
                if let programs = ch.programs
                {
                    for index in 0..<programs.count
                    {
                        var program = programs[index]
                        var tileSize = sizeForProgramTile(program)
                        var frame = CGRectMake(xPos, yPos, tileSize.width, tileSize.height)
                        var rectString = NSStringFromCGRect(frame)
                        var indexPath = NSIndexPath(forItem: index, inSection: i)
                        framesInfo![indexPath] = rectString
                        xPos = xPos+tileSize.width
                    }
                }
                yPos += TILE_HEIGHT
            }
        }
    }
    func frameForItemAtIndexPath(indexPath : NSIndexPath) -> CGRect
    {
        if let infoDic = framesInfo
        {
            var rectString = infoDic[indexPath] as String
            return CGRectFromString(rectString)
        }
        return CGRectZero
    }
    
    override func collectionViewContentSize() -> CGSize
    {
        if let chs = channels
        {
            var intervals = epgEndTime.timeIntervalSinceDate(epgStartTime)
            var numberOfHours = CGFloat(intervals / 3600)
            var width = numberOfHours * TILE_WIDTH
            var height = CGFloat(chs.count) * TILE_HEIGHT
            
            return CGSize(width: width, height: height)
            
        }
        return CGSizeZero
    }
    override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool
    {
        return !CGSizeEqualToSize(collectionView!.bounds.size, newBounds.size)
    }
    override func invalidateLayout()
    {
        xPos = 0
        yPos = 0
        calculateFramesForAllPrograms()
        super.invalidateLayout()
    }
}<commit_msg>Moving frame calculation in prepare layout method<commit_after>//
//  EpgGridLayout.swift
//  EPG
//
//  Created by <NAME> on 2/14/15.
//  Copyright (c) 2015 CX. All rights reserved.
//

import UIKit

class EpgGridLayout: UICollectionViewLayout
{
    let cellIdentifier = "EPGCollectionViewCell"
    let RELATIVE_HOUR : NSTimeInterval = (240.0)
    let ACTUAL_HOUR : NSTimeInterval = 3600.0

    var epgStartTime : NSDate!
    var epgEndTime : NSDate!
    var xPos:CGFloat = 0
    var yPos:CGFloat = 0
    var layoutInfo : NSMutableDictionary?
    var framesInfo : NSMutableDictionary?
    
    let weekTimeInterval : NSTimeInterval = (60 * 60 * 24 * 7)
    let TILE_WIDTH : CGFloat = 200
    let TILE_HEIGHT : CGFloat = 70
    
    var channels : [Channel]?
    
    override init()
    {
        super.init()
        setup()
    }

    required init(coder aDecoder: NSCoder)
    {
        super.init(coder: aDecoder)
        setup()
    }
    func setup()
    {
        var cal = NSCalendar.currentCalendar()
        var date = NSDate()
        date = cal.startOfDayForDate(date)
        date = cal.dateByAddingUnit(.DayCalendarUnit, value: -1, toDate: date, options: nil)!
        epgStartTime = date
        epgEndTime = epgStartTime.dateByAddingTimeInterval(weekTimeInterval)
        layoutInfo = NSMutableDictionary()
        framesInfo = NSMutableDictionary()
        channels = Channel.channels()
    }
    override func prepareLayout()
    {
        calculateFramesForAllPrograms()
        var newLayoutInfo = NSMutableDictionary()
        var cellLayoutInfo = NSMutableDictionary()
        if let chs = channels
        {
            var sections = chs.count
            for section in 0..<sections
            {
                var ch = chs[section]
                if let programs = ch.programs
                {
                    var numberOfItems = programs.count
                    for index in 0..<numberOfItems
                    {
                        var indexPath = NSIndexPath(forItem: index, inSection: section)
                        var itemAttributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
                        itemAttributes.frame = frameForItemAtIndexPath(indexPath)
                        cellLayoutInfo[indexPath] = itemAttributes
                    }
                }
                
            }
            newLayoutInfo[cellIdentifier] = cellLayoutInfo
            layoutInfo = newLayoutInfo
        }
        
    }

    override func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]?
    {
        var layoutAttributes  = NSMutableArray()
        layoutInfo?.enumerateKeysAndObjectsUsingBlock({ (object: AnyObject!, elementInfo: AnyObject!, stop: UnsafeMutablePointer<ObjCBool>) -> Void in
            var infoDic = elementInfo as NSDictionary!
            infoDic.enumerateKeysAndObjectsUsingBlock( { (object: AnyObject!, attributes: AnyObject!, stop: UnsafeMutablePointer<ObjCBool>) -> Void in
                var attr = attributes as UICollectionViewLayoutAttributes!
                if (CGRectIntersectsRect(rect, attributes.frame))
                {
                    layoutAttributes.addObject(attributes)
                }
                
            })
        })

        return layoutAttributes
    }
    override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes!
    {
        var attributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
        attributes.frame = self.frameForItemAtIndexPath(indexPath);
        return attributes
    }

    func sizeForProgramTile(program : Program) -> CGSize
    {
        var duartionFactor = program.duration / ACTUAL_HOUR
        var width :CGFloat = CGFloat(duartionFactor * RELATIVE_HOUR)
        return CGSizeMake(width, TILE_HEIGHT)
    }
    func calculateFramesForAllPrograms()
    {
        if let chs = channels
        {
            for i in 0..<chs.count
            {
                xPos = 0
                var ch = chs[i]
                if let programs = ch.programs
                {
                    for index in 0..<programs.count
                    {
                        var program = programs[index]
                        var tileSize = sizeForProgramTile(program)
                        var frame = CGRectMake(xPos, yPos, tileSize.width, tileSize.height)
                        var rectString = NSStringFromCGRect(frame)
                        var indexPath = NSIndexPath(forItem: index, inSection: i)
                        framesInfo![indexPath] = rectString
                        xPos = xPos+tileSize.width
                    }
                }
                yPos += TILE_HEIGHT
            }
        }
    }
    func frameForItemAtIndexPath(indexPath : NSIndexPath) -> CGRect
    {
        if let infoDic = framesInfo
        {
            var rectString = infoDic[indexPath] as String
            return CGRectFromString(rectString)
        }
        return CGRectZero
    }
    
    override func collectionViewContentSize() -> CGSize
    {
        if let chs = channels
        {
            var intervals = epgEndTime.timeIntervalSinceDate(epgStartTime)
            var numberOfHours = CGFloat(intervals / 3600)
            var width = numberOfHours * TILE_WIDTH
            var height = CGFloat(chs.count) * TILE_HEIGHT
            
            return CGSize(width: width, height: height)
            
        }
        return CGSizeZero
    }
    override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool
    {
        return !CGSizeEqualToSize(collectionView!.bounds.size, newBounds.size)
    }
    override func invalidateLayout()
    {
        xPos = 0
        yPos = 0
        super.invalidateLayout()
    }
}
{'fraction_non_alphanumeric': 0.07173576466540513, 'fraction_numerical': 0.007569241355582315, 'mean_word_length': 1.7749403341288783, 'pattern_counts': {'":': 0, '<': 17, '<?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': '3190865', 'n_tokens_mistral': 3269, 'n_tokens_neox': 3055, 'n_words': 693}