Files
WhiteNights_iOS/WhiteNights/Widgets/RouteSelectionView.swift
2025-08-24 14:44:50 +03:00

53 lines
1.6 KiB
Swift

import SwiftUI
struct RouteSelectionView: View {
@EnvironmentObject var appState: AppState
@State private var routes: [Route] = []
@State private var isLoading = true
var body: some View {
VStack {
if isLoading {
ProgressView("Загрузка маршрутов...")
.padding()
} else {
List(routes, id: \.id) { route in
Button(action: {
appState.selectedRoute = route
}) {
HStack {
Text("\(route.routeNumber)")
.font(.headline)
}
.padding(.vertical, 8)
}
}
.listStyle(PlainListStyle())
}
}
.navigationTitle("Выберите маршрут")
.onAppear {
Task {
await fetchRoutes()
}
}
}
// MARK: - Fetch Routes
private func fetchRoutes() async {
isLoading = true
defer { isLoading = false }
guard let url = URL(string: "https://white-nights.krbl.ru/services/content/route") else { return }
do {
let (data, _) = try await URLSession.shared.data(from: url)
let fetchedRoutes = try JSONDecoder().decode([Route].self, from: data)
routes = fetchedRoutes
} catch {
print("Ошибка загрузки маршрутов: \(error)")
}
}
}