<commit_before>//
//  Element.swift
//  Elemental
//
//  Created by <NAME> on 2/23/17.
//  Copyright © 2017 Incipia. All rights reserved.
//

import UIKit
import Bindable

public enum InputElementState {
   case focused, unfocused
   
   // may be temporary
   public var other: InputElementState {
      switch self {
      case .focused: return .unfocused
      case .unfocused: return .focused
      }
   }
}

// optionally override the target state with the return value
public typealias InputElementAction = (_ currentState: InputElementState, _ proposedNextState: InputElementState?) -> InputElementState?

// called when the accessory button is pressed
public typealias AccessoryElementAction = () -> ()

public protocol ElementalSizeDelegate: class {
   func size(for element: Elemental, constrainedWidth width: CGFloat) -> CGSize
}

public enum ElementalLayoutPosition {
   case none, start, center, end
   
   // MARK: - Public Properties
   var horizontalScrollPosition: UICollectionViewScrollPosition {
      switch self {
      case .none: return []
      case .start: return .left
      case .center: return .centeredHorizontally
      case .end: return .right
      }
   }
   
   var verticalScrollPosition: UICollectionViewScrollPosition {
      switch self {
      case .none: return []
      case .start: return .top
      case .center: return .centeredVertically
      case .end: return .bottom
      }
   }

   // MARK: - Init
   init(scrollPosition: UICollectionViewScrollPosition) {
      switch scrollPosition {
      case UICollectionViewScrollPosition.top, UICollectionViewScrollPosition.left: self = .start
      case UICollectionViewScrollPosition.centeredVertically, UICollectionViewScrollPosition.centeredHorizontally: self = .center
      case UICollectionViewScrollPosition.bottom, UICollectionViewScrollPosition.right: self = .end
      default: self = .none
      }
   }
}

public protocol ElementalLayoutDelegate: class {
   func reloadLayout(for element: Elemental, animated: Bool, scrollPosition: ElementalLayoutPosition)
}

open class Element: Elemental {
   // MARK: - Public Properties
   open class var defaultCellID: String {
      let classString = NSStringFromClass(self)
      let elements = classString.components(separatedBy: ".")
      assert(elements.count > 0, "Failed extract class name from \(classString)")
      return "\(elements.last!)Cell"
   }
   
   open var elementID: String?
   open var cellID: String { return type(of: self).defaultCellID }
   private weak var _containerViewController: UIViewController?
   public private(set) weak var cell: ElementCell?
   
   // MARK: - Elemental Protocol
   open var elementalConfig: ElementalConfiguring
   
   open func configure(cell: UICollectionViewCell, in containerViewController: UIViewController?) {
      guard let cell = cell as? ElementCell else { fatalError() }
      self.cell = cell
      _containerViewController = containerViewController
      cell.layoutDelegate = containerViewController as? ElementalLayoutDelegate
      cell.configure(with: self)
      if let bindableCell = cell as? BindableElementCell {
         bindableCell.bind(with: self)
      }
   }
   
   open func reconfigure() {
      guard let cell = cell, cell.element === self else { return }
      configure(cell: cell, in: _containerViewController)
      cell.layoutDelegate?.reloadLayout(for: self, animated: true, scrollPosition: .none)
   }
   
   open func size(forConstrainedSize size: CGSize, layoutDirection direction: ElementalLayoutDirection) -> CGSize { fatalError() }
   
   // MARK: - Public Class Methods
   public class func form(_ elements: [Element]) -> [Elemental] {
      return elements
   }
   
   open func register(collectionView cv: UICollectionView) {
      let nib = UINib(nibName: cellID, bundle: Bundle(for: type(of:self)))
      cv.register(nib, forCellWithReuseIdentifier: cellID)
   }
   <commit_msg>Fixed a bug that prevented elements from reconfiguring when reconfigure(elements:) was called on ElementalViewController.<commit_after>//
//  Element.swift
//  Elemental
//
//  Created by <NAME> on 2/23/17.
//  Copyright © 2017 Incipia. All rights reserved.
//

import UIKit
import Bindable

public enum InputElementState {
   case focused, unfocused
   
