AutocompleteBaseViewController.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. /// All other autocomplete demo classes inherit from this class. This class optionally adds a button
  16. /// to present the autocomplete widget, and displays the results when these are selected.
  17. class AutocompleteBaseViewController: UIViewController {
  18. private lazy var textView: UITextView = {
  19. let textView = UITextView()
  20. textView.isEditable = false
  21. textView.textContainerInset = UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)
  22. return textView
  23. }()
  24. private lazy var photoButton: UIBarButtonItem = {
  25. return UIBarButtonItem(
  26. title: "Photos", style: .plain, target: self, action: #selector(showPhotos))
  27. }()
  28. private lazy var pagingPhotoView: PagingPhotoView = {
  29. let photoView = PagingPhotoView()
  30. photoView.isHidden = true
  31. return photoView
  32. }()
  33. var autocompleteConfiguration: AutocompleteConfiguration?
  34. override func viewDidLoad() {
  35. super.viewDidLoad()
  36. if #available(iOS 13.0, *) {
  37. view.backgroundColor = .systemBackground
  38. } else {
  39. view.backgroundColor = .white
  40. }
  41. view.addSubview(textView)
  42. textView.translatesAutoresizingMaskIntoConstraints = false
  43. view.addSubview(pagingPhotoView)
  44. pagingPhotoView.translatesAutoresizingMaskIntoConstraints = false
  45. let guide = view.safeAreaLayoutGuide
  46. NSLayoutConstraint.activate([
  47. textView.topAnchor.constraint(equalTo: guide.topAnchor),
  48. textView.bottomAnchor.constraint(equalTo: guide.bottomAnchor),
  49. textView.leadingAnchor.constraint(equalTo: guide.leadingAnchor),
  50. textView.trailingAnchor.constraint(equalTo: guide.trailingAnchor),
  51. ])
  52. NSLayoutConstraint.activate([
  53. pagingPhotoView.topAnchor.constraint(equalTo: guide.topAnchor),
  54. pagingPhotoView.bottomAnchor.constraint(equalTo: guide.bottomAnchor),
  55. pagingPhotoView.leadingAnchor.constraint(equalTo: guide.leadingAnchor),
  56. pagingPhotoView.trailingAnchor.constraint(equalTo: guide.trailingAnchor),
  57. ])
  58. }
  59. @objc func showPhotos() {
  60. pagingPhotoView.isHidden = false
  61. navigationItem.rightBarButtonItem = nil
  62. textView.isHidden = true
  63. }
  64. func autocompleteDidSelectPlace(_ place: GMSPlace) {
  65. let text = NSMutableAttributedString(string: place.description)
  66. text.append(NSAttributedString(string: "\nPlace status: "))
  67. text.append(NSAttributedString(string: place.isOpen().description))
  68. if let attributions = place.attributions {
  69. text.append(NSAttributedString(string: "\n\n"))
  70. text.append(attributions)
  71. }
  72. if #available(iOS 13.0, *) {
  73. text.addAttribute(.foregroundColor, value: UIColor.label, range: NSMakeRange(0, text.length))
  74. }
  75. textView.attributedText = text
  76. textView.isHidden = false
  77. pagingPhotoView.isHidden = true
  78. if let photos = place.photos, photos.count > 0 {
  79. preloadPhotoList(photos: photos)
  80. }
  81. }
  82. func autocompleteDidFail(_ error: Error) {
  83. textView.text = String(
  84. format: NSLocalizedString(
  85. "Demo.Content.Autocomplete.FailedErrorMessage",
  86. comment: "Format string for 'autocomplete failed with error' message"), error as NSError)
  87. }
  88. func autocompleteDidCancel() {
  89. textView.text = NSLocalizedString(
  90. "Demo.Content.Autocomplete.WasCanceledMessage",
  91. comment: "String for 'autocomplete canceled message'")
  92. }
  93. override func viewWillTransition(
  94. to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator
  95. ) {
  96. super.viewWillTransition(to: size, with: coordinator)
  97. pagingPhotoView.shouldRedraw = true
  98. }
  99. }
  100. extension AutocompleteBaseViewController {
  101. // Preload the photos to be displayed.
  102. func preloadPhotoList(photos: [GMSPlacePhotoMetadata]) {
  103. var attributedPhotos: [AttributedPhoto] = []
  104. let placeClient = GMSPlacesClient.shared()
  105. DispatchQueue.global().async {
  106. let downloadGroup = DispatchGroup()
  107. photos.forEach { photo in
  108. downloadGroup.enter()
  109. placeClient.loadPlacePhoto(photo) { imageData, error in
  110. if let image = imageData, let attributions = photo.attributions {
  111. attributedPhotos.append(AttributedPhoto(image: image, attributions: attributions))
  112. }
  113. downloadGroup.leave()
  114. }
  115. }
  116. downloadGroup.notify(queue: DispatchQueue.main) {
  117. self.navigationItem.rightBarButtonItem = self.photoButton
  118. self.pagingPhotoView.updatePhotos(attributedPhotos)
  119. }
  120. }
  121. }
  122. }
  123. extension GMSPlaceOpenStatus: CustomStringConvertible {
  124. public var description: String {
  125. switch self {
  126. case .open:
  127. return "Open"
  128. case .closed:
  129. return "Closed"
  130. case .unknown:
  131. return "Unknown"
  132. default:
  133. return ""
  134. }
  135. }
  136. }