-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathConnectButton+ProgressBar.swift
71 lines (55 loc) · 2.12 KB
/
ConnectButton+ProgressBar.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//
// ConnectButton+ProgressBar.swift
// IFTTT SDK
//
// Copyright © 2019 IFTTT. All rights reserved.
//
import UIKit
extension ConnectButton {
/// A `PassthroughView` subclass that represents the progress bar of the connect button.
final class ProgressBar: PassthroughView {
/// The value of how much of the progress bar has completed.
var fractionComplete: CGFloat = 0 {
didSet {
update()
}
}
private let track = UIView()
private let bar = PassthroughView()
private var defaultColor: UIColor {
return Color.dynamicColor(
light: Color.grey,
dark: Color.lightGrey
)
}
/// Configures the progress bar background with the optionally provided `Service`.
///
/// - Parameter service: An optional `Service` to set the backgrund color to.
func configure(with service: Service?) {
bar.backgroundColor = service?.brandColor.contrasting() ?? defaultColor
}
private func update() {
bar.transform = CGAffineTransform(translationX: (1 - fractionComplete) * -bounds.width, y: 0)
track.layer.cornerRadius = 0.5 * bounds.height // Progress bar should match rounded corners of connect button
}
override func layoutSubviews() {
super.layoutSubviews()
update()
}
/// Creates a `ProgressBar`.
init() {
super.init(frame: .zero)
// The track ensures that the progress bar stays within its intended bounds
track.clipsToBounds = true
addSubview(track)
track.constrain.edges(to: layoutMarginsGuide)
track.addSubview(bar)
bar.constrain.edges(to: track)
layoutMargins = .zero
}
@available(*, unavailable)
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
}