-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathLoadingCircle.swift
122 lines (100 loc) · 3.76 KB
/
LoadingCircle.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//
// LoadingCircle.swift
// Lockdown
//
// Created by Johnny Lin on 1/21/20.
// Copyright © 2020 Confirmed, Inc. All rights reserved.
//
import Foundation
import SwiftUI
struct TunnelState {
var color: Color = Color.gray
var circleColor: Color = Color.gray
init() {
}
init(color: Color, circleColor: Color) {
self.color = color
self.circleColor = circleColor
}
}
struct StatusLabel: View {
let text: String
let color: Color
var body: some View {
Text(text)
.font(.system(size: 10, weight: .semibold))
.foregroundColor(.white)
.padding(EdgeInsets(top: 0, leading: 7, bottom: 0, trailing: 7))
.background(color)
.overlay(
RoundedRectangle(cornerRadius: 3)
.stroke(color, lineWidth: 4)
)
}
}
struct LoadingCircle: View {
let side: CGFloat
let tunnelState: TunnelState
let link: String
init(tunnelState: TunnelState, side: CGFloat, link: String) {
self.tunnelState = tunnelState
self.side = side
self.link = link
}
var body: some View {
ZStack {
Circle()
.stroke(lineWidth: 2.5)
.frame(width: side * 0.4, height: side * 0.4)
.padding(4)
.foregroundColor(tunnelState.circleColor)
.zIndex(10)
Circle()
.fill()
.frame(width: side * 0.4, height: side * 0.4)
.shadow(color: .powerButtonShadowColor, radius: 8, x: 0, y: 3.5)
.padding(4)
.foregroundColor(Color.panelSecondaryBackground)
.background(Color.panelBackground)
.zIndex(1)
Link.init(destination: URL.init(string: self.link)!, label: {
Image(uiImage: UIImage(named: "power")!.withRenderingMode(.alwaysTemplate).withTintColor(UIColor(tunnelState.circleColor)).resized(toFit: CGSize(width: side * 0.30, height: side * 0.30)))
.foregroundColor(tunnelState.circleColor)
.zIndex(40)
})
}
}
}
struct BlankButtonStyle: ButtonStyle {
func makeBody(configuration: Self.Configuration) -> some View {
configuration.label
}
}
extension Color {
static let confirmedBlue = Color(red: 0/255.0, green: 173/255.0, blue: 231/255.0)
static let panelBackground = Color("Panel Background")
static let panelSecondaryBackground = Color("Panel Secondary Background")
static let powerButtonShadowColor = Color("Power Button Shadow Color")
static let mainBackground = Color("Main Background")
static let lightGray = Color(UIColor.lightGray)
static let flatRed = Color(red: 231/255, green: 76/255, blue: 60/255)
}
extension UIImage {
func resized(toFit size: CGSize) -> UIImage {
assert(size.width > 0 && size.height > 0, "You cannot safely scale an image to a zero width or height")
let imageAspectRatio = self.size.width / self.size.height
let canvasAspectRatio = size.width / size.height
var resizeFactor: CGFloat
if imageAspectRatio > canvasAspectRatio {
resizeFactor = size.width / self.size.width
} else {
resizeFactor = size.height / self.size.height
}
let scaledSize = CGSize(width: self.size.width * resizeFactor, height: self.size.height * resizeFactor)
UIGraphicsBeginImageContextWithOptions(scaledSize, false, 0.0)
draw(in: CGRect(origin: .zero, size: scaledSize))
let scaledImage = UIGraphicsGetImageFromCurrentImageContext() ?? self
UIGraphicsEndImageContext()
return scaledImage
}
}