<commit_before>/*
 Copyright 2015-present the Material Components for iOS authors. All Rights Reserved.
 
 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 CatalogByConvention

import MaterialComponents.MaterialFlexibleHeader
import MaterialComponents.MaterialLibraryInfo
import MaterialComponents.MaterialShadowElevations
import MaterialComponents.MaterialShadowLayer
import MaterialComponents.MaterialThemes
import MaterialComponents.MaterialTypography

import UIKit

class MDCDragonsController: UIViewController,
                            UITableViewDelegate,
                            UITableViewDataSource,
                            UISearchBarDelegate,
                            UIGestureRecognizerDelegate {
  
  fileprivate struct Constants {
    static let headerScrollThreshold: CGFloat = 50
    static let headerViewMaxHeight: CGFloat = 113
    static let headerViewMinHeight: CGFloat = 53
    static let bgColor = UIColor(white: 0.97, alpha: 1)
    static let headerColor = UIColor(red: 0.384, green: 0, blue: 0.933, alpha: 1.0)
    static let titleColor = UIColor(white: 0, alpha: 0.87)
    static let subtitleColor = UIColor(white: 0, alpha: 0.60)
  }
  fileprivate var cellsBySection: [[DragonCell]]
  fileprivate var searched: [DragonCell]!
  fileprivate var results: [DragonCell]!
  fileprivate var tableView: UITableView!
  fileprivate var isSearchActive = false
  
  fileprivate lazy var headerViewController = MDCFlexibleHeaderViewController()
  var headerView: HeaderView!

  init(node: CBCNode) {
    let filteredPresentable = node.children.filter { return $0.isPresentable() }
    let filteredDragons = Set(node.children).subtracting(filteredPresentable)
    cellsBySection = [filteredDragons.map { DragonCell(node: $0) },
                      filteredPresentable.map { DragonCell(node: $0) }]
    cellsBySection = cellsBySection.map { $0.sorted() { $0.node.title < $1.node.title } }
    super.init(nibName: nil, bundle: nil)
    results = getLeafNodes(node: node)
    searched = results
  }
  
  func getLeafNodes(node: CBCNode) -> [DragonCell] {
    if node.children.count == 0 {
      return [DragonCell(node: node)]
    }
    
    var cells = [DragonCell]()
    for child in node.children {
      cells += getLeafNodes(node: child)
    }
    
    return cells
  }

  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
  
  override func viewDidLoad() {
    super.viewDidLoad()
    title = "Material Dragons"
    addChildViewController(headerViewController)
    headerViewController.headerView.minMaxHeightIncludesSafeArea = false
    headerViewController.headerView.maximumHeight = Constants.headerViewMaxHeight
    headerViewController.headerView.minimumHeight = Constants.headerViewMinHeight
    tableView = UITableView(frame: self.view.bounds, style: .grouped)
    tableView.register(MDCDragonsTableViewCell.self,
                       forCellReuseIdentifier: "MDCDragonsTableViewCell")
    tableView.backgroundColor = Constants.bgColor
    tableView.delegate = self
    tableView.dataSource = self
    tableView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(tableView)
    view.backgroundColor = Constants.bgColor

    #if swift(>=3.2)
      if #available(iOS 11, *) {
        let guide = view.safeAreaLayoutGuide
        NSLayoutConstraint.activate([tableView.leftAnchor.constraint(equalTo: guide.leftAnchor),
                                    tableView.rightAnchor.constraint(equalTo: guide.rightAnchor),
                                    tableView.topAnchor.constraint(equalTo: view.topAnchor),
                                    tableView.bottomAnchor.constraint(equalTo: guide.bottomAnchor)])
      } else {
        preiOS11Constraints()
      }
    #else
      preiOS11Constraints()
    #endif

    setupHeaderView()
    let tapgesture = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
    tapgesture.delegate = self
    view.addGestureRecognizer(tapgesture)

    #if swift(>=3.2)
      if #available(iOS 11.0, *) {
        tableView.contentInsetAdjustmentBehavior = .always
      }
    #endif
  }
  
  func preiOS11Constraints() {
    view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[view]|",
                                                       options: [],
                                                       metrics: nil,
                                                       views: ["view": tableView]));
    view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[view]|",
                                                       options: [],
                                                       metrics: nil,
                                                       views: ["view": tableView]));
  }
  
  func setupHeaderView() {
    headerView = HeaderView(frame: headerViewController.headerView.bounds)
    headerView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    headerView.title.text = title!
    headerView.searchBar.delegate = self
    
    headerViewController.headerView.addSubview(headerView)
    headerViewController.headerView.forwardTouchEvents(for: headerView)
    headerViewController.headerView.backgroundColor = Constants.headerColor
    headerViewController.headerView.trackingScrollView = tableView
    view.addSubview(headerViewController.view)
    headerViewController.didMove(toParentViewController: self)
  }<commit_msg>[Catalog] Fix blank list view of samples on iOS 9 (#4773)<commit_after>/*
 Copyright 2015-present the Material Components for iOS authors. All Rights Reserved.
 
 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 CatalogByConvention

import MaterialComponents.MaterialFlexibleHeader
import MaterialComponents.MaterialLibraryInfo
import MaterialComponents.MaterialShadowElevations
import MaterialComponents.MaterialShadowLayer
import MaterialComponents.MaterialThemes
import MaterialComponents.MaterialTypography

import UIKit

class MDCDragonsController: UIViewController,
                            UITableViewDelegate,
                            UITableViewDataSource,
                            UISearchBarDelegate,
                            UIGestureRecognizerDelegate {
  
  fileprivate struct Constants {
    static let headerScrollThreshold: CGFloat = 50
    static let headerViewMaxHeight: CGFloat = 113
    static let headerViewMinHeight: CGFloat = 53
    static let bgColor = UIColor(white: 0.97, alpha: 1)
    static let headerColor = UIColor(red: 0.384, green: 0, blue: 0.933, alpha: 1.0)
    static let titleColor = UIColor(white: 0, alpha: 0.87)
    static let subtitleColor = UIColor(white: 0, alpha: 0.60)
  }
  fileprivate var cellsBySection: [[DragonCell]]
  fileprivate var searched: [DragonCell]!
  fileprivate var results: [DragonCell]!
  fileprivate var tableView: UITableView!
  fileprivate var isSearchActive = false
  
  fileprivate lazy var headerViewController = MDCFlexibleHeaderViewController()
  var headerView: HeaderView!

  init(node: CBCNode) {
    let filteredPresentable = node.children.filter { return $0.isPresentable() }
    let filteredDragons = Set(node.children).subtracting(filteredPresentable)
    cellsBySection = [filteredDragons.map { DragonCell(node: $0) },
                      filteredPresentable.map { DragonCell(node: $0) }]
    cellsBySection = cellsBySection.map { $0.sorted() { $0.node.title < $1.node.title } }
    super.init(nibName: nil, bundle: nil)
    results = getLeafNodes(node: node)
    searched = results
  }
  
  func getLeafNodes(node: CBCNode) -> [DragonCell] {
    if node.children.count == 0 {
      return [DragonCell(node: node)]
    }
    
    var cells = [DragonCell]()
    for child in node.children {
      cells += getLeafNodes(node: child)
    }
    
    return cells
  }

  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
  
  override func viewDidLoad() {
    super.viewDidLoad()
    title = "Material Dragons"
    addChildViewController(headerViewController)
    headerViewController.headerView.minMaxHeightIncludesSafeArea = false
    headerViewController.headerView.maximumHeight = Constants.headerViewMaxHeight
    headerViewController.headerView.minimumHeight = Constants.headerViewMinHeight
    tableView = UITableView(frame: self.view.bounds, style: .grouped)
    tableView.register(MDCDragonsTableViewCell.self,
                       forCellReuseIdentifier: "MDCDragonsTableViewCell")
    tableView.backgroundColor = Constants.bgColor
    tableView.delegate = self
    tableView.dataSource = self
    view.addSubview(tableView)
    view.backgroundColor = Constants.bgColor

    #if swift(>=3.2)
      if #available(iOS 11, *) {
        tableView.translatesAutoresizingMaskIntoConstraints = false

        let guide = view.safeAreaLayoutGuide
        NSLayoutConstraint.activate([tableView.leftAnchor.constraint(equalTo: guide.leftAnchor),
                                    tableView.rightAnchor.constraint(equalTo: guide.rightAnchor),
                                    tableView.topAnchor.constraint(equalTo: view.topAnchor),
                                    tableView.bottomAnchor.constraint(equalTo: guide.bottomAnchor)])
      } else {
        preiOS11Constraints()
      }
    #else
      preiOS11Constraints()
    #endif

    setupHeaderView()
    let tapgesture = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
    tapgesture.delegate = self
    view.addGestureRecognizer(tapgesture)

    #if swift(>=3.2)
      if #available(iOS 11.0, *) {
        tableView.contentInsetAdjustmentBehavior = .always
      }
    #endif
  }
  
  func preiOS11Constraints() {
    tableView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  }
  
  func setupHeaderView() {
    headerView = HeaderView(frame: headerViewController.headerView.bounds)
    headerView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    headerView.title.text = title!
    headerView.searchBar.delegate = self
    
    headerViewController.headerView.addSubview(headerView)
    headerViewController.headerView.forwardTouchEvents(for: headerView)
    headerViewController.headerView.backgroundColor = Constants.headerColor
    headerViewController.headerView.trackingScrollView = tableView
    view.addSubview(headerViewController.view)
    headerViewController.didMove(toParentViewController: self)
  }
{'fraction_non_alphanumeric': 0.07928433608138923, 'fraction_numerical': 0.01096298894930714, 'mean_word_length': 3.0944344703770197, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4431728', 'n_tokens_mistral': 3180, 'n_tokens_neox': 2982, 'n_words': 727}