cards-native/Sources/Features/Study/StudySessionView.swift
Till JS 73f9081fa1 feat(decks): γ-1 bis γ-8 — AI/CSV-Import, Card-Edit, Pull-Update, Marketplace-Publish + Moderation + PDF
Vervollständigt die Cardecky-Web-Parität für Deck- und Card-Workflows.

γ-1+γ-2 (AI-Deck-Generierung)
- 4-Modi-Picker im DeckEditorView Create-Sheet: Leer/KI/Bild/CSV
- POST /api/v1/decks/generate für Text-Prompt + 10/min Rate-Limit-UI
- POST /api/v1/decks/from-image mit PhotosPicker + PDF-Importer
  (max 5 Files, 10 MiB/Bild, 30 MiB/PDF), Multipart-Body in
  CardsAPI+Generation
- Loading-Overlay mit Task-Cancellation, Error-Mapping für 429/413/502

γ-3 (Card-Edit)
- CardEditorView mit Mode .create(deckId:) / .edit(card:)
- Image-Occlusion + Audio-Front behalten bestehenden Media-Ref, solange
  User nicht ersetzt — MediaCache lädt Bild nach
- Type-Picker im Edit-Modus aus (Server-immutable)
- CardEditorPayload + CardEditorMediaFields als Sub-Views

γ-4 (Pull-Update + Duplicate + Archive)
- POST /marketplace/private/:id/pull-update mit Smart-Merge-Anzeige
- POST /decks/:id/duplicate
- Archive-Toggle im Edit-Modus, Server filtert Liste serverseitig
- DeckSecondaryActions als eigenes Sub-View

γ-6 (CSV-Import)
- RFC-4180-ish Parser (Quote-Escape, Header-Detect, BOM-strip)
- Preview-Liste + sequentielle Card-Inserts mit Live-Progress
- Image-Occlusion/Audio-Front werden geskipped (UI flaggt)

γ-7 (Marketplace-Publish) + Follow-up (Report + Block + Re-Publish)
- MarketplacePublishView mit lazy Author-Setup + Init + Publish 1.0.0
- Re-Publish-Modus: Picker für eigene Marketplace-Decks +
  Auto-Semver-Bump (Minor +1)
- MarketplaceCardConverter (typing → type-in, audio-front → skipped,
  image-occlusion → skipped — Server hat keinen MP-Media-Re-Upload)
- Toolbar-Menü auf PublicDeckView: „Deck melden …" + Author-Blockieren
  (App-Store-Guideline 5.1.1(v))
- ReportDeckSheet mit Reason-Picker (6 Kategorien) + optional Message
- BlockedAuthorsView in Settings mit Swipe-Entblocken

γ-8 (PDF-Export)
- DeckPrintView mit SFSafariViewController auf
  cardecky.mana.how/decks/:id/print — iOS Share-Sheet → PDF speichern

Side-Fixes (mid-stream)
- StudySessionView: Card-Aspect-Ratio springt nicht mehr beim Flip
  (Bottom-Bar in ZStack fixer Höhe)
- RootView: Glass-Pille für „Neues Deck"-Accessory + .guest- und
  .twoFactorRequired-Cases nachgezogen
- DeckListView: Account-Toolbar-Button entfernt (Account-Tab unten
  ist alleinige Anlaufstelle)

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

196 lines
6.8 KiB
Swift

