SampleListViewController.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2020 Google LLC. All rights reserved.
  2. //
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
  5. // file except in compliance with the License. You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software distributed under
  10. // the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  11. // ANY KIND, either express or implied. See the License for the specific language governing
  12. // permissions and limitations under the License.
  13. import GooglePlaces
  14. import UIKit
  15. /// The class which displays the list of demos.
  16. class SampleListViewController: UITableViewController {
  17. static let sampleCellIdentifier = "sampleCellIdentifier"
  18. let sampleSections = Samples.allSamples()
  19. let configuration: AutocompleteConfiguration = {
  20. let fields: [GMSPlaceField] = [
  21. .name, .placeID, .plusCode, .coordinate, .openingHours, .phoneNumber, .formattedAddress,
  22. .rating, .userRatingsTotal, .priceLevel, .types, .website, .viewport, .addressComponents,
  23. .photos, .utcOffsetMinutes, .businessStatus, .iconImageURL, .iconBackgroundColor,
  24. ]
  25. return AutocompleteConfiguration(
  26. autocompleteFilter: GMSAutocompleteFilter(),
  27. placeFields: GMSPlaceField(rawValue: fields.reduce(0) { $0 | $1.rawValue }))
  28. }()
  29. private lazy var editButton: UIBarButtonItem = {
  30. UIBarButtonItem(
  31. title: "Edit", style: .plain, target: self, action: #selector(showConfiguration))
  32. }()
  33. override func viewDidLoad() {
  34. super.viewDidLoad()
  35. tableView.register(
  36. UITableViewCell.self, forCellReuseIdentifier: SampleListViewController.sampleCellIdentifier)
  37. tableView.dataSource = self
  38. tableView.delegate = self
  39. navigationItem.rightBarButtonItem = editButton
  40. }
  41. func sample(at indexPath: IndexPath) -> Sample? {
  42. guard indexPath.section >= 0 && indexPath.section < sampleSections.count else { return nil }
  43. let section = sampleSections[indexPath.section]
  44. guard indexPath.row >= 0 && indexPath.row < section.samples.count else { return nil }
  45. return section.samples[indexPath.row]
  46. }
  47. @objc private func showConfiguration(_sender: UIButton) {
  48. navigationController?.present(
  49. ConfigurationViewController(configuration: configuration), animated: true)
  50. }
  51. // MARK: - Override UITableView
  52. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  53. guard section <= sampleSections.count else {
  54. return 0
  55. }
  56. return sampleSections[section].samples.count
  57. }
  58. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)
  59. -> UITableViewCell
  60. {
  61. let cell = tableView.dequeueReusableCell(
  62. withIdentifier: SampleListViewController.sampleCellIdentifier, for: indexPath)
  63. if let sample = sample(at: indexPath) {
  64. cell.textLabel?.text = sample.title
  65. }
  66. return cell
  67. }
  68. override func numberOfSections(in tableView: UITableView) -> Int {
  69. return sampleSections.count
  70. }
  71. override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?
  72. {
  73. guard section <= sampleSections.count else {
  74. return nil
  75. }
  76. return sampleSections[section].name
  77. }
  78. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  79. tableView.deselectRow(at: indexPath, animated: true)
  80. if let sample = sample(at: indexPath) {
  81. let viewController = sample.viewControllerClass.init()
  82. if let controller = viewController as? AutocompleteBaseViewController {
  83. controller.autocompleteConfiguration = configuration
  84. }
  85. navigationController?.pushViewController(viewController, animated: true)
  86. }
  87. }
  88. }