-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathMapUserLocationButton.swift
More file actions
67 lines (57 loc) · 1.85 KB
/
MapUserLocationButton.swift
File metadata and controls
67 lines (57 loc) · 1.85 KB
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
//
// MapUserLocationButton.swift
// berkeley-mobile
//
// Created by Justin Wong on 2/27/25.
// Copyright © 2025 ASUC OCTO. All rights reserved.
//
import SwiftUI
class MapUserLocationButtonViewModel: ObservableObject {
@Published var isHomingInOnUserLocation = false
@Published var isUserLocationAvailable = false
init() {
BMLocationManager.notificationCenter.addObserver(
self,
selector: #selector(userLocationIsUpdated),
name: .locationUpdated,
object: nil
)
}
@objc
func userLocationIsUpdated() {
withAnimation {
isUserLocationAvailable = BMLocationManager.shared.userLocation != nil
}
}
}
struct MapUserLocationButton: View {
@EnvironmentObject var viewModel: MapUserLocationButtonViewModel
var homeInUserLocationCompletionHandler: () -> Void
var body: some View {
Button(action: {
guard viewModel.isUserLocationAvailable else {
BMLocationManager.shared.handleLocationAuthorization()
return
}
if !viewModel.isHomingInOnUserLocation {
withAnimation {
viewModel.isHomingInOnUserLocation = true
homeInUserLocationCompletionHandler()
}
}
}) {
locationImage
}
.buttonStyle(BMControlButtonStyle())
}
@ViewBuilder
private var locationImage: some View {
let hasLocationSymbolName = viewModel.isHomingInOnUserLocation ? "location.fill" : "location"
Image(systemName: viewModel.isUserLocationAvailable ? hasLocationSymbolName : "location.slash")
.font(.system(size: 24))
}
}
#Preview {
MapUserLocationButton {}
.environmentObject(MapUserLocationButtonViewModel())
}