Skip to content

Commit d3c953f

Browse files
committed
Improve code readability, implement step chart, fix warnings
1 parent b540129 commit d3c953f

14 files changed

Lines changed: 313 additions & 279 deletions
Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,76 @@
11
{
22
"images" : [
33
{
4-
"idiom" : "ios-marketing",
5-
"size" : "1024x1024",
6-
"scale" : "1x",
7-
"filename" : "icon_1024.png"
8-
},
9-
{
10-
"idiom" : "iphone",
114
"filename" : "icon_40.png",
12-
"size" : "20x20",
13-
"scale" : "2x"
5+
"idiom" : "iphone",
6+
"scale" : "2x",
7+
"size" : "20x20"
148
},
159
{
16-
"scale" : "3x",
1710
"filename" : "icon_60.png",
18-
"size" : "20x20",
19-
"idiom" : "iphone"
11+
"idiom" : "iphone",
12+
"scale" : "3x",
13+
"size" : "20x20"
2014
},
2115
{
22-
"idiom" : "iphone",
2316
"filename" : "icon_58.png",
17+
"idiom" : "iphone",
2418
"scale" : "2x",
2519
"size" : "29x29"
2620
},
2721
{
28-
"size" : "29x29",
29-
"scale" : "3x",
3022
"filename" : "icon_87.png",
31-
"idiom" : "iphone"
23+
"idiom" : "iphone",
24+
"scale" : "3x",
25+
"size" : "29x29"
3226
},
3327
{
34-
"scale" : "2x",
35-
"idiom" : "iphone",
3628
"filename" : "icon_80.png",
29+
"idiom" : "iphone",
30+
"scale" : "2x",
3731
"size" : "40x40"
3832
},
3933
{
4034
"filename" : "icon_120.png",
41-
"scale" : "3x",
4235
"idiom" : "iphone",
36+
"scale" : "3x",
4337
"size" : "40x40"
4438
},
4539
{
46-
"scale" : "2x",
47-
"size" : "60x60",
40+
"filename" : "icon_120.png",
4841
"idiom" : "iphone",
49-
"filename" : "icon_120.png"
42+
"scale" : "2x",
43+
"size" : "60x60"
5044
},
5145
{
52-
"size" : "60x60",
5346
"filename" : "icon_180.png",
47+
"idiom" : "iphone",
5448
"scale" : "3x",
55-
"idiom" : "iphone"
49+
"size" : "60x60"
5650
},
5751
{
58-
"size" : "20x20",
5952
"filename" : "icon_20.png",
6053
"idiom" : "ipad",
61-
"scale" : "1x"
54+
"scale" : "1x",
55+
"size" : "20x20"
6256
},
6357
{
64-
"scale" : "2x",
65-
"size" : "20x20",
6658
"filename" : "icon_40.png",
67-
"idiom" : "ipad"
59+
"idiom" : "ipad",
60+
"scale" : "2x",
61+
"size" : "20x20"
6862
},
6963
{
70-
"size" : "29x29",
71-
"scale" : "1x",
64+
"filename" : "icon_29.png",
7265
"idiom" : "ipad",
73-
"filename" : "icon_29.png"
66+
"scale" : "1x",
67+
"size" : "29x29"
7468
},
7569
{
76-
"size" : "29x29",
77-
"scale" : "2x",
7870
"filename" : "icon_58.png",
79-
"idiom" : "ipad"
71+
"idiom" : "ipad",
72+
"scale" : "2x",
73+
"size" : "29x29"
8074
},
8175
{
8276
"filename" : "icon_40.png",
@@ -85,32 +79,38 @@
8579
"size" : "40x40"
8680
},
8781
{
88-
"size" : "40x40",
82+
"filename" : "icon_80.png",
8983
"idiom" : "ipad",
9084
"scale" : "2x",
91-
"filename" : "icon_80.png"
85+
"size" : "40x40"
9286
},
9387
{
94-
"idiom" : "ipad",
9588
"filename" : "icon_76.png",
96-
"size" : "76x76",
97-
"scale" : "1x"
89+
"idiom" : "ipad",
90+
"scale" : "1x",
91+
"size" : "76x76"
9892
},
9993
{
100-
"idiom" : "ipad",
101-
"size" : "76x76",
10294
"filename" : "icon_152.png",
103-
"scale" : "2x"
95+
"idiom" : "ipad",
96+
"scale" : "2x",
97+
"size" : "76x76"
10498
},
10599
{
106-
"size" : "83.5x83.5",
107100
"filename" : "icon_167.png",
108101
"idiom" : "ipad",
109-
"scale" : "2x"
102+
"scale" : "2x",
103+
"size" : "83.5x83.5"
104+
},
105+
{
106+
"filename" : "icon_1024.png",
107+
"idiom" : "ios-marketing",
108+
"scale" : "1x",
109+
"size" : "1024x1024"
110110
}
111111
],
112112
"info" : {
113113
"author" : "xcode",
114114
"version" : 1
115115
}
116-
}
116+
}

InfiniLink/BLE/BLEFS.swift

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,49 +12,50 @@ import SwiftUI
1212

1313
class BLEFSHandler: ObservableObject {
1414
static var shared = BLEFSHandler()
15+
1516
let bleManager = BLEManager.shared
1617
let dfuUpdater = DFUUpdater.shared
1718

18-
var informationTransfer : [InformationFS] = []
19-
var readFileFS : ReadFileFS = ReadFileFS()
20-
var writeFileFS : WriteFileFS = WriteFileFS()
19+
var informationTransfer: [InformationFS] = []
20+
var readFileFS: ReadFileFS = ReadFileFS()
21+
var writeFileFS: WriteFileFS = WriteFileFS()
2122

2223
struct WriteFileFS {
2324
var group = DispatchGroup()
24-
var offset : Int = 0
25-
var freeSpace : UInt32 = 0
25+
var offset: Int = 0
26+
var freeSpace: UInt32 = 0
2627
var data = Data()
27-
var completed : Bool = false
28-
var valid : Bool = false
28+
var completed: Bool = false
29+
var valid: Bool = false
2930
}
3031

3132
struct ReadFileFS {
3233
var group = DispatchGroup()
33-
var chunkOffset : UInt32 = 0
34-
var totalLength : UInt32 = 0
35-
var chunkLength : UInt32 = 0
34+
var chunkOffset: UInt32 = 0
35+
var totalLength: UInt32 = 0
36+
var chunkLength: UInt32 = 0
3637
var data = Data()
3738
var completed : Bool = false
3839
var valid : Bool = false
3940
}
4041

4142
struct InformationFS {
4243
var group = DispatchGroup()
43-
var dirList : DirList = DirList()
44-
var valid : Bool = false
44+
var dirList: DirList = DirList()
45+
var valid: Bool = false
4546
}
4647

4748
struct DirList {
4849
var parentPath = ""
49-
var ls : [Dir] = []
50-
var valid : Bool = false
50+
var ls: [Dir] = []
51+
var valid: Bool = false
5152
}
5253

5354
struct Dir {
54-
var modificationTime : Int = 0
55-
var fileSize : Int = 0
56-
var flags : Int = 0
57-
var pathNames : String = ""
55+
var modificationTime: Int = 0
56+
var fileSize: Int = 0
57+
var flags: Int = 0
58+
var pathNames: String = ""
5859
}
5960

6061
enum Commands : UInt8 {
@@ -98,7 +99,7 @@ class BLEFSHandler: ObservableObject {
9899
@Published var progress: Int = 0
99100
@Published var externalResourcesSize: Int = 0
100101

101-
func downloadTransfer(completion: @escaping() -> Void) {
102+
func uploadExternalResources(completion: @escaping() -> Void) {
102103
DispatchQueue.global(qos: .default).async { [self] in
103104
do {
104105
let unzipDirectory = try Zip.quickUnzipFile(dfuUpdater.resourceURL)
@@ -177,10 +178,11 @@ class BLEFSHandler: ObservableObject {
177178
}
178179

179180
func readFile(path: String, offset: UInt32) -> ReadFileFS {
180-
var read = ReadFileFS()
181-
read.group = DispatchGroup()
182-
read.group.enter()
183181
var writeData = Data()
182+
183+
readFileFS = ReadFileFS()
184+
readFileFS.group = DispatchGroup()
185+
readFileFS.group.enter()
184186

185187
writeData.append(Commands.readInit.rawValue)
186188
writeData.append(Commands.padding.rawValue)
@@ -194,7 +196,6 @@ class BLEFSHandler: ObservableObject {
194196
let pathData = path.data(using: .utf8)!
195197
writeData.append(pathData)
196198

197-
readFileFS = read
198199
bleManager.infiniTime.writeValue(writeData, for: BLEManager.shared.blefsTransfer!, type: .withResponse)
199200
readFileFS.group.wait()
200201

@@ -494,7 +495,8 @@ class BLEFSHandler: ObservableObject {
494495
switch responseData[1] {
495496
case Responses.ok.rawValue:
496497
informationTransfer[0].valid = true
497-
default: break
498+
default:
499+
break
498500
//print("error response code \(responseData[1])")
499501
}
500502
informationTransfer[0].group.leave()

InfiniLink/BLE/BLEManager.swift

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ import SwiftUI
1212
class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeripheralDelegate {
1313
static let shared = BLEManager()
1414

15+
// BLECharacteristicHandler and DeviceManager both create an instance of BLEManager, so they need to lazy to avoid a crash
1516
lazy var characteristicHandler = BLECharacteristicHandler()
1617
lazy var deviceManager = DeviceManager.shared
18+
1719
let downloadManager = DownloadManager.shared
1820

1921
var central: CBCentralManager!
@@ -135,13 +137,15 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph
135137
guard central.state == .poweredOn else { return }
136138

137139
if let pairedDeviceID = pairedDeviceID,
138-
let uuid = UUID(uuidString: pairedDeviceID), !isPairingNewDevice {
139-
140+
let uuid = UUID(uuidString: pairedDeviceID), !isPairingNewDevice { // The user has a paired device and they're not trying to pair a new one
140141
let peripherals = central.retrievePeripherals(withIdentifiers: [uuid])
141142
log("\(peripherals)", type: .info, caller: "BLEManager - startScanning")
142143

143144
if let peripheral = peripherals.first, !isConnectedToPinetime {
145+
// FIXME: should we add a check to confirm the watch is already not connected to the system (state != .connected), or let it pair this way?
144146
connect(peripheral: peripheral) {}
147+
} else {
148+
scanForNewDevices()
145149
}
146150
} else {
147151
scanForNewDevices()
@@ -159,7 +163,6 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph
159163
if peripheral.name == "InfiniTime" {
160164
isConnecting = true
161165
peripheralToConnect = peripheral
162-
pairedDeviceID = peripheral.identifier.uuidString
163166
central.connect(peripheralToConnect, options: nil)
164167

165168
completion()
@@ -195,21 +198,25 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph
195198

196199
func unpair(device: Device? = nil) {
197200
if let pairedDevice {
201+
// Delete the device object we have said for this watch
198202
deviceManager.removeDevice(device ?? pairedDevice)
199203
}
204+
// Update the list of user watches
200205
deviceManager.fetchAllDevices()
201206

202207
if let first = deviceManager.watches.first, deviceManager.watches.count <= 1 {
208+
// Switch to the user's next watch
203209
pairedDeviceID = first.uuid
204210
pairedDevice = deviceManager.fetchDevice()
205211
} else {
212+
// The user doesn't have another watch, this will show the welcome view
206213
pairedDeviceID = nil
207214
}
208215

209216
log("Unpaired from \(pairedDevice?.name ?? "InfiniTime")", type: .info, caller: "BLEManager", target: .ble)
210217

211218
if device == nil {
212-
// This only disconnects and removes the watch from the recognized device list. If using secure pairing, the bond will still be kept
219+
// 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
213220
disconnect()
214221
startScanning()
215222
}
@@ -218,6 +225,8 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph
218225
func disconnect() {
219226
if let infiniTime = infiniTime {
220227
self.central.cancelPeripheralConnection(infiniTime)
228+
229+
// Update the rest of the app to reflect the disconnected state
221230
self.infiniTime = nil
222231
self.blefsTransfer = nil
223232
self.currentTimeService = nil
@@ -230,9 +239,11 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph
230239
}
231240

232241
func switchDevice(device: Device) {
242+
// We just switched devices, update the UI
233243
self.pairedDeviceID = device.uuid
234244
self.pairedDevice = deviceManager.fetchDevice()
235245
self.deviceManager.getSettings()
246+
236247
self.disconnect()
237248
self.startScanning()
238249
}
@@ -241,9 +252,9 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph
241252
if let pairedDeviceID = pairedDeviceID, pairedDeviceID == peripheral.identifier.uuidString && !isPairingNewDevice {
242253
connect(peripheral: peripheral) {}
243254
}
244-
if peripheral.name == "InfiniTime" && !newPeripherals.contains(where: { $0.identifier.uuidString == peripheral.identifier.uuidString }) {
245-
if isPairingNewDevice {
246-
if !deviceManager.watches.compactMap({ $0.uuid }).contains(peripheral.identifier.uuidString) {
255+
if peripheral.name == "InfiniTime" && !newPeripherals.contains(where: { $0.identifier.uuidString == peripheral.identifier.uuidString }) { // The peripheral has not already been discovered
256+
if isPairingNewDevice { // Only check if we know the device when pairing a new watch, because we don't one to show up
257+
if !deviceManager.watches.compactMap({ $0.uuid }).contains(peripheral.identifier.uuidString) { // The discovered watch is not already paired
247258
newPeripherals.append(peripheral)
248259
}
249260
} else {
@@ -268,20 +279,21 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph
268279
}
269280
}
270281

282+
// The connection was abruptly terminated, so we try connecting again
271283
connect(peripheral: peripheral) {}
272284
}
273285

274286
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
287+
// The connection was successfully, update state vars and start service discovery
275288
onConnect()
276289
}
277290

278291
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
279292
isConnectedToPinetime = false
280293
notifyCharacteristic = nil
281294

282-
if pairedDeviceID != nil, pairedDeviceID == peripheral.identifier.uuidString && error == nil {
295+
if let error {
283296
connect(peripheral: peripheral) {}
284-
} else if let error {
285297
log(error.localizedDescription, caller: "didDisconnectPeripheral", target: .ble)
286298
}
287299
}

0 commit comments

Comments
 (0)