Skip to content

Commit

Permalink
Improve code readability, implement step chart, fix warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
liamcharger committed Jan 31, 2025
1 parent b540129 commit d3c953f
Show file tree
Hide file tree
Showing 14 changed files with 313 additions and 279 deletions.
92 changes: 46 additions & 46 deletions InfiniLink/Assets.xcassets/AppIcon.appiconset/Contents.json
Original file line number Diff line number Diff line change
@@ -1,82 +1,76 @@
{
"images" : [
{
"idiom" : "ios-marketing",
"size" : "1024x1024",
"scale" : "1x",
"filename" : "icon_1024.png"
},
{
"idiom" : "iphone",
"filename" : "icon_40.png",
"size" : "20x20",
"scale" : "2x"
"idiom" : "iphone",
"scale" : "2x",
"size" : "20x20"
},
{
"scale" : "3x",
"filename" : "icon_60.png",
"size" : "20x20",
"idiom" : "iphone"
"idiom" : "iphone",
"scale" : "3x",
"size" : "20x20"
},
{
"idiom" : "iphone",
"filename" : "icon_58.png",
"idiom" : "iphone",
"scale" : "2x",
"size" : "29x29"
},
{
"size" : "29x29",
"scale" : "3x",
"filename" : "icon_87.png",
"idiom" : "iphone"
"idiom" : "iphone",
"scale" : "3x",
"size" : "29x29"
},
{
"scale" : "2x",
"idiom" : "iphone",
"filename" : "icon_80.png",
"idiom" : "iphone",
"scale" : "2x",
"size" : "40x40"
},
{
"filename" : "icon_120.png",
"scale" : "3x",
"idiom" : "iphone",
"scale" : "3x",
"size" : "40x40"
},
{
"scale" : "2x",
"size" : "60x60",
"filename" : "icon_120.png",
"idiom" : "iphone",
"filename" : "icon_120.png"
"scale" : "2x",
"size" : "60x60"
},
{
"size" : "60x60",
"filename" : "icon_180.png",
"idiom" : "iphone",
"scale" : "3x",
"idiom" : "iphone"
"size" : "60x60"
},
{
"size" : "20x20",
"filename" : "icon_20.png",
"idiom" : "ipad",
"scale" : "1x"
"scale" : "1x",
"size" : "20x20"
},
{
"scale" : "2x",
"size" : "20x20",
"filename" : "icon_40.png",
"idiom" : "ipad"
"idiom" : "ipad",
"scale" : "2x",
"size" : "20x20"
},
{
"size" : "29x29",
"scale" : "1x",
"filename" : "icon_29.png",
"idiom" : "ipad",
"filename" : "icon_29.png"
"scale" : "1x",
"size" : "29x29"
},
{
"size" : "29x29",
"scale" : "2x",
"filename" : "icon_58.png",
"idiom" : "ipad"
"idiom" : "ipad",
"scale" : "2x",
"size" : "29x29"
},
{
"filename" : "icon_40.png",
Expand All @@ -85,32 +79,38 @@
"size" : "40x40"
},
{
"size" : "40x40",
"filename" : "icon_80.png",
"idiom" : "ipad",
"scale" : "2x",
"filename" : "icon_80.png"
"size" : "40x40"
},
{
"idiom" : "ipad",
"filename" : "icon_76.png",
"size" : "76x76",
"scale" : "1x"
"idiom" : "ipad",
"scale" : "1x",
"size" : "76x76"
},
{
"idiom" : "ipad",
"size" : "76x76",
"filename" : "icon_152.png",
"scale" : "2x"
"idiom" : "ipad",
"scale" : "2x",
"size" : "76x76"
},
{
"size" : "83.5x83.5",
"filename" : "icon_167.png",
"idiom" : "ipad",
"scale" : "2x"
"scale" : "2x",
"size" : "83.5x83.5"
},
{
"filename" : "icon_1024.png",
"idiom" : "ios-marketing",
"scale" : "1x",
"size" : "1024x1024"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
}
50 changes: 26 additions & 24 deletions InfiniLink/BLE/BLEFS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,49 +12,50 @@ import SwiftUI

class BLEFSHandler: ObservableObject {
static var shared = BLEFSHandler()

let bleManager = BLEManager.shared
let dfuUpdater = DFUUpdater.shared

var informationTransfer : [InformationFS] = []
var readFileFS : ReadFileFS = ReadFileFS()
var writeFileFS : WriteFileFS = WriteFileFS()
var informationTransfer: [InformationFS] = []
var readFileFS: ReadFileFS = ReadFileFS()
var writeFileFS: WriteFileFS = WriteFileFS()

struct WriteFileFS {
var group = DispatchGroup()
var offset : Int = 0
var freeSpace : UInt32 = 0
var offset: Int = 0
var freeSpace: UInt32 = 0
var data = Data()
var completed : Bool = false
var valid : Bool = false
var completed: Bool = false
var valid: Bool = false
}

struct ReadFileFS {
var group = DispatchGroup()
var chunkOffset : UInt32 = 0
var totalLength : UInt32 = 0
var chunkLength : UInt32 = 0
var chunkOffset: UInt32 = 0
var totalLength: UInt32 = 0
var chunkLength: UInt32 = 0
var data = Data()
var completed : Bool = false
var valid : Bool = false
}

struct InformationFS {
var group = DispatchGroup()
var dirList : DirList = DirList()
var valid : Bool = false
var dirList: DirList = DirList()
var valid: Bool = false
}

struct DirList {
var parentPath = ""
var ls : [Dir] = []
var valid : Bool = false
var ls: [Dir] = []
var valid: Bool = false
}

struct Dir {
var modificationTime : Int = 0
var fileSize : Int = 0
var flags : Int = 0
var pathNames : String = ""
var modificationTime: Int = 0
var fileSize: Int = 0
var flags: Int = 0
var pathNames: String = ""
}

enum Commands : UInt8 {
Expand Down Expand Up @@ -98,7 +99,7 @@ class BLEFSHandler: ObservableObject {
@Published var progress: Int = 0
@Published var externalResourcesSize: Int = 0

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

func readFile(path: String, offset: UInt32) -> ReadFileFS {
var read = ReadFileFS()
read.group = DispatchGroup()
read.group.enter()
var writeData = Data()

readFileFS = ReadFileFS()
readFileFS.group = DispatchGroup()
readFileFS.group.enter()

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

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

Expand Down Expand Up @@ -494,7 +495,8 @@ class BLEFSHandler: ObservableObject {
switch responseData[1] {
case Responses.ok.rawValue:
informationTransfer[0].valid = true
default: break
default:
break
//print("error response code \(responseData[1])")
}
informationTransfer[0].group.leave()
Expand Down
30 changes: 21 additions & 9 deletions InfiniLink/BLE/BLEManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import SwiftUI
class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeripheralDelegate {
static let shared = BLEManager()

// BLECharacteristicHandler and DeviceManager both create an instance of BLEManager, so they need to lazy to avoid a crash
lazy var characteristicHandler = BLECharacteristicHandler()
lazy var deviceManager = DeviceManager.shared

let downloadManager = DownloadManager.shared

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

if let pairedDeviceID = pairedDeviceID,
let uuid = UUID(uuidString: pairedDeviceID), !isPairingNewDevice {

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

if let peripheral = peripherals.first, !isConnectedToPinetime {
// 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?
connect(peripheral: peripheral) {}
} else {
scanForNewDevices()
}
} else {
scanForNewDevices()
Expand All @@ -159,7 +163,6 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph
if peripheral.name == "InfiniTime" {
isConnecting = true
peripheralToConnect = peripheral
pairedDeviceID = peripheral.identifier.uuidString
central.connect(peripheralToConnect, options: nil)

completion()
Expand Down Expand Up @@ -195,21 +198,25 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph

func unpair(device: Device? = nil) {
if let pairedDevice {
// Delete the device object we have said for this watch
deviceManager.removeDevice(device ?? pairedDevice)
}
// Update the list of user watches
deviceManager.fetchAllDevices()

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

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

if device == nil {
// This only disconnects and removes the watch from the recognized device list. If using secure pairing, the bond will still be kept
// 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
disconnect()
startScanning()
}
Expand All @@ -218,6 +225,8 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph
func disconnect() {
if let infiniTime = infiniTime {
self.central.cancelPeripheralConnection(infiniTime)

// Update the rest of the app to reflect the disconnected state
self.infiniTime = nil
self.blefsTransfer = nil
self.currentTimeService = nil
Expand All @@ -230,9 +239,11 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph
}

func switchDevice(device: Device) {
// We just switched devices, update the UI
self.pairedDeviceID = device.uuid
self.pairedDevice = deviceManager.fetchDevice()
self.deviceManager.getSettings()

self.disconnect()
self.startScanning()
}
Expand All @@ -241,9 +252,9 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph
if let pairedDeviceID = pairedDeviceID, pairedDeviceID == peripheral.identifier.uuidString && !isPairingNewDevice {
connect(peripheral: peripheral) {}
}
if peripheral.name == "InfiniTime" && !newPeripherals.contains(where: { $0.identifier.uuidString == peripheral.identifier.uuidString }) {
if isPairingNewDevice {
if !deviceManager.watches.compactMap({ $0.uuid }).contains(peripheral.identifier.uuidString) {
if peripheral.name == "InfiniTime" && !newPeripherals.contains(where: { $0.identifier.uuidString == peripheral.identifier.uuidString }) { // The peripheral has not already been discovered
if isPairingNewDevice { // Only check if we know the device when pairing a new watch, because we don't one to show up
if !deviceManager.watches.compactMap({ $0.uuid }).contains(peripheral.identifier.uuidString) { // The discovered watch is not already paired
newPeripherals.append(peripheral)
}
} else {
Expand All @@ -268,20 +279,21 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph
}
}

// The connection was abruptly terminated, so we try connecting again
connect(peripheral: peripheral) {}
}

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

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

if pairedDeviceID != nil, pairedDeviceID == peripheral.identifier.uuidString && error == nil {
if let error {
connect(peripheral: peripheral) {}
} else if let error {
log(error.localizedDescription, caller: "didDisconnectPeripheral", target: .ble)
}
}
Expand Down
Loading

0 comments on commit d3c953f

Please sign in to comment.