added iOS source code
[wl-app.git] / iOS / WolneLektury / Screens / Library / Cells / LibraryCollectionTableViewCell.swift
1 //
2 //  LibraryCollectionTableViewCell.swift
3 //  WolneLektury
4 //
5 //  Created by Pawel Dabrowski on 18/06/2018.
6 //  Copyright © 2018 Fundacja Nowoczesna Polska. All rights reserved.
7 //
8
9 import UIKit
10
11 enum LibraryCollectionType{
12     case reading_now
13     case newest
14     case recommended
15     
16     var title: String{
17         return "\(self)".localized.uppercased()
18     }
19     
20     var noDataTitle: String{
21         switch self {
22         case .reading_now:
23             return "read_now_library_empty".localized
24         default:
25             return ""
26         }
27     }
28 }
29
30 protocol LibraryCollectionTableViewCellDelegate: class {
31     func libraryCollectionTableViewCellDelegateRefreshButtonTapped(collectionViewType: LibraryCollectionType)
32     func libraryCollectionTableViewCellDelegateShowAllButtonTapped(collectionViewType: LibraryCollectionType)
33     func libraryCollectionTableViewCellDelegateDidSelect(bookModel: BookModel)
34
35 }
36
37 class LibraryCollectionTableViewCell: WLTableViewCell {
38     var delegate: LibraryCollectionTableViewCellDelegate?
39     
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
45         return cell
46     }
47     
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!
55     
56     var dataSource = [BookModel]()
57     
58     override func awakeFromNib() {
59         super.awakeFromNib()
60         showAllButton.text = "see_all".localized
61         
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
67     }
68     
69     @IBAction func refreshButtonAction(_ sender: Any) {
70         delegate?.libraryCollectionTableViewCellDelegateRefreshButtonTapped(collectionViewType: libraryCollectionType)
71     }
72     
73     @IBAction func showAllButtonAction(_ sender: Any) {
74         delegate?.libraryCollectionTableViewCellDelegateShowAllButtonTapped(collectionViewType: libraryCollectionType)
75     }
76     
77     func setup(state: ActivityIndicatorButtonState, dataSource: [BookModel]?) {
78         
79         refreshButton.setIndicatorButtonState(state: state)
80         switch state{
81         case .hidden:
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
88         }
89     }
90     
91     override func getHeight() -> CGFloat {
92         return 221
93     }
94 }
95
96 extension LibraryCollectionTableViewCell: UICollectionViewDataSource{
97     public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
98         
99         return dataSource.count
100     }
101     
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{
104         
105         let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BookCollectionViewCell", for: indexPath) as! BookCollectionViewCell
106         
107         cell.setup(bookModel: dataSource[indexPath.row])
108         
109         return cell
110     }
111 }
112
113 extension LibraryCollectionTableViewCell: UICollectionViewDelegate{
114     func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
115         delegate?.libraryCollectionTableViewCellDelegateDidSelect(bookModel: dataSource[indexPath.row])
116     }
117 }
118
119