2 // BookDetailsButtonTableViewCell.swift
5 // Created by Pawel Dabrowski on 20/06/2018.
6 // Copyright © 2018 Fundacja Nowoczesna Polska. All rights reserved.
13 enum BookDetailsButtonType{
15 case download_ebook_loading
16 case download_ebook_read
17 case download_audiobook
18 case download_audiobook_listen
19 case download_audiobook_loading
24 case .download_ebook, .download_ebook_loading, .download_ebook_read:
25 return UIColor(red:0.91, green:0.31, blue:0.20, alpha:1.00)
26 case .download_audiobook, .download_audiobook_listen, .download_audiobook_loading:
27 return UIColor(red:0.00, green:0.53, blue:0.56, alpha:1.00)
29 return UIColor(red:1.00, green:0.64, blue:0.26, alpha:1.00)
33 func getTitle() -> String{
34 return "\(self)".localized.uppercased()
39 case .download_ebook, .download_ebook_loading, .download_ebook_read:
40 return #imageLiteral(resourceName: "icon_glasses-mid")
41 case .download_audiobook, .download_audiobook_listen, .download_audiobook_loading:
42 return #imageLiteral(resourceName: "icon_speaker-mid")
44 return #imageLiteral(resourceName: "icon_star-mid")
48 var tintColor: UIColor{
51 case .download_ebook_read, .download_audiobook_listen, .support_us:
58 var deleteButtonHidden: Bool{
60 case .download_ebook, .download_audiobook, .support_us:
68 protocol BookDetailsButtonTableViewCellDelegate: class{
69 func bookDetailsButtonTableViewCellButtonTapped(buttonType: BookDetailsButtonType)
70 func bookDetailsButtonTableViewCellDeleteButtonTapped(buttonType: BookDetailsButtonType)
73 class BookDetailsButtonTableViewCell: WLTableViewCell {
75 weak var delegate: BookDetailsButtonTableViewCellDelegate?
76 // @IBOutlet weak var button: UIButton!
77 @IBOutlet weak var button: ProgressLabel!
78 @IBOutlet weak var buttonIcon: UIImageView!
79 @IBOutlet weak var deleteButton: UIButton!
80 var buttonType: BookDetailsButtonType!
82 class func instance(delegate: BookDetailsButtonTableViewCellDelegate, bookDetailsButtonType: BookDetailsButtonType, bookDetailsModel: BookDetailsModel) -> BookDetailsButtonTableViewCell{
83 let cell = BookDetailsButtonTableViewCell.instance(type: BookDetailsButtonTableViewCell.self)
84 cell.delegate = delegate
85 cell.setup(bookDetailsButtonType: bookDetailsButtonType, progress: nil, bookDetailsModel: bookDetailsModel)
89 override func awakeFromNib() {
91 deleteButton.isHidden = true
92 // button.layer.cornerRadius = 10
93 // button.layer.borderWidth = 1
94 button.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(buttonAction)))
97 func setup(bookDetailsButtonType: BookDetailsButtonType, progress:Float?, bookDetailsModel: BookDetailsModel?){
98 self.buttonType = bookDetailsButtonType
99 let tintColor = bookDetailsButtonType.tintColor
100 button.color = bookDetailsButtonType.color
101 // button.backgroundColor = bookDetailsButtonType.bgColor
102 // button.layer.borderColor = bookDetailsButtonType.color.cgColor
103 buttonIcon.tintColor = tintColor
104 buttonIcon.image = bookDetailsButtonType.icon
105 button.fullProgress = false
106 if let progress = progress{
107 button.progress = progress * 100
110 var customText = bookDetailsButtonType.getTitle()
111 if bookDetailsButtonType == .download_audiobook_listen {
112 button.fullProgress = true
113 if let bookDetailsModel = bookDetailsModel, bookDetailsModel.totalAudioChapters > 0 {
114 customText += " \(bookDetailsModel.currentAudioChapter + 1)/\(bookDetailsModel.totalAudioChapters)"
117 else if bookDetailsButtonType == .download_ebook_read {
118 button.fullProgress = true
119 if let bookDetailsModel = bookDetailsModel, bookDetailsModel.totalChapters > 0 {
120 customText += " \(bookDetailsModel.currentChapter)/\(bookDetailsModel.totalChapters)"
124 button.customText = customText
127 deleteButton.isHidden = bookDetailsButtonType.deleteButtonHidden
130 @objc func buttonAction(_ sender: Any) {
131 delegate?.bookDetailsButtonTableViewCellButtonTapped(buttonType: buttonType)
134 @IBAction func deleteButtonAction(_ sender: Any) {
135 delegate?.bookDetailsButtonTableViewCellDeleteButtonTapped(buttonType: buttonType)
138 override func getHeight() -> CGFloat {
143 print("deinit \(self)")