<commit_before>//
//  DefaultTableCellDataDelegate.swift
//  Pods
//
//  Created by <NAME> on 3/15/17.
//
//

import Foundation

open class DefaultTableCellDataDelegate : NSObject, TableCellDataDelegate {
   
   var surveyQuestions : SurveyQuestions
   var tableView : UITableView
   var submitCompletionHandler: (Data?, URLResponse?, Error?) -> Void
   var activeTextView: UIView?
   
   public init(_ surveyQuestions : SurveyQuestions, tableView: UITableView, submitCompletionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) {
      self.surveyQuestions = surveyQuestions
      self.tableView = tableView
      self.submitCompletionHandler = submitCompletionHandler
      super.init()
      registerForKeyboardNotifications()
   }
   
   public func update(updateId: String, data: Any) {
      TableUIUpdater.updateTable(surveyQuestions.update(id: updateId, data: data), tableView: tableView)
   }
   
   public func markFinished(updateId: String) {
      TableUIUpdater.updateTable(surveyQuestions.markFinished(updateId: updateId), tableView: tableView)
   }
   
   public func updateUI() {
      self.tableView.beginUpdates()
      self.tableView.endUpdates()
   }
   
   open func submitData() {
      let session = URLSession.shared
      let url = URL(string: surveyQuestions.submitUrl())
      var request = URLRequest(url: url!)
      let jsonData = try? JSONSerialization.data(withJSONObject: surveyQuestions.submitJson())
      request.httpMethod = "POST"
      request.httpBody = jsonData
      let task = session.dataTask(with: request as URLRequest, completionHandler: self.submitCompletionHandler)
      task.resume()
   }

   open func getSurveyQuestions() -> SurveyQuestions {
      return surveyQuestions
   }
   
   // MARK: keyboard methods
   
   public func updateActiveTextView(_ view: UIView) {
      self.activeTextView = view
   }
   
   func registerForKeyboardNotifications() {
      NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(_:)), name: Notification.Name.UIKeyboardDidShow, object: nil)
      NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(_:)), name: Notification.Name.UIKeyboardWillHide, object: nil)
   }
   
   func keyboardWasShown(_ notification: Notification) {
      let info = notification.userInfo
      if let kbSize = (info?[UIKeyboardFrameBeginUserInfoKey] as? CGRect)?.size {
         let contentInsets = UIEdgeInsetsMake(0, 0, kbSize.height, 0);
         self.tableView.contentInset = contentInsets
         self.tableView.scrollIndicatorInsets = contentInsets
         
         if activeTextView != nil {
            var aRect = self.tableView.frame
            aRect.size.height -= kbSize.height
            let viewFrame = activeTextView!.convert(activeTextView!.bounds, to: self.tableView)
            if (!aRect.contains(viewFrame.origin)) {
               self.tableView.scrollRectToVisible(viewFrame, animated: true)
            }
         }
      }
   }
   
   func keyboardWillBeHidden(_ notification: Notification) {
      let contentInsets = UIEdgeInsets.zero
      self.tableView.contentInset = contentInsets
      self.tableView.scrollIndicatorInsets = contentInsets
   }
}
<commit_msg>Respect existing content inset for top when adjusting bottom inset when keyboard hidden/shown<commit_after>//
//  DefaultTableCellDataDelegate.swift
//  Pods
//
//  Created by <NAME> on 3/15/17.
//
//

import Foundation

open class DefaultTableCellDataDelegate : NSObject, TableCellDataDelegate {
   
   var surveyQuestions : SurveyQuestions
   var tableView : UITableView
   var submitCompletionHandler: (Data?, URLResponse?, Error?) -> Void
   var activeTextView: UIView?
   
   public init(_ surveyQuestions : SurveyQuestions, tableView: UITableView, submitCompletionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) {
      self.surveyQuestions = surveyQuestions
      self.tableView = tableView
      self.submitCompletionHandler = submitCompletionHandler
      super.init()
      registerForKeyboardNotifications()
   }
   
   public func update(updateId: String, data: Any) {
      TableUIUpdater.updateTable(surveyQuestions.update(id: updateId, data: data), tableView: tableView)
   }
   
   public func markFinished(updateId: String) {
      TableUIUpdater.updateTable(surveyQuestions.markFinished(updateId: updateId), tableView: tableView)
   }
   
   public func updateUI() {
      self.tableView.beginUpdates()
      self.tableView.endUpdates()
   }
   
   open func submitData() {
      let session = URLSession.shared
      let url = URL(string: surveyQuestions.submitUrl())
      var request = URLRequest(url: url!)
      let jsonData = try? JSONSerialization.data(withJSONObject: surveyQuestions.submitJson())
      request.httpMethod = "POST"
      request.httpBody = jsonData
      let task = session.dataTask(with: request as URLRequest, completionHandler: self.submitCompletionHandler)
      task.resume()
   }

   open func getSurveyQuestions() -> SurveyQuestions {
      return surveyQuestions
   }
   
   // MARK: keyboard methods
   
   public func updateActiveTextView(_ view: UIView) {
      self.activeTextView = view
   }
   
   func registerForKeyboardNotifications() {
      NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(_:)), name: Notification.Name.UIKeyboardDidShow, object: nil)
      NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(_:)), name: Notification.Name.UIKeyboardWillHide, object: nil)
   }
   
   func keyboardWasShown(_ notification: Notification) {
      let info = notification.userInfo
      if let kbSize = (info?[UIKeyboardFrameBeginUserInfoKey] as? CGRect)?.size {
         let contentInsets = UIEdgeInsetsMake(self.tableView.contentInset.top, 0, kbSize.height, 0)
         self.tableView.contentInset = contentInsets
         self.tableView.scrollIndicatorInsets = contentInsets
         
         if activeTextView != nil {
            var aRect = self.tableView.frame
            aRect.size.height -= kbSize.height
            let viewFrame = activeTextView!.convert(activeTextView!.bounds, to: self.tableView)
            if (!aRect.contains(viewFrame.origin)) {
               self.tableView.scrollRectToVisible(viewFrame, animated: true)
            }
         }
      }
   }
   
   func keyboardWillBeHidden(_ notification: Notification) {
      let contentInsets = UIEdgeInsetsMake(self.tableView.contentInset.top, 0, 0, 0)
      self.tableView.contentInset = contentInsets
      self.tableView.scrollIndicatorInsets = contentInsets
   }
}

{'fraction_non_alphanumeric': 0.09132143937103114, 'fraction_numerical': 0.002721499848805564, 'mean_word_length': 3.340551181102362, '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': '4260290', 'n_tokens_mistral': 1885, 'n_tokens_neox': 1819, 'n_words': 397}