<commit_before>//
//  Copyright (c) 2016 Google Inc.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

import UIKit
import Firebase
import FirebaseDatabaseUI
import FirebaseAuthUI

/// View controller demonstrating using a FirebaseCollectionViewDataSource
/// to populate a collection view with chat messages. The relevant code
/// is in the call to `collectionViewDataSource.populateCellWithBlock`.
class FIRChatViewController: UIViewController, UICollectionViewDelegateFlowLayout {
  // All of the error handling in this controller is done with `fatalError`;
  // please don't copy paste it into your production code.
  
  fileprivate static let reuseIdentifier = "FIRChatCollectionViewCell"
  
  @IBOutlet fileprivate var collectionView: UICollectionView!
  @IBOutlet fileprivate var textView: UITextView! {
    didSet {
      textView.layer.borderColor = UIColor.gray.withAlphaComponent(0.5).cgColor
      textView.layer.borderWidth = 1
      textView.layer.cornerRadius = 8
      textView.layer.masksToBounds = true
    }
  }
  @IBOutlet fileprivate var sendButton: UIButton!
  
  /// Used to shift view contents up when the keyboard appears.
  @IBOutlet fileprivate var bottomConstraint: NSLayoutConstraint!
  
  fileprivate let auth = FIRAuth.auth()
  fileprivate let chatReference = FIRDatabase.database().reference().child("swift_demo-chat")
  
  fileprivate var collectionViewDataSource: FirebaseCollectionViewDataSource!
  
  fileprivate var user: FIRUser?
  fileprivate var query: FIRDatabaseQuery?
  
  fileprivate var authStateListenerHandle: FIRAuthStateDidChangeListenerHandle?
  
  // MARK: - Interesting stuff
  
  override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    
    self.authStateListenerHandle = self.auth?.addStateDidChangeListener { (auth, user) in
      self.user = user
      self.query = self.chatReference.queryLimited(toLast: 50)
      
      // The initializer called below--though it takes a collection view--
      // doesn't actually set the collection view's data source, so if
      // we don't set it before trying to populate our view our app will crash.
      self.collectionViewDataSource = FirebaseCollectionViewDataSource(query: self.query!,
        prototypeReuseIdentifier: FIRChatViewController.reuseIdentifier,
        view: self.collectionView)
      self.collectionView.dataSource = self.collectionViewDataSource
      
      self.collectionViewDataSource.populateCell { (anyCell, data) in
        guard let cell = anyCell as? FIRChatCollectionViewCell else {
          fatalError("Unexpected collection view cell class \(anyCell.self)")
        }
        
        let chat = Chat(snapshot: data as! FIRDataSnapshot)!
        cell.populateCellWithChat(chat, user: self.user, maxWidth: self.view.frame.size.width)
      }
      
      // FirebaseArray has a delegate method `childAdded` that could be used here,
      // but unfortunately FirebaseCollectionViewDataSource uses the FirebaseArray
      // delegate methods to update its own internal state, so in order to scroll
      // on new insertions we still need to use the query directly.
      self.query!.observe(.childAdded, with: { [unowned self] _ in
        self.scrollToBottom(animated: true)
        })
    }
    
    self.auth?.signInAnonymously { (user, error) in<commit_msg>use new database syntax in sample<commit_after>//
//  Copyright (c) 2016 Google Inc.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//

import UIKit
import Firebase
import FirebaseDatabaseUI
import FirebaseAuthUI

/// View controller demonstrating using a FirebaseCollectionViewDataSource
/// to populate a collection view with chat messages. The relevant code
/// is in the call to `collectionViewDataSource.populateCellWithBlock`.
class FIRChatViewController: UIViewController, UICollectionViewDelegateFlowLayout {
  // All of the error handling in this controller is done with `fatalError`;
  // please don't copy paste it into your production code.
  
  fileprivate static let reuseIdentifier = "FIRChatCollectionViewCell"
  
  @IBOutlet fileprivate var collectionView: UICollectionView!
  @IBOutlet fileprivate var textView: UITextView! {
    didSet {
      textView.layer.borderColor = UIColor.gray.withAlphaComponent(0.5).cgColor
      textView.layer.borderWidth = 1
      textView.layer.cornerRadius = 8
      textView.layer.masksToBounds = true
    }
  }
  @IBOutlet fileprivate var sendButton: UIButton!
  
  /// Used to shift view contents up when the keyboard appears.
  @IBOutlet fileprivate var bottomConstraint: NSLayoutConstraint!
  
  fileprivate let auth = FIRAuth.auth()
  fileprivate let chatReference = FIRDatabase.database().reference().child("swift_demo-chat")
  
  fileprivate var collectionViewDataSource: FirebaseCollectionViewDataSource!
  
  fileprivate var user: FIRUser?
  fileprivate var query: FIRDatabaseQuery?
  
  fileprivate var authStateListenerHandle: FIRAuthStateDidChangeListenerHandle?
  
  // MARK: - Interesting stuff
  
  override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    
    self.authStateListenerHandle = self.auth?.addStateDidChangeListener { (auth, user) in
      self.user = user
      self.query = self.chatReference.queryLimited(toLast: 50)

      self.collectionViewDataSource =
        FirebaseCollectionViewDataSource(query: self.query!,
                                         view: self.collectionView,
                                         populateCell: { (view, indexPath, snap) -> UICollectionViewCell in
        let cell = view.dequeueReusableCell(withReuseIdentifier: FIRChatViewController.reuseIdentifier,
                                            for: indexPath) as! FIRChatCollectionViewCell
        let chat = Chat(snapshot: snap)!
        cell.populateCellWithChat(chat, user: self.user, maxWidth: self.view.frame.size.width)
        return cell
      })
      self.collectionView.dataSource = self.collectionViewDataSource

      // FirebaseArray has a delegate method `childAdded` that could be used here,
      // but unfortunately FirebaseCollectionViewDataSource uses the FirebaseArray
      // delegate methods to update its own internal state, so in order to scroll
      // on new insertions we still need to use the query directly.
      self.query!.observe(.childAdded, with: { [unowned self] _ in
        self.scrollToBottom(animated: true)
        })
    }
    
    self.auth?.signInAnonymously { (user, error) in
{'fraction_non_alphanumeric': 0.07408890668802563, 'fraction_numerical': 0.003737818715792284, 'mean_word_length': 3.3331405436668593, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '681840', 'n_tokens_mistral': 1993, 'n_tokens_neox': 1933, 'n_words': 664}