|
6 | 6 | // Copyright © 2016 Nathan Racklyeft. All rights reserved. |
7 | 7 | // |
8 | 8 |
|
| 9 | +import LoopKit |
| 10 | +import LoopKitUI |
| 11 | +import SwiftUI |
9 | 12 | import UIKit |
10 | 13 |
|
11 | | -final class LoopStateView: UIView { |
12 | | - var firstDataUpdate = true |
| 14 | +class WrappedLoopStateViewModel: ObservableObject { |
| 15 | + @Published var loopStatusColors: StateColorPalette |
| 16 | + @Published var closedLoop: Bool |
| 17 | + @Published var freshness: LoopCompletionFreshness |
| 18 | + @Published var animating: Bool |
13 | 19 |
|
14 | | - override func tintColorDidChange() { |
15 | | - super.tintColorDidChange() |
16 | | - |
17 | | - updateTintColor() |
| 20 | + init( |
| 21 | + loopStatusColors: StateColorPalette = StateColorPalette(unknown: .black, normal: .black, warning: .black, error: .black), |
| 22 | + closedLoop: Bool = true, |
| 23 | + freshness: LoopCompletionFreshness = .stale, |
| 24 | + animating: Bool = false |
| 25 | + ) { |
| 26 | + self.loopStatusColors = loopStatusColors |
| 27 | + self.closedLoop = closedLoop |
| 28 | + self.freshness = freshness |
| 29 | + self.animating = animating |
18 | 30 | } |
| 31 | +} |
19 | 32 |
|
20 | | - private func updateTintColor() { |
21 | | - shapeLayer.strokeColor = tintColor.cgColor |
| 33 | +struct WrappedLoopCircleView: View { |
| 34 | + |
| 35 | + @ObservedObject var viewModel: WrappedLoopStateViewModel |
| 36 | + |
| 37 | + var body: some View { |
| 38 | + LoopCircleView(closedLoop: viewModel.closedLoop, freshness: viewModel.freshness, animating: viewModel.animating) |
| 39 | + .environment(\.loopStatusColorPalette, viewModel.loopStatusColors) |
22 | 40 | } |
| 41 | +} |
23 | 42 |
|
24 | | - var open = false { |
25 | | - didSet { |
26 | | - if open != oldValue { |
27 | | - if open, animated { |
28 | | - animated = false |
29 | | - } |
30 | | - shapeLayer.path = drawPath() |
31 | | - } |
32 | | - } |
| 43 | +class LoopCircleHostingController: UIHostingController<WrappedLoopCircleView> { |
| 44 | + init(viewModel: WrappedLoopStateViewModel) { |
| 45 | + super.init( |
| 46 | + rootView: WrappedLoopCircleView( |
| 47 | + viewModel: viewModel |
| 48 | + ) |
| 49 | + ) |
33 | 50 | } |
34 | | - |
35 | | - override class var layerClass : AnyClass { |
36 | | - return CAShapeLayer.self |
| 51 | + |
| 52 | + required init?(coder aDecoder: NSCoder) { |
| 53 | + fatalError() |
37 | 54 | } |
| 55 | +} |
38 | 56 |
|
39 | | - private var shapeLayer: CAShapeLayer { |
40 | | - return layer as! CAShapeLayer |
41 | | - } |
42 | 57 |
|
| 58 | +final class LoopStateView: UIView { |
| 59 | + |
43 | 60 | override init(frame: CGRect) { |
44 | 61 | super.init(frame: frame) |
45 | | - |
46 | | - shapeLayer.lineWidth = 8 |
47 | | - shapeLayer.fillColor = UIColor.clear.cgColor |
48 | | - updateTintColor() |
49 | | - |
50 | | - shapeLayer.path = drawPath() |
| 62 | + |
| 63 | + setupViews() |
51 | 64 | } |
52 | | - |
53 | | - required init?(coder aDecoder: NSCoder) { |
54 | | - super.init(coder: aDecoder) |
55 | | - |
56 | | - shapeLayer.lineWidth = 8 |
57 | | - shapeLayer.fillColor = UIColor.clear.cgColor |
58 | | - updateTintColor() |
59 | | - |
60 | | - shapeLayer.path = drawPath() |
| 65 | + |
| 66 | + required init?(coder: NSCoder) { |
| 67 | + super.init(coder: coder) |
| 68 | + |
| 69 | + setupViews() |
61 | 70 | } |
62 | | - |
63 | | - override func layoutSubviews() { |
64 | | - super.layoutSubviews() |
65 | | - |
66 | | - shapeLayer.path = drawPath() |
| 71 | + |
| 72 | + var loopStatusColors: StateColorPalette = StateColorPalette(unknown: .black, normal: .black, warning: .black, error: .black) { |
| 73 | + didSet { |
| 74 | + viewModel.loopStatusColors = loopStatusColors |
| 75 | + } |
67 | 76 | } |
68 | 77 |
|
69 | | - private func drawPath(lineWidth: CGFloat? = nil) -> CGPath { |
70 | | - let center = CGPoint(x: bounds.midX, y: bounds.midY) |
71 | | - let lineWidth = lineWidth ?? shapeLayer.lineWidth |
72 | | - let radius = min(bounds.width / 2, bounds.height / 2) - lineWidth / 2 |
73 | | - |
74 | | - let startAngle = open ? -CGFloat.pi / 4 : 0 |
75 | | - let endAngle = open ? 5 * CGFloat.pi / 4 : 2 * CGFloat.pi |
76 | | - |
77 | | - let path = UIBezierPath( |
78 | | - arcCenter: center, |
79 | | - radius: radius, |
80 | | - startAngle: startAngle, |
81 | | - endAngle: endAngle, |
82 | | - clockwise: true |
83 | | - ) |
84 | | - |
85 | | - return path.cgPath |
| 78 | + var freshness: LoopCompletionFreshness = .stale { |
| 79 | + didSet { |
| 80 | + viewModel.freshness = freshness |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + var open = false { |
| 85 | + didSet { |
| 86 | + viewModel.closedLoop = !open |
| 87 | + } |
86 | 88 | } |
87 | | - |
88 | | - private static let AnimationKey = "com.loudnate.Naterade.breatheAnimation" |
89 | 89 |
|
90 | 90 | var animated: Bool = false { |
91 | 91 | didSet { |
92 | | - if animated != oldValue { |
93 | | - if animated, !open { |
94 | | - let path = CABasicAnimation(keyPath: "path") |
95 | | - path.fromValue = shapeLayer.path ?? drawPath() |
96 | | - path.toValue = drawPath(lineWidth: 16) |
97 | | - |
98 | | - let width = CABasicAnimation(keyPath: "lineWidth") |
99 | | - width.fromValue = shapeLayer.lineWidth |
100 | | - width.toValue = 10 |
101 | | - |
102 | | - let group = CAAnimationGroup() |
103 | | - group.animations = [path, width] |
104 | | - group.duration = firstDataUpdate ? 0 : 1 |
105 | | - group.repeatCount = HUGE |
106 | | - group.autoreverses = true |
107 | | - group.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut) |
108 | | - |
109 | | - shapeLayer.add(group, forKey: type(of: self).AnimationKey) |
110 | | - } else { |
111 | | - shapeLayer.removeAnimation(forKey: type(of: self).AnimationKey) |
112 | | - } |
113 | | - } |
114 | | - firstDataUpdate = false |
| 92 | + viewModel.animating = animated |
115 | 93 | } |
116 | 94 | } |
| 95 | + |
| 96 | + private let viewModel = WrappedLoopStateViewModel() |
| 97 | + |
| 98 | + private func setupViews() { |
| 99 | + let hostingController = LoopCircleHostingController(viewModel: viewModel) |
| 100 | + |
| 101 | + hostingController.view.backgroundColor = .clear |
| 102 | + hostingController.view.translatesAutoresizingMaskIntoConstraints = false |
| 103 | + |
| 104 | + addSubview(hostingController.view) |
| 105 | + |
| 106 | + NSLayoutConstraint.activate([ |
| 107 | + hostingController.view.leadingAnchor.constraint(equalTo: leadingAnchor), |
| 108 | + hostingController.view.trailingAnchor.constraint(equalTo: trailingAnchor), |
| 109 | + hostingController.view.topAnchor.constraint(equalTo: topAnchor), |
| 110 | + hostingController.view.bottomAnchor.constraint(equalTo: bottomAnchor) |
| 111 | + ]) |
| 112 | + } |
117 | 113 | } |
118 | 114 |
|
0 commit comments