Skip to content

Commit fff549d

Browse files
Better device reconnection
1 parent 91ed6af commit fff549d

1 file changed

Lines changed: 41 additions & 26 deletions

File tree

WunderLINQ/MainCollectionViewController.swift

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,6 @@ class MainCollectionViewController: UIViewController, UICollectionViewDataSource
7979
var referenceAttitude: CMAttitude?
8080

8181
private let notificationCenter = NotificationCenter.default
82-
/*
83-
lazy var menu = Templates.UIKitMenu(sourceView: menuBtn!) {
84-
Templates.MenuButton(title: NSLocalizedString("bike_info_label", comment: ""), systemImage: nil) { self.openBikeInfo() }
85-
Templates.MenuButton(title: NSLocalizedString("geodata_label", comment: ""), systemImage: nil) { self.openGeoData() }
86-
Templates.MenuButton(title: NSLocalizedString("appsettings_label", comment: ""), systemImage: nil) { self.openAppSettings() }
87-
Templates.MenuButton(title: NSLocalizedString("hwsettings_label", comment: ""), systemImage: nil) { self.openHWSettings() }
88-
Templates.MenuButton(title: NSLocalizedString("about_label", comment: ""), systemImage: nil) { self.openAbout() }
89-
Templates.MenuButton(title: NSLocalizedString("close_label", comment: ""), systemImage: nil) { exit(0)}
90-
}
91-
*/
9282

9383
var navBarTimeout = 10
9484
var navBarTimer = Timer()
@@ -161,13 +151,13 @@ class MainCollectionViewController: UIViewController, UICollectionViewDataSource
161151
return $0
162152
}(CLLocationManager())
163153

154+
var lastLocationRequest: Date = .distantPast
155+
164156
func getQuickLocationUpdate() {
165-
// Request location authorization
166-
self.locationManager.requestWhenInUseAuthorization()
167-
168-
// Request a location update
157+
let now = Date()
158+
guard now.timeIntervalSince(lastLocationRequest) > 10 else { return }
159+
lastLocationRequest = now
169160
self.locationManager.requestLocation()
170-
// Note: requestLocation may timeout and produce an error if authorization has not yet been granted by the user
171161
}
172162

173163
private func orientationAdjustment() -> CGFloat {
@@ -426,6 +416,8 @@ class MainCollectionViewController: UIViewController, UICollectionViewDataSource
426416
}
427417

428418
centralManager = CBCentralManager(delegate: self, queue: nil, options: [CBCentralManagerOptionRestoreIdentifierKey: "com.blackboxembedded.wunderlinq"])
419+
keepScanning = true
420+
NotificationCenter.default.addObserver(self, selector: #selector(self.appDidBecomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)
429421

430422
// Scheduling timer to Call the function "updateTime" with the interval of 1 seconds
431423
timeTimer = Timer.scheduledTimer(timeInterval: 0.25, target: self, selector: #selector(self.updatePhoneSensorData), userInfo: nil, repeats: true)
@@ -438,6 +430,17 @@ class MainCollectionViewController: UIViewController, UICollectionViewDataSource
438430
showDisclaimerAlert()
439431
}
440432

433+
@objc func appDidBecomeActive() {
434+
// If not connected and auto-scan is enabled, attempt to resume scanning
435+
if self.wunderLINQ == nil && keepScanning {
436+
self.resumeScan()
437+
}
438+
}
439+
440+
deinit {
441+
NotificationCenter.default.removeObserver(self, name: UIApplication.didBecomeActiveNotification, object: nil)
442+
}
443+
441444
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
442445
NSLog("MainCollectionViewController: viewWillTransition")
443446
super.viewWillTransition(to: size, with: coordinator)
@@ -691,8 +694,11 @@ class MainCollectionViewController: UIViewController, UICollectionViewDataSource
691694
}
692695

693696
// MARK: - Updating UI
694-
func updateDisplay() {
695-
697+
698+
private var needsDisplayUpdate = false
699+
private var displayUpdateScheduled = false
700+
701+
private func performDisplayUpdate() {
696702
// Update Buttons
697703
if (faults.getallActiveDesc().isEmpty){
698704
faultsBtn.tintColor = UIColor.clear
@@ -709,7 +715,19 @@ class MainCollectionViewController: UIViewController, UICollectionViewDataSource
709715
for i in 1...validCellCount {
710716
setCell(i)
711717
}
712-
718+
}
719+
720+
func updateDisplay() {
721+
needsDisplayUpdate = true
722+
guard !displayUpdateScheduled else { return }
723+
displayUpdateScheduled = true
724+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { [weak self] in
725+
guard let self = self else { return }
726+
self.displayUpdateScheduled = false
727+
guard self.needsDisplayUpdate else { return }
728+
self.needsDisplayUpdate = false
729+
self.performDisplayUpdate()
730+
}
713731
}
714732

715733
func setCell(_ cellNumber: Int){
@@ -728,15 +746,13 @@ class MainCollectionViewController: UIViewController, UICollectionViewDataSource
728746
}
729747

730748
func parseCommandResponse(_ data:Data) {
731-
let dataLength = data.count / MemoryLayout<UInt8>.size
732-
var dataArray = [UInt8](repeating: 0, count: dataLength)
733-
(data as NSData).getBytes(&dataArray, length: dataLength * MemoryLayout<Int16>.size)
749+
let dataArray = [UInt8](data)
734750

735751
if UserDefaults.standard.bool(forKey: "debug_logging_preference") {
736752
var messageHexString = ""
737-
for i in 0 ..< dataLength {
753+
for i in 0 ..< dataArray.count {
738754
messageHexString += String(format: "%02X", dataArray[i])
739-
if i < dataLength - 1 {
755+
if i < dataArray.count - 1 {
740756
messageHexString += ","
741757
}
742758
}
@@ -1118,9 +1134,7 @@ class MainCollectionViewController: UIViewController, UICollectionViewDataSource
11181134
}
11191135
}
11201136
} else if characteristic.uuid == CBUUID(string: Device.WunderLINQPerformanceCharacteristicUUID) {
1121-
let dataLength = dataBytes.count / MemoryLayout<UInt8>.size
1122-
var dataArray = [UInt8](repeating: 0, count: dataLength)
1123-
(dataBytes as NSData).getBytes(&dataArray, length: dataLength * MemoryLayout<Int16>.size)
1137+
let dataArray = [UInt8](dataBytes)
11241138

11251139
// Log raw messages
11261140
if UserDefaults.standard.bool(forKey: "debug_logging_preference") {
@@ -1920,3 +1934,4 @@ class MainCollectionViewController: UIViewController, UICollectionViewDataSource
19201934
countdownTimer = nil
19211935
}
19221936
}
1937+

0 commit comments

Comments
 (0)