Skip to content

Commit ed5027b

Browse files
committed
Update display of forecast days
1 parent 8bc5509 commit ed5027b

3 files changed

Lines changed: 84 additions & 49 deletions

File tree

InfiniLink/BLE/BLEManagerVariables.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class BLEManagerVal: NSObject, ObservableObject {
1919

2020
let cbuuidList = BLEManager.cbuuidList()
2121
var musicChars = BLEManager.musicCharacteristics()
22-
22+
2323
let settings = UserDefaults.standard
2424

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

4242
@Published var weatherInformation = WeatherInformation()
43+
@Published var weatherForecastDays = [WeatherForecastDay]()
4344
@Published var loadingWeather = true
4445

4546
@Published var heartBPM: Double = 0
46-
47+
4748
@Published var firmwareVersion: String = ""
4849

4950
@Published var stepCount: Int = 0
50-
@Published var stepCountTests: Int = 0
51-
@Published var stepCounting: Int = 0
5251

5352
@Published var lastWeatherUpdateNWS: Int = 0
5453
@Published var lastWeatherUpdateWAPI: Int = 0
5554
@Published var latitude: Double = 0.0
5655
@Published var longitude: Double = 0.0
5756
}
57+
58+
struct WeatherForecastDay {
59+
var maxTemperature: Double
60+
var minTemperature: Double
61+
var icon: UInt8
62+
var name: String
63+
}

InfiniLink/Core/Weather/Views/WeatherDetailView.swift

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,19 @@ struct WeatherDetailView: View {
3030
return "slash.circle"
3131
}
3232
}
33+
3334
var celsius: Bool {
3435
(UnitTemperature.current == .celsius && deviceData.chosenWeatherMode == "System") || deviceData.chosenWeatherMode == "Metric"
3536
}
3637

3738
let deviceData: DeviceData = DeviceData()
39+
var dateFormatter = DateFormatter()
40+
41+
@State var forecastDates: [String] = []
42+
43+
init() {
44+
dateFormatter.dateFormat = "E"
45+
}
3846

