-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthService.swift, APIService.swift
More file actions
109 lines (94 loc) · 3.48 KB
/
AuthService.swift, APIService.swift
File metadata and controls
109 lines (94 loc) · 3.48 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//
// AuthService.swift, APIService.swift
// StudyApp
//
// Created by Kevin Mello on 8/27/25.
//
import Foundation
import CoreLocation
import Combine
class APIService: ObservableObject {
static let shared = APIService()
@Published var studySpots: [StudySpot] = []
private let apiKey = "YOUR_GOOGLE_PLACES_API_KEY"
private init() { }
// MARK: - Fetch mock study spots (for testing)
func fetchMockStudySpots(near coordinate: CLLocationCoordinate2D) {
studySpots = [
StudySpot(
name: "Cafe A",
address: "123 Main St",
coordinate: CLLocationCoordinate2D(latitude: coordinate.latitude + 0.001, longitude: coordinate.longitude + 0.001),
hasWiFi: true,
hasCharging: false,
isQuiet: true
),
StudySpot(
name: "Library B",
address: "456 Elm St",
coordinate: CLLocationCoordinate2D(latitude: coordinate.latitude - 0.001, longitude: coordinate.longitude - 0.001),
hasWiFi: true,
hasCharging: true,
isQuiet: false
),
StudySpot(
name: "Quiet Study Room",
address: "789 Oak Ave",
coordinate: CLLocationCoordinate2D(latitude: coordinate.latitude + 0.002, longitude: coordinate.longitude - 0.001),
hasWiFi: false,
hasCharging: true,
isQuiet: true
)
]
}
// MARK: - Fetch from Google Places API
func fetchStudySpots(near location: CLLocationCoordinate2D) {
// Use mock if API key is empty
guard !apiKey.isEmpty else {
fetchMockStudySpots(near: location)
return
}
let urlString = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=\(location.latitude),\(location.longitude)&radius=1500&type=cafe&key=\(apiKey)"
guard let url = URL(string: urlString) else { return }
URLSession.shared.dataTask(with: url) { data, _, error in
guard let data = data, error == nil else { return }
do {
let response = try JSONDecoder().decode(GooglePlacesResponse.self, from: data)
DispatchQueue.main.async {
self.studySpots = response.results.map {
StudySpot(
name: $0.name,
address: $0.vicinity,
coordinate: CLLocationCoordinate2D(
latitude: $0.geometry.location.lat,
longitude: $0.geometry.location.lng
),
// Random attributes for demonstration
hasWiFi: Bool.random(),
hasCharging: Bool.random(),
isQuiet: Bool.random()
)
}
}
} catch {
print("Failed to decode: \(error)")
}
}.resume()
}
}
// MARK: - Google Places API structs
struct GooglePlacesResponse: Codable {
let results: [PlaceResult]
}
struct PlaceResult: Codable {
let name: String
let vicinity: String
let geometry: Geometry
}
struct Geometry: Codable {
let location: Location
}
struct Location: Codable {
let lat: Double
let lng: Double
}