2 // LibraryCollectionTableViewCell.swift
5 // Created by Pawel Dabrowski on 18/06/2018.
6 // Copyright © 2018 Fundacja Nowoczesna Polska. All rights reserved.
11 enum LibraryCollectionType{
17 return "\(self)".localized.uppercased()
20 var noDataTitle: String{
23 return "read_now_library_empty".localized
30 protocol LibraryCollectionTableViewCellDelegate: class {
31 func libraryCollectionTableViewCellDelegateRefreshButtonTapped(collectionViewType: LibraryCollectionType)
32 func libraryCollectionTableViewCellDelegateShowAllButtonTapped(collectionViewType: LibraryCollectionType)
33 func libraryCollectionTableViewCellDelegateDidSelect(bookModel: BookModel)
37 class LibraryCollectionTableViewCell: WLTableViewCell {
38 var delegate: LibraryCollectionTableViewCellDelegate?
40 class func instance(libraryCollectionType: LibraryCollectionType) -> LibraryCollectionTableViewCell{
41 let cell = LibraryCollectionTableViewCell.instance(type: LibraryCollectionTableViewCell.self)
42 cell.libraryCollectionType = libraryCollectionType
43 cell.titleLabel.text = libraryCollectionType.title
44 cell.noDataLabel.text = libraryCollectionType.noDataTitle
48 var libraryCollectionType: LibraryCollectionType!
49 @IBOutlet weak var titleLabel: UILabel!
50 @IBOutlet weak var showAllButton: UIButton!
51 @IBOutlet weak var collectionView: UICollectionView!
52 @IBOutlet weak var refreshButton: ActivityIndicatorButton!
53 @IBOutlet weak var showAllArrowImageView: UIImageView!
54 @IBOutlet weak var noDataLabel: UILabel!
56 var dataSource = [BookModel]()
58 override func awakeFromNib() {
60 showAllButton.text = "see_all".localized
62 collectionView.register(UINib(nibName: "BookCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "BookCollectionViewCell")
63 collectionView.delegate = self
64 collectionView.dataSource = self
65 showAllArrowImageView.tintColor = Constants.Colors.grayTextColor()
66 noDataLabel.isHidden = true
69 @IBAction func refreshButtonAction(_ sender: Any) {
70 delegate?.libraryCollectionTableViewCellDelegateRefreshButtonTapped(collectionViewType: libraryCollectionType)
73 @IBAction func showAllButtonAction(_ sender: Any) {
74 delegate?.libraryCollectionTableViewCellDelegateShowAllButtonTapped(collectionViewType: libraryCollectionType)
77 func setup(state: ActivityIndicatorButtonState, dataSource: [BookModel]?) {
79 refreshButton.setIndicatorButtonState(state: state)
82 self.dataSource = dataSource ?? [BookModel]()
83 collectionView.reloadData()
84 collectionView.isHidden = false
85 noDataLabel.isHidden = self.dataSource.count > 0
86 case .button, .loading:
87 collectionView.isHidden = true
91 override func getHeight() -> CGFloat {
96 extension LibraryCollectionTableViewCell: UICollectionViewDataSource{
97 public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
99 return dataSource.count
102 // The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
103 public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
105 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BookCollectionViewCell", for: indexPath) as! BookCollectionViewCell
107 cell.setup(bookModel: dataSource[indexPath.row])
113 extension LibraryCollectionTableViewCell: UICollectionViewDelegate{
114 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
115 delegate?.libraryCollectionTableViewCellDelegateDidSelect(bookModel: dataSource[indexPath.row])