   // may be temporary
   public var other: InputElementState {
      switch self {
      case .focused: return .unfocused
      case .unfocused: return .focused
      }
   }
}

// optionally override the target state with the return value
public typealias InputElementAction = (_ currentState: InputElementState, _ proposedNextState: InputElementState?) -> InputElementState?

// called when the accessory button is pressed
public typealias AccessoryElementAction = () -> ()

public protocol ElementalSizeDelegate: class {
   func size(for element: Elemental, constrainedWidth width: CGFloat) -> CGSize
}

public enum ElementalLayoutPosition {
   case none, start, center, end
   
   // MARK: - Public Properties
   var horizontalScrollPosition: UICollectionViewScrollPosition {
      switch self {
      case .none: return []
      case .start: return .left
      case .center: return .centeredHorizontally
      case .end: return .right
      }
   }
   
   var verticalScrollPosition: UICollectionViewScrollPosition {
      switch self {
      case .none: return []
      case .start: return .top
      case .center: return .centeredVertically
      case .end: return .bottom
      }
   }

   // MARK: - Init
   init(scrollPosition: UICollectionViewScrollPosition) {
      switch scrollPosition {
      case UICollectionViewScrollPosition.top, UICollectionViewScrollPosition.left: self = .start
      case UICollectionViewScrollPosition.centeredVertically, UICollectionViewScrollPosition.centeredHorizontally: self = .center
      case UICollectionViewScrollPosition.bottom, UICollectionViewScrollPosition.right: self = .end
      default: self = .none
      }
   }
}

public protocol ElementalLayoutDelegate: class {
   func reloadLayout(for element: Elemental, animated: Bool, scrollPosition: ElementalLayoutPosition)
}

open class Element: Elemental {
   // MARK: - Public Properties
   open class var defaultCellID: String {
      let classString = NSStringFromClass(self)
      let elements = classString.components(separatedBy: ".")
      assert(elements.count > 0, "Failed extract class name from \(classString)")
      return "\(elements.last!)Cell"
   }
   
   open var elementID: String?
   open var cellID: String { return type(of: self).defaultCellID }
   private weak var _containerViewController: UIViewController?
   public private(set) weak var cell: ElementCell?
   
   // MARK: - Elemental Protocol
   open var elementalConfig: ElementalConfiguring
   
   open func configure(cell: UICollectionViewCell, in containerViewController: UIViewController?) {
      guard let cell = cell as? ElementCell else { fatalError() }
      self.cell = cell
      _containerViewController = containerViewController
      cell.layoutDelegate = containerViewController as? ElementalLayoutDelegate
      cell.configure(with: self)
      if let bindableCell = cell as? BindableElementCell {
         bindableCell.bind(with: self)
      }
   }
   
   open func reconfigure(cell: UICollectionViewCell, for element: Elemental, in containerViewController: UIViewController?) {
      guard (element as? Element) === self else { return }
      
      reconfigure()
   }
   
   open func reconfigure() {
      guard let cell = cell, cell.element === self else { return }
      configure(cell: cell, in: _containerViewController)
      cell.layoutDelegate?.reloadLayout(for: self, animated: true, scrollPosition: .none)
   }
   
   open func size(forConstrainedSize size: CGSize, layoutDirection direction: ElementalLayoutDirection) -> CGSize { fatalError() }
   
   // MARK: - Public Class Methods
   public class func form(_ elements: [Element]) -> [Elemental] {
      return elements
   }
   
   open func register(collectionView cv: UICollectionView) {
      let nib = UINib(nibName: cellID, bundle: Bundle(for: type(of:self)))
      cv.register(nib, forCellWithReuseIdentifier: cellID)
   }
   
{'fraction_non_alphanumeric': 0.08254745866503369, 'fraction_numerical': 0.002449479485609308, 'mean_word_length': 3.3691813804173356, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, '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': '2352457', 'n_tokens_mistral': 2229, 'n_tokens_neox': 2180, 'n_words': 665}