TempTrack-iOS/TempTrack/Views/DeviceInfoView.swift

161 lines
5.2 KiB
Swift
Raw Normal View History

2023-06-03 08:15:00 +02:00
import SwiftUI
import SFSafeSymbols
private let df: DateFormatter = {
let df = DateFormatter()
df.locale = .current
df.dateStyle = .short
df.timeStyle = .short
return df
}()
struct DeviceInfoView: View {
2023-06-03 08:15:00 +02:00
private let storageWarnBytes = 500
let info: DeviceInfo
@Binding
var isPresented: Bool
2023-06-03 08:15:00 +02:00
private var runTimeString: String {
let number = info.time.secondsSincePowerOn
2023-06-03 08:15:00 +02:00
guard number >= 60 else {
2023-06-13 17:14:57 +02:00
return "\(number) s"
2023-06-03 08:15:00 +02:00
}
let minutes = number / 60
guard minutes > 1 else {
2023-06-13 17:14:57 +02:00
return "1 min"
2023-06-03 08:15:00 +02:00
}
guard minutes >= 60 else {
2023-06-13 17:14:57 +02:00
return "\(minutes) min"
2023-06-03 08:15:00 +02:00
}
let hours = minutes / 60
guard hours > 1 else {
return "1 hour"
}
guard hours >= 60 else {
return "\(hours) hours"
}
let days = hours / 24
guard days > 1 else {
return "1 day"
}
return "\(days) days"
}
private var nextUpdateText: String {
2023-07-03 13:28:51 +02:00
let secs = -info.time.nextMeasurement.secondsToNow
2023-06-03 08:15:00 +02:00
guard secs > 1 else {
return "Now"
}
2023-06-13 17:14:57 +02:00
return "In \(secs) s"
2023-06-03 08:15:00 +02:00
}
private var storageIcon: SFSymbol {
if info.storageSize - info.numberOfRecordedBytes < storageWarnBytes {
return .externaldriveTrianglebadgeExclamationmark
}
return .externaldrive
}
private var storageText: String {
if info.storageSize <= 0 {
return "\(info.numberOfRecordedBytes)"
}
return "\(info.numberOfRecordedBytes) / \(info.storageSize) Bytes (\(info.storageFillPercentage) %)"
}
func sensorView(_ sensor: TemperatureSensor?, id: Int) -> some View {
2023-06-08 14:57:40 +02:00
VStack(alignment: .leading, spacing: 5) {
Text("Sensor \(id)")
.font(.headline)
if let sensor {
IconAndTextView(
icon: sensor.temperatureIcon,
text: "\(sensor.temperatureText) (\(sensor.updateText))")
IconAndTextView(
icon: .tag,
text: sensor.hexAddress)
2023-06-08 14:57:40 +02:00
} else {
IconAndTextView(
icon: .thermometerMediumSlash,
text: "Not connected")
2023-06-03 08:15:00 +02:00
}
}
}
var updateText: String {
guard info.time.date.secondsToNow > 3 else {
2023-06-08 14:57:40 +02:00
return "Updated Now"
}
return "Updated \(info.time.date.timePassedText)"
2023-06-08 14:57:40 +02:00
}
2023-06-03 08:15:00 +02:00
var body: some View {
2023-06-08 14:57:40 +02:00
NavigationView {
2023-06-03 08:15:00 +02:00
VStack(alignment: .leading, spacing: 5) {
2023-06-08 14:57:40 +02:00
VStack(alignment: .leading, spacing: 5) {
2023-06-13 17:14:57 +02:00
Text("System")
2023-06-08 14:57:40 +02:00
.font(.headline)
IconAndTextView(
icon: .power,
text: "\(df.string(from: info.time.deviceStartTime)) (\(runTimeString))")
2023-07-03 13:28:51 +02:00
IconAndTextView(
icon: .touchid,
text: "\(info.uniqueIdOfPowerCycle)")
IconAndTextView(
icon: .autostartstop,
text: "Wakeup: \(info.wakeupReason.text)")
2023-06-13 17:14:57 +02:00
}
VStack(alignment: .leading, spacing: 5) {
Text("Recording")
.font(.headline)
IconAndTextView(
icon: .stopwatch,
text: "\(nextUpdateText) (Every \(info.measurementInterval) s)")
2023-06-03 08:15:00 +02:00
}
2023-06-08 14:57:40 +02:00
VStack(alignment: .leading, spacing: 5) {
Text("Storage")
.font(.headline)
IconAndTextView(
icon: .speedometer,
text: "\(info.numberOfStoredMeasurements) Measurements (\(info.time.totalNumberOfMeasurements) total)")
IconAndTextView(
icon: storageIcon,
text: storageText)
IconAndTextView(
icon: .iphoneAndArrowForward,
text: "\(info.transferBlockSize) Byte Block Size")
2023-07-03 13:28:51 +02:00
IconAndTextView(
icon: .externaldriveBadgeCheckmark,
text: String(format: "0x%02X 0x%02X",
UInt8(info.dataChecksum >> 8), UInt8( info.dataChecksum & 0xFF)))
2023-06-03 08:15:00 +02:00
}
2023-06-08 14:57:40 +02:00
sensorView(info.sensor0, id: 0)
sensorView(info.sensor1, id: 1)
Spacer()
2023-06-03 08:15:00 +02:00
HStack {
Spacer()
2023-06-08 14:57:40 +02:00
TimelineView(.periodic(from: Date(), by: 1)) { context in
Text(updateText)
.font(.footnote)
.textCase(.uppercase)
}
2023-06-03 08:15:00 +02:00
Spacer()
}
}
2023-06-08 14:57:40 +02:00
.padding()
.navigationTitle("Device Info")
.navigationBarTitleDisplayMode(.large)
}
2023-06-03 08:15:00 +02:00
}
}
struct DeviceInfoView_Previews: PreviewProvider {
static var previews: some View {
DeviceInfoView(info: .mock, isPresented: .constant(true))
2023-06-03 08:15:00 +02:00
}
}