-
Notifications
You must be signed in to change notification settings - Fork 0
/
WaypointScreen.swift
244 lines (216 loc) · 12.1 KB
/
WaypointScreen.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import SwiftUI
import CoreLocation
struct WaypointScreen: View {
@ObservedObject var locationManager: LocationManager
@StateObject private var userSettings = UserSettings()
@State private var backgroundColor = Color.black
@State private var textColor = Color.white
@State private var circleColor = Color(hex: "#00ff81")
@State private var ringColor = Color.gray
@State private var distanceInFeet: Double = 0
@State private var showInfoSheet = false
@State private var disableWaypointButton = false
@State private var creatingWaypoint = false
let buttonColor = Color(hex: "#00ff81")
var waypointExists: Bool {
locationManager.averagedWaypointLocation != nil
}
var bearingToWaypoint: Double {
locationManager.bearingToWaypoint
}
var distanceText: String {
DistanceFormatter.shared.formatDistance(distanceInFeet)
}
var body: some View {
GeometryReader { geometry in
ZStack {
backgroundColor
.edgesIgnoringSafeArea(.all)
VStack(spacing: 0) {
VStack {
if locationManager.latestLocation != nil, locationManager.averagedWaypointLocation != nil {
Text(distanceText)
.font(.system(size: 45))
.bold()
.foregroundColor(.white)
.frame(maxWidth: .infinity, alignment: .center)
ZStack {
Image(systemName: "arrow.up")
.font(.system(size: 60))
.bold()
.foregroundColor(circleColor)
.rotationEffect(.degrees(bearingToWaypoint))
.opacity(distanceInFeet > 20 ? 1 : 0)
.scaleEffect(distanceInFeet > 10 ? 1 : 0.1)
.animation(.easeInOut(duration: 0.5), value: distanceInFeet)
.offset(y:-10)
if distanceInFeet <= 20 {
Circle()
.fill(Color.gray.opacity(0.5))
.scaleEffect(distanceInFeet / 20)
.frame(width: 100, height: 100)
.offset(y: 16)
.animation(.easeInOut(duration: 0.5), value: distanceInFeet)
Image(systemName: "mappin")
.font(.system(size: 60))
.foregroundColor(.white)
.offset(y: -10)
.opacity(distanceInFeet <= 20 ? 1 : 0)
.scaleEffect(distanceInFeet <= 20 ? 1 : 0.1)
.animation(.easeInOut(duration: 0.5), value: distanceInFeet)
}
}
.frame(width: geometry.size.width, height: geometry.size.height / 2, alignment: .center)
} else {
Spacer()
GeometryReader { geometry in
VStack {
Spacer()
HStack {
Image(systemName: "location.circle.fill")
.foregroundColor(Color(hex: "#00ff81"))
.font(.headline)
Text("Waytracer")
.foregroundColor(.white)
.font(.title2)
.bold()
}
.frame(maxWidth: .infinity, alignment: .center)
.offset(y: geometry.size.height / 396 * 20 - 15)
Button(action: { showInfoSheet.toggle() }) {
HStack(alignment: .center, spacing: 10) {
Image(systemName: "questionmark.circle")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 20, height: 20)
.foregroundColor(.white)
Text("What's this?")
.foregroundColor(.white)
}
.frame(maxWidth: .infinity)
.padding()
.background(Color(hex: "#222223"))
.cornerRadius(15)
}
.buttonStyle(PlainButtonStyle())
Button(action: {
createWaypoint()
}) {
HStack(alignment: .center, spacing: 10) {
if creatingWaypoint {
Shine2(
iconName: "mappin.and.ellipse",
text: "Creating",
baseColor: Color(hex: "#545454")
)
} else {
Image(systemName: "mappin.and.ellipse")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 20, height: 20)
.foregroundColor(buttonColor)
Text("Create Waypoint")
.foregroundColor(.white)
}
}
.frame(maxWidth: .infinity)
.padding()
.background(creatingWaypoint ? Color(hex: "#222223") : Color(hex: "#0c3617"))
.cornerRadius(15)
}
.buttonStyle(PlainButtonStyle())
.disabled(creatingWaypoint)
Spacer()
}
.sheet(isPresented: $showInfoSheet) {
ScrollView {
VStack(alignment: .leading, spacing: 16) {
Text("Worried about finding your way back?")
.font(.headline)
.bold()
Text("Waytracer allows you to mark any location and then effortlessly find that spot later through detailed navigation.")
Text("For example, you can retrace your steps to a viewpoint you discovered, or mark your vehicle's location before going on a backcountry trek.")
Button(action: {
createWaypoint()
}) {
HStack {
Image(systemName: "mappin.and.ellipse")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 20, height: 20)
.foregroundColor(.white)
if disableWaypointButton {
ShimmeringText(text: "Creating", baseColor: .white)
} else {
Text("Create Waypoint")
.foregroundColor(.white)
}
}
.frame(maxWidth: .infinity)
.padding()
.background(disableWaypointButton ? Color(hex: "#222223") : Color(hex: "#222223"))
.cornerRadius(15)
}
.buttonStyle(PlainButtonStyle())
.disabled(disableWaypointButton)
}
}
}
}
}
Spacer()
}
}
.pauseOverlay(locationManager: locationManager)
.onChange(of: locationManager.latestLocation) { _, _ in
updateWaypointInfo()
}
}
}
}
private func updateWaypointInfo() {
guard let currentLocation = locationManager.latestLocation,
let waypointLocation = locationManager.averagedWaypointLocation else { return }
// Update distance
let distanceInMeters = currentLocation.distance(from: waypointLocation)
distanceInFeet = distanceInMeters * 3.28084
// Update colors
withAnimation {
if distanceInFeet < 20 {
backgroundColor = .green
textColor = .white
circleColor = .white
ringColor = Color(hex: "#8de4b6")
} else {
backgroundColor = .black
textColor = .black
circleColor = Color(hex: "#00ff81")
ringColor = .gray
}
}
}
private func createWaypoint() {
disableWaypointButton = true
locationManager.startWaypointCalculation()
locationManager.averagedWaypointLocation = nil
creatingWaypoint = true
locationManager.averageWaypointLocation { averagedLocation in
if let averagedLocation = averagedLocation {
locationManager.averagedWaypointLocation = averagedLocation
}
creatingWaypoint = false
disableWaypointButton = false
Haptics.vibrate(.success)
}
}
}
extension View {
@ViewBuilder
func `if`<Content: View>(_ condition: Bool, transform: (Self) -> Content) -> some View {
if condition {
transform(self)
} else {
self
}
}
}