Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.60 KB | None | 0 0
  1.  
  2. public protocol CellRepresentable {
  3.     static var reuseIdentifier: String { get }
  4. }
  5.  
  6. public extension CellRepresentable {
  7.     static var reuseIdentifier: String {
  8.         return String(describing: Self.self)
  9.     }
  10. }
  11.  
  12. public typealias ViewModelConfigurable = Configurable & AnyConfigurable
  13.  
  14. public protocol Configurable {
  15.     associatedtype ViewModel
  16.     func configure(with model: ViewModel)
  17. }
  18.  
  19. public protocol AnyConfigurable {
  20.     func configure(with model: Any)
  21. }
  22.  
  23. public extension AnyConfigurable where Self: Configurable {
  24.     func configure(with model: Any) {
  25.         guard let viewModel = model as? ViewModel else { return }
  26.         configure(with: viewModel)
  27.     }
  28. }
  29.  
  30. // Создает дата сурс, способный извлекать события из ячеек таблицы (damn good, right?)
  31. /// Для этого возвращает промежуточное замыкание в котором имеем объект содержащий эмиттер (иными словами - rx-subject)
  32. /// Содержащий интересующие нас события из ячеек
  33. /// В этом замыкании необходимо обработать эмиттер как правило построить биндинг
  34. /// S - тип возвращаемого ивента
  35. func eventEmittableDataSource<T: SectionModelType, S>()
  36.     -> (_ emittersHandler: @escaping (S) -> Void)
  37.     -> RxTableViewSectionedReloadDataSource<T> {
  38.         return { emittersHandler in
  39.             let dataSource = RxTableViewSectionedReloadDataSource<T>(configureCell: { dataSource, tableView, indexPath, model -> UITableViewCell in
  40.                 guard let model = model as? CellRepresentable else { return UITableViewCell() }
  41.                 let cell = tableView.dequeueReusableCell(withIdentifier: type(of: model).reuseIdentifier, for: indexPath)
  42.                 if let configurableCell = cell as? AnyConfigurable {
  43.                     configurableCell.configure(with: model)
  44.                 }
  45.                 if let emitterCell = cell as? AnyEventsCollector {
  46.                     let events = EventsCollectableViewModel<S>(viewModel: emitterCell).collectEvents()
  47.                     emittersHandler(events)
  48.                 }
  49.                 return cell
  50.             })
  51.             return dataSource
  52.         }
  53. }
  54.  
  55. public func defaultDataSource<T: SectionModelType>() -> RxTableViewSectionedReloadDataSource<T> {
  56.     let dataSource = RxTableViewSectionedReloadDataSource<T>(configureCell: { dataSource, tableView, indexPath, model -> UITableViewCell in
  57.         guard let model = model as? CellRepresentable else { return UITableViewCell() }
  58.         let cell = tableView.dequeueReusableCell(withIdentifier: type(of: model).reuseIdentifier, for: indexPath)
  59.         if let configurableCell = cell as? AnyConfigurable {
  60.             configurableCell.configure(with: model)
  61.         }
  62.         return cell
  63.     })
  64.     return dataSource
  65. }
  66.  
  67. /// Это реализовываем во всем что может создавать события (etc. - ячейка)
  68. public typealias EventsEmitter = AnyEventsCollector & EventsCollector
  69.  
  70.  
  71. /// Внутренняя реализация
  72. /// Структура для избежания каста в дата сурсах
  73. /// или на любой принимающей стороне
  74. /// Необходимо быть осторожным с типами, если ивент не совпадет - крашится
  75. /// Тип ивента должен совпадать с тем что возвращает эмиттер
  76. struct EventsCollectableViewModel<E>: EventsCollector {
  77.     private let viewModel: AnyEventsCollector
  78.    
  79.     init(viewModel: AnyEventsCollector) {
  80.         self.viewModel = viewModel
  81.     }
  82.    
  83.     func collectEvents() -> E {
  84.         guard let event = collectEvents() as? Event else {
  85.             fatalError("Event not casted")
  86.         }
  87.         return event
  88.     }
  89. }
  90.  
  91. /// Протокол для обхода ограничения на генерик параметры
  92. /// для передачи эмиттера в функции использовать его
  93. /// После чего обернуть в структуру выше и уже у нее дергать ивенты
  94. /// Или же использовать каст из Any но это менее красиво
  95. public protocol AnyEventsCollector {
  96.     func collectEvents() -> Any
  97. }
  98.  
  99. public extension AnyEventsCollector where Self: EventsCollector {
  100.     func collectEvents() -> Any {
  101.         return collectEvents() as Event
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement