Skip to content

Commit dc77d46

Browse files
committed
multiple: remove unnecessary code, fix bugs, continue working on Core Data fixes
1 parent 89875e8 commit dc77d46

10 files changed

Lines changed: 57 additions & 98 deletions

.DS_Store

0 Bytes
Binary file not shown.

InfiniLink/BLE/BLEFS.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -688,14 +688,6 @@ class BLEFSHandler: ObservableObject {
688688
return result
689689
}
690690

691-
@discardableResult func writeSettings(_ settings: Settings) -> WriteFileFS {
692-
let settingsData = serializeSettings(settings)
693-
694-
DeviceManager.shared.updateSettings(settings: settings)
695-
696-
return writeFile(data: settingsData, path: "/settings.dat", offset: 0)
697-
}
698-
699691
private func serializeSettings(_ settings: Settings) -> Data {
700692
var data = Data()
701693

InfiniLink/BLE/BLEManager.swift

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,6 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph
193193
}
194194
}
195195

196-
func resetDevice() {
197-
BLEFSHandler.shared.writeSettings(Settings())
198-
deviceManager.settings = Settings()
199-
}
200-
201196
func unpair(device: Device? = nil) {
202197
Task {
203198
if let pairedDevice {
@@ -206,23 +201,22 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph
206201
}
207202
// Update the list of user watches
208203
await deviceManager.fetchAllDevices()
209-
}
210-
211-
if let first = deviceManager.watches.first, deviceManager.watches.count <= 1 {
212-
// Switch to the user's next watch
213-
pairedDeviceID = first.uuid
214-
pairedDevice = deviceManager.fetchDevice()
215-
} else {
216-
// The user doesn't have another watch, this will show the welcome view
217-
pairedDeviceID = nil
218-
}
219-
220-
log("Unpaired from \(pairedDevice?.name ?? "InfiniTime")", type: .info, caller: "BLEManager", target: .ble)
221-
222-
if device == nil {
223-
// FIXME: this only disconnects and removes the watch from the recognized device list in the app. If using secure pairing, iOS will still keep the bond
224-
disconnect()
225-
startScanning()
204+
205+
if let first = deviceManager.watches.first, deviceManager.watches.count <= 1 {
206+
// Switch to the user's next watch
207+
pairedDeviceID = first.uuid
208+
} else {
209+
// The user doesn't have another watch, this will show the welcome view
210+
pairedDeviceID = nil
211+
}
212+
213+
log("Unpaired from \(pairedDevice?.name ?? "InfiniTime")", type: .info, caller: "BLEManager", target: .ble)
214+
215+
if device == nil {
216+
// FIXME: this only disconnects and removes the watch from the recognized device list in the app. If using secure pairing, iOS will still keep the bond
217+
disconnect()
218+
startScanning()
219+
}
226220
}
227221
}
228222

InfiniLink/BLE/BLEWriteManager.swift

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ struct BLEWriteManager {
1515
@AppStorage("watchNotifications") var watchNotifications = true
1616

1717
func writeToMusicApp(message: String, characteristic: CBCharacteristic) -> Void {
18+
guard bleManager.infiniTime != nil else { return }
1819
guard let writeData = message.data(using: .ascii) else {
1920
// There's no title/artst, so update it with a blank string
2021
bleManager.infiniTime.writeValue("".data(using: .ascii)!, for: characteristic, type: .withResponse)
@@ -24,11 +25,15 @@ struct BLEWriteManager {
2425
}
2526

2627
func writeHexToMusicApp(message: [UInt8], characteristic: CBCharacteristic) -> Void {
28+
guard bleManager.infiniTime != nil else { return }
2729
let writeData = Data(bytes: message, count: message.capacity)
30+
2831
bleManager.infiniTime.writeValue(writeData, for: characteristic, type: .withResponse)
2932
}
3033

3134
func setTime(characteristic: CBCharacteristic) {
35+
guard bleManager.infiniTime != nil else { return }
36+
3237
do {
3338
try bleManager.infiniTime.writeValue(SetTime().currentTime().hexData, for: characteristic, type: .withResponse)
3439
} catch {
@@ -37,31 +42,36 @@ struct BLEWriteManager {
3742
}
3843

3944
func sendNotification(_ notif: AppNotification) {
45+
guard bleManager.infiniTime != nil else { return }
4046
guard let titleData = (" " + notif.title + "\0").data(using: .ascii) else { return }
4147
guard let bodyData = (notif.subtitle + "\0").data(using: .ascii) else { return }
4248

4349
var notification = titleData
4450

4551
notification.append(bodyData)
4652

47-
if !notification.isEmpty && watchNotifications && bleManager.infiniTime != nil {
53+
if !notification.isEmpty && watchNotifications {
4854
bleManager.infiniTime.writeValue(notification, for: bleManager.notifyCharacteristic, type: .withResponse)
4955
}
5056
}
5157

5258
func sendLostNotification() {
59+
guard bleManager.infiniTime != nil else { return }
60+
5361
let hexPrefix = Data([0x03, 0x01, 0x00]) // Hexadecimal representation of "\x03\x01\x00"
5462
let nameData = "InfiniLink".data(using: .ascii) ?? Data()
5563

5664
let notification = hexPrefix + nameData
5765

58-
if notification.count > 0 && watchNotifications && bleManager.infiniTime != nil {
66+
if notification.count > 0 && watchNotifications {
5967
bleManager.infiniTime.writeValue(notification, for: bleManager.notifyCharacteristic, type: .withResponse)
6068
}
6169
}
6270

6371
func writeCurrentWeatherData(currentTemperature: Double, minimumTemperature: Double, maximumTemperature: Double, location: String, icon: UInt8) {
64-
var bytes : [UInt8] = [0, 0] // Message Type and Message Version
72+
guard bleManager.infiniTime != nil else { return }
73+
74+
var bytes: [UInt8] = [0, 0] // Message Type and Message Version
6575
bytes.append(contentsOf: timeSince1970())
6676
bytes.append(contentsOf: convertTemperature(value: Int(round(currentTemperature)))) // Current temperature
6777
bytes.append(contentsOf: convertTemperature(value: Int(round(minimumTemperature)))) // Minimum temperature
@@ -96,6 +106,8 @@ struct BLEWriteManager {
96106
}
97107

98108
func writeForecastWeatherData(minimumTemperature: [Double], maximumTemperature: [Double], icon: [UInt8]) {
109+
guard bleManager.infiniTime != nil else { return }
110+
99111
if (minimumTemperature.count + maximumTemperature.count + icon.count) / 3 != minimumTemperature.count && minimumTemperature.count >= 5 && minimumTemperature.count < 1 {
100112
log("Forecast data arrays do not match, forecast larger than 5 days, or forecast data is empty", caller: "BLEWriteManager")
101113
return
@@ -125,6 +137,7 @@ struct BLEWriteManager {
125137
}
126138

127139
func writeNavigationUpdate() {
140+
guard bleManager.infiniTime != nil else { return }
128141
guard bleManager.navigationFlagsCharacteristic != nil && bleManager.navigationNarrativeCharacteristic != nil && bleManager.navigationDistanceCharacteristic != nil && bleManager.navigationProgressCharacteristic != nil && bleManager.infiniTime != nil else { return }
129142

130143
guard let icon = "fork".data(using: .ascii) else { return }

InfiniLink/BLE/DeviceManager.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,22 +183,22 @@ class DeviceManager: ObservableObject {
183183
}
184184

185185
func removeDevice(_ device: Device) async {
186-
do {
187-
try await persistenceController.container.viewContext.perform {
188-
self.persistenceController.container.viewContext.delete(device)
189-
try self.persistenceController.container.viewContext.save()
186+
await persistenceController.container.performBackgroundTask { context in
187+
do {
188+
context.delete(device)
189+
try context.save()
190+
} catch {
191+
log(error.localizedDescription, caller: "DeviceManager - removeDevice")
190192
}
191-
} catch {
192-
log(error.localizedDescription, caller: "DeviceManager - removeDevice")
193193
}
194194
}
195195

196196
func fetchAllDevices() async {
197197
let fetchRequest: NSFetchRequest<Device> = Device.fetchRequest()
198198

199199
do {
200-
try await persistenceController.container.viewContext.perform {
201-
self.watches = try self.persistenceController.container.viewContext.fetch(fetchRequest)
200+
try await persistenceController.container.performBackgroundTask { context in
201+
self.watches = try context.fetch(fetchRequest)
202202
}
203203
} catch {
204204
log("Error fetching devices: \(error.localizedDescription)", caller: "DeviceManager")

InfiniLink/ContentView.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import EventKit
1010

1111
struct ContentView: View {
1212
@ObservedObject var bleManager = BLEManager.shared
13-
@ObservedObject var deviceManager = DeviceManager.shared
1413
@ObservedObject var remindersManager = RemindersManager.shared
1514
@ObservedObject var personalizationController = PersonalizationController.shared
1615
@ObservedObject var notificationManager = NotificationManager.shared

InfiniLink/Core/Settings/General/GeneralSettingsView.swift

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,6 @@ struct GeneralSettingsView: View {
7171
Text(bleManager.isConnectedToPinetime ? "Disconnect": "Connect")
7272
}
7373
.disabled(bleManager.isBusy)
74-
Button {
75-
showResetConfirmation = true
76-
} label: {
77-
Text("Reset")
78-
}
79-
.disabled(bleManager.blefsTransfer == nil)
80-
.alert("Are you sure you want to reset all content and settings from \(deviceManager.name)? You'll need to restart \(deviceManager.name) for the changes to take effect.", isPresented: $showResetConfirmation) {
81-
Button(role: .destructive) {
82-
bleManager.resetDevice()
83-
dismiss()
84-
} label: {
85-
Text("Reset")
86-
}
87-
}
8874
Button(role: .destructive) {
8975
showUnpairConfirmation = true
9076
} label: {

InfiniLink/Core/StepsView.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ struct StepsView: View {
4646
}
4747
}
4848
.navigationTitle("Steps")
49+
.navigationBarTitleDisplayMode(.inline)
4950
}
5051
}
5152

InfiniLink/Localizable.xcstrings

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -216,16 +216,6 @@
216216
},
217217
"Are you sure you want to end the exercise? %@" : {
218218

219-
},
220-
"Are you sure you want to reset all content and settings from %@? You'll need to restart %@ for the changes to take effect." : {
221-
"localizations" : {
222-
"en" : {
223-
"stringUnit" : {
224-
"state" : "new",
225-
"value" : "Are you sure you want to reset all content and settings from %1$@? You'll need to restart %2$@ for the changes to take effect."
226-
}
227-
}
228-
}
229219
},
230220
"Are you sure you want to unpair from %@?" : {
231221

@@ -574,6 +564,16 @@
574564
},
575565
"Model Name" : {
576566

567+
},
568+
"Monthly Overview • %@ - %@" : {
569+
"localizations" : {
570+
"en" : {
571+
"stringUnit" : {
572+
"state" : "new",
573+
"value" : "Monthly Overview • %1$@ - %2$@"
574+
}
575+
}
576+
}
577577
},
578578
"Music" : {
579579

@@ -655,9 +655,6 @@
655655
},
656656
"Rename" : {
657657

658-
},
659-
"Reset" : {
660-
661658
},
662659
"Screen Timeout" : {
663660

@@ -848,6 +845,9 @@
848845
},
849846
"You can start one by choosing one from the list below." : {
850847

848+
},
849+
"You reached your daily goal on Monday, but didn't even come close on Tuesday. Come on Liam, let's do better." : {
850+
851851
},
852852
"You've reached your steps goal" : {
853853

InfiniLink/Utils/PersistenceController.swift

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,6 @@ struct PersistenceController {
1212

1313
let container: NSPersistentContainer
1414

15-
@MainActor
16-
static let preview: PersistenceController = {
17-
let result = PersistenceController(inMemory: true)
18-
let viewContext = result.container.viewContext
19-
20-
for _ in 0..<1 {
21-
let exercise = UserExercise(context: viewContext)
22-
23-
exercise.exerciseId = "soccer"
24-
exercise.startDate = Date.distantPast
25-
exercise.endDate = Date()
26-
exercise.heartPoints = []
27-
exercise.id = UUID()
28-
}
29-
30-
do {
31-
try viewContext.save()
32-
} catch {
33-
// Replace this implementation with code to handle the error appropriately.
34-
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
35-
let nsError = error as NSError
36-
log("Unresolved error \(nsError), \(nsError.userInfo)", caller: "PersistenceController")
37-
}
38-
return result
39-
}()
40-
4115
init(inMemory: Bool = false) {
4216
container = NSPersistentContainer(name: "InfiniLink")
4317
if inMemory {
@@ -48,7 +22,7 @@ struct PersistenceController {
4822
description?.shouldInferMappingModelAutomatically = true
4923
}
5024
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
51-
if let error = error as NSError? {
25+
if let error {
5226
/*
5327
Typical reasons for an error here include:
5428
* The parent directory does not exist, cannot be created, or disallows writing.
@@ -65,8 +39,8 @@ struct PersistenceController {
6539

6640
func save() async {
6741
do {
68-
try await container.viewContext.perform {
69-
try container.viewContext.save()
42+
try await container.performBackgroundTask { context in
43+
try context.save()
7044
}
7145
} catch {
7246
log("Unresolved error saving context: \(error.localizedDescription)", caller: "PersistenceController")

0 commit comments

Comments
 (0)