Ban Yaro Go — Phase 1 Foundation
SwiftUI/SwiftData iOS-Client, redet mit https://banyaro.app FastAPI-Backend. Bundle-ID app.banyaro.ios, Xcode-26-Projekt mit synchronisierten Ordnern. Drin: - APIClient (URLSession + Bearer + convertFromSnakeCase Decoder) - KeychainStore + AuthSession (@Observable) für persistenten Login - LoginView, MainTabView, SettingsView (mit Logout) - RoutesListView + RouteDetailView mit MapKit-Polyline aus preview_track - DogsListView mit Foto-Avatar - App-Icon (Pfote auf Banyaro-Amber)
This commit is contained in:
commit
81681130e6
20 changed files with 1129 additions and 0 deletions
67
BanYaroGo/API/APIClient.swift
Normal file
67
BanYaroGo/API/APIClient.swift
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
import Foundation
|
||||
|
||||
final class APIClient {
|
||||
static let shared = APIClient()
|
||||
|
||||
let baseURL = URL(string: "https://banyaro.app")!
|
||||
var token: String?
|
||||
|
||||
private let session: URLSession = .shared
|
||||
private let decoder: JSONDecoder = {
|
||||
let d = JSONDecoder()
|
||||
d.keyDecodingStrategy = .convertFromSnakeCase
|
||||
return d
|
||||
}()
|
||||
private let encoder: JSONEncoder = {
|
||||
let e = JSONEncoder()
|
||||
return e
|
||||
}()
|
||||
|
||||
func get<T: Decodable>(_ path: String) async throws -> T {
|
||||
try await perform(method: "GET", path: path, body: nil)
|
||||
}
|
||||
|
||||
func post<T: Decodable, B: Encodable>(_ path: String, body: B) async throws -> T {
|
||||
let data = try encoder.encode(body)
|
||||
return try await perform(method: "POST", path: path, body: data)
|
||||
}
|
||||
|
||||
func post<T: Decodable>(_ path: String) async throws -> T {
|
||||
try await perform(method: "POST", path: path, body: nil)
|
||||
}
|
||||
|
||||
private func perform<T: Decodable>(method: String, path: String, body: Data?) async throws -> T {
|
||||
let url = baseURL.appending(path: path)
|
||||
var req = URLRequest(url: url)
|
||||
req.httpMethod = method
|
||||
req.setValue("application/json", forHTTPHeaderField: "Accept")
|
||||
if let token {
|
||||
req.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
|
||||
}
|
||||
if let body {
|
||||
req.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
||||
req.httpBody = body
|
||||
}
|
||||
|
||||
let (data, response) = try await session.data(for: req)
|
||||
guard let http = response as? HTTPURLResponse else {
|
||||
throw APIError.invalidResponse
|
||||
}
|
||||
guard (200..<300).contains(http.statusCode) else {
|
||||
let detail = Self.parseErrorDetail(from: data)
|
||||
throw APIError.server(status: http.statusCode, message: detail)
|
||||
}
|
||||
return try decoder.decode(T.self, from: data)
|
||||
}
|
||||
|
||||
/// FastAPI returns errors as {"detail": "message"} or {"detail": [{...}]}.
|
||||
private static func parseErrorDetail(from data: Data) -> String? {
|
||||
if let obj = try? JSONSerialization.jsonObject(with: data) as? [String: Any] {
|
||||
if let s = obj["detail"] as? String { return s }
|
||||
if let arr = obj["detail"] as? [[String: Any]],
|
||||
let first = arr.first,
|
||||
let msg = first["msg"] as? String { return msg }
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
16
BanYaroGo/API/APIError.swift
Normal file
16
BanYaroGo/API/APIError.swift
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import Foundation
|
||||
|
||||
enum APIError: LocalizedError {
|
||||
case invalidResponse
|
||||
case server(status: Int, message: String?)
|
||||
|
||||
var errorDescription: String? {
|
||||
switch self {
|
||||
case .invalidResponse:
|
||||
return "Ungültige Server-Antwort."
|
||||
case .server(let status, let message):
|
||||
if let msg = message, !msg.isEmpty { return msg }
|
||||
return "Fehler vom Server (HTTP \(status))."
|
||||
}
|
||||
}
|
||||
}
|
||||
60
BanYaroGo/API/DTOs.swift
Normal file
60
BanYaroGo/API/DTOs.swift
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
import Foundation
|
||||
|
||||
// MARK: - Auth
|
||||
|
||||
struct LoginRequest: Encodable {
|
||||
let email: String
|
||||
let password: String
|
||||
}
|
||||
|
||||
struct LoginResponse: Decodable {
|
||||
let token: String
|
||||
let name: String
|
||||
let isPremium: Bool
|
||||
}
|
||||
|
||||
// MARK: - Dogs
|
||||
|
||||
struct Dog: Decodable, Identifiable {
|
||||
let id: Int
|
||||
let name: String
|
||||
let rasse: String?
|
||||
let fotoUrl: String?
|
||||
let geburtstag: String?
|
||||
}
|
||||
|
||||
// MARK: - Routes
|
||||
|
||||
struct GPSPoint: Codable, Hashable {
|
||||
let lat: Double
|
||||
let lon: Double
|
||||
let alt: Double?
|
||||
}
|
||||
|
||||
struct RouteListItem: Decodable, Identifiable {
|
||||
let id: Int
|
||||
let userId: Int
|
||||
let name: String
|
||||
let beschreibung: String?
|
||||
let distanzKm: Double?
|
||||
let dauerMin: Int?
|
||||
let createdAt: String?
|
||||
let previewTrack: [GPSPoint]
|
||||
let fotoUrls: [String]?
|
||||
let userName: String?
|
||||
let isPublic: Bool?
|
||||
}
|
||||
|
||||
struct RouteDetail: Decodable, Identifiable {
|
||||
let id: Int
|
||||
let userId: Int
|
||||
let name: String
|
||||
let beschreibung: String?
|
||||
let distanzKm: Double?
|
||||
let dauerMin: Int?
|
||||
let gpsTrack: [GPSPoint]
|
||||
let fotoUrls: [String]?
|
||||
let createdAt: String?
|
||||
let userName: String?
|
||||
let dogIds: [Int]?
|
||||
}
|
||||
20
BanYaroGo/Assets.xcassets/AccentColor.colorset/Contents.json
Normal file
20
BanYaroGo/Assets.xcassets/AccentColor.colorset/Contents.json
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
{
|
||||
"colors" : [
|
||||
{
|
||||
"color" : {
|
||||
"color-space" : "srgb",
|
||||
"components" : {
|
||||
"alpha" : "1.000",
|
||||
"blue" : "0x3A",
|
||||
"green" : "0x84",
|
||||
"red" : "0xC4"
|
||||
}
|
||||
},
|
||||
"idiom" : "universal"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
}
|
||||
}
|
||||
BIN
BanYaroGo/Assets.xcassets/AppIcon.appiconset/AppIcon.png
Normal file
BIN
BanYaroGo/Assets.xcassets/AppIcon.appiconset/AppIcon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 721 KiB |
14
BanYaroGo/Assets.xcassets/AppIcon.appiconset/Contents.json
Normal file
14
BanYaroGo/Assets.xcassets/AppIcon.appiconset/Contents.json
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
{
|
||||
"images" : [
|
||||
{
|
||||
"filename" : "AppIcon.png",
|
||||
"idiom" : "universal",
|
||||
"platform" : "ios",
|
||||
"size" : "1024x1024"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
}
|
||||
}
|
||||
6
BanYaroGo/Assets.xcassets/Contents.json
Normal file
6
BanYaroGo/Assets.xcassets/Contents.json
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"info" : {
|
||||
"author" : "xcode",
|
||||
"version" : 1
|
||||
}
|
||||
}
|
||||
50
BanYaroGo/Auth/AuthSession.swift
Normal file
50
BanYaroGo/Auth/AuthSession.swift
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
import Foundation
|
||||
import Observation
|
||||
|
||||
@Observable
|
||||
@MainActor
|
||||
final class AuthSession {
|
||||
var token: String?
|
||||
var userName: String?
|
||||
var isPremium: Bool = false
|
||||
var isLoggingIn: Bool = false
|
||||
var errorMessage: String?
|
||||
|
||||
private let tokenKey = "by_token"
|
||||
|
||||
init() {
|
||||
if let savedToken = KeychainStore.read(tokenKey) {
|
||||
token = savedToken
|
||||
APIClient.shared.token = savedToken
|
||||
}
|
||||
}
|
||||
|
||||
var isLoggedIn: Bool { token != nil }
|
||||
|
||||
func login(email: String, password: String) async {
|
||||
isLoggingIn = true
|
||||
errorMessage = nil
|
||||
defer { isLoggingIn = false }
|
||||
do {
|
||||
let response: LoginResponse = try await APIClient.shared.post(
|
||||
"/api/auth/login",
|
||||
body: LoginRequest(email: email, password: password)
|
||||
)
|
||||
KeychainStore.save(response.token, for: tokenKey)
|
||||
APIClient.shared.token = response.token
|
||||
self.token = response.token
|
||||
self.userName = response.name
|
||||
self.isPremium = response.isPremium
|
||||
} catch {
|
||||
self.errorMessage = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
func logout() {
|
||||
KeychainStore.delete(tokenKey)
|
||||
APIClient.shared.token = nil
|
||||
token = nil
|
||||
userName = nil
|
||||
isPremium = false
|
||||
}
|
||||
}
|
||||
42
BanYaroGo/Auth/KeychainStore.swift
Normal file
42
BanYaroGo/Auth/KeychainStore.swift
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
import Foundation
|
||||
import Security
|
||||
|
||||
enum KeychainStore {
|
||||
static let service = "app.banyaro.ios"
|
||||
|
||||
static func save(_ value: String, for key: String) {
|
||||
let data = Data(value.utf8)
|
||||
let query: [String: Any] = [
|
||||
kSecClass as String: kSecClassGenericPassword,
|
||||
kSecAttrService as String: service,
|
||||
kSecAttrAccount as String: key,
|
||||
]
|
||||
SecItemDelete(query as CFDictionary)
|
||||
var item = query
|
||||
item[kSecValueData as String] = data
|
||||
SecItemAdd(item as CFDictionary, nil)
|
||||
}
|
||||
|
||||
static func read(_ key: String) -> String? {
|
||||
let query: [String: Any] = [
|
||||
kSecClass as String: kSecClassGenericPassword,
|
||||
kSecAttrService as String: service,
|
||||
kSecAttrAccount as String: key,
|
||||
kSecReturnData as String: true,
|
||||
kSecMatchLimit as String: kSecMatchLimitOne,
|
||||
]
|
||||
var item: CFTypeRef?
|
||||
let status = SecItemCopyMatching(query as CFDictionary, &item)
|
||||
guard status == errSecSuccess, let data = item as? Data else { return nil }
|
||||
return String(data: data, encoding: .utf8)
|
||||
}
|
||||
|
||||
static func delete(_ key: String) {
|
||||
let query: [String: Any] = [
|
||||
kSecClass as String: kSecClassGenericPassword,
|
||||
kSecAttrService as String: service,
|
||||
kSecAttrAccount as String: key,
|
||||
]
|
||||
SecItemDelete(query as CFDictionary)
|
||||
}
|
||||
}
|
||||
13
BanYaroGo/BanYaroGoApp.swift
Normal file
13
BanYaroGo/BanYaroGoApp.swift
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import SwiftUI
|
||||
|
||||
@main
|
||||
struct BanYaroGoApp: App {
|
||||
@State private var auth = AuthSession()
|
||||
|
||||
var body: some Scene {
|
||||
WindowGroup {
|
||||
RootView()
|
||||
.environment(auth)
|
||||
}
|
||||
}
|
||||
}
|
||||
96
BanYaroGo/Views/DogsListView.swift
Normal file
96
BanYaroGo/Views/DogsListView.swift
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
import SwiftUI
|
||||
|
||||
struct DogsListView: View {
|
||||
@State private var dogs: [Dog] = []
|
||||
@State private var isLoading = false
|
||||
@State private var errorMessage: String?
|
||||
|
||||
var body: some View {
|
||||
NavigationStack {
|
||||
content
|
||||
.navigationTitle("Hunde")
|
||||
.task { await load() }
|
||||
}
|
||||
}
|
||||
|
||||
@ViewBuilder
|
||||
private var content: some View {
|
||||
if isLoading && dogs.isEmpty {
|
||||
ProgressView()
|
||||
} else if let error = errorMessage, dogs.isEmpty {
|
||||
ContentUnavailableView(
|
||||
"Konnte Hunde nicht laden",
|
||||
systemImage: "wifi.slash",
|
||||
description: Text(error)
|
||||
)
|
||||
} else if dogs.isEmpty {
|
||||
ContentUnavailableView(
|
||||
"Keine Hunde",
|
||||
systemImage: "pawprint",
|
||||
description: Text("Lege deinen ersten Hund in der PWA an.")
|
||||
)
|
||||
} else {
|
||||
List(dogs) { dog in
|
||||
DogRow(dog: dog)
|
||||
}
|
||||
.refreshable { await load() }
|
||||
}
|
||||
}
|
||||
|
||||
private func load() async {
|
||||
isLoading = true
|
||||
errorMessage = nil
|
||||
defer { isLoading = false }
|
||||
do {
|
||||
dogs = try await APIClient.shared.get("/api/dogs")
|
||||
} catch {
|
||||
errorMessage = error.localizedDescription
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct DogRow: View {
|
||||
let dog: Dog
|
||||
|
||||
var body: some View {
|
||||
HStack(spacing: 12) {
|
||||
avatar
|
||||
.frame(width: 56, height: 56)
|
||||
.background(.background.secondary)
|
||||
.clipShape(Circle())
|
||||
|
||||
VStack(alignment: .leading, spacing: 2) {
|
||||
Text(dog.name).font(.headline)
|
||||
if let rasse = dog.rasse, !rasse.isEmpty {
|
||||
Text(rasse)
|
||||
.font(.subheadline)
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding(.vertical, 4)
|
||||
}
|
||||
|
||||
@ViewBuilder
|
||||
private var avatar: some View {
|
||||
if let path = dog.fotoUrl, let url = URL(string: "https://banyaro.app\(path)") {
|
||||
AsyncImage(url: url) { phase in
|
||||
switch phase {
|
||||
case .success(let img):
|
||||
img.resizable().scaledToFill()
|
||||
default:
|
||||
placeholder
|
||||
}
|
||||
}
|
||||
} else {
|
||||
placeholder
|
||||
}
|
||||
}
|
||||
|
||||
private var placeholder: some View {
|
||||
Image(systemName: "pawprint.fill")
|
||||
.font(.title2)
|
||||
.foregroundStyle(.secondary)
|
||||
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
||||
}
|
||||
}
|
||||
78
BanYaroGo/Views/LoginView.swift
Normal file
78
BanYaroGo/Views/LoginView.swift
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
import SwiftUI
|
||||
|
||||
struct LoginView: View {
|
||||
@Environment(AuthSession.self) private var auth
|
||||
|
||||
@State private var email = ""
|
||||
@State private var password = ""
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 24) {
|
||||
Spacer()
|
||||
|
||||
Image(systemName: "pawprint.fill")
|
||||
.font(.system(size: 80))
|
||||
.foregroundStyle(Color.accentColor)
|
||||
|
||||
VStack(spacing: 6) {
|
||||
Text("Ban Yaro Go")
|
||||
.font(.largeTitle.bold())
|
||||
Text("Melde dich mit deinem banyaro-Account an.")
|
||||
.font(.callout)
|
||||
.foregroundStyle(.secondary)
|
||||
.multilineTextAlignment(.center)
|
||||
}
|
||||
|
||||
VStack(spacing: 12) {
|
||||
TextField("E-Mail", text: $email)
|
||||
.textContentType(.emailAddress)
|
||||
.keyboardType(.emailAddress)
|
||||
.textInputAutocapitalization(.never)
|
||||
.autocorrectionDisabled()
|
||||
.padding()
|
||||
.background(.background.secondary, in: RoundedRectangle(cornerRadius: 12))
|
||||
|
||||
SecureField("Passwort", text: $password)
|
||||
.textContentType(.password)
|
||||
.padding()
|
||||
.background(.background.secondary, in: RoundedRectangle(cornerRadius: 12))
|
||||
}
|
||||
|
||||
if let error = auth.errorMessage {
|
||||
Text(error)
|
||||
.font(.footnote)
|
||||
.foregroundStyle(.red)
|
||||
.multilineTextAlignment(.center)
|
||||
}
|
||||
|
||||
Button {
|
||||
Task {
|
||||
await auth.login(
|
||||
email: email.trimmingCharacters(in: .whitespacesAndNewlines),
|
||||
password: password
|
||||
)
|
||||
}
|
||||
} label: {
|
||||
Group {
|
||||
if auth.isLoggingIn {
|
||||
ProgressView().tint(.white)
|
||||
} else {
|
||||
Text("Login").bold()
|
||||
}
|
||||
}
|
||||
.frame(maxWidth: .infinity, minHeight: 50)
|
||||
}
|
||||
.background(Color.accentColor, in: RoundedRectangle(cornerRadius: 12))
|
||||
.foregroundStyle(.white)
|
||||
.disabled(auth.isLoggingIn || email.isEmpty || password.isEmpty)
|
||||
|
||||
Spacer()
|
||||
}
|
||||
.padding(.horizontal, 28)
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
LoginView()
|
||||
.environment(AuthSession())
|
||||
}
|
||||
16
BanYaroGo/Views/MainTabView.swift
Normal file
16
BanYaroGo/Views/MainTabView.swift
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import SwiftUI
|
||||
|
||||
struct MainTabView: View {
|
||||
var body: some View {
|
||||
TabView {
|
||||
RoutesListView()
|
||||
.tabItem { Label("Touren", systemImage: "map.fill") }
|
||||
|
||||
DogsListView()
|
||||
.tabItem { Label("Hunde", systemImage: "pawprint.fill") }
|
||||
|
||||
SettingsView()
|
||||
.tabItem { Label("Mehr", systemImage: "person.crop.circle") }
|
||||
}
|
||||
}
|
||||
}
|
||||
42
BanYaroGo/Views/MiniRouteMap.swift
Normal file
42
BanYaroGo/Views/MiniRouteMap.swift
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
import SwiftUI
|
||||
import MapKit
|
||||
|
||||
/// Non-interactive map showing a polyline for a GPS track. Suitable for
|
||||
/// list-row previews as well as larger detail headers.
|
||||
struct MiniRouteMap: View {
|
||||
let track: [GPSPoint]
|
||||
var lineWidth: CGFloat = 3
|
||||
|
||||
var body: some View {
|
||||
Map(initialPosition: .region(region)) {
|
||||
MapPolyline(coordinates: coordinates)
|
||||
.stroke(Color.accentColor, style: StrokeStyle(lineWidth: lineWidth, lineJoin: .round))
|
||||
}
|
||||
.mapStyle(.standard(elevation: .flat, pointsOfInterest: .excludingAll))
|
||||
.allowsHitTesting(false)
|
||||
}
|
||||
|
||||
private var coordinates: [CLLocationCoordinate2D] {
|
||||
track.map { CLLocationCoordinate2D(latitude: $0.lat, longitude: $0.lon) }
|
||||
}
|
||||
|
||||
private var region: MKCoordinateRegion {
|
||||
let lats = track.map(\.lat)
|
||||
let lons = track.map(\.lon)
|
||||
let minLat = lats.min() ?? 0
|
||||
let maxLat = lats.max() ?? 0
|
||||
let minLon = lons.min() ?? 0
|
||||
let maxLon = lons.max() ?? 0
|
||||
let center = CLLocationCoordinate2D(
|
||||
latitude: (minLat + maxLat) / 2,
|
||||
longitude: (minLon + maxLon) / 2
|
||||
)
|
||||
// Padding ~20% beyond bounding box; minimum span so very small tracks stay visible.
|
||||
let latDelta = max((maxLat - minLat) * 1.4, 0.002)
|
||||
let lonDelta = max((maxLon - minLon) * 1.4, 0.002)
|
||||
return MKCoordinateRegion(
|
||||
center: center,
|
||||
span: MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: lonDelta)
|
||||
)
|
||||
}
|
||||
}
|
||||
13
BanYaroGo/Views/RootView.swift
Normal file
13
BanYaroGo/Views/RootView.swift
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import SwiftUI
|
||||
|
||||
struct RootView: View {
|
||||
@Environment(AuthSession.self) private var auth
|
||||
|
||||
var body: some View {
|
||||
if auth.isLoggedIn {
|
||||
MainTabView()
|
||||
} else {
|
||||
LoginView()
|
||||
}
|
||||
}
|
||||
}
|
||||
121
BanYaroGo/Views/RouteDetailView.swift
Normal file
121
BanYaroGo/Views/RouteDetailView.swift
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
import SwiftUI
|
||||
|
||||
struct RouteDetailView: View {
|
||||
let routeId: Int
|
||||
let fallbackName: String
|
||||
|
||||
@State private var detail: RouteDetail?
|
||||
@State private var isLoading = false
|
||||
@State private var errorMessage: String?
|
||||
|
||||
var body: some View {
|
||||
ScrollView {
|
||||
VStack(alignment: .leading, spacing: 16) {
|
||||
if let detail {
|
||||
MiniRouteMap(track: detail.gpsTrack, lineWidth: 4)
|
||||
.frame(height: 320)
|
||||
.clipShape(RoundedRectangle(cornerRadius: 16))
|
||||
.padding(.horizontal)
|
||||
|
||||
HStack(spacing: 12) {
|
||||
StatTile(value: formatKm(detail.distanzKm), label: "Distanz", icon: "ruler")
|
||||
StatTile(value: formatMin(detail.dauerMin), label: "Dauer", icon: "clock")
|
||||
StatTile(value: "\(detail.gpsTrack.count)", label: "Punkte", icon: "point.3.connected.trianglepath.dotted")
|
||||
}
|
||||
.padding(.horizontal)
|
||||
|
||||
if let beschreibung = detail.beschreibung, !beschreibung.isEmpty {
|
||||
Text(beschreibung)
|
||||
.font(.body)
|
||||
.padding(.horizontal)
|
||||
}
|
||||
|
||||
if let urls = detail.fotoUrls, !urls.isEmpty {
|
||||
VStack(alignment: .leading, spacing: 8) {
|
||||
Text("Fotos").font(.headline)
|
||||
ScrollView(.horizontal, showsIndicators: false) {
|
||||
HStack(spacing: 10) {
|
||||
ForEach(urls, id: \.self) { path in
|
||||
photoThumb(path)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding(.horizontal)
|
||||
}
|
||||
|
||||
Spacer(minLength: 24)
|
||||
} else if isLoading {
|
||||
ProgressView().padding(.top, 80)
|
||||
} else if let error = errorMessage {
|
||||
ContentUnavailableView(
|
||||
"Fehler",
|
||||
systemImage: "exclamationmark.triangle",
|
||||
description: Text(error)
|
||||
)
|
||||
.padding(.top, 60)
|
||||
}
|
||||
}
|
||||
}
|
||||
.navigationTitle(detail?.name ?? fallbackName)
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
.task { await load() }
|
||||
}
|
||||
|
||||
private func photoThumb(_ path: String) -> some View {
|
||||
let url = URL(string: "https://banyaro.app\(path)")
|
||||
return AsyncImage(url: url) { phase in
|
||||
switch phase {
|
||||
case .success(let img):
|
||||
img.resizable().scaledToFill()
|
||||
default:
|
||||
Rectangle().fill(.gray.opacity(0.15))
|
||||
}
|
||||
}
|
||||
.frame(width: 160, height: 160)
|
||||
.clipShape(RoundedRectangle(cornerRadius: 10))
|
||||
}
|
||||
|
||||
private func load() async {
|
||||
isLoading = true
|
||||
errorMessage = nil
|
||||
defer { isLoading = false }
|
||||
do {
|
||||
detail = try await APIClient.shared.get("/api/routes/\(routeId)")
|
||||
} catch {
|
||||
errorMessage = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
private func formatKm(_ km: Double?) -> String {
|
||||
guard let km else { return "—" }
|
||||
return String(format: "%.2f km", km)
|
||||
}
|
||||
|
||||
private func formatMin(_ mins: Int?) -> String {
|
||||
guard let mins else { return "—" }
|
||||
if mins >= 60 { return "\(mins / 60) h \(mins % 60) min" }
|
||||
return "\(mins) min"
|
||||
}
|
||||
}
|
||||
|
||||
private struct StatTile: View {
|
||||
let value: String
|
||||
let label: String
|
||||
let icon: String
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 6) {
|
||||
Image(systemName: icon)
|
||||
.foregroundStyle(Color.accentColor)
|
||||
Text(value)
|
||||
.font(.headline)
|
||||
Text(label)
|
||||
.font(.caption)
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
.frame(maxWidth: .infinity)
|
||||
.padding(.vertical, 12)
|
||||
.background(.background.secondary, in: RoundedRectangle(cornerRadius: 12))
|
||||
}
|
||||
}
|
||||
111
BanYaroGo/Views/RoutesListView.swift
Normal file
111
BanYaroGo/Views/RoutesListView.swift
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
import SwiftUI
|
||||
|
||||
struct RoutesListView: View {
|
||||
@State private var routes: [RouteListItem] = []
|
||||
@State private var isLoading = false
|
||||
@State private var errorMessage: String?
|
||||
|
||||
var body: some View {
|
||||
NavigationStack {
|
||||
content
|
||||
.navigationTitle("Touren")
|
||||
.task { await load() }
|
||||
}
|
||||
}
|
||||
|
||||
@ViewBuilder
|
||||
private var content: some View {
|
||||
if isLoading && routes.isEmpty {
|
||||
ProgressView()
|
||||
} else if let error = errorMessage, routes.isEmpty {
|
||||
ContentUnavailableView(
|
||||
"Konnte Touren nicht laden",
|
||||
systemImage: "wifi.slash",
|
||||
description: Text(error)
|
||||
)
|
||||
} else if routes.isEmpty {
|
||||
ContentUnavailableView(
|
||||
"Keine Touren",
|
||||
systemImage: "map",
|
||||
description: Text("Lege deine erste Gassi-Tour in der PWA an — oder warte auf Phase 2.")
|
||||
)
|
||||
} else {
|
||||
List(routes) { route in
|
||||
NavigationLink {
|
||||
RouteDetailView(routeId: route.id, fallbackName: route.name)
|
||||
} label: {
|
||||
RouteRowView(route: route)
|
||||
}
|
||||
}
|
||||
.refreshable { await load() }
|
||||
}
|
||||
}
|
||||
|
||||
private func load() async {
|
||||
isLoading = true
|
||||
errorMessage = nil
|
||||
defer { isLoading = false }
|
||||
do {
|
||||
routes = try await APIClient.shared.get("/api/routes")
|
||||
} catch {
|
||||
errorMessage = error.localizedDescription
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct RouteRowView: View {
|
||||
let route: RouteListItem
|
||||
|
||||
var body: some View {
|
||||
VStack(alignment: .leading, spacing: 8) {
|
||||
HStack {
|
||||
Text(route.name)
|
||||
.font(.headline)
|
||||
Spacer()
|
||||
if let km = route.distanzKm {
|
||||
Text(String(format: "%.1f km", km))
|
||||
.font(.subheadline.monospacedDigit())
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
}
|
||||
HStack(spacing: 12) {
|
||||
if let mins = route.dauerMin {
|
||||
Label("\(mins) min", systemImage: "clock")
|
||||
}
|
||||
if let date = route.createdAt {
|
||||
Text(DateUtil.format(date))
|
||||
}
|
||||
Spacer()
|
||||
}
|
||||
.font(.caption)
|
||||
.foregroundStyle(.secondary)
|
||||
|
||||
if route.previewTrack.count >= 2 {
|
||||
MiniRouteMap(track: route.previewTrack)
|
||||
.frame(height: 110)
|
||||
.clipShape(RoundedRectangle(cornerRadius: 10))
|
||||
}
|
||||
}
|
||||
.padding(.vertical, 4)
|
||||
}
|
||||
}
|
||||
|
||||
enum DateUtil {
|
||||
/// Parses backend timestamps (SQLite `YYYY-MM-DD HH:MM:SS` or ISO-8601)
|
||||
/// into a German short date.
|
||||
static func format(_ input: String) -> String {
|
||||
let parser = DateFormatter()
|
||||
parser.locale = Locale(identifier: "en_US_POSIX")
|
||||
parser.timeZone = TimeZone(identifier: "UTC")
|
||||
for format in ["yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd'T'HH:mm:ss", "yyyy-MM-dd'T'HH:mm:ssZ"] {
|
||||
parser.dateFormat = format
|
||||
if let date = parser.date(from: input) {
|
||||
let out = DateFormatter()
|
||||
out.locale = Locale(identifier: "de_DE")
|
||||
out.dateStyle = .medium
|
||||
return out.string(from: date)
|
||||
}
|
||||
}
|
||||
return String(input.prefix(10))
|
||||
}
|
||||
}
|
||||
27
BanYaroGo/Views/SettingsView.swift
Normal file
27
BanYaroGo/Views/SettingsView.swift
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import SwiftUI
|
||||
|
||||
struct SettingsView: View {
|
||||
@Environment(AuthSession.self) private var auth
|
||||
|
||||
var body: some View {
|
||||
NavigationStack {
|
||||
Form {
|
||||
Section("Account") {
|
||||
LabeledContent("Name", value: auth.userName ?? "—")
|
||||
LabeledContent("Premium", value: auth.isPremium ? "Ja" : "Nein")
|
||||
}
|
||||
Section {
|
||||
Button("Abmelden", role: .destructive) {
|
||||
auth.logout()
|
||||
}
|
||||
}
|
||||
Section("Über") {
|
||||
Text("Ban Yaro Go ist die native iOS-Ergänzung zur banyaro.app PWA. Phase 1: deine Touren ansehen.")
|
||||
.font(.footnote)
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
}
|
||||
.navigationTitle("Mehr")
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue