DataSourceProvider.swift 4.8 KB

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