From 373aa9c5116503892979338bb171d1fa1852d5c0 Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Tue, 14 Mar 2023 14:33:27 -0700 Subject: [PATCH] Fix lint errors --- Meshtastic/Views/Nodes/DeviceMetricsLog.swift | 6 ++-- .../Views/Nodes/EnvironmentMetricsLog.swift | 2 +- Meshtastic/Views/Nodes/NodeDetail.swift | 31 +++++++++---------- Meshtastic/Views/Nodes/PositionLog.swift | 2 +- Meshtastic/Views/Settings/ShareChannels.swift | 2 +- 5 files changed, 20 insertions(+), 23 deletions(-) diff --git a/Meshtastic/Views/Nodes/DeviceMetricsLog.swift b/Meshtastic/Views/Nodes/DeviceMetricsLog.swift index 3456447b..98ba306b 100644 --- a/Meshtastic/Views/Nodes/DeviceMetricsLog.swift +++ b/Meshtastic/Views/Nodes/DeviceMetricsLog.swift @@ -23,7 +23,7 @@ struct DeviceMetricsLog: View { let data = node.telemetries?.filtered(using: NSPredicate(format: "metricsType == 0 && time !=nil && time >= %@", oneDayAgo! as CVarArg)) ?? [] if data.count > 0 { GroupBox(label: Label("battery.level.trend", systemImage: "battery.100")) { - Chart(data.array as! [TelemetryEntity], id: \.self) { + Chart(data.array as? [TelemetryEntity] ?? [], id: \.self) { LineMark( x: .value("Hour", $0.time!.formattedDate(format: "ha")), y: .value("Value", $0.batteryLevel) @@ -40,7 +40,7 @@ struct DeviceMetricsLog: View { let dateFormatString = (localeDateFormat ?? "MM/dd/YY j:mma").replacingOccurrences(of: ",", with: "") if UIDevice.current.userInterfaceIdiom == .pad || UIDevice.current.userInterfaceIdiom == .mac { // Add a table for mac and ipad - Table(node.telemetries!.reversed() as! [TelemetryEntity]) { + Table(node.telemetries?.reversed() as? [TelemetryEntity] ?? []) { TableColumn("battery.level") { dm in if dm.metricsType == 0 { @@ -102,7 +102,7 @@ struct DeviceMetricsLog: View { .font(.caption) .fontWeight(.bold) } - ForEach(node.telemetries!.reversed() as! [TelemetryEntity], id: \.self) { (dm: TelemetryEntity) in + ForEach(node.telemetries?.reversed() as? [TelemetryEntity] ?? [], id: \.self) { (dm: TelemetryEntity) in if dm.metricsType == 0 { GridRow { if dm.batteryLevel == 0 { diff --git a/Meshtastic/Views/Nodes/EnvironmentMetricsLog.swift b/Meshtastic/Views/Nodes/EnvironmentMetricsLog.swift index 4758a95f..13033dc9 100644 --- a/Meshtastic/Views/Nodes/EnvironmentMetricsLog.swift +++ b/Meshtastic/Views/Nodes/EnvironmentMetricsLog.swift @@ -90,7 +90,7 @@ struct EnvironmentMetricsLog: View { .font(.caption) .fontWeight(.bold) } - ForEach(node.telemetries!.reversed() as! [TelemetryEntity], id: \.self) { (em: TelemetryEntity) in + ForEach(node.telemetries?.reversed() as? [TelemetryEntity] ?? [], id: \.self) { (em: TelemetryEntity) in if em.metricsType == 1 { diff --git a/Meshtastic/Views/Nodes/NodeDetail.swift b/Meshtastic/Views/Nodes/NodeDetail.swift index cadf57e4..d914ec74 100644 --- a/Meshtastic/Views/Nodes/NodeDetail.swift +++ b/Meshtastic/Views/Nodes/NodeDetail.swift @@ -104,9 +104,8 @@ struct NodeDetail: View { Text("Today's Weather Forecast") .font(.title) .padding() - let nodeLocation = node.positions?.lastObject as! PositionEntity - - NodeWeatherForecastView(location: CLLocation(latitude: nodeLocation.nodeCoordinate!.latitude, longitude: nodeLocation.nodeCoordinate!.longitude) ) + let nodeLocation = node.positions?.lastObject as? PositionEntity + NodeWeatherForecastView(location: CLLocation(latitude: nodeLocation?.nodeCoordinate!.latitude ?? LocationHelper.currentLocation.latitude, longitude: nodeLocation?.nodeCoordinate!.longitude ?? LocationHelper.currentLocation.longitude) ) .frame(height: 250) } #else @@ -114,10 +113,8 @@ struct NodeDetail: View { Text("Today's Weather Forecast") .font(.title) .padding() - - let nodeLocation = node.positions?.lastObject as! PositionEntity - NodeWeatherForecastView(location: CLLocation(latitude: nodeLocation.nodeCoordinate!.latitude, longitude: nodeLocation.nodeCoordinate!.longitude) ) - .frame(height: 250) + let nodeLocation = node.positions?.lastObject as? PositionEntity + NodeWeatherForecastView(location: CLLocation(latitude: nodeLocation?.nodeCoordinate!.latitude ?? LocationHelper.currentLocation.latitude, longitude: nodeLocation?.nodeCoordinate!.longitude ?? LocationHelper.currentLocation.longitude) ).frame(height: 250) .presentationDetents([.medium]) .presentationDragIndicator(.automatic) } @@ -181,13 +178,13 @@ struct NodeDetail: View { if node.telemetries?.count ?? 0 >= 1 { - let mostRecent = node.telemetries?.lastObject as! TelemetryEntity + let mostRecent = node.telemetries?.lastObject as? TelemetryEntity Divider() VStack(alignment: .center) { - BatteryGauge(batteryLevel: Double(mostRecent.batteryLevel)) - if mostRecent.voltage > 0 { + BatteryGauge(batteryLevel: Double(mostRecent?.batteryLevel ?? 0)) + if mostRecent?.voltage ?? 0 > 0 { - Text(String(format: "%.2f", mostRecent.voltage) + " V") + Text(String(format: "%.2f", mostRecent?.voltage ?? 0.0) + " V") .font(.title) .foregroundColor(.gray) .fixedSize() @@ -290,13 +287,13 @@ struct NodeDetail: View { } if node.telemetries?.count ?? 0 >= 1 { - let mostRecent = node.telemetries?.lastObject as! TelemetryEntity + let mostRecent = node.telemetries?.lastObject as? TelemetryEntity Divider() VStack(alignment: .center) { - BatteryGauge(batteryLevel: Double(mostRecent.batteryLevel)) - if mostRecent.voltage > 0 { + BatteryGauge(batteryLevel: Double(mostRecent?.batteryLevel ?? 0)) + if mostRecent?.voltage ?? 0 > 0 { - Text(String(format: "%.2f", mostRecent.voltage) + " V") + Text(String(format: "%.2f", mostRecent?.voltage ?? 0) + " V") .font(.callout) .foregroundColor(.gray) .fixedSize() @@ -494,9 +491,9 @@ struct NodeDetail: View { if node.positions?.count ?? 0 > 0 { - let mostRecent = node.positions?.lastObject as! PositionEntity + let mostRecent = node.positions?.lastObject as? PositionEntity - let weather = try await WeatherService.shared.weather(for: mostRecent.nodeLocation!) + let weather = try await WeatherService.shared.weather(for: mostRecent?.nodeLocation ?? CLLocation(latitude: LocationHelper.currentLocation.latitude, longitude: LocationHelper.currentLocation.longitude)) condition = weather.currentWeather.condition temperature = weather.currentWeather.temperature humidity = Int(weather.currentWeather.humidity * 100) diff --git a/Meshtastic/Views/Nodes/PositionLog.swift b/Meshtastic/Views/Nodes/PositionLog.swift index d27997ba..dcb2e262 100644 --- a/Meshtastic/Views/Nodes/PositionLog.swift +++ b/Meshtastic/Views/Nodes/PositionLog.swift @@ -26,7 +26,7 @@ struct PositionLog: View { if UIDevice.current.userInterfaceIdiom == .pad || UIDevice.current.userInterfaceIdiom == .mac { // Add a table for mac and ipad - Table(node.positions!.reversed() as! [PositionEntity]) { + Table(node.positions?.reversed() as? [PositionEntity] ?? []) { TableColumn("SeqNo") { position in Text(String(position.seqNo)) } diff --git a/Meshtastic/Views/Settings/ShareChannels.swift b/Meshtastic/Views/Settings/ShareChannels.swift index 97cb1b98..44d9cbfc 100644 --- a/Meshtastic/Views/Settings/ShareChannels.swift +++ b/Meshtastic/Views/Settings/ShareChannels.swift @@ -290,7 +290,7 @@ struct ShareChannels: View { loRaConfig.sx126XRxBoostedGain = node?.loRaConfig?.sx126xRxBoostedGain ?? false channelSet.loraConfig = loRaConfig if node?.myInfo?.channels != nil && node?.myInfo?.channels?.count ?? 0 > 0 { - for ch in node!.myInfo!.channels!.array as! [ChannelEntity] { + for ch in node?.myInfo?.channels?.array as? [ChannelEntity] ?? [] { if ch.role > 0 { if ch.index == 0 && includeChannel0 || ch.index == 1 && includeChannel1 || ch.index == 2 && includeChannel2 || ch.index == 3 && includeChannel3 ||