-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDemo_code_RainAlert.swift
52 lines (47 loc) · 1.6 KB
/
Demo_code_RainAlert.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//
// ContentView.swift
// RainAlert
//
// Created by Leo Wang on 2022-09-17.
//
// Basic demonstration of the premise of how to retrieve weather data and a basic UI
//
// To fully implement app, still need to create functions to process the data, and send push notification
//
// Uses WeatherKit API to get weather data and CoreLocation API to provide location
import SwiftUI
import WeatherKit
import CoreLocation
struct ContentView: View {
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundColor(.accentColor)
Text("Weather App")
// Button to get weather data
Button("Get Day Weather", action: {
getCurrWeather(location: testLocation) // button that calls function to get weather data
})
}
.padding()
}
let weatherService = WeatherService() //creates a WeatherService object
let testLocation = CLLocation(latitude: 49.2827, longitude: 123.1207) // creates test location (Vancouver)
// gets current weather at given location
func getCurrWeather(location: CLLocation) {
Task {
do {
let currWeather = try await weatherService.weather(for: location) // stores current weather in currWeather
print(currWeather) // print result to console
} catch {
print("error getting weather")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
}