3947
var body: some View {
4048
VStack {
@@ -86,50 +94,41 @@ struct WeatherDetailView: View {
8694
.foregroundColor(.gray)
8795
.font(.body.weight(.semibold))
8896
}
89-
.padding(.bottom)
97+
.padding()
98+
Divider()
9099
VStack {
91-
Divider()
92-
.padding(.horizontal, -16)
93-
VStack(spacing: 8) {
94-
HStack {
95-
ForEach(bleManagerVal.weatherInformation.forecastIcon, id: \.self) { icon in
96-
Image(systemName: getIcon(icon: Int(icon)))
97-
.frame(maxWidth: .infinity, alignment: .center)
98-
}
99-
}
100-
HStack {
101-
ForEach(bleManagerVal.weatherInformation.forecastMaxTemperature, id: \.self) { temp in
102-
Group {
103-
if celsius {
104-
Text(String(Int(round(temp))) + "°")
105-
} else {
106-
Text(String(Int(round(temp * 1.8 + 32))) + "°")
100+
ScrollView(.horizontal, showsIndicators: false) {
101+
HStack(spacing: 10) {
102+
ForEach(bleManagerVal.weatherForecastDays, id: \.name) { day in
103+
VStack(spacing: 6) {
104+
Text(day.name)
105+
.foregroundColor(.gray)
106+
.font(.system(size: 14).weight(.medium))
107+
Image(systemName: getIcon(icon: Int(day.icon)))
108+
.imageScale(.large)
109+
.font(.system(size: 18).weight(.medium))
110+
VStack {
111+
if celsius {
112+
Text(String(Int(round(day.maxTemperature))) + "°")
113+
Text(String(Int(round(day.minTemperature))) + "°")
114+
} else {
115+
Text(String(Int(round(day.maxTemperature * 1.8 + 32))) + "°")
116+
Text(String(Int(round(day.minTemperature * 1.8 + 32))) + "°")
117+
}
107118
}
108119
}
109-
.frame(maxWidth: .infinity, alignment: .center)
110-
}
111-
}
112-
HStack {
113-
ForEach(bleManagerVal.weatherInformation.forecastMinTemperature, id: \.self) { temp in
114-
Group {
115-
if celsius {
116-
Text(String(Int(round(temp))) + "°")
117-
} else {
118-
Text(String(Int(round(temp * 1.8 + 32))) + "°")
119-
}
120-
}
121-
.frame(maxWidth: .infinity, alignment: .center)
120+
.padding(12)
121+
.frame(width: 95)
122+
.background(Color.gray.opacity(0.3))
123+
.cornerRadius(12)
122124
}
123125
}
126+
.padding()
124127
}
125-
.padding(.vertical)
126-
Divider()
127-
.padding(.horizontal, -16)
128128
}
129129
Spacer()
130130
}
131131
}
132-
.padding()
133132
}
134133
}
135134
}
@@ -142,8 +141,6 @@ struct WeatherDetailView: View {
142141
BLEManagerVal.shared.weatherInformation.temperature = 4
143142
BLEManagerVal.shared.weatherInformation.maxTemperature = 3
144143
BLEManagerVal.shared.weatherInformation.maxTemperature = 5
145-
BLEManagerVal.shared.weatherInformation.forecastIcon = [4, 3, 0, 0, 4, 6, 2]
146-
BLEManagerVal.shared.weatherInformation.forecastMaxTemperature = [35, 31, 39, 42, 53, 41, 37]
147-
BLEManagerVal.shared.weatherInformation.forecastMinTemperature = [25, 22, 31, 32, 45, 36, 28]
144+
BLEManagerVal.shared.weatherForecastDays.append(WeatherForecastDay(maxTemperature: 3, minTemperature: 2, icon: 2, name: "Sat"))
148145
}
149146
}

InfiniLink/Core/Weather/WeatherController.swift

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ class WeatherController: NSObject, ObservableObject, CLLocationManagerDelegate {
2424

2525
private let locationManager = CLLocationManager()
2626

27-
let weatherapiKey : String = "cc80d76d17a740ebb8a160008241801"
28-
let weatherapiBaseURL : String = "https://api.weatherapi.com/v1"
29-
var weatherapiFailed : Bool = false
30-
var nwsapiFailed : Bool = false
27+
let weatherapiKey: String = "cc80d76d17a740ebb8a160008241801"
28+
let weatherapiBaseURL: String = "https://api.weatherapi.com/v1"
29+
var weatherapiFailed: Bool = false
30+
var nwsapiFailed: Bool = false
3131

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

195195
if json["forecast"]["forecastday"][0].count > 1 {
196+
let calendar = Calendar.current
197+
let currentDate = Date()
198+
196199
for idx in 1...json["forecast"]["forecastday"][0].count {
197200
bleManagerVal.weatherInformation.forecastIcon.append(getIcon_WAPI(description: json["forecast"]["forecastday"][idx]["day"]["condition"]["text"].stringValue))
198201
bleManagerVal.weatherInformation.forecastMaxTemperature.append(json["forecast"]["forecastday"][idx]["day"]["maxtemp_c"].doubleValue)
199202
bleManagerVal.weatherInformation.forecastMinTemperature.append(json["forecast"]["forecastday"][idx]["day"]["mintemp_c"].doubleValue)
200-
if idx >= 5 {break}
203+
204+
if let futureDate = calendar.date(byAdding: .day, value: idx, to: currentDate) {
205+
let dateFormatter = DateFormatter()
206+
dateFormatter.dateFormat = "EEE"
207+
let dayName = dateFormatter.string(from: futureDate)
208+
209+
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))
210+
}
211+
212+
if idx >= 5 {
213+
break
214+
}
201215
}
202216
}
203217

@@ -377,10 +391,28 @@ class WeatherController: NSObject, ObservableObject, CLLocationManagerDelegate {
377391

378392
bleManagerVal.weatherInformation.forecastMaxTemperature = []
379393
bleManagerVal.weatherInformation.forecastMinTemperature = []
394+
395+
let currentDate = Date()
396+
let calendar = Calendar.current
397+
380398
for idx in 1...json["properties"]["maxTemperature"]["values"].count {
381-
bleManagerVal.weatherInformation.forecastMaxTemperature.append(json["properties"]["maxTemperature"]["values"][idx]["value"].doubleValue)
382-
bleManagerVal.weatherInformation.forecastMinTemperature.append(json["properties"]["minTemperature"]["values"][idx]["value"].doubleValue)
383-
if idx >= 5 {break}
399+
let maxTemp = json["properties"]["maxTemperature"]["values"][idx]["value"].doubleValue
400+
let minTemp = json["properties"]["minTemperature"]["values"][idx]["value"].doubleValue
401+
402+
bleManagerVal.weatherInformation.forecastMaxTemperature.append(maxTemp)
403+
bleManagerVal.weatherInformation.forecastMinTemperature.append(minTemp)
404+
405+
if let futureDate = calendar.date(byAdding: .day, value: idx, to: currentDate) {
406+
let dateFormatter = DateFormatter()
407+
dateFormatter.dateFormat = "EEE"
408+
let dayName = dateFormatter.string(from: futureDate)
409+
410+
bleManagerVal.weatherForecastDays.append(WeatherForecastDay(maxTemperature: maxTemp, minTemperature: minTemp, icon: getIcon_NWS(description: ""), name: dayName))
411+
}
412+
413+
if idx >= 5 {
414+
break
415+
}
384416
}
385417

386418
if bleManagerVal.weatherInformation.maxTemperature == 0.0 && bleManagerVal.weatherInformation.minTemperature == 0.0 {

0 commit comments

Comments
 (0)