Skip to content

Commit

Permalink
UI fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
NanoSector committed Mar 27, 2022
1 parent d0069cc commit 10b92bf
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 31 deletions.
12 changes: 4 additions & 8 deletions Shared/BLEDeviceModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,29 @@ class BLEDeviceModel: NSObject, ObservableObject, CBPeripheralDelegate {
@Published private(set) var hasTemperatureSupport = false
@Published private(set) var hasHumiditySupport = false

@Published private(set) var lastSynced: Date? = nil

@Published private(set) var batteryPercentage: Int? = nil
@Published private(set) var currentTime: Date? = nil
@Published private(set) var currentTemperature: Double? = nil
@Published private(set) var currentHumidity: Int? = nil

@Published private(set) var name: String

private var _peripheral: CBPeripheral

// MARK: Wrappers for CBPeripheral fields.

var name: String { peripheral.name ?? "Unknown name" }
var identifier: String { peripheral.identifier.uuidString }

// get-only wrapper
var peripheral: CBPeripheral { self._peripheral }

required init(_ peripheral: CBPeripheral) {
self._peripheral = peripheral
self.name = peripheral.name ?? "Unknown name"
super.init()
peripheral.delegate = self
}

func sync() {
lastSynced = Date()

batteryPercentage = nil
currentTime = nil

Expand Down Expand Up @@ -91,8 +88,7 @@ class BLEDeviceModel: NSObject, ObservableObject, CBPeripheralDelegate {
}

func peripheralDidUpdateName(_ peripheral: CBPeripheral) {
objectWillChange.send()
DateFormatter().string(from: lastSynced!)
self.name = peripheral.name ?? "Unknown name"
}

func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
Expand Down
17 changes: 6 additions & 11 deletions Shared/Views/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,16 @@ struct ContentView: View {
}
}
.toolbar {
if bleClient.scanning {
Button(action: {
Button(action: {
if bleClient.scanning {
bleClient.stopScan()
}) {
Image(systemName: "stop.circle.fill")
}
ProgressView().controlSize(.small)
} else {
Button(action: {
} else {
bleClient.triggerScan()
}) {
Image(systemName: "arrow.clockwise")
}
}) {
Image(systemName: bleClient.scanning ? "stop.circle.fill" : "arrow.clockwise")
}
}
}.navigationTitle("Discovered Devices")
}.environmentObject(bleClient)
.onDisappear {
bleClient.stopScan()
Expand Down
15 changes: 3 additions & 12 deletions Shared/Views/DeviceView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ struct DeviceView: View {
}
}

GroupBox(label: Text("Device information and discovered capabilities")) {

Text("\(peripheral.name) - \(peripheral.identifier)").font(.callout)

GroupBox(label: Text("Discovered capabilities")) {
VStack(alignment: .leading) {
HStack {
Image(systemName: peripheral.hasTimeSupport ? "checkmark.circle.fill" : "xmark.circle")
Expand All @@ -86,12 +83,6 @@ struct DeviceView: View {
}.padding()
}.padding()
}.toolbar {
if let lastSynced = peripheral.lastSynced {
Text(lastSynced, style: .relative).font(.footnote)
} else {
Text("Not synced")
}

Button(action: {
peripheral.sync()
}) {
Expand Down Expand Up @@ -122,12 +113,12 @@ struct DeviceView: View {
Text("Sync with device")
}
}.padding()
}
}.navigationTitle(peripheral.name)
}
}

// struct DeviceView_Previews: PreviewProvider {
// static var previews: some View {
// DeviceView()
// DeviceView(peripheral: BLEDeviceModel())
// }
// }

0 comments on commit 10b92bf

Please sign in to comment.