Deck-Liste mit Web-Parität: alle eigenen Decks aus cardecky-api, Card-/Due-Counts pro Deck (Web-Pattern: separate Calls), Pull-to- Refresh, Offline-Read via SwiftData, Inbox-Banner für Marketplace- Forks. - Deck-Codable-DTO mit snake_case-CodingKeys (DeckCategory, DeckVisibility, FsrsSettings) - ISO8601-Date-Decoder mit Fractional-Seconds-Toleranz - CardsAPI.listDecks() + cardCount() + dueCount() - CachedDeck SwiftData-Model mit lastFetchedAt - DeckListStore (API + Cache, paralleles Counts-Fetching via TaskGroup) - DeckListView mit forest-Theme, deck.color-Streifen, Inbox-Banner - AccountView mit Sign-out - DashboardView durch DeckListView ersetzt - 6 Unit-Tests + 1 UI-Test grün Phasen-Plan: mana/docs/playbooks/CARDS_NATIVE_GREENFIELD.md Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
88 lines
3.5 KiB
Swift
88 lines
3.5 KiB
Swift
import Foundation
|
|
import ManaCore
|
|
|
|
/// Cards-spezifischer API-Client. Wrapper um `AuthenticatedTransport`
|
|
/// aus ManaCore, der die Cardecky-Endpoints kennt.
|
|
actor CardsAPI {
|
|
private let transport: AuthenticatedTransport
|
|
private let decoder: JSONDecoder
|
|
|
|
init(auth: AuthClient) {
|
|
transport = AuthenticatedTransport(baseURL: AppConfig.apiBaseURL, auth: auth)
|
|
decoder = JSONDecoder()
|
|
decoder.dateDecodingStrategy = .iso8601withFractional
|
|
}
|
|
|
|
/// Health-Probe — verifiziert dass cardecky-api erreichbar ist
|
|
/// und der eigene JWT akzeptiert wird.
|
|
func healthCheck() async throws -> Bool {
|
|
let (_, http) = try await transport.request(path: "/healthz")
|
|
return http.statusCode == 200
|
|
}
|
|
|
|
// MARK: - Decks
|
|
|
|
/// `GET /api/v1/decks?archived=false` — alle aktiven Decks des Users.
|
|
/// Optional: `forkedFromMarketplaceOnly` filtert auf Inbox-Decks
|
|
/// (für den Inbox-Banner).
|
|
func listDecks(forkedFromMarketplaceOnly: Bool = false) async throws -> [Deck] {
|
|
var path = "/api/v1/decks"
|
|
if forkedFromMarketplaceOnly {
|
|
path += "?forked_from_marketplace=true"
|
|
}
|
|
let (data, http) = try await transport.request(path: path)
|
|
try ensureOK(http, data: data)
|
|
let body = try decoder.decode(DeckListResponse.self, from: data)
|
|
return body.decks
|
|
}
|
|
|
|
/// `GET /api/v1/cards?deck_id=...` — Anzahl Karten in einem Deck.
|
|
/// Web macht das pro Deck einzeln; identisches Pattern hier.
|
|
func cardCount(deckId: String) async throws -> Int {
|
|
let (data, http) = try await transport.request(path: "/api/v1/cards?deck_id=\(deckId)")
|
|
try ensureOK(http, data: data)
|
|
return try decoder.decode(CardListResponse.self, from: data).total
|
|
}
|
|
|
|
/// `GET /api/v1/reviews/due?deck_id=...&limit=500` — Anzahl fälliger
|
|
/// Reviews in einem Deck.
|
|
func dueCount(deckId: String) async throws -> Int {
|
|
let (data, http) = try await transport.request(
|
|
path: "/api/v1/reviews/due?deck_id=\(deckId)&limit=500"
|
|
)
|
|
try ensureOK(http, data: data)
|
|
return try decoder.decode(DueReviewsResponse.self, from: data).total
|
|
}
|
|
|
|
// MARK: - Helpers
|
|
|
|
private func ensureOK(_ http: HTTPURLResponse, data: Data) throws {
|
|
guard (200 ..< 300).contains(http.statusCode) else {
|
|
let message = (try? JSONDecoder().decode(CardsServerError.self, from: data))?.error
|
|
throw AuthError.serverError(status: http.statusCode, message: message)
|
|
}
|
|
}
|
|
}
|
|
|
|
private struct CardsServerError: Decodable {
|
|
let error: String?
|
|
}
|
|
|
|
extension JSONDecoder.DateDecodingStrategy {
|
|
/// Cards-API liefert ISO8601 mit Fractional-Seconds aus
|
|
/// `.toISOString()`. Standard-Strategy `.iso8601` akzeptiert die
|
|
/// fractional seconds nicht — wir nutzen einen eigenen Formatter.
|
|
static let iso8601withFractional: JSONDecoder.DateDecodingStrategy = .custom { decoder in
|
|
let container = try decoder.singleValueContainer()
|
|
let raw = try container.decode(String.self)
|
|
let f = ISO8601DateFormatter()
|
|
f.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
|
|
if let date = f.date(from: raw) { return date }
|
|
f.formatOptions = [.withInternetDateTime]
|
|
if let date = f.date(from: raw) { return date }
|
|
throw DecodingError.dataCorruptedError(
|
|
in: container,
|
|
debugDescription: "Cannot decode ISO8601 date: \(raw)"
|
|
)
|
|
}
|
|
}
|