<commit_before>//
//  ChatViewController.swift
//  Example-Swift
//
//  Created by <NAME> on 29/8/16.
//  Copyright © 2016 Yalantis. All rights reserved.
//

import UIKit

private let ChatDemoImageName = "imageName"
private let DemoUserName = "userName"
private let ChatDemoMessageText = "messageText"
private let ChatDemeDateText = "dateText"

private let reuseIdentifier = "ChatCollectionViewCell"

class ChatViewController: UIViewController {
    
    typealias Message = [String: String]
    
    fileprivate var messages: [Message] = []

    @IBOutlet weak var collectionView: UICollectionView!
    
    //MARK: - View & VC life cycle
    override func viewDidLoad() {
        super.viewDidLoad()

        messages = NSArray(contentsOfFile: Bundle.main.path(forResource: "YALChatDemoList", ofType: "plist")!) as! [Message]
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        prepareVisibleCellsForAnimation()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        animateVisibleCells()
    }
    
}

extension ChatViewController: UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return messages.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! ChatCollectionViewCell
        
        let message = messages[(indexPath as NSIndexPath).row]
        
        cell.configure(
            withImage: UIImage(named: message[ChatDemoImageName]!)!,
            userName: message[DemoUserName]!,
            messageText: message[ChatDemoMessageText]!,
            dateText: message[ChatDemeDateText]!
        )
        
        return cell
    }
}

extension ChatViewController: UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        return CGSize(width: view.bounds.width, height: layout.itemSize.height)
    }
}

extension ChatViewController {
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        segue.destination.hidesBottomBarWhenPushed = true
    }
}

//MARK: - Cell's animation
private extension ChatViewController {
    
    func prepareVisibleCellsForAnimation() {
        for itemIndex in 0..<collectionView.visibleCells.count {
         let cell = collectionView.cellForItem(at: IndexPath(item: itemIndex, section: 0)) as! ChatCollectionViewCell
            cell.frame = CGRect(x: -cell.bounds.width, y: cell.frame.origin.y, width: cell.bounds.width, height: cell.bounds.height)
            cell.alpha = 0
        }
    }
    
    func animateVisibleCells() {
        for itemIndex in 0..<collectionView.visibleCells.count {
            let cell = collectionView.cellForItem(at: IndexPath(item: itemIndex, section: 0)) as! ChatCollectionViewCell
            cell.alpha = 1
            UIView.animate(withDuration: 0.25, delay: Double(itemIndex) * 0.1, options: .curveEaseOut, animations: {
                cell.frame = CGRect(x: 0, y: cell.frame.origin.y, width: cell.bounds.width, height: cell.bounds.height)
            })
            
        }
    }
    
}<commit_msg>refactor code<commit_after>//
//  ChatViewController.swift
//  Example-Swift
//
//  Created by <NAME> on 29/8/16.
//  Copyright © 2016 Yalantis. All rights reserved.
//

import UIKit

private let ChatDemoImageName = "imageName"
private let DemoUserName = "userName"
private let ChatDemoMessageText = "messageText"
private let ChatDemeDateText = "dateText"

private let reuseIdentifier = "ChatCollectionViewCell"

class ChatViewController: UIViewController {
  
  typealias Message = [String: String]
  
  fileprivate var messages: [Message] = []
  fileprivate let cellAnumationDuration: Double = 0.25
  fileprivate let animationDelayStep: Double = 0.1
  
  @IBOutlet weak var collectionView: UICollectionView!
  
  //MARK: - View & VC life cycle
  override func viewDidLoad() {
    super.viewDidLoad()
    
    messages = NSArray(contentsOfFile: Bundle.main.path(forResource: "YALChatDemoList", ofType: "plist")!) as! [Message]
  }
  
  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    
    prepareVisibleCellsForAnimation()
  }
  
  override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    
    animateVisibleCells()
  }
  
}

extension ChatViewController: UICollectionViewDataSource {
  
  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return messages.count
  }
  
  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! ChatCollectionViewCell
    
    let message = messages[(indexPath as NSIndexPath).row]
    
    cell.configure(
      withImage: UIImage(named: message[ChatDemoImageName]!)!,
      userName: message[DemoUserName]!,
      messageText: message[ChatDemoMessageText]!,
      dateText: message[ChatDemeDateText]!
    )
    
    return cell
  }
}

extension ChatViewController: UICollectionViewDelegateFlowLayout {
  
  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let layout = collectionViewLayout as! UICollectionViewFlowLayout
    return CGSize(width: view.bounds.width, height: layout.itemSize.height)
  }
}

extension ChatViewController {
  
  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    segue.destination.hidesBottomBarWhenPushed = true
  }
}

//MARK: - Cell's animation

private extension ChatViewController {
  
  func prepareVisibleCellsForAnimation() {
    collectionView.visibleCells.forEach {
      $0.frame = CGRect(
        x: -$0.bounds.width,
        y: $0.frame.origin.y,
        width: $0.bounds.width,
        height: $0.bounds.height
      )
      $0.alpha = 0
    }
  }
  
  func animateVisibleCells() {
    collectionView.visibleCells.enumerated().forEach { offset, cell in
      cell.alpha = 1
      UIView.animate(
        withDuration: self.cellAnumationDuration,
        delay: Double(offset) * self.animationDelayStep,
        options: .curveEaseOut,
        animations: {
          cell.frame = CGRect(
            x: 0,
            y: cell.frame.origin.y,
            width: cell.bounds.width,
            height: cell.bounds.height
          )
      })
    }
  }
  
}
{'fraction_non_alphanumeric': 0.09090909090909091, 'fraction_numerical': 0.006349206349206349, 'mean_word_length': 3.150299401197605, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '258696', 'n_tokens_mistral': 2074, 'n_tokens_neox': 1988, 'n_words': 455}