import ManaCore
import SwiftData
import SwiftUI
#if canImport(UIKit)
import UIKit
#endif
/// Vollbild-Study-View. Wird per Navigation aus DeckListView geöffnet.
struct StudySessionView: View {
let deckId: String
let deckName: String
@Environment(AuthClient.self) private var auth
@Environment(\.modelContext) private var context
@Environment(\.dismiss) private var dismiss
@State private var session: StudySession?
var body: some View {
ZStack {
CardsTheme.background.ignoresSafeArea()
content
}
.navigationTitle(deckName)
#if os(iOS)
.navigationBarTitleDisplayMode(.inline)
#endif
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
if let session, case .studying = session.phase {
Text("\(session.remaining)")
.font(.subheadline.weight(.semibold))
.foregroundStyle(CardsTheme.mutedForeground)
.accessibilityLabel("\(session.remaining) Karten übrig")
}
}
}
.task {
if session == nil {
let s = StudySession(deckId: deckId, deckName: deckName, auth: auth, context: context)
session = s
await s.start()
}
}
}
@ViewBuilder
private var content: some View {
if let session {
switch session.phase {
case .loading:
ProgressView("Karten werden geladen …")
.tint(CardsTheme.primary)
case .studying:
studyingView(session: session)
case .finished:
finishedView(session: session)
case let .failed(message):
failedView(message: message, session: session)
}
} else {
ProgressView()
.tint(CardsTheme.primary)
}
}
private func studyingView(session: StudySession) -> some View {
VStack(spacing: 16) {
if let due = session.current {
cardSurface(due: due, isFlipped: session.isFlipped)
.onTapGesture {
flipHaptic()
session.flip()
}
keyboardShortcuts(session: session)
bottomBar(session: session)
}
}
.padding(.bottom, 20)
.animation(.easeInOut(duration: 0.2), value: session.isFlipped)
.animation(.easeInOut(duration: 0.2), value: session.currentIndex)
}
/// Fixe Höhe, damit der Wechsel zwischen "Antwort anzeigen" und
/// `RatingBar` die Card oben nicht stauchen kann sonst proportioniert
/// `.aspectRatio(.fit)` die Card neu und das Layout springt.
private func bottomBar(session: StudySession) -> some View {
ZStack {
if session.isFlipped {
RatingBar { rating in
Task { await session.grade(rating) }
}
.transition(.opacity)
} else {
Button {
flipHaptic()
session.flip()
} label: {
Text("Antwort anzeigen")
.font(.subheadline.weight(.semibold))
.frame(maxWidth: .infinity)
.padding(.vertical, 14)
.background(CardsTheme.primary, in: RoundedRectangle(cornerRadius: 10))
.foregroundStyle(CardsTheme.primaryForeground)
}
.buttonStyle(.plain)
.padding(.horizontal, 16)
.transition(.opacity)
}
}
.frame(height: 52)
}
private func cardSurface(due: DueReview, isFlipped: Bool) -> some View {
CardSurface(size: .hero, elevation: .raised) {
CardRenderer(
card: due.card,
subIndex: due.review.subIndex,
isFlipped: isFlipped
)
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
.padding(.horizontal, 16)
.padding(.top, 12)
}
private func finishedView(session: StudySession) -> some View {
VStack(spacing: 16) {
Image(systemName: "checkmark.seal.fill")
.font(.system(size: 64))
.foregroundStyle(CardsTheme.success)
Text(session.totalGraded == 0 ? "Keine Karten fällig" : "Fertig!")
.font(.title.bold())
.foregroundStyle(CardsTheme.foreground)
if session.totalGraded > 0 {
Text("\(session.totalGraded) Karten gelernt")
.font(.subheadline)
.foregroundStyle(CardsTheme.mutedForeground)
}
Button("Zurück") { dismiss() }
.padding(.top, 24)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
private func failedView(message: String, session: StudySession) -> some View {
VStack(spacing: 16) {
Image(systemName: "wifi.exclamationmark")
.font(.system(size: 48))
.foregroundStyle(CardsTheme.error)
Text("Karten konnten nicht geladen werden")
.font(.headline)
.foregroundStyle(CardsTheme.foreground)
Text(message)
.font(.caption)
.multilineTextAlignment(.center)
.foregroundStyle(CardsTheme.mutedForeground)
.padding(.horizontal, 32)
Button("Erneut versuchen") {
Task { await session.start() }
}
.padding(.top, 16)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
/// Unsichtbare Buttons mit Keyboard-Shortcuts. Funktionieren auf
/// iPad (Magic Keyboard) und macOS. Space = flip, 1-4 = Rating.
private func keyboardShortcuts(session: StudySession) -> some View {
Group {
Button("Flip") {
flipHaptic()
session.flip()
}
.keyboardShortcut(.space, modifiers: [])
if session.isFlipped {
ForEach(Rating.allCases, id: \.self) { rating in
Button(rating.label) {
Task { await session.grade(rating) }
}
.keyboardShortcut(KeyEquivalent(Character(rating.shortcut)), modifiers: [])
}
}
}
.frame(width: 0, height: 0)
.opacity(0)
.accessibilityHidden(true)
}
private func flipHaptic() {
#if canImport(UIKit)
UIImpactFeedbackGenerator(style: .soft).impactOccurred()
#endif
}
}