DataSourceProvider.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. public func updateItem(at indexPath: IndexPath, item: Item) -> DataSource.Section.Item {
  38. return editSectionItem(at: indexPath, item: item)
  39. }
  40. // MARK: - UITableViewDataSource
  41. func numberOfSections(in tableView: UITableView) -> Int {
  42. updateBackgroundViewIfNeeded(for: tableView)
  43. return sections.count
  44. }
  45. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  46. return sections[section].items.count
  47. }
  48. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  49. return sections[section].headerDescription?.isEmpty ?? true ? 20 : 40
  50. }
  51. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  52. var label = UILabel()
  53. let section = sections[section]
  54. config(&label, for: section)
  55. return label
  56. }
  57. func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
  58. return sections[section].footerDescription
  59. }
  60. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  61. var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
  62. ?? UITableViewCell(style: .subtitle, reuseIdentifier: "cell")
  63. let item = sectionItem(at: indexPath)
  64. config(&cell, for: item)
  65. return cell
  66. }
  67. // MARK: - UITableViewDelegate
  68. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  69. tableView.deselectRow(at: indexPath, animated: true)
  70. delegate?.didSelectRowAt(indexPath, on: tableView)
  71. }
  72. func scrollViewDidScroll(_ scrollView: UIScrollView) {
  73. guard let tableView = scrollView as? UITableView else { return }
  74. delegate?.tableViewDidScroll(tableView)
  75. }
  76. // MARK: - Private Helpers
  77. private func updateBackgroundViewIfNeeded(for tableView: UITableView) {
  78. tableView.backgroundView = sections.isEmpty ? emptyView : nil
  79. tableView.isScrollEnabled = !sections.isEmpty
  80. }
  81. private func sectionItem(at indexPath: IndexPath) -> DataSource.Section.Item {
  82. return sections[indexPath.section].items[indexPath.row]
  83. }
  84. private func editSectionItem(at indexPath: IndexPath, item: Item) -> DataSource.Section.Item {
  85. sections[indexPath.section].items[indexPath.row] = item as! DataSource.Section.Item
  86. return sectionItem(at: indexPath)
  87. }
  88. private func config(_ label: inout UILabel, for section: DataSource.Section) {
  89. label.text = section.headerDescription
  90. label.textColor = .label
  91. label.font = UIFont.boldSystemFont(ofSize: 19.0)
  92. }
  93. private func config(_ cell: inout UITableViewCell, for item: DataSource.Section.Item) {
  94. cell.textLabel?.text = item.title
  95. cell.textLabel?.textColor = item.textColor
  96. cell.detailTextLabel?.text = item.detailTitle
  97. cell.detailTextLabel?.textColor = .secondaryLabel
  98. cell.imageView?.image = item.image
  99. cell.accessoryView = item.isEditable ? editableImageView() : nil
  100. cell.accessoryType = item.hasNestedContent ? .disclosureIndicator : .none
  101. cell.accessoryType = item.isChecked ? .checkmark : cell.accessoryType
  102. }
  103. private func editableImageView() -> UIImageView {
  104. let image = UIImage(systemName: "pencil")?
  105. .withTintColor(.systemOrange, renderingMode: .alwaysOriginal)
  106. return UIImageView(image: image)
  107. }
  108. }