chore: wire in location services
Signed-off-by: Alan Brault <alan.brault@visus.io>
This commit is contained in:
+31
-1
@@ -6,12 +6,42 @@ import TempestasDomain
|
||||
@MainActor
|
||||
public final class MainViewModel {
|
||||
public private(set) var isLoading: Bool = false
|
||||
public private(set) var currentLocation: CurrentLocation?
|
||||
public private(set) var forecast: Forecast?
|
||||
public private(set) var loadError: Error?
|
||||
|
||||
private let forecastRepository: ForecastRepository
|
||||
private let locationService: LocationService
|
||||
|
||||
public init(
|
||||
forecastRepository: ForecastRepository
|
||||
forecastRepository: ForecastRepository,
|
||||
locationService: LocationService
|
||||
) {
|
||||
self.forecastRepository = forecastRepository
|
||||
self.locationService = locationService
|
||||
}
|
||||
|
||||
public func load(forceRefresh: Bool = false) async {
|
||||
guard !isLoading else { return }
|
||||
|
||||
isLoading = true
|
||||
defer { isLoading = false }
|
||||
|
||||
loadError = nil
|
||||
|
||||
do {
|
||||
await locationService.requestAuthorization()
|
||||
|
||||
let location = try await locationService.getCurrentLocation()
|
||||
currentLocation = location
|
||||
|
||||
forecast = try await forecastRepository.get(
|
||||
latitude: location.coordinate.latitude,
|
||||
longitude: location.coordinate.longitude,
|
||||
forceRefresh: forceRefresh
|
||||
)
|
||||
} catch {
|
||||
loadError = error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,16 +2,18 @@ import SwiftUI
|
||||
|
||||
public struct MainView: View {
|
||||
let viewModel: MainViewModel
|
||||
|
||||
public init (
|
||||
|
||||
public init(
|
||||
viewModel: MainViewModel
|
||||
) {
|
||||
self.viewModel = viewModel
|
||||
}
|
||||
|
||||
|
||||
public var body: some View {
|
||||
NavigationStack {
|
||||
|
||||
}.frame(width: 320, height: 400)
|
||||
NavigationStack {}
|
||||
.frame(width: 320, height: 400)
|
||||
.task {
|
||||
await viewModel.load()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user