cards-native/Sources/Features/Study/CardRenderer.swift
Till JS 3b861af3fb v0.3.0 — Phase β-2 Study-Loop
Voller Lern-Flow mit Web-Parität: fällige Karten via /reviews/due
laden, flip + rate (4 Buttons + Haptic), Grades via Offline-Queue
ans Server-FSRS schicken.

- Card/Review/DueReview DTOs mit snake_case + camelCase-deckId-
  Sonderfall im embedded card-Subobjekt
- CardType-Enum (alle 7 Typen), Rating-Enum mit deutschen Labels
- Cloze-Helper 1:1-Port aus cards-domain (extractClusterIds,
  subIndexCount, clusterId, renderPrompt/Answer, hint)
- CardsAPI.dueReviews(deckId:) + gradeReview(cardId,subIndex,rating,reviewedAt)
- PendingGrade SwiftData-Model + GradeQueue (FIFO-Drain, originaler
  Timestamp bleibt, bei Netzfehler in Queue, Retry beim nächsten Drain)
- StudySession @Observable State-Machine
- CardRenderer für basic, basic-reverse, cloze; Placeholder für
  image-occlusion/audio-front/typing/multiple-choice (β-3/β-4)
- RatingBar mit UIImpactFeedbackGenerator (medium/heavy)
- StudySessionView per NavigationLink aus DeckListView
- 9 neue Tests (Cloze: 8, Review-Decoding: 3), insgesamt 17 grün

Server-authoritative FSRS bleibt — kein ts-fsrs-Port.
Endurance-Test auf realem Gerät steht aus (siehe PLAN.md).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-13 00:16:11 +02:00

88 lines
3 KiB
Swift

import SwiftUI
/// Rendert die Karten-Inhalte je nach `CardType`. Front-/Back-Seite
/// werden über `isFlipped` gesteuert.
///
/// β-2 deckt `basic`, `basic-reverse`, `cloze` ab. Restliche Typen
/// zeigen einen Placeholder mit Hinweis auf die kommende Phase.
struct CardRenderer: View {
let card: ReviewCard
let subIndex: Int
let isFlipped: Bool
var body: some View {
Group {
switch card.type {
case .basic:
basicView(front: "front", back: "back")
case .basicReverse:
// sub_index 0 = frontback, sub_index 1 = backfront
if subIndex == 0 {
basicView(front: "front", back: "back")
} else {
basicView(front: "back", back: "front")
}
case .cloze:
clozeView
case .imageOcclusion, .audioFront, .typing, .multipleChoice:
placeholderView
}
}
.padding(24)
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
@ViewBuilder
private func basicView(front frontKey: String, back backKey: String) -> some View {
VStack(spacing: 16) {
text(card.fields[frontKey] ?? "")
.font(.title2)
.foregroundStyle(CardsTheme.foreground)
if isFlipped {
Divider().background(CardsTheme.border)
text(card.fields[backKey] ?? "")
.font(.title3)
.foregroundStyle(CardsTheme.mutedForeground)
}
}
}
@ViewBuilder
private var clozeView: some View {
let raw = card.fields["text"] ?? ""
let clusterId = Cloze.clusterId(for: raw, subIndex: subIndex) ?? 1
let rendered = isFlipped
? Cloze.renderAnswer(raw, activeClusterId: clusterId)
: Cloze.renderPrompt(raw, activeClusterId: clusterId)
VStack(spacing: 12) {
text(rendered)
.font(.title3)
.foregroundStyle(CardsTheme.foreground)
}
}
@ViewBuilder
private var placeholderView: some View {
VStack(spacing: 8) {
Image(systemName: "questionmark.square.dashed")
.font(.largeTitle)
.foregroundStyle(CardsTheme.mutedForeground)
Text("Card-Type »\(card.type.rawValue)« kommt in einer späteren Phase")
.font(.caption)
.multilineTextAlignment(.center)
.foregroundStyle(CardsTheme.mutedForeground)
}
}
/// Markdown-Bold (`**...**`) wird auf SwiftUI's AttributedString gemappt.
private func text(_ markdown: String) -> some View {
let attributed = (try? AttributedString(
markdown: markdown,
options: AttributedString.MarkdownParsingOptions(
interpretedSyntax: .inlineOnlyPreservingWhitespace
)
)) ?? AttributedString(markdown)
return Text(attributed)
.multilineTextAlignment(.center)
}
}