added iOS source code
[wl-app.git] / iOS / WolneLektury / Screens / Library / Cells / LibraryEarlyAccessTableViewCell.swift
1 //
2 //  LibraryEarlyAccessTableViewCell.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 import Kingfisher
11
12 protocol LibraryEarlyAccessTableViewCellDelegate: class {
13     func libraryEarlyAccessTableViewCellRefreshButtonTapped()
14 }
15
16 class LibraryEarlyAccessTableViewCell: WLTableViewCell {
17     var delegate: LibraryEarlyAccessTableViewCellDelegate?
18
19     class func instance() -> LibraryEarlyAccessTableViewCell{
20         let cell = LibraryEarlyAccessTableViewCell.instance(type: LibraryEarlyAccessTableViewCell.self)
21         return cell
22     }
23     
24     @IBOutlet weak var containerView: UIView!
25     @IBOutlet weak var bookDescriptionView: BookDescriptionView!
26     @IBOutlet weak var miniatureImageView: UIImageView!
27     @IBOutlet weak var miniatureOverlayView: BookImageOverlayView!
28     @IBOutlet weak var miniatureBgView: UIView!
29     @IBOutlet weak var refreshButton: ActivityIndicatorButton!
30     var book: BookModel?
31     @IBOutlet weak var noPremiumBookLabel: UILabel!
32     private var height: CGFloat = 199
33
34     override func awakeFromNib() {
35         super.awakeFromNib()
36         miniatureBgView.layer.cornerRadius = 5
37         noPremiumBookLabel.text = "library_empty_header".localized
38     }
39     
40     func setup(state: ActivityIndicatorButtonState, bookModel: BookModel?) {
41         
42         self.book = bookModel
43         
44         noPremiumBookLabel.isHidden = true
45         containerView.isHidden = true
46         refreshButton.setIndicatorButtonState(state: state)
47         height = 199
48         switch state{
49         case .hidden:
50             if let book = self.book {
51                 containerView.isHidden = false
52                 bookDescriptionView.setup(bookModel: book, isPremium: true)
53                 miniatureOverlayView.setup(bookModel: book)
54                 miniatureImageView.kf.cancelDownloadTask()
55                 miniatureImageView.image = #imageLiteral(resourceName: "list_nocover")
56                 
57                 if let url = book.getCoverThumbUrl() {
58                     
59                     miniatureImageView.kf.setImage(with: ImageResource(downloadURL: url),
60                                                placeholder: #imageLiteral(resourceName: "list_nocover"),
61                                                options: [.transition(.fade(1))],
62                                                progressBlock: nil,
63                                                completionHandler: { (image, error, cacheType, url) in
64                     })
65                 }
66             }
67             else {
68                 noPremiumBookLabel.isHidden = false
69                 height = 100
70             }
71         case .button, .loading:
72             break
73         }
74     }
75     
76     @IBAction func refreshButtonAction(_ sender: Any) {
77         delegate?.libraryEarlyAccessTableViewCellRefreshButtonTapped()
78     }
79     
80     override func getHeight() -> CGFloat {
81
82         return height
83     }
84 }