feat(offline): text-only Cleanup + ζ-1 Offline-Sync
Drei zusammenhängende Blöcke in einem Commit (Files überlappen sich
zwischen den Themen — sauberer Split nicht ohne Friktion möglich):
1. Wordeck-Text-Only-Cleanup
Image-Occlusion + Audio-Front-Code raus. Server ist seit Migration
0004_wordeck_text_only.sql text-only (in Prod waren 0 Karten der
Typen, 0 Media-Files). Native-Code war Build-11-Altlast.
- Gelöscht: MediaCache, MediaEnvironment, RemoteImage,
AudioPlayerButton, MaskEditorView, CardEditorMediaFields,
CardEditorPayload, Media.swift
- CardType-Enum auf 5 Werte: basic / basic-reverse / cloze /
typing / multiple-choice
- media_refs aus Card, CardCreateBody, CardUpdateBody, call-sites
- WordeckAPI.uploadMedia / .fetchMedia / .deleteMedia + Single-File-
makeMultipartBody gestrichen
- MarketplaceCardConverter ohne Media-Cases
- CardRenderer ohne imageOcclusionView / audioFrontView
2. AI-Media-Mode raus
/decks/from-image-Endpoint existiert serverseitig nicht (server
registriert nur /decks/generate für Text-Prompts). Native-Aufrufe
wären 404 — toter Code.
- aiMedia-Case aus DeckEditorView.CreateMode, ModePicker auf
3 Optionen (Leer / KI / CSV)
- AIMediaFormSections, MediaFileRow, mediaPickers, thumbnail,
ingestPhotoItems, handlePDFImport raus
- generateDeckFromMedia + makeFromImageMultipartBody raus
- GenerationMediaFile-Struct + PhotosUI-Import + PlatformImage-
typealias raus
- NSPhotoLibraryUsageDescription aus project.yml entfernt (es gibt
keinen Photo-Library-Zugriff mehr)
- maxMediaFiles/maxImageBytes/maxPDFBytes + inferImageMimeType +
imageExtension aus DeckEditorHelpers raus
3. ζ-1 Offline-Sync
Konzept in docs/OFFLINE_SYNC.md. Server-authoritative-FSRS bleibt —
kein lokales FSRS, nur Snapshot-Modell.
- Neue SwiftData-Models: CachedCard + CachedDueReview, beide mit
userId/deckId-Indizes
- ModelContainer um die zwei Models erweitert (additive Migration,
sollte automatisch laufen — vor TestFlight verifizieren)
- DueReview bekommt programmatischen init(review:card:) für die
Cache-Rekonstruktion
- DeckListStore.refresh() zieht Cards + Due-Reviews pro Deck
parallel in einer TaskGroup; applyToCache in drei Helpers
gesplittet (applyDecks / applyCards / applyDueReviews)
- Karten: Upsert mit Orphan-Cleanup
- Due-Reviews: voll ersetzt pro Refresh (Server-`due`-Zeiten
ändern sich, Merge wäre falsch)
- StudySession.start() fällt bei Netz-Fehler auf
CachedDueReview-Snapshot zurück, setzt isOfflineSession-Flag
- StudySessionView zeigt offline-Banner und am Ende der Session
einen Hinweis „Weitere Karten erst nach Verbindung verfügbar"
- AccountView.wipeLocalCache(): DSGVO-Wipe vor signOut() und nach
deleteAccount → CachedDeck + CachedCard + CachedDueReview +
PendingGrade werden gelöscht
Plus: Keychain-Test in WordeckNativeTests.swift fix — erwartete
"ev.mana.wordeck", muss seit Cross-App-SSO-Commit 19fee75
ManaSharedKeychainGroup nutzen. Auf Konstant-Reference umgestellt,
damit's nicht wieder driftet.
Verifikation:
- xcodebuild iOS-Simulator: BUILD SUCCEEDED
- swiftlint --strict: 0 violations in 68 files
- swiftformat: clean
- 37/37 Tests grün (inkl. fix-Keychain-Test)
- macOS-Build scheitert an pre-existing .topBarTrailing in
StudySessionView (iOS-only API seit 2026-05-13, nicht durch
diesen Commit verursacht)
Pflicht-Verifikation vor TestFlight (in PLAN.md verewigt):
- SwiftData-Migration auf Bestandsbuilder
- Offline-Endurance (50+ Karten Flugmodus)
- Logout-Wipe mit Account-Switch
- Cross-Check Web ↔ Native nach Offline-Grade
Diff: 35 files, +869 / -1622, netto ~−750 LOC.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
19fee75c47
commit
9527240bcc
36 changed files with 728 additions and 1565 deletions
|
|
@ -1,10 +1,8 @@
|
|||
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.
|
||||
/// werden über `isFlipped` gesteuert. Wordeck ist text-only — alle
|
||||
/// Card-Types rendern ausschließlich Markdown-Text.
|
||||
struct CardRenderer: View {
|
||||
let card: ReviewCard
|
||||
let subIndex: Int
|
||||
|
|
@ -24,10 +22,6 @@ struct CardRenderer: View {
|
|||
}
|
||||
case .cloze:
|
||||
clozeView
|
||||
case .imageOcclusion:
|
||||
imageOcclusionView
|
||||
case .audioFront:
|
||||
audioFrontView
|
||||
case .multipleChoice:
|
||||
MultipleChoiceCardView(card: card, isFlipped: isFlipped)
|
||||
case .typing:
|
||||
|
|
@ -66,82 +60,6 @@ struct CardRenderer: View {
|
|||
}
|
||||
}
|
||||
|
||||
@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(WordeckTheme.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(WordeckTheme.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(WordeckTheme.primary)
|
||||
}
|
||||
if let note = card.fields["note"], !note.isEmpty {
|
||||
Text(note)
|
||||
.font(.caption)
|
||||
.foregroundStyle(WordeckTheme.mutedForeground)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ViewBuilder
|
||||
private var audioFrontView: some View {
|
||||
let audioRef = card.fields["audio_ref"] ?? ""
|
||||
VStack(spacing: 16) {
|
||||
AudioPlayerButton(mediaId: audioRef)
|
||||
if isFlipped {
|
||||
Divider().background(WordeckTheme.border)
|
||||
text(card.fields["back"] ?? "")
|
||||
.font(.title3)
|
||||
.foregroundStyle(WordeckTheme.foreground)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var placeholderView: some View {
|
||||
VStack(spacing: 8) {
|
||||
Image(systemName: "questionmark.square.dashed")
|
||||
.font(.largeTitle)
|
||||
.foregroundStyle(WordeckTheme.mutedForeground)
|
||||
Text("Card-Type »\(card.type.rawValue)« kommt in einer späteren Phase")
|
||||
.font(.caption)
|
||||
.multilineTextAlignment(.center)
|
||||
.foregroundStyle(WordeckTheme.mutedForeground)
|
||||
}
|
||||
}
|
||||
|
||||
/// Markdown-Bold (`**...**`) wird auf SwiftUI's AttributedString gemappt.
|
||||
private func text(_ markdown: String) -> some View {
|
||||
let attributed = (try? AttributedString(
|
||||
|
|
|
|||
|
|
@ -5,6 +5,11 @@ import SwiftData
|
|||
|
||||
/// State-Machine für eine Lern-Session. Lädt Due-Reviews beim Start,
|
||||
/// rendert eine Karte nach der anderen, schickt Grades via GradeQueue ab.
|
||||
///
|
||||
/// Seit ζ-1 (2026-05-18): wenn der Server-Call scheitert, fällt die
|
||||
/// Session auf den `CachedDueReview`-Snapshot vom letzten Sync zurück.
|
||||
/// Der User lernt dann offline. Grades laufen wie immer in die
|
||||
/// `GradeQueue` und drainen beim Reconnect.
|
||||
@MainActor
|
||||
@Observable
|
||||
final class StudySession {
|
||||
|
|
@ -20,16 +25,21 @@ final class StudySession {
|
|||
private(set) var currentIndex: Int = 0
|
||||
private(set) var isFlipped: Bool = false
|
||||
private(set) var totalGraded: Int = 0
|
||||
/// `true` wenn die Session aus dem lokalen Snapshot statt vom Server
|
||||
/// gestartet wurde. View kann ein Offline-Banner zeigen.
|
||||
private(set) var isOfflineSession: Bool = false
|
||||
|
||||
let deckId: String
|
||||
let deckName: String
|
||||
|
||||
private let api: WordeckAPI
|
||||
private let context: ModelContext
|
||||
private let gradeQueue: GradeQueue
|
||||
|
||||
init(deckId: String, deckName: String, auth: AuthClient, context: ModelContext) {
|
||||
self.deckId = deckId
|
||||
self.deckName = deckName
|
||||
self.context = context
|
||||
api = WordeckAPI(auth: auth)
|
||||
gradeQueue = GradeQueue(api: api, context: context)
|
||||
}
|
||||
|
|
@ -50,6 +60,7 @@ final class StudySession {
|
|||
currentIndex = 0
|
||||
isFlipped = false
|
||||
totalGraded = 0
|
||||
isOfflineSession = false
|
||||
if queue.isEmpty {
|
||||
phase = .finished
|
||||
} else {
|
||||
|
|
@ -59,12 +70,37 @@ final class StudySession {
|
|||
let id = deckId
|
||||
Log.study.info("Session start — \(count, privacy: .public) due in deck \(id, privacy: .public)")
|
||||
} catch {
|
||||
let msg = (error as? LocalizedError)?.errorDescription ?? String(describing: error)
|
||||
phase = .failed(msg)
|
||||
Log.study.error("Session start failed: \(msg, privacy: .public)")
|
||||
// Server nicht erreichbar oder Auth-Fehler → Cache-Fallback.
|
||||
queue = loadFromCache()
|
||||
currentIndex = 0
|
||||
isFlipped = false
|
||||
totalGraded = 0
|
||||
if queue.isEmpty {
|
||||
let msg = (error as? LocalizedError)?.errorDescription ?? String(describing: error)
|
||||
phase = .failed(msg)
|
||||
Log.study.error("Session start failed (no cache): \(msg, privacy: .public)")
|
||||
} else {
|
||||
isOfflineSession = true
|
||||
phase = .studying
|
||||
let count = queue.count
|
||||
let id = deckId
|
||||
Log.study
|
||||
.notice("Offline-Session — \(count, privacy: .public) cached due in deck \(id, privacy: .public)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func loadFromCache() -> [DueReview] {
|
||||
let deckId = deckId
|
||||
var descriptor = FetchDescriptor<CachedDueReview>(
|
||||
predicate: #Predicate<CachedDueReview> { $0.deckId == deckId },
|
||||
sortBy: [SortDescriptor(\.due, order: .forward)]
|
||||
)
|
||||
descriptor.fetchLimit = 500
|
||||
let cached = (try? context.fetch(descriptor)) ?? []
|
||||
return cached.compactMap { $0.toDueReview() }
|
||||
}
|
||||
|
||||
func flip() {
|
||||
guard case .studying = phase else { return }
|
||||
isFlipped.toggle()
|
||||
|
|
|
|||
|
|
@ -66,6 +66,9 @@ struct StudySessionView: View {
|
|||
|
||||
private func studyingView(session: StudySession) -> some View {
|
||||
VStack(spacing: 16) {
|
||||
if session.isOfflineSession {
|
||||
offlineBanner
|
||||
}
|
||||
if let due = session.current {
|
||||
cardSurface(due: due, isFlipped: session.isFlipped)
|
||||
.onTapGesture {
|
||||
|
|
@ -81,6 +84,24 @@ struct StudySessionView: View {
|
|||
.animation(.easeInOut(duration: 0.2), value: session.currentIndex)
|
||||
}
|
||||
|
||||
/// Banner für Offline-Sessions. Erklärt dem User ehrlich, dass er
|
||||
/// gerade die Karten lernt, die zum letzten Sync fällig waren —
|
||||
/// neue Karten kommen erst nach Wiederverbindung.
|
||||
private var offlineBanner: some View {
|
||||
HStack(spacing: 8) {
|
||||
Image(systemName: "wifi.slash")
|
||||
Text("Offline — Karten vom letzten Sync")
|
||||
}
|
||||
.font(.caption.weight(.medium))
|
||||
.foregroundStyle(WordeckTheme.mutedForeground)
|
||||
.padding(.horizontal, 12)
|
||||
.padding(.vertical, 6)
|
||||
.background(WordeckTheme.muted, in: Capsule())
|
||||
.padding(.horizontal, 16)
|
||||
.padding(.top, 4)
|
||||
.transition(.opacity)
|
||||
}
|
||||
|
||||
/// 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.
|
||||
|
|
@ -137,6 +158,14 @@ struct StudySessionView: View {
|
|||
.font(.subheadline)
|
||||
.foregroundStyle(WordeckTheme.mutedForeground)
|
||||
}
|
||||
if session.isOfflineSession {
|
||||
Text("Weitere Karten erst nach Verbindung verfügbar.")
|
||||
.font(.caption)
|
||||
.multilineTextAlignment(.center)
|
||||
.foregroundStyle(WordeckTheme.mutedForeground)
|
||||
.padding(.horizontal, 32)
|
||||
.padding(.top, 4)
|
||||
}
|
||||
Button("Zurück") { dismiss() }
|
||||
.padding(.top, 24)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue