Files
WhiteNights_iOS/WhiteNights/Widgets/RouteSelectionView.swift
15lu.akari a87a3d12ab big update
2025-08-26 23:37:39 +03:00

65 lines
2.2 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import SwiftUI
struct RouteSelectionView: View {
@EnvironmentObject var appState: AppState
@Environment(\.dismiss) private var dismiss
@State private var routes: [Route] = []
@State private var isLoading = true
var body: some View {
VStack {
if isLoading {
ProgressView(
appState.selectedLanguage == "ru" ? "Загрузка маршрутов..." :
appState.selectedLanguage == "zh" ? "正在加载路线..." :
"Loading routes..."
)
.padding()
} else {
List(routes, id: \.id) { route in
Button(action: {
appState.selectedRoute = route
dismiss()
}) {
HStack {
Text("\(route.routeNumber)")
.font(.headline)
}
.padding(.vertical, 8)
}
}
.listStyle(PlainListStyle())
}
}
.navigationTitle(
appState.selectedLanguage == "ru" ? "Выберите маршрут" :
appState.selectedLanguage == "zh" ? "选择路线" :
"Select route"
)
.onAppear {
Task {
await fetchRoutes()
}
}
.onChange(of: appState.selectedLanguage) { _ in
// просто перерисовываем view, navigationTitle автоматически обновится
}
}
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)")
}
}
}