diff --git a/InfiniLink/BLE/BLEManagerVariables.swift b/InfiniLink/BLE/BLEManagerVariables.swift index 276a249..7854cb2 100644 --- a/InfiniLink/BLE/BLEManagerVariables.swift +++ b/InfiniLink/BLE/BLEManagerVariables.swift @@ -19,7 +19,7 @@ class BLEManagerVal: NSObject, ObservableObject { let cbuuidList = BLEManager.cbuuidList() var musicChars = BLEManager.musicCharacteristics() - + let settings = UserDefaults.standard struct WeatherInformation { @@ -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 +} diff --git a/InfiniLink/Core/Weather/Views/WeatherDetailView.swift b/InfiniLink/Core/Weather/Views/WeatherDetailView.swift index 05028fd..0032877 100644 --- a/InfiniLink/Core/Weather/Views/WeatherDetailView.swift +++ b/InfiniLink/Core/Weather/Views/WeatherDetailView.swift @@ -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 { @@ -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() } } } @@ -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")) } } diff --git a/InfiniLink/Core/Weather/WeatherController.swift b/InfiniLink/Core/Weather/WeatherController.swift index e380966..3697e89 100644 --- a/InfiniLink/Core/Weather/WeatherController.swift +++ b/InfiniLink/Core/Weather/WeatherController.swift @@ -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 @@ -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 + } } } @@ -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 {