<commit_before>//
//  CustomKeyboard.swift
//  KeyboardLayoutEngine
//
//  Created by <NAME> on 11/05/16.
//  Copyright © 2016 Prototapp. All rights reserved.
//

import UIKit

// MARK: - CustomKeyboardDelegate
@objc public protocol CustomKeyboardDelegate {
  optional func customKeyboardKeyButtonPressed(customKeyboard: CustomKeyboard, key: String)
  optional func customKeyboardSpaceButtonPressed(customKeyboard: CustomKeyboard)
  optional func customKeyboardBackspaceButtonPressed(customKeyboard: CustomKeyboard)
  optional func customKeyboardGlobeButtonPressed(customKeyboard: CustomKeyboard)
  optional func customKeyboardReturnButtonPressed(customKeyboard: CustomKeyboard)
  optional func customKeyboardButtonPressed(customKeyboard: CustomKeyboard, keyboardButton: KeyboardButton)
}

// MARK: - CustomKeyboard
public class CustomKeyboard: UIView, KeyboardLayoutDelegate {
  public var uppercaseToggledLayout: KeyboardLayout!
  public var uppercaseLayout: KeyboardLayout!
  public var lowercaseLayout: KeyboardLayout!
  public var numbersLayout: KeyboardLayout!
  public var symbolsLayout: KeyboardLayout!

  public var shiftToggleInterval: NSTimeInterval = 0.5
  private var shiftToggleTimer: NSTimer?
  private var shiftCanBeToggled: Bool = false
  private var uppercaseOnce: Bool = true

  public var backspaceDeleteInterval: NSTimeInterval = 0.1
  public var backspaceAutoDeleteModeInterval: NSTimeInterval = 0.5
  private var backspaceDeleteTimer: NSTimer?
  private var backspaceAutoDeleteModeTimer: NSTimer?

  private var lastLetterLayout: KeyboardLayout?
  private(set) var currentLayout: KeyboardLayout! {
    didSet {
      oldValue?.delegate = nil
      oldValue?.removeFromSuperview()
      currentLayout.delegate = self
      addSubview(currentLayout)
    }
  }

  public weak var delegate: CustomKeyboardDelegate?

  // MARK: Init
  public init() {
    super.init(frame: CGRect.zero)
    defaultInit()
  }

  public override init(frame: CGRect) {
    super.init(frame: frame)
    defaultInit()
  }

  public required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    defaultInit()
  }

  private func defaultInit() {
    uppercaseToggledLayout = CustomKeyboardLayout.UppercaseToggled.keyboardLayout
    uppercaseLayout = CustomKeyboardLayout.Uppercase.keyboardLayout
    lowercaseLayout = CustomKeyboardLayout.Lowercase.keyboardLayout
    numbersLayout = CustomKeyboardLayout.Numbers.keyboardLayout
    symbolsLayout = CustomKeyboardLayout.Symbols.keyboardLayout
    
    currentLayout = uppercaseLayout
    addSubview(currentLayout)
  }

  public override func layoutSubviews() {
    super.layoutSubviews()
    currentLayout?.frame = CGRect(
      x: 0,
      y: 0,
      width: frame.size.width,
      height: frame.size.height)
  }

  // MARK: Backspace Auto Delete
  private func startBackspaceAutoDeleteModeTimer() {
    backspaceAutoDeleteModeTimer = NSTimer.scheduledTimerWithTimeInterval(
      backspaceAutoDeleteModeInterval,
      target: self,
      selector: #selector(CustomKeyboard.startBackspaceAutoDeleteMode),
      userInfo: nil,
      repeats: false)
  }

  private func startBackspaceDeleteTimer() {
    backspaceDeleteTimer = NSTimer.scheduledTimerWithTimeInterval(
      backspaceDeleteInterval,
      target: self,
      selector: #selector(CustomKeyboard.autoDelete),
      userInfo: nil,
      repeats: true)
  }

  private func invalidateBackspaceAutoDeleteModeTimer() {
    backspaceAutoDeleteModeTimer?.invalidate()
    backspaceAutoDeleteModeTimer = nil
  }<commit_msg>Add reload function<commit_after>//
//  CustomKeyboard.swift
//  KeyboardLayoutEngine
//
//  Created by <NAME> on 11/05/16.
//  Copyright © 2016 Prototapp. All rights reserved.
//

import UIKit

// MARK: - CustomKeyboardDelegate
@objc public protocol CustomKeyboardDelegate {
  optional func customKeyboardKeyButtonPressed(customKeyboard: CustomKeyboard, key: String)
  optional func customKeyboardSpaceButtonPressed(customKeyboard: CustomKeyboard)
  optional func customKeyboardBackspaceButtonPressed(customKeyboard: CustomKeyboard)
  optional func customKeyboardGlobeButtonPressed(customKeyboard: CustomKeyboard)
  optional func customKeyboardReturnButtonPressed(customKeyboard: CustomKeyboard)
  optional func customKeyboardButtonPressed(customKeyboard: CustomKeyboard, keyboardButton: KeyboardButton)
}

// MARK: - CustomKeyboard
public class CustomKeyboard: UIView, KeyboardLayoutDelegate {
  public var shiftToggleInterval: NSTimeInterval = 0.5
  private var shiftToggleTimer: NSTimer?
  private var shiftCanBeToggled: Bool = false
  private var uppercaseOnce: Bool = true

  public var backspaceDeleteInterval: NSTimeInterval = 0.1
  public var backspaceAutoDeleteModeInterval: NSTimeInterval = 0.5
  private var backspaceDeleteTimer: NSTimer?
  private var backspaceAutoDeleteModeTimer: NSTimer?

  public var uppercaseToggledLayout: KeyboardLayout! {
    didSet {
      guard currentLayout != nil && oldValue != nil else { return }
      if currentLayout == oldValue {
        currentLayout = uppercaseToggledLayout
      }
    }
  }

  public var uppercaseLayout: KeyboardLayout! {
    didSet {
      guard currentLayout != nil && oldValue != nil else { return }
      if currentLayout == oldValue {
        currentLayout = uppercaseLayout
      }
    }
  }

  public var lowercaseLayout: KeyboardLayout! {
    didSet {
      guard currentLayout != nil && oldValue != nil else { return }
      if currentLayout == oldValue {
        currentLayout = lowercaseLayout
      }
    }
  }

  public var numbersLayout: KeyboardLayout! {
    didSet {
      guard currentLayout != nil && oldValue != nil else { return }
      if currentLayout == oldValue {
        currentLayout = numbersLayout
      }
    }
  }

  public var symbolsLayout: KeyboardLayout! {
    didSet {
      guard currentLayout != nil && oldValue != nil else { return }
      if currentLayout == oldValue {
        currentLayout = symbolsLayout
      }
    }
  }

  private var lastLetterLayout: KeyboardLayout?
  private(set) var currentLayout: KeyboardLayout! {
    didSet {
      oldValue?.delegate = nil
      oldValue?.removeFromSuperview()
      currentLayout.delegate = self
      addSubview(currentLayout)
    }
  }

  public weak var delegate: CustomKeyboardDelegate?

  // MARK: Init
  public init() {
    super.init(frame: CGRect.zero)
    defaultInit()
  }

  public override init(frame: CGRect) {
    super.init(frame: frame)
    defaultInit()
  }

  public required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    defaultInit()
  }

  private func defaultInit() {
    reload()
    currentLayout = uppercaseLayout
    addSubview(currentLayout)
  }

  // MARK: Layout
  public override func layoutSubviews() {
    super.layoutSubviews()
    currentLayout?.frame = CGRect(
      x: 0,
      y: 0,
      width: frame.size.width,
      height: frame.size.height)
  }

  // MARK: Reload
  public func reload() {
    uppercaseToggledLayout = CustomKeyboardLayout.UppercaseToggled.keyboardLayout
    uppercaseLayout = CustomKeyboardLayout.Uppercase.keyboardLayout
    lowercaseLayout = CustomKeyboardLayout.Lowercase.keyboardLayout
    numbersLayout = CustomKeyboardLayout.Numbers.keyboardLayout
    symbolsLayout = CustomKeyboardLayout.Symbols.keyboardLayout
  }

  // MARK: Backspace Auto Delete
  private func startBackspaceAutoDeleteModeTimer() {
    backspaceAutoDeleteModeTimer = NSTimer.scheduledTimerWithTimeInterval(
      backspaceAutoDeleteModeInterval,
      target: self,
      selector: #selector(CustomKeyboard.startBackspaceAutoDeleteMode),
      userInfo: nil,
      repeats: false)
  }

  private func startBackspaceDeleteTimer() {
    backspaceDeleteTimer = NSTimer.scheduledTimerWithTimeInterval(
      backspaceDeleteInterval,
      target: self,
      selector: #selector(CustomKeyboard.autoDelete),
      userInfo: nil,
      repeats: true)
  }

  private func invalidateBackspaceAutoDeleteModeTimer() {
    backspaceAutoDeleteModeTimer?.invalidate()
    backspaceAutoDeleteModeTimer = nil
  }
{'fraction_non_alphanumeric': 0.07363715385570595, 'fraction_numerical': 0.004470383707934931, 'mean_word_length': 3.9931804091754493, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '518912', 'n_tokens_mistral': 2267, 'n_tokens_neox': 2255, 'n_words': 527}