<commit_before>import UIKit
import SwiftyJSON

open class MediaItemsController: InfiniteCollectionViewController {
  public class var SegueIdentifier: String { return  "MediaItems" }

  public var callSeque = false

  var CellIdentifier: String { return  "MediaItemCell" }
  var HeaderViewIdentifier: String { return  "MediaItemsHeader" }
  var localizer = Localizer("com.rubikon.TVSetKit")

  static public func instantiate() -> Self {
    let bundle = Bundle(identifier: "com.rubikon.TVSetKit")!

    return AppStoryboard.instantiateController("Player", bundle: bundle, viewControllerClass: self)
  }

  override open func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.title = ""
    clearsSelectionOnViewWillAppear = false

    collectionView?.backgroundView = activityIndicatorView
    adapter.spinner = PlainSpinner(activityIndicatorView)

    loadInitialData()
  }

  // MARK: UICollectionViewDataSource

  override open func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
  }

  override open func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return items.count
  }

  override open func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CellIdentifier, for: indexPath) as! MediaItemCell

    let item = items[indexPath.row]

    let localizedName = localizer.localize(item.name!)

    cell.configureCell(item: item, localizedName: localizedName, target: self, action: #selector(self.tapped(_:)))

    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tapped(_:)))

    tapGesture.allowedPressTypes = [NSNumber(value: UIPressType.playPause.rawValue)]

    cell.addGestureRecognizer(tapGesture)

    let itemSize = (collectionView.collectionViewLayout as! UICollectionViewFlowLayout).itemSize

    cell.thumb.frame = CGRect(x: 10, y: 0, width: itemSize.width, height: itemSize.height)
    cell.title.frame = CGRect(x: 10, y: itemSize.height, width: itemSize.width, height: 100)

    return cell
  }

  override open func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let currentOffset = scrollView.contentOffset.y
    let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height
    let deltaOffset = maximumOffset - currentOffset

    if deltaOffset <= 0 {
      if adapter.nextPageAvailable(dataCount: items.count, index: items.count-1) {
        loadMoreData(items.count-1)
      }
    }
  }

  override open func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let mediaItem = items[indexPath.row]

    let view = collectionView.cellForItem(at: indexPath)

    if mediaItem.isContainer() {
      navigate(from: mediaItem, view: view!)
    }
    else {
      performSegue(withIdentifier: VideoPlayerController.SegueIdentifier, sender: view)
    }
  }

  func tapped(_ gesture: UITapGestureRecognizer) {
    let indexPath = collectionView?.indexPath(for: gesture.view as! UICollectionViewCell)
    let mediaItem = items[indexPath!.row]

    if mediaItem.isContainer() {
      navigate(from: mediaItem, view: view)
    }
    else {
      performSegue(withIdentifier: VideoPlayerController.SegueIdentifier, sender: gesture.view!)
    }
  }

  func navigate(from mediaItem: MediaItem, view: UIView) {
    let type = mediaItem.type

    if type != nil && type!.isEmpty {
      mediaItem.resolveType()
    }

    if mediaItem.isContainer() {
      if mediaItem.isAudioContainer() {
        performSegue(withIdentifier: AudioItemsController.SegueIdentifier, sender: view)
      }
      else {
        if callSeque {
          performSegue(withIdentifier: MediaItemsController.SegueIdentifier, sender: view)
        }<commit_msg>refactor code<commit_after>import UIKit
import SwiftyJSON

open class MediaItemsController: InfiniteCollectionViewController {
  public class var SegueIdentifier: String { return  "MediaItems" }

  public var callSeque = false

  var CellIdentifier: String { return  "MediaItemCell" }
  var HeaderViewIdentifier: String { return  "MediaItemsHeader" }
  var localizer = Localizer("com.rubikon.TVSetKit")

  static public func instantiate() -> Self {
    let bundle = Bundle(identifier: "com.rubikon.TVSetKit")!

    return AppStoryboard.instantiateController("Player", bundle: bundle, viewControllerClass: self)
  }

  override open func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.title = ""
    clearsSelectionOnViewWillAppear = false

    collectionView?.backgroundView = activityIndicatorView
    adapter.spinner = PlainSpinner(activityIndicatorView)

    loadInitialData()
  }

  // MARK: UICollectionViewDataSource

  override open func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
  }

  override open func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return items.count
  }

  override open func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CellIdentifier, for: indexPath) as! MediaItemCell

    let item = items[indexPath.row]

    let localizedName = localizer.localize(item.name!)

    var action: Selector?

