<commit_before>//
//  InfiniteScrollView.swift
//  Swimgur
//
//  Created by <NAME> on 11/15/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import Foundation
import UIKit

enum InfiniteScrollViewMoveDirection {
  case ToBeginning
  case ToEnd
}

protocol InfiniteScrollViewDelegate {
  func movedView(movedView:UIView, direction:InfiniteScrollViewMoveDirection, nextToView:UIView)
  func newCenterView(centerView:UIView)
}

class InfiniteScrollView: UIScrollView {
  var infiniteScrollViewDelegate:InfiniteScrollViewDelegate?
  
  override func layoutSubviews() {
    super.layoutSubviews()
    
    let pageWidth = self.frame.size.width
    var fractionalPage = self.contentOffset.x / pageWidth
    let page = lround(Double(fractionalPage))
    if 1 != page {
      let subviewsSorted:[UIView] = (self.subviews as [UIView]).sorted({ (first, second) -> Bool in
        return CGRectGetMinX(first.frame) < CGRectGetMinX(second.frame)
      })
      
      if 1 < page {
        self.contentOffset = CGPointMake(self.contentOffset.x-(subviews.first! as UIView).frame.size.width, self.contentOffset.y)
        for view in subviews {
          if let view = view as? UIView {
            view.frame.origin.x = view.frame.origin.x - view.frame.size.width
          }
        }
        // move front item to being in back of the last
        //tableArray.first!.galleryIndex = tableArray.last!.galleryIndex + 1
        if let infiniteScrollViewDelegate = infiniteScrollViewDelegate {
          infiniteScrollViewDelegate.movedView(subviewsSorted.first!, direction: .ToEnd, nextToView: subviewsSorted.last!)
          infiniteScrollViewDelegate.newCenterView(subviewsSorted.last!)
        }
        subviewsSorted.first!.frame.origin.x = subviewsSorted.last!.frame.origin.x + subviewsSorted.last!.frame.size.width
      } else {
        self.contentOffset = CGPointMake(self.contentOffset.x+(subviews.first! as UIView).frame.size.width, self.contentOffset.y)
        for view in subviews {
          if let view = view as? UIView {
            view.frame.origin.x = view.frame.origin.x + view.frame.size.width
          }
        }
        // move last item to being in front of the first
        //tableArray.last!.galleryIndex = tableArray.first!.galleryIndex - 1
        if let infiniteScrollViewDelegate = infiniteScrollViewDelegate {
          infiniteScrollViewDelegate.movedView(subviewsSorted.last!, direction: .ToBeginning, nextToView: subviewsSorted.first!)
          infiniteScrollViewDelegate.newCenterView(subviewsSorted.first!)
        }
        subviewsSorted.last!.frame.origin.x = subviewsSorted.first!.frame.origin.x - subviewsSorted.first!.frame.size.width
      }
      //previousPage = page
    }
    
    /*let currentOffset = self.contentOffset
    let contentWidth = self.contentSize.width
    let centerOffsetX = (contentWidth-self.bounds.size.width) / 2.0
    let distanceFromCenter = fabs(currentOffset.x - centerOffsetX)
    
    if distanceFromCenter > contentWidth/4.0 {
      self.contentOffset = CGPointMake(centerOffsetX, currentOffset.y)
      
      for view in subviews {
        
      }
    }*/
  }
}<commit_msg>Clean up and document InfiniteScrollView<commit_after>//
//  InfiniteScrollView.swift
//  Swimgur
//
//  Created by <NAME> on 11/15/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import Foundation
import UIKit

enum InfiniteScrollViewMoveDirection {
  case ToBeginning
  case ToEnd
}

protocol InfiniteScrollViewDelegate {
  func movedView(movedView:UIView, direction:InfiniteScrollViewMoveDirection, nextToView:UIView)
  func newCenterView(centerView:UIView)
}

class InfiniteScrollView: UIScrollView {
  var infiniteScrollViewDelegate:InfiniteScrollViewDelegate?
  
  override func layoutSubviews() {
    super.layoutSubviews()
    
    let pageWidth = self.frame.size.width
    var fractionalPage = self.contentOffset.x / pageWidth
    let page = lround(Double(fractionalPage))
    
    // 1 is center page as there are only 3 item
    // page will not be 1 once it has beens scrolled 50% of a page left or right
    if 1 != page {
      let subviewsSorted:[UIView] = (self.subviews as [UIView]).sorted({ (first, second) -> Bool in
        return CGRectGetMinX(first.frame) < CGRectGetMinX(second.frame)
      })
      
      if page > 1 {
        self.contentOffset = CGPointMake(self.contentOffset.x-(subviews.first! as UIView).frame.size.width, self.contentOffset.y)
        for view in subviews {
          // move each view left the width of a page
          if let view = view as? UIView {
            view.frame.origin.x = view.frame.origin.x - pageWidth
          }
        }
        // move front item to being in back of the last
        if let infiniteScrollViewDelegate = infiniteScrollViewDelegate {
          infiniteScrollViewDelegate.movedView(subviewsSorted.first!, direction: .ToEnd, nextToView: subviewsSorted.last!)
          infiniteScrollViewDelegate.newCenterView(subviewsSorted.last!)
        }
        subviewsSorted.first!.frame.origin.x = subviewsSorted.last!.frame.origin.x + subviewsSorted.last!.frame.size.width
      } else {
        self.contentOffset = CGPointMake(self.contentOffset.x+(subviews.first! as UIView).frame.size.width, self.contentOffset.y)
        // move each view right the width of a page
        for view in subviews {
          if let view = view as? UIView {
            view.frame.origin.x = view.frame.origin.x + pageWidth
          }
        }
        // move last item to being in front of the first
        if let infiniteScrollViewDelegate = infiniteScrollViewDelegate {
          infiniteScrollViewDelegate.movedView(subviewsSorted.last!, direction: .ToBeginning, nextToView: subviewsSorted.first!)
          infiniteScrollViewDelegate.newCenterView(subviewsSorted.first!)
        }
        subviewsSorted.last!.frame.origin.x = subviewsSorted.first!.frame.origin.x - subviewsSorted.first!.frame.size.width
      }
    }
  }
}
{'fraction_non_alphanumeric': 0.09399127223900638, 'fraction_numerical': 0.005874454514937899, 'mean_word_length': 2.8947712418300653, 'pattern_counts': {'":': 0, '<': 10, '<?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': '1789998', 'n_tokens_mistral': 1806, 'n_tokens_neox': 1741, 'n_words': 387}