Added Android code
[wl-app.git] / iOS / WolneLektury / Controls / ActivityIndicatorButton.swift
1 //
2 //  ActivityIndicatorButton.swift
3 //  WolneLektury
4 //
5 //  Created by Pawel Dabrowski on 12/06/2018.
6 //  Copyright © 2018 Fundacja Nowoczesna Polska. All rights reserved.
7 //
8
9 import UIKit
10
11 enum ActivityIndicatorButtonState{
12     case loading
13     case button
14     case hidden
15 }
16
17 class ActivityIndicatorButton: UIButton {
18
19     private var indicatorButtonState: ActivityIndicatorButtonState = .loading
20     
21     private var activityIndicatorView: UIActivityIndicatorView!
22     override init(frame: CGRect) {
23         super.init(frame: frame)
24         customInit()
25     }
26     
27     required init?(coder aDecoder: NSCoder) {
28         super.init(coder: aDecoder)
29         customInit()
30     }
31     
32     func customInit() {
33         activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
34         activityIndicatorView.hidesWhenStopped = true
35         activityIndicatorView.tintColor = tintColor
36         activityIndicatorView.color = tintColor
37
38         activityIndicatorView.translatesAutoresizingMaskIntoConstraints = false
39         self.addSubview(activityIndicatorView)
40         activityIndicatorView.centerInSuperview()
41         setIndicatorButtonState(state: .loading)
42         titleLabel?.text = ""
43     }
44     
45     func setIndicatorButtonState(state: ActivityIndicatorButtonState){
46         indicatorButtonState = state
47         
48         switch state {
49         case .loading:
50             isHidden = false
51             activityIndicatorView.startAnimating()
52             setImage(nil, for: .normal)
53         case .button:
54             isHidden = false
55             activityIndicatorView.stopAnimating()
56             setImage(#imageLiteral(resourceName: "ic_reload"), for: .normal)
57         case .hidden:
58             isHidden = true
59         }
60     }
61 }