DataSourceProtocols.swift 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. /// Models an object that can be used as an item in a tableview
  16. protocol Itemable {
  17. var title: String? { get }
  18. var detailTitle: String? { get }
  19. var image: UIImage? { get set }
  20. var textColor: UIColor? { get }
  21. var isEditable: Bool { get }
  22. var hasNestedContent: Bool { get set }
  23. var isChecked: Bool { get set }
  24. }
  25. /// Models an object that can be used as a section in a tableview
  26. protocol Sectionable {
  27. associatedtype Item: Itemable
  28. var headerDescription: String? { get }
  29. var footerDescription: String? { get }
  30. var items: [Item] { get set }
  31. }
  32. /// Delegate Protocol to handle cell selection and tableview scrolling
  33. protocol DataSourceProviderDelegate: AnyObject {
  34. func didSelectRowAt(_ indexPath: IndexPath, on tableView: UITableView)
  35. func tableViewDidScroll(_ tableView: UITableView)
  36. }
  37. extension DataSourceProviderDelegate {
  38. /// Provide default implementation to prevent a required implementation when conforming to this
  39. /// protocol
  40. func tableViewDidScroll(_ tableView: UITableView) {}
  41. }
  42. /// Models a type that can be used for the datasource in a DataSourceProvider
  43. protocol DataSourceProvidable {
  44. associatedtype Section: Sectionable
  45. var sections: [Section] { get }
  46. }