Skip to content

Commit

Permalink
Update display of forecast days
Browse files Browse the repository at this point in the history
  • Loading branch information
liamcharger committed Mar 22, 2024
1 parent 8bc5509 commit ed5027b
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 49 deletions.
14 changes: 10 additions & 4 deletions InfiniLink/BLE/BLEManagerVariables.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class BLEManagerVal: NSObject, ObservableObject {

let cbuuidList = BLEManager.cbuuidList()
var musicChars = BLEManager.musicCharacteristics()

let settings = UserDefaults.standard

struct WeatherInformation {
Expand All @@ -40,18 +40,24 @@ class BLEManagerVal: NSObject, ObservableObject {
@Published var timeFormat: ClockType?

@Published var weatherInformation = WeatherInformation()
@Published var weatherForecastDays = [WeatherForecastDay]()
@Published var loadingWeather = true

@Published var heartBPM: Double = 0

@Published var firmwareVersion: String = ""

@Published var stepCount: Int = 0
@Published var stepCountTests: Int = 0
@Published var stepCounting: Int = 0

@Published var lastWeatherUpdateNWS: Int = 0
@Published var lastWeatherUpdateWAPI: Int = 0
@Published var latitude: Double = 0.0
@Published var longitude: Double = 0.0
}

struct WeatherForecastDay {
var maxTemperature: Double
var minTemperature: Double
var icon: UInt8
var name: String
}
71 changes: 34 additions & 37 deletions InfiniLink/Core/Weather/Views/WeatherDetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,19 @@ struct WeatherDetailView: View {
return "slash.circle"
}
}

var celsius: Bool {
(UnitTemperature.current == .celsius && deviceData.chosenWeatherMode == "System") || deviceData.chosenWeatherMode == "Metric"
}

let deviceData: DeviceData = DeviceData()
var dateFormatter = DateFormatter()

@State var forecastDates: [String] = []

init() {
dateFormatter.dateFormat = "E"
}

var body: some View {
VStack {
Expand Down Expand Up @@ -86,50 +94,41 @@ struct WeatherDetailView: View {
.foregroundColor(.gray)
.font(.body.weight(.semibold))
}
.padding(.bottom)
.padding()
Divider()
VStack {
Divider()
.padding(.horizontal, -16)
VStack(spacing: 8) {
HStack {
ForEach(bleManagerVal.weatherInformation.forecastIcon, id: \.self) { icon in
Image(systemName: getIcon(icon: Int(icon)))
.frame(maxWidth: .infinity, alignment: .center)
}
}
HStack {
ForEach(bleManagerVal.weatherInformation.forecastMaxTemperature, id: \.self) { temp in
Group {
if celsius {
Text(String(Int(round(temp))) + "°")
} else {
Text(String(Int(round(temp * 1.8 + 32))) + "°")
ScrollView(.horizontal, showsIndicators: false) {
HStack(spacing: 10) {
ForEach(bleManagerVal.weatherForecastDays, id: \.name) { day in
VStack(spacing: 6) {
Text(day.name)
.foregroundColor(.gray)
.font(.system(size: 14).weight(.medium))
Image(systemName: getIcon(icon: Int(day.icon)))
.imageScale(.large)
.font(.system(size: 18).weight(.medium))
VStack {
if celsius {
Text(String(Int(round(day.maxTemperature))) + "°")
Text(String(Int(round(day.minTemperature))) + "°")
} else {
Text(String(Int(round(day.maxTemperature * 1.8 + 32))) + "°")
Text(String(Int(round(day.minTemperature * 1.8 + 32))) + "°")
}
}
}
.frame(maxWidth: .infinity, alignment: .center)
}
}
HStack {
ForEach(bleManagerVal.weatherInformation.forecastMinTemperature, id: \.self) { temp in
Group {
if celsius {
Text(String(Int(round(temp))) + "°")
} else {
Text(String(Int(round(temp * 1.8 + 32))) + "°")
}
}
.frame(maxWidth: .infinity, alignment: .center)
.padding(12)
.frame(width: 95)
.background(Color.gray.opacity(0.3))
.cornerRadius(12)
}
}
.padding()
}
.padding(.vertical)
Divider()
.padding(.horizontal, -16)
}
Spacer()
}
}
.padding()
}
}
}
Expand All @@ -142,8 +141,6 @@ struct WeatherDetailView: View {
BLEManagerVal.shared.weatherInformation.temperature = 4
BLEManagerVal.shared.weatherInformation.maxTemperature = 3
BLEManagerVal.shared.weatherInformation.maxTemperature = 5
BLEManagerVal.shared.weatherInformation.forecastIcon = [4, 3, 0, 0, 4, 6, 2]
BLEManagerVal.shared.weatherInformation.forecastMaxTemperature = [35, 31, 39, 42, 53, 41, 37]
BLEManagerVal.shared.weatherInformation.forecastMinTemperature = [25, 22, 31, 32, 45, 36, 28]
BLEManagerVal.shared.weatherForecastDays.append(WeatherForecastDay(maxTemperature: 3, minTemperature: 2, icon: 2, name: "Sat"))
}
}
48 changes: 40 additions & 8 deletions InfiniLink/Core/Weather/WeatherController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ class WeatherController: NSObject, ObservableObject, CLLocationManagerDelegate {

private let locationManager = CLLocationManager()

let weatherapiKey : String = "cc80d76d17a740ebb8a160008241801"
let weatherapiBaseURL : String = "https://api.weatherapi.com/v1"
var weatherapiFailed : Bool = false
var nwsapiFailed : Bool = false
let weatherapiKey: String = "cc80d76d17a740ebb8a160008241801"
let weatherapiBaseURL: String = "https://api.weatherapi.com/v1"
var weatherapiFailed: Bool = false
var nwsapiFailed: Bool = false

enum WeatherAPI_Type {
case wapi, nws
Expand Down Expand Up @@ -193,11 +193,25 @@ class WeatherController: NSObject, ObservableObject, CLLocationManagerDelegate {
bleManagerVal.weatherInformation.shortDescription = json["forecast"]["forecastday"][0]["day"]["condition"]["text"].stringValue

if json["forecast"]["forecastday"][0].count > 1 {
let calendar = Calendar.current
let currentDate = Date()

for idx in 1...json["forecast"]["forecastday"][0].count {
bleManagerVal.weatherInformation.forecastIcon.append(getIcon_WAPI(description: json["forecast"]["forecastday"][idx]["day"]["condition"]["text"].stringValue))
bleManagerVal.weatherInformation.forecastMaxTemperature.append(json["forecast"]["forecastday"][idx]["day"]["maxtemp_c"].doubleValue)
bleManagerVal.weatherInformation.forecastMinTemperature.append(json["forecast"]["forecastday"][idx]["day"]["mintemp_c"].doubleValue)
if idx >= 5 {break}

if let futureDate = calendar.date(byAdding: .day, value: idx, to: currentDate) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE"
let dayName = dateFormatter.string(from: futureDate)

bleManagerVal.weatherForecastDays.append(WeatherForecastDay(maxTemperature: json["forecast"]["forecastday"][idx]["day"]["maxtemp_c"].doubleValue, minTemperature: json["forecast"]["forecastday"][idx]["day"]["mintemp_c"].doubleValue, icon: getIcon_WAPI(description: json["forecast"]["forecastday"][idx]["day"]["condition"]["text"].stringValue), name: dayName))
}

if idx >= 5 {
break
}
}
}

Expand Down Expand Up @@ -377,10 +391,28 @@ class WeatherController: NSObject, ObservableObject, CLLocationManagerDelegate {

bleManagerVal.weatherInformation.forecastMaxTemperature = []
bleManagerVal.weatherInformation.forecastMinTemperature = []

let currentDate = Date()
let calendar = Calendar.current

for idx in 1...json["properties"]["maxTemperature"]["values"].count {
bleManagerVal.weatherInformation.forecastMaxTemperature.append(json["properties"]["maxTemperature"]["values"][idx]["value"].doubleValue)
bleManagerVal.weatherInformation.forecastMinTemperature.append(json["properties"]["minTemperature"]["values"][idx]["value"].doubleValue)
if idx >= 5 {break}
let maxTemp = json["properties"]["maxTemperature"]["values"][idx]["value"].doubleValue
let minTemp = json["properties"]["minTemperature"]["values"][idx]["value"].doubleValue

bleManagerVal.weatherInformation.forecastMaxTemperature.append(maxTemp)
bleManagerVal.weatherInformation.forecastMinTemperature.append(minTemp)

if let futureDate = calendar.date(byAdding: .day, value: idx, to: currentDate) {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE"
let dayName = dateFormatter.string(from: futureDate)

bleManagerVal.weatherForecastDays.append(WeatherForecastDay(maxTemperature: maxTemp, minTemperature: minTemp, icon: getIcon_NWS(description: ""), name: dayName))
}

if idx >= 5 {
break
}
}

if bleManagerVal.weatherInformation.maxTemperature == 0.0 && bleManagerVal.weatherInformation.minTemperature == 0.0 {
Expand Down

0 comments on commit ed5027b

Please sign in to comment.