added iOS source code
[wl-app.git] / iOS / WolneLektury / Screens / BookDetails / Cells / BookDetailsButtonTableViewCell.swift
1 //
2 //  BookDetailsButtonTableViewCell.swift
3 //  WolneLektury
4 //
5 //  Created by Pawel Dabrowski on 20/06/2018.
6 //  Copyright © 2018 Fundacja Nowoczesna Polska. All rights reserved.
7 //
8
9 import UIKit
10
11
12
13 enum BookDetailsButtonType{
14     case download_ebook
15     case download_ebook_loading
16     case download_ebook_read
17     case download_audiobook
18     case download_audiobook_listen
19     case download_audiobook_loading
20     case support_us
21     
22     var color: UIColor{
23         switch self{
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)
28         case .support_us:
29             return UIColor(red:1.00, green:0.64, blue:0.26, alpha:1.00)
30         }
31     }
32     
33     func getTitle() -> String{
34         return "\(self)".localized.uppercased()
35     }
36     
37     var icon: UIImage{
38         switch self {
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")
43         case .support_us:
44             return #imageLiteral(resourceName: "icon_star-mid")
45         }
46     }
47     
48     var tintColor: UIColor{
49         
50         switch self {
51         case .download_ebook_read, .download_audiobook_listen, .support_us:
52             return UIColor.white
53         default:
54             return color
55         }
56     }
57     
58     var deleteButtonHidden: Bool{
59         switch self {
60         case .download_ebook, .download_audiobook, .support_us:
61             return true
62         default:
63             return false
64         }
65     }
66 }
67
68 protocol BookDetailsButtonTableViewCellDelegate: class{
69     func bookDetailsButtonTableViewCellButtonTapped(buttonType: BookDetailsButtonType)
70     func bookDetailsButtonTableViewCellDeleteButtonTapped(buttonType: BookDetailsButtonType)
71 }
72
73 class BookDetailsButtonTableViewCell: WLTableViewCell {
74     
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!
81
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)
86         return cell
87     }
88     
89     override func awakeFromNib() {
90         super.awakeFromNib()
91         deleteButton.isHidden = true
92 //        button.layer.cornerRadius = 10
93 //        button.layer.borderWidth = 1
94         button.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(buttonAction)))
95     }
96
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
108         }
109         else {
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)"
115                 }
116             }
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)"
121                 }
122             }
123
124             button.customText = customText
125         }
126         
127         deleteButton.isHidden = bookDetailsButtonType.deleteButtonHidden
128     }
129     
130     @objc func buttonAction(_ sender: Any) {
131         delegate?.bookDetailsButtonTableViewCellButtonTapped(buttonType: buttonType)
132     }
133     
134     @IBAction func deleteButtonAction(_ sender: Any) {
135         delegate?.bookDetailsButtonTableViewCellDeleteButtonTapped(buttonType: buttonType)
136     }
137     
138     override func getHeight() -> CGFloat {
139         return 55
140     }
141     
142     deinit {
143         print("deinit \(self)")
144     }
145 }