#if os(tvOS)
    action = #selector(self.tapped(_:))
    let tapGesture = UITapGestureRecognizer(target: self, action: action)

    tapGesture.allowedPressTypes = [NSNumber(value: UIPressType.playPause.rawValue)]

    cell.addGestureRecognizer(tapGesture)
#endif

    cell.configureCell(item: item, localizedName: localizedName, target: self, action: action)

    let itemSize = (collectionView.collectionViewLayout as! UICollectionViewFlowLayout).itemSize

    cell.thumb.frame = CGRect(x: 10, y: 0, width: itemSize.width, height: itemSize.height)
    cell.title.frame = CGRect(x: 10, y: itemSize.height, width: itemSize.width, height: 100)

    return cell
  }

  override open func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let currentOffset = scrollView.contentOffset.y
    let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height
    let deltaOffset = maximumOffset - currentOffset

    if deltaOffset <= 0 {
      if adapter.nextPageAvailable(dataCount: items.count, index: items.count-1) {
        loadMoreData(items.count-1)
      }
    }
  }

  override open func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let mediaItem = items[indexPath.row]

    let view = collectionView.cellForItem(at: indexPath)

    if mediaItem.isContainer() {
      navigate(from: mediaItem, view: view!)
    }
    else {
      performSegue(withIdentifier: VideoPlayerController.SegueIdentifier, sender: view)
    }
  }

#if os(tvOS)
  func tapped(_ gesture: UITapGestureRecognizer) {
    let indexPath = collectionView?.indexPath(for: gesture.view as! UICollectionViewCell)
    let mediaItem = items[indexPath!.row]

    if mediaItem.isContainer() {
      navigate(from: mediaItem, view: view)
    }
    else {
      performSegue(withIdentifier: VideoPlayerController.SegueIdentifier, sender: gesture.view!)
    }
  }
#endif

  func navigate(from mediaItem: MediaItem, view: UIView) {
    let type = mediaItem.type

    if type != nil && type!.isEmpty {
      mediaItem.resolveType()
    }

    if mediaItem.isContainer() {
      if mediaItem.isAudioContainer() {
        performSegue(withIdentifier: AudioItemsController.SegueIdentifier, sender: view)
      }
      else {
        if callSeque {
//          performSegue(withIdentifier: MediaItemsController.SegueIdentifier, sender: view)
          let BundleIdentifier = "com.rubikon.EtvnetSite-iOS"

          let bundle = Bundle(identifier: BundleIdentifier)!

          let storyboard = UIStoryboard(name: "Etvnet-iOS", bundle: bundle)

          let destination = storyboard.instantiateViewController(withIdentifier: "MediaItemsController") as! MediaItemsController

          destination.callSeque = callSeque

          let newAdapter = adapter.clone()
          newAdapter.selectedItem = mediaItem

          newAdapter.parentId = mediaItem.id
          newAdapter.parentName = mediaItem.name
          newAdapter.isContainer = true

          destination.adapter = newAdapter

          destination.collectionView?.collectionViewLayout = adapter.buildLayout()!

          navigationController!.pushViewController(destination, animated: true)
        }
{'fraction_non_alphanumeric': 0.08849146440599233, 'fraction_numerical': 0.002787132737196609, 'mean_word_length': 3.95512082853855, 'pattern_counts': {'":': 0, '<': 5, '<?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': '2211806', 'n_tokens_mistral': 2530, 'n_tokens_neox': 2411, 'n_words': 563}