Created
February 23, 2017 20:27
-
-
Save douglastaquary/7dd534e4ea8312c656e3c6f43c3b9dbe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| final class DashboardCollectionDataSource: NSObject, DashboardCollectionViewDataSource { | |
| var items: [Tray] = [] | |
| var hidden: [Bool] = [] | |
| var isSearchOn = false | |
| var filteredTray: [Tray] = [] | |
| weak var collectionView: UICollectionView? | |
| weak var delegate: UICollectionViewDelegate? | |
| required init(items: [Tray], collectionView: UICollectionView, delegate: UICollectionViewDelegate) { | |
| self.items = items | |
| self.collectionView = collectionView | |
| self.delegate = delegate | |
| super.init() | |
| collectionView.register(cellType: DashboardViewCell.self) | |
| collectionView.register(supplementaryViewType: DashboardSectionHeader.self, ofKind: String(describing: DashboardSectionHeader.self)) | |
| self.setupCollectionView() | |
| } | |
| func numberOfSections(in collectionView: UICollectionView) -> Int { | |
| return self.items.count | |
| } | |
| func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | |
| if isSearchOn == true && !(filteredTray[section].behaviors?.isEmpty)! { | |
| return (filteredTray[section].behaviors?.count)! | |
| } | |
| if hidden[section] { | |
| return 0 | |
| } else { | |
| return (items[section].behaviors?.count)! | |
| } | |
| } | |
| func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { | |
| let cell = collectionView.dequeueReusableCell(for: indexPath, cellType: DashboardViewCell.self) | |
| if isSearchOn == true && !(filteredTray[indexPath.section].behaviors?.isEmpty)! { | |
| let item = self.filteredTray[indexPath.section].behaviors?[indexPath.row] | |
| cell.setup(behavior: item!) | |
| return cell | |
| } else { | |
| let item = self.items[indexPath.section].behaviors?[indexPath.row] | |
| cell.setup(behavior: item!) | |
| return cell | |
| } | |
| } | |
| func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { | |
| if kind == UICollectionElementKindSectionHeader { | |
| let sectionHeaderView = collectionView.dequeueReusableSupplementaryView(ofKind: String(describing: DashboardSectionHeader.self), for: indexPath, viewType: DashboardSectionHeader.self) | |
| let tray = items[indexPath.section] | |
| sectionHeaderView.setup(with: tray) | |
| sectionHeaderView.tag = indexPath.section | |
| //scrollToSection(sectionHeaderView.tag) | |
| let tap = UITapGestureRecognizer(target: self, action: #selector(DashboardCollectionDataSource.expandableCell)) | |
| sectionHeaderView.isUserInteractionEnabled = true | |
| sectionHeaderView.addGestureRecognizer(tap) | |
| return sectionHeaderView | |
| } | |
| return UICollectionReusableView() | |
| } | |
| func updateItems(_ items: [Tray]) { | |
| self.items = items | |
| self.collectionView?.reloadData() | |
| } | |
| } | |
| class DashboardCollectionDelegate: NSObject, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { | |
| let delegate: DashboardDelegate | |
| init(_ delegate: DashboardDelegate) { | |
| self.delegate = delegate | |
| } | |
| func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { | |
| delegate.didSelectIndicator(at: indexPath) | |
| } | |
| func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { | |
| let width = collectionView.bounds.size.width - 30 | |
| return DashboardViewCell.size(for: width) | |
| } | |
| func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { | |
| return UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10) | |
| } | |
| func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { | |
| return CGSize(width: collectionView.bounds.size.width, height: 50) | |
| } | |
| func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { | |
| return 0 | |
| } | |
| func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat{ | |
| return 0 | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment