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

109 lines
4.5 KiB
Swift
Raw 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 RouteView: View {
@EnvironmentObject var appState: AppState
@State private var firstStationName: String = "Загрузка..."
@State private var lastStationName: String = "Загрузка..."
@State private var engStationsName: String = "Загрузка..."
private var topBackgroundColor = Color(hex: 0xFCD500)
var body: some View {
NavigationLink(destination: RouteSelectionView()) {
GeometryReader { geo in
VStack(spacing: 0) {
// Верхняя половина: номер маршрута
VStack {
if let route = appState.selectedRoute {
Text("\(route.routeNumber)")
.font(.system(size: 36, weight: .black))
.foregroundColor(.black)
.multilineTextAlignment(.center)
.frame(maxHeight: .infinity, alignment: .center)
}
}
.frame(height: geo.size.height / 2)
.frame(maxWidth: .infinity)
.background(topBackgroundColor)
.cornerRadius(16, corners: [.topLeft, .topRight])
// Нижняя половина: станции
VStack(spacing: 0) {
MarqueeText(
text: firstStationName,
font: .headline.bold(),
foregroundColor: .white
)
MarqueeText(
text: lastStationName,
font: .headline.bold(),
foregroundColor: .white
)
MarqueeText(
text: engStationsName,
font: .caption,
foregroundColor: .white.opacity(0.5)
)
}
.padding(10)
.frame(height: geo.size.height / 2)
.frame(maxWidth: .infinity)
.background(
ZStack {
Color(hex: 0x806C59)
LinearGradient(
stops: [
.init(color: .white.opacity(0.0), location: 0.0871),
.init(color: .white.opacity(0.16), location: 0.6969)
],
startPoint: .bottomLeading,
endPoint: .topTrailing
)
}
)
.cornerRadius(16, corners: [.bottomLeft, .bottomRight])
}
}
.aspectRatio(1, contentMode: .fit)
.shadow(color: Color.black.opacity(0.10), radius: 8, x: 0, y: 2)
}
.buttonStyle(.plain)
.onChange(of: appState.selectedRoute?.id) { newRouteID in
guard let routeID = newRouteID else { return }
Task {
await fetchStations(forRoute: routeID)
}
}
}
// MARK: - Fetch Stations
private func fetchStations(forRoute routeID: Int) async {
firstStationName = "Загрузка..."
lastStationName = "Загрузка..."
engStationsName = "Loading..."
guard let url = URL(string: "https://white-nights.krbl.ru/services/content/route/\(routeID)/station"),
let urlEng = URL(string: "https://white-nights.krbl.ru/services/content/route/\(routeID)/station?lang=en") else { return }
do {
let (data, _) = try await URLSession.shared.data(from: url)
let (dataEn, _) = try await URLSession.shared.data(from: urlEng)
let stations = try JSONDecoder().decode([Station].self, from: data)
let stationsEn = try JSONDecoder().decode([Station].self, from: dataEn)
if let firstStation = stations.first { firstStationName = firstStation.name }
if let lastStation = stations.last { lastStationName = lastStation.name }
let firstStationEn = stationsEn.first?.name ?? "Loading..."
let lastStationEn = stationsEn.last?.name ?? "Loading..."
engStationsName = "\(firstStationEn) - \(lastStationEn)"
} catch {
print("Ошибка загрузки станций: \(error)")
}
}
}