DataSourceProvider.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2020 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // 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
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import UIKit
  15. /// Abstracts away view/view controller based tableview configuration by acting as a tableview's
  16. /// datasource and delegate
  17. class DataSourceProvider<DataSource: DataSourceProvidable>: NSObject, UITableViewDataSource,
  18. UITableViewDelegate {
  19. weak var delegate: DataSourceProviderDelegate?
  20. private var emptyView: UIView?
  21. private var sections: [DataSource.Section]!
  22. convenience init(dataSource: [DataSource.Section]?, emptyStateView: UIView? = nil,
  23. tableView: UITableView? = nil) {
  24. self.init()
  25. emptyView = emptyStateView
  26. sections = dataSource ?? [DataSource.Section]()
  27. tableView?.dataSource = self
  28. tableView?.delegate = self
  29. }
  30. // MARK: Public Section and Item Getters
  31. public func section(at indexPath: IndexPath) -> DataSource.Section {
  32. return sections[indexPath.section]
  33. }
  34. public func item(at indexPath: IndexPath) -> DataSource.Section.Item {
  35. return sectionItem(at: indexPath)
  36. }
  37. // MARK: - UITableViewDataSource
  38. func numberOfSections(in tableView: UITableView) -> Int {
  39. updateBackgroundViewIfNeeded(for: tableView)
  40. return sections.count
  41. }
  42. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  43. return sections[section].items.count
  44. }
  45. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  46. return sections[section].headerDescription?.isEmpty ?? true ? 20 : 40
  47. }
  48. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  49. var label = UILabel()
  50. let section = sections[section]
  51. config(&label, for: section)
  52. return label
  53. }
  54. func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
  55. return sections[section].footerDescription
  56. }
  57. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  58. var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
  59. ?? UITableViewCell(style: .subtitle, reuseIdentifier: "cell")
  60. let item = sectionItem(at: indexPath)
  61. config(&cell, for: item)
  62. return cell
  63. }
  64. // MARK: - UITableViewDelegate
  65. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  66. tableView.deselectRow(at: indexPath, animated: true)
  67. delegate?.didSelectRowAt(indexPath, on: tableView)
  68. }
  69. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  70. guard let tableView = scrollView as? UITableView else { return }
  71. delegate?.tableViewDidScroll(tableView)
  72. }
  73. // MARK: - Private Helpers
  74. private func updateBackgroundViewIfNeeded(for tableView: UITableView) {
  75. tableView.backgroundView = sections.isEmpty ? emptyView : nil
  76. tableView.isScrollEnabled = !sections.isEmpty
  77. }
  78. private func sectionItem(at indexPath: IndexPath) -> DataSource.Section.Item {
  79. return sections[indexPath.section].items[indexPath.row]
  80. }
  81. private func config(_ label: inout UILabel, for section: DataSource.Section) {
  82. label.text = section.headerDescription
  83. label.textColor = .label
  84. label.font = UIFont.boldSystemFont(ofSize: 19.0)
  85. }
  86. private func config(_ cell: inout UITableViewCell, for item: DataSource.Section.Item) {
  87. cell.textLabel?.text = item.title
  88. cell.textLabel?.textColor = item.textColor
  89. cell.detailTextLabel?.text = item.detailTitle
  90. cell.detailTextLabel?.textColor = .secondaryLabel
  91. cell.imageView?.image = item.image
  92. cell.accessoryView = item.isEditable ? editableImageView() : nil
  93. cell.accessoryType = item.hasNestedContent ? .disclosureIndicator : .none
  94. cell.accessoryType = item.isChecked ? .checkmark : cell.accessoryType
  95. }
  96. private func editableImageView() -> UIImageView {
  97. let image = UIImage(systemName: "pencil")?
  98. .withTintColor(.systemOrange, renderingMode: .alwaysOriginal)
  99. return UIImageView(image: image)
  100. }
  101. }