cards-native/Sources/Features/Study/CardRenderer.swift
Till JS 8b1dd5158f feat(study): Multiple-Choice-Karten gerendert
CardRenderer für multipleChoice ist nicht mehr Placeholder. Web-
Vorbild: MultipleChoiceView.svelte.

MultipleChoiceCardView (Features/Study/):
- Lädt Distractors vom Server beim card.id-Wechsel
  (CardsAPI.distractors(deckId, cardId, field, count))
- Versucht erst field=answer, fallback field=back (für Decks mit
  basic/basic-reverse-Karten daneben)
- Fallback auf distractor_pool-Feld (newline-separated) wenn
  Deck zu klein
- 4 Optionen shuffled = [answer + 3 Distractors]
- User-Tap markiert Auswahl (kein erneutes Pick möglich)
- Vor Flip: nur Selected-Hint (primary border)
- Nach Flip: richtige = green-check, falsche-gewählte = red-cross,
  unselected richtige bleibt green-highlight
- Fallback "tooFew" (< 1 Distractor): zeigt Antwort nach Flip
  ohne Auswahl

CardsAPI.distractors → DistractorsResponse {distractors: [String]}.

Typing bleibt Placeholder — eigene UI-Pattern (Text-Input + Diff)
brauchen mehr Design-Arbeit, separate Phase.

Build 7 → 8, 35 Tests grün.

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

158 lines
6 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:
imageOcclusionView
case .audioFront:
audioFrontView
case .multipleChoice:
MultipleChoiceCardView(card: card, isFlipped: isFlipped)
case .typing:
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 imageOcclusionView: some View {
let imageRef = card.fields["image_ref"] ?? ""
let maskJSON = card.fields["mask_regions"] ?? "[]"
let regions = MaskRegions.parse(maskJSON)
let activeRegion = regions.indices.contains(subIndex) ? regions[subIndex] : nil
VStack(spacing: 12) {
GeometryReader { geo in
ZStack(alignment: .topLeading) {
RemoteImage(mediaId: imageRef, contentMode: .fit)
.frame(width: geo.size.width, height: geo.size.height)
ForEach(regions) { region in
let isActive = region.id == activeRegion?.id
// Front: aktive Maske opak, andere transparent.
// Back: alle Masken transparent (Bild komplett sichtbar).
if !isFlipped, isActive {
Rectangle()
.fill(CardsTheme.primary.opacity(0.92))
.frame(
width: region.w * geo.size.width,
height: region.h * geo.size.height
)
.offset(x: region.x * geo.size.width, y: region.y * geo.size.height)
.overlay(
Text(region.label?.isEmpty == false ? region.label! : "?")
.font(.caption.weight(.bold))
.foregroundStyle(CardsTheme.primaryForeground)
.offset(x: region.x * geo.size.width, y: region.y * geo.size.height),
alignment: .topLeading
)
}
}
}
}
.aspectRatio(4 / 3, contentMode: .fit)
if isFlipped, let label = activeRegion?.label, !label.isEmpty {
Text(label)
.font(.title3.weight(.semibold))
.foregroundStyle(CardsTheme.primary)
}
if let note = card.fields["note"], !note.isEmpty {
Text(note)
.font(.caption)
.foregroundStyle(CardsTheme.mutedForeground)
}
}
}
@ViewBuilder
private var audioFrontView: some View {
let audioRef = card.fields["audio_ref"] ?? ""
VStack(spacing: 16) {
AudioPlayerButton(mediaId: audioRef)
if isFlipped {
Divider().background(CardsTheme.border)
text(card.fields["back"] ?? "")
.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)
}
}