Skip to content

Commit 1bdf72a

Browse files
committed
improve the location stuff
1 parent b61fecb commit 1bdf72a

5 files changed

Lines changed: 25 additions & 8 deletions

File tree

StikJIT/StikJITApp.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,6 @@ struct HeartbeatApp: App {
134134
if UserDefaults.standard.bool(forKey: "keepAliveAudio") {
135135
BackgroundAudioManager.shared.start()
136136
}
137-
if UserDefaults.standard.bool(forKey: "keepAliveLocation") {
138-
BackgroundLocationManager.shared.start()
139-
}
140137
if let fixMethod = class_getInstanceMethod(UIDocumentPickerViewController.self, #selector(UIDocumentPickerViewController.fix_init(forOpeningContentTypes:asCopy:))),
141138
let origMethod = class_getInstanceMethod(UIDocumentPickerViewController.self, #selector(UIDocumentPickerViewController.init(forOpeningContentTypes:asCopy:))) {
142139
method_exchangeImplementations(origMethod, fixMethod)

StikJIT/Utilities/BackgroundLocationManager.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ final class BackgroundLocationManager: NSObject, CLLocationManagerDelegate {
1010

1111
private let locationManager = CLLocationManager()
1212
private var isRunning = false
13+
private var activityCount = 0
1314

1415
private override init() {
1516
super.init()
@@ -39,6 +40,20 @@ final class BackgroundLocationManager: NSObject, CLLocationManagerDelegate {
3940
locationManager.stopUpdatingLocation()
4041
}
4142

43+
func requestStart() {
44+
activityCount += 1
45+
if activityCount == 1, UserDefaults.standard.bool(forKey: "keepAliveLocation") {
46+
start()
47+
}
48+
}
49+
50+
func requestStop() {
51+
activityCount = max(activityCount - 1, 0)
52+
if activityCount == 0 {
53+
stop()
54+
}
55+
}
56+
4257
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
4358
guard isRunning else { return }
4459
switch manager.authorizationStatus {

StikJIT/Views/HomeView.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ struct HomeView: View {
449449
private func startJITInBackground(bundleID: String? = nil, pid: Int? = nil, scriptData: Data? = nil, scriptName: String? = nil, triggeredByURLScheme: Bool = false) {
450450
isProcessing = true
451451
LogManager.shared.addInfoLog("Starting Debug for \(bundleID ?? String(pid ?? 0))")
452+
BackgroundLocationManager.shared.requestStart()
452453

453454
if triggeredByURLScheme {
454455
pubHeartBeat = false
@@ -463,6 +464,7 @@ struct HomeView: View {
463464
let finishProcessing = {
464465
DispatchQueue.main.async {
465466
isProcessing = false
467+
BackgroundLocationManager.shared.requestStop()
466468
}
467469
}
468470

StikJIT/Views/MapSelectionView.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ struct LocationSimulationView: View {
195195
}
196196
.onDisappear {
197197
stopResendLoop()
198+
if backgroundTaskID != .invalid {
199+
BackgroundLocationManager.shared.requestStop()
200+
}
198201
endBackgroundTask()
199202
}
200203
}
@@ -225,6 +228,7 @@ struct LocationSimulationView: View {
225228
if code == 0 {
226229
beginBackgroundTask()
227230
startResendLoop()
231+
BackgroundLocationManager.shared.requestStart()
228232
} else {
229233
alertTitle = "Simulation Failed"
230234
alertMessage = "Could not simulate location (error \(code)). Make sure the device is connected and the DDI is mounted."
@@ -245,6 +249,7 @@ struct LocationSimulationView: View {
245249
if code == 0 {
246250
coordinate = nil
247251
endBackgroundTask()
252+
BackgroundLocationManager.shared.requestStop()
248253
} else {
249254
alertTitle = "Clear Failed"
250255
alertMessage = "Could not clear simulated location (error \(code))."

StikJIT/Views/SettingsView.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,16 @@ struct SettingsView: View {
117117
Toggle(isOn: $keepAliveLocation) {
118118
VStack(alignment: .leading, spacing: 2) {
119119
Text("Background Location")
120-
Text("Uses low-accuracy location to stay alive even when another app plays audio.")
120+
Text("Uses low-accuracy location to stay alive when an activity needs it.")
121121
.font(.caption).foregroundStyle(.secondary)
122122
}
123123
}
124124
.onChange(of: keepAliveLocation) { _, enabled in
125-
if enabled { BackgroundLocationManager.shared.start() }
126-
else { BackgroundLocationManager.shared.stop() }
125+
if !enabled { BackgroundLocationManager.shared.stop() }
127126
}
127+
128128
} header: {
129129
Text("Background Keep-Alive")
130-
} footer: {
131-
Text("For Background Location to work reliably, go to **Settings → Privacy & Security → Location Services → StikDebug** and select **Always**.")
132130
}
133131

134132
// 6) Behavior

0 commit comments

Comments
 (0)