|
| 1 | +// |
| 2 | +// BaseMagazineLayoutVC.swift |
| 3 | +// Spendo |
| 4 | +// |
| 5 | +// Created by Igors Nemenonoks on 11/05/2020. |
| 6 | +// Copyright © 2020 Chili Labs. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import UIKit |
| 10 | +import MagazineLayout |
| 11 | +import RxSwift |
| 12 | +import RxCocoa |
| 13 | +import SnapKit |
| 14 | + |
| 15 | +protocol PBaseMagazineLayoutVC: UICollectionViewDelegateMagazineLayout, UICollectionViewDataSource { |
| 16 | + var sections: Binder<[MagazineLayoutSection]> { get } |
| 17 | + var collectionView: UICollectionView { get } |
| 18 | + func didSelectItem<DataType: Diffable, CellType: ConfigurableCell>() -> Observable<MagazineCellConfigurator<DataType, CellType>> |
| 19 | +} |
| 20 | + |
| 21 | +class BaseMagazineLayoutVC: UIViewController, PBaseMagazineLayoutVC { |
| 22 | + |
| 23 | + var sections: Binder<[MagazineLayoutSection]> { |
| 24 | + return Binder(self) { vc, items in |
| 25 | + vc._sections.accept(items) |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + fileprivate let _sections = BehaviorRelay(value: [MagazineLayoutSection]()) |
| 30 | + |
| 31 | + //published when cell got selected |
| 32 | + private let selectPublisher = PublishRelay<CellConfigurator>() |
| 33 | + private lazy var cellsRegistrator = CollectionViewCellsRegistrator(collectionView: self.collectionView) |
| 34 | + |
| 35 | + private let bag = DisposeBag() |
| 36 | + |
| 37 | + // Collection view |
| 38 | + lazy var collectionView: UICollectionView = { |
| 39 | + let layout = MagazineLayout() |
| 40 | + let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout) |
| 41 | + collectionView.dataSource = self |
| 42 | + collectionView.delegate = self |
| 43 | + collectionView.backgroundColor = .white |
| 44 | + collectionView.contentInsetAdjustmentBehavior = .always |
| 45 | + return collectionView |
| 46 | + }() |
| 47 | + |
| 48 | + override func viewDidLoad() { |
| 49 | + super.viewDidLoad() |
| 50 | + |
| 51 | + self.view.backgroundColor = .white |
| 52 | + self.view.insertSubview(self.collectionView, at: 0) |
| 53 | + |
| 54 | + self.collectionView.translatesAutoresizingMaskIntoConstraints = false |
| 55 | + self.collectionView.snp.makeConstraints { $0.edges.equalTo(0) } |
| 56 | + |
| 57 | + self._sections.subscribe(onNext: { [weak self] sections in |
| 58 | + self?.cellsRegistrator.registerCells(for: sections) |
| 59 | + }).disposed(by: bag) |
| 60 | + } |
| 61 | + |
| 62 | + func didSelectItem<DataType: Diffable, CellType: ConfigurableCell>() -> Observable<MagazineCellConfigurator<DataType, CellType>> { |
| 63 | + return self.selectPublisher |
| 64 | + .filter({ (configurator) -> Bool in |
| 65 | + return configurator is MagazineCellConfigurator<DataType, CellType> |
| 66 | + }) |
| 67 | + .map { configurator -> MagazineCellConfigurator<DataType, CellType> in |
| 68 | + return configurator as! MagazineCellConfigurator<DataType, CellType> |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +extension BaseMagazineLayoutVC: UICollectionViewDelegateMagazineLayout { |
| 74 | + func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeModeForItemAt indexPath: IndexPath) -> MagazineLayoutItemSizeMode { |
| 75 | + return self._sections.value[indexPath.section].items[indexPath.row].sizeMode |
| 76 | + } |
| 77 | + |
| 78 | + func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, visibilityModeForHeaderInSectionAtIndex index: Int) -> MagazineLayoutHeaderVisibilityMode { |
| 79 | + return self._sections.value[index].header.visibilityMode |
| 80 | + } |
| 81 | + |
| 82 | + func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, visibilityModeForFooterInSectionAtIndex index: Int) -> MagazineLayoutFooterVisibilityMode { |
| 83 | + return self._sections.value[index].footer.visibilityMode |
| 84 | + } |
| 85 | + // swiftlint:disable line_length |
| 86 | + func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, visibilityModeForBackgroundInSectionAtIndex index: Int) -> MagazineLayoutBackgroundVisibilityMode { |
| 87 | + return self._sections.value[index].background.visibilityMode |
| 88 | + } |
| 89 | + |
| 90 | + func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, horizontalSpacingForItemsInSectionAtIndex index: Int) -> CGFloat { |
| 91 | + return self._sections.value[index].itemsInset.right |
| 92 | + } |
| 93 | + |
| 94 | + func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, verticalSpacingForElementsInSectionAtIndex index: Int) -> CGFloat { |
| 95 | + return self._sections.value[index].itemsInset.bottom |
| 96 | + } |
| 97 | + |
| 98 | + func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetsForSectionAtIndex index: Int) -> UIEdgeInsets { |
| 99 | + return self._sections.value[index].sectionInset |
| 100 | + } |
| 101 | + |
| 102 | + func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetsForItemsInSectionAtIndex index: Int) -> UIEdgeInsets { |
| 103 | + return self._sections.value[index].itemsInset |
| 104 | + } |
| 105 | +} |
| 106 | +extension BaseMagazineLayoutVC: UICollectionViewDataSource { |
| 107 | + |
| 108 | + func numberOfSections(in collectionView: UICollectionView) -> Int { |
| 109 | + return self._sections.value.count |
| 110 | + } |
| 111 | + |
| 112 | + func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { |
| 113 | + return self._sections.value[section].items.count |
| 114 | + } |
| 115 | + |
| 116 | + func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { |
| 117 | + let cellConfigurator = self._sections.value[indexPath.section].items[indexPath.row] |
| 118 | + let cell = collectionView.dequeueReusableCell(withReuseIdentifier: type(of: cellConfigurator).cellReuseIdentifier, for: indexPath) |
| 119 | + cellConfigurator.configure(cell: cell) |
| 120 | + return cell |
| 121 | + } |
| 122 | + |
| 123 | + func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { |
| 124 | + |
| 125 | + let section = _sections.value[indexPath.section] |
| 126 | + if let sectionItem = section.header.item, kind == MagazineLayout.SupplementaryViewKind.sectionHeader { |
| 127 | + let cell = collectionView.dequeueReusableSupplementaryView(ofKind: MagazineLayout.SupplementaryViewKind.sectionHeader, |
| 128 | + withReuseIdentifier: type(of: sectionItem).cellReuseIdentifier, |
| 129 | + for: indexPath) |
| 130 | + sectionItem.configure(cell: cell) |
| 131 | + return cell |
| 132 | + } else if let sectionItem = section.footer.item, kind == MagazineLayout.SupplementaryViewKind.sectionFooter { |
| 133 | + let cell = collectionView.dequeueReusableSupplementaryView(ofKind: MagazineLayout.SupplementaryViewKind.sectionFooter, |
| 134 | + withReuseIdentifier: type(of: sectionItem).cellReuseIdentifier, |
| 135 | + for: indexPath) |
| 136 | + sectionItem.configure(cell: cell) |
| 137 | + return cell |
| 138 | + } |
| 139 | + |
| 140 | + fatalError("Not supported") |
| 141 | + } |
| 142 | + |
| 143 | + func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { |
| 144 | + self.selectPublisher.accept(self._sections.value[indexPath.section].items[indexPath.row]) |
| 145 | + self._sections.value[indexPath.section].items[indexPath.row].didSelect() |
| 146 | + } |
| 147 | +} |
0 commit comments