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:
rene 2026-05-30 09:25:48 +02:00
commit 81681130e6
20 changed files with 1129 additions and 0 deletions

View 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
}
}

View 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
View 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]?
}