Created
November 17, 2017 13:43
-
-
Save Dimillian/dc308b0ca61b30be0e84990db606b3a0 to your computer and use it in GitHub Desktop.
BooksCollectionInterfaceProvider.swift
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
// | |
// BooksCollectionProvider.swift | |
// Glose | |
// | |
// Created by Thomas Ricouard on 10/11/2017. | |
// Copyright © 2017 Thomas Ricouard. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
protocol BooksCollectionInterfaceProviderDelegate: class { | |
func booksCollectionInterfaceProviderDidChangeDisplayMode(provider: BooksCollectionInterfaceProvider, | |
displayMode: BooksCollectionInterfaceProvider.DisplayMode) | |
func booksCollectionInterfaceProviderScrollViewDidScroll(scrollView: UIScrollView) | |
func booksCollectionInterfaceProviderCollectionView(collectionView: UICollectionView, | |
layout collectionViewLayout: UICollectionViewLayout, | |
referenceSizeForHeaderInSection section: Int) -> CGSize | |
func booksCollectionInterfaceProviderCollectionView(collectionView: UICollectionView, | |
viewForSupplementaryElementOfKind kind: String, | |
at indexPath: IndexPath) -> UICollectionReusableView | |
} | |
class BooksCollectionInterfaceProvider: NSObject { | |
weak var parentController: UIViewController? | |
weak var collectionView: UICollectionView? | |
var datasource: [ObjectId] = [] { | |
didSet { | |
collectionView?.reloadData() | |
} | |
} | |
weak var delegate: BooksCollectionInterfaceProviderDelegate? | |
enum DisplayMode { | |
case grid, list | |
func margin() -> CGFloat { | |
switch self { | |
case .grid: | |
return 10 | |
default: | |
return 0 | |
} | |
} | |
} | |
var displayMode = DisplayMode.grid { | |
didSet { | |
delegate?.booksCollectionInterfaceProviderDidChangeDisplayMode(provider: self, displayMode: displayMode) | |
collectionView?.reloadData() | |
} | |
} | |
init(parentController: UIViewController, collectionView: UICollectionView) { | |
self.parentController = parentController | |
self.collectionView = collectionView | |
super.init() | |
self.collectionView?.dataSource = self | |
self.collectionView?.delegate = self | |
registerBooksCells() | |
self.parentController?.registerForPreviewing(with: self, sourceView: self.collectionView!) | |
collectionView.reloadData() | |
} | |
func registerBooksCells() { | |
var nib = UINib(nibName: "BookCollectionViewCell", bundle: Bundle.main) | |
collectionView?.register(nib, forCellWithReuseIdentifier: BookCollectionViewCell.id) | |
nib = UINib(nibName: "ListBookCollectionViewCell", bundle: Bundle.main) | |
collectionView?.register(nib, forCellWithReuseIdentifier: ListBookCollectionViewCell.id) | |
} | |
} | |
extension BooksCollectionInterfaceProvider: UICollectionViewDelegate, | |
UICollectionViewDataSource, | |
UICollectionViewDelegateFlowLayout { | |
func numberOfSections(in collectionView: UICollectionView) -> Int { | |
return 1 | |
} | |
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { | |
return datasource.count | |
} | |
func collectionView(_ collectionView: UICollectionView, | |
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { | |
if displayMode == .grid { | |
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: BookCollectionViewCell.id, | |
for: indexPath) as? BookCollectionViewCell else { | |
return UICollectionViewCell() | |
} | |
cell.bookId = datasource[indexPath.row] | |
return cell | |
} | |
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ListBookCollectionViewCell.id, | |
for: indexPath) as? ListBookCollectionViewCell else { | |
return UICollectionViewCell() | |
} | |
cell.delegate = self | |
cell.bookId = datasource[indexPath.row] | |
return cell | |
} | |
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { | |
if let vc = Storyboards.Controllers.book.instantiate() as? BookViewController { | |
vc.bookId = datasource[indexPath.row] | |
parentController?.navigationController?.pushViewController(vc, animated: true) | |
} | |
} | |
func collectionView(_ collectionView: UICollectionView, layout | |
collectionViewLayout: UICollectionViewLayout, | |
sizeForItemAt indexPath: IndexPath) -> CGSize { | |
if displayMode == .grid { | |
let size = // Your logic for your grid cell size. In my case I want each cell to be 1/3 of the width of the screen - my margin. | |
return size | |
} | |
return CGSize(width: collectionView.frame.size.width, height: ListBookCollectionViewCell.height) | |
} | |
func collectionView(_ collectionView: UICollectionView, | |
layout collectionViewLayout: UICollectionViewLayout, | |
minimumLineSpacingForSectionAt section: Int) -> CGFloat { | |
return displayMode.margin() | |
} | |
func collectionView(_ collectionView: UICollectionView, | |
layout collectionViewLayout: UICollectionViewLayout, | |
minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { | |
return displayMode.margin() | |
} | |
func collectionView(_ collectionView: UICollectionView, | |
layout collectionViewLayout: UICollectionViewLayout, | |
insetForSectionAt section: Int) -> UIEdgeInsets { | |
return UIEdgeInsets(top: 0, | |
left: displayMode.margin(), | |
bottom: 0, | |
right: displayMode.margin()) | |
} | |
func collectionView(_ collectionView: UICollectionView, | |
layout collectionViewLayout: UICollectionViewLayout, | |
referenceSizeForHeaderInSection section: Int) -> CGSize { | |
return delegate?.booksCollectionInterfaceProviderCollectionView(collectionView: collectionView, | |
layout: collectionViewLayout, | |
referenceSizeForHeaderInSection: section) ?? CGSize.zero | |
} | |
func collectionView(_ collectionView: UICollectionView, | |
viewForSupplementaryElementOfKind kind: String, | |
at indexPath: IndexPath) -> UICollectionReusableView { | |
return delegate?.booksCollectionInterfaceProviderCollectionView(collectionView: collectionView, | |
viewForSupplementaryElementOfKind: kind, | |
at: indexPath) ?? UICollectionReusableView() | |
} | |
func scrollViewDidScroll(_ scrollView: UIScrollView) { | |
delegate?.booksCollectionInterfaceProviderScrollViewDidScroll(scrollView: scrollView) | |
} | |
} | |
extension BooksCollectionInterfaceProvider: UIViewControllerPreviewingDelegate { | |
func previewingContext(_ previewingContext: UIViewControllerPreviewing, | |
viewControllerForLocation location: CGPoint) -> UIViewController? { | |
// Your preview context code. | |
} | |
func previewingContext(_ previewingContext: UIViewControllerPreviewing, | |
commit viewControllerToCommit: UIViewController) { | |
// Your preview context commit code. | |
} | |
} | |
extension BooksCollectionInterfaceProvider: ListBookCollectionViewCellDelegate { | |
func onListBookCellAddToListButton(cell: ListBookCollectionViewCell) { | |
} | |
func onListBookCellActionButton(cell: ListBookCollectionViewCell) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment