<commit_before>//
//  UITextField+Signal.swift
//  SessionSwift
//
//  Created by aleksey on 24.10.15.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import UIKit

extension UITextField {
  
  public var textSignal: Observable<String> {
    let textSignal = signalForControlEvents(.EditingChanged).map { ($0 as! UITextField).text! }
    let textObservable = textSignal.observable()
    let onClearSignal = sig_delegate.clearSignal.map { [weak self] in self?.text ?? "" }.filter { $0 != nil }.map { $0! }
    
    let observable = Observable<String>(text ?? "")
    textObservable.value == text ?? ""
    
    Signals.merge([textObservable, onClearSignal]).bindTo(observable)
    
    return observable
  }
  
  public var editingSignal: Observable<Bool> {
    let observable = Signals.merge([editingBeginSignal.map { true }, editingEndSignal.map { false }]).observable()
    observable.value = editing
    
    return observable
  }
  
  public var editingBeginSignal: Pipe<Void> {
    return signalForControlEvents(.EditingDidBegin).map { _ in return Void() } as! Pipe<Void>
  }
  
  public var editingEndSignal: Pipe<Void> {
    return signalForControlEvents(.EditingDidEnd).map { _ in return Void() } as! Pipe<Void>
  }
  
  public var returnSignal: Pipe<Void> {
    return signalForControlEvents([.EditingDidEndOnExit]).map { _ in return Void() } as! Pipe<Void>
  }
  
}

private class TextFieldDelegate: NSObject, UITextFieldDelegate {
 
  let clearSignal = Pipe<Void>()
  
  private static var DelegateHandler: Int = 0
  
  @objc private func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    return true
  }
  
  @objc private func textFieldShouldClear(textField: UITextField) -> Bool {
    dispatch_async(dispatch_get_main_queue()) { self.clearSignal.sendNext() }
    
    return true
  }
  
}

private extension UITextField {
  
  var sig_delegate: TextFieldDelegate {
    var delegate = objc_getAssociatedObject(self, &TextFieldDelegate.DelegateHandler) as? TextFieldDelegate
    if (delegate == nil) {<commit_msg>fixed crash in text signal<commit_after>//
//  UITextField+Signal.swift
//  SessionSwift
//
//  Created by aleksey on 24.10.15.
//  Copyright © 2015 <NAME>. All rights reserved.
//

import UIKit

extension UITextField {
  
  public var textSignal: Observable<String> {
    let textSignal = signalForControlEvents(.EditingChanged).map { ($0 as! UITextField).text ?? "" }
    let textObservable = textSignal.observable()
    textObservable.value = text ?? ""
    let onClearSignal = sig_delegate.clearSignal.map { [weak self] in self?.text ?? "" }.filter { $0 != nil }.map { $0! }
    
    let observable = Observable<String>(text ?? "")
    
    
    Signals.merge([textObservable, onClearSignal]).bindTo(observable)
    
    return observable
  }
  
  public var editingSignal: Observable<Bool> {
    let observable = Signals.merge([editingBeginSignal.map { true }, editingEndSignal.map { false }]).observable()
    observable.value = editing
    
    return observable
  }
  
  public var editingBeginSignal: Pipe<Void> {
    return signalForControlEvents(.EditingDidBegin).map { _ in return Void() } as! Pipe<Void>
  }
  
  public var editingEndSignal: Pipe<Void> {
    return signalForControlEvents(.EditingDidEnd).map { _ in return Void() } as! Pipe<Void>
  }
  
  public var returnSignal: Pipe<Void> {
    return signalForControlEvents([.EditingDidEndOnExit]).map { _ in return Void() } as! Pipe<Void>
  }
  
}

private class TextFieldDelegate: NSObject, UITextFieldDelegate {
  
  let clearSignal = Pipe<Void>()
  
  private static var DelegateHandler: Int = 0
  
  @objc private func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    return true
  }
  
  @objc private func textFieldShouldClear(textField: UITextField) -> Bool {
    dispatch_async(dispatch_get_main_queue()) { self.clearSignal.sendNext() }
    
    return true
  }
  
}

private extension UITextField {
  
  var sig_delegate: TextFieldDelegate {
    var delegate = objc_getAssociatedObject(self, &TextFieldDelegate.DelegateHandler) as? TextFieldDelegate
    if (delegate == nil) {
{'fraction_non_alphanumeric': 0.10846498467342608, 'fraction_numerical': 0.006602216458382457, 'mean_word_length': 3.729096989966555, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 29, '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': '2392472', 'n_tokens_mistral': 1346, 'n_tokens_neox': 1295, 'n_words': 312}