Initial commit
This commit is contained in:
52
WhiteNights/Widgets/RouteSelectionView.swift
Normal file
52
WhiteNights/Widgets/RouteSelectionView.swift
Normal file
@ -0,0 +1,52 @@
|
||||
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)")
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user