-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudyModeView.swift
More file actions
448 lines (398 loc) · 19.8 KB
/
StudyModeView.swift
File metadata and controls
448 lines (398 loc) · 19.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
import SwiftUI
import CoreData
import AVFoundation
import Translation
import NaturalLanguage
import Combine
struct StudyModeView: View {
let deck: CDDeck
@Environment(\.managedObjectContext) private var viewContext
@Environment(\.dismiss) private var dismiss
@FetchRequest var cards: FetchedResults<CDCard>
@State private var currentIndex = 0
@State private var showingBack = false
@State private var shuffledIndices: [Int] = []
@State private var isShuffled = false
@State private var selectedLinkedCard: CDCard?
@State private var audioPlayer: AVAudioPlayer?
@State private var showTranslation = false
@State private var textToTranslate = ""
@StateObject private var speechHelper = SpeechHelper()
init(deck: CDDeck) {
self.deck = deck
let request = NSFetchRequest<CDCard>(entityName: "CDCard")
request.predicate = NSPredicate(format: "deck == %@", deck)
request.sortDescriptors = [NSSortDescriptor(key: "createdAt", ascending: true)]
self._cards = FetchRequest(fetchRequest: request)
}
var currentCard: CDCard? {
guard !cards.isEmpty else { return nil }
let index = isShuffled ? shuffledIndices[currentIndex] : currentIndex
return cards[index]
}
var body: some View {
ZStack {
Color(UIColor.systemBackground)
.ignoresSafeArea()
VStack(spacing: 0) {
// Top bar
HStack {
Button(action: { dismiss() }) {
Image(systemName: "xmark")
.font(.system(size: 20))
.foregroundColor(.primary)
.frame(width: 44, height: 44)
}
Spacer()
Text("\(currentIndex + 1) / \(cards.count)")
.font(.headline)
.foregroundColor(.secondary)
Spacer()
Button(action: { toggleShuffle() }) {
Image(systemName: isShuffled ? "shuffle.circle.fill" : "shuffle.circle")
.font(.system(size: 20))
.foregroundColor(isShuffled ? Color(hexString: deck.colorHex) : .primary)
.frame(width: 44, height: 44)
}
Button(action: {
if let card = currentCard {
let text = loadAttributedText(card: card, isBack: showingBack).string
if !text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
speechHelper.toggleSpeech(text)
}
}
}) {
Image(systemName: speechHelper.isSpeaking ? "speaker.wave.2.fill" : "speaker.wave.2")
.font(.system(size: 18))
.foregroundColor(speechHelper.isSpeaking ? .blue : .primary)
.frame(width: 44, height: 44)
}
Button(action: {
if let card = currentCard {
let text = loadAttributedText(card: card, isBack: showingBack).string
if !text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
textToTranslate = text
showTranslation = true
}
}
}) {
Image(systemName: "translate")
.font(.system(size: 18))
.foregroundColor(.primary)
.frame(width: 44, height: 44)
}
}
.padding()
.background(Color(UIColor.systemBackground))
// Card content
if let card = currentCard {
ZStack(alignment: .bottom) {
// Main scrollable content
ZStack {
Color(UIColor.secondarySystemBackground)
ScrollView {
VStack(spacing: 0) {
// Fixed header at top with FRONT/BACK label
VStack(spacing: 30) {
Text(showingBack ? "BACK" : "FRONT")
.font(.system(size: 11, weight: .semibold))
.foregroundColor(.secondary)
.tracking(1)
.padding(.top, 30)
// Text content with links
AttributedTextDisplay(
attributedText: loadAttributedText(card: card, isBack: showingBack),
onLinkTap: { url in
handleLinkTap(url)
}
)
.frame(maxWidth: .infinity)
.padding(.horizontal, 20)
// Images (all of them)
let imagePaths = showingBack ? card.backImagePathArray : card.frontImagePathArray
ForEach(Array(imagePaths.enumerated()), id: \.offset) { _, path in
if let image = CDCard.loadImage(from: path) {
Image(uiImage: image)
.resizable()
.scaledToFit()
.frame(maxWidth: 400)
.frame(maxHeight: 300)
.cornerRadius(12)
.shadow(color: Color.black.opacity(0.1), radius: 8, x: 0, y: 4)
.padding(.horizontal, 40)
.padding(.top, 20)
}
}
// Audio players (all of them)
let audioPaths = showingBack ? card.backAudioPathArray : card.frontAudioPathArray
ForEach(Array(audioPaths.enumerated()), id: \.offset) { index, audioPath in
Button(action: { playAudio(path: audioPath) }) {
HStack(spacing: 12) {
Image(systemName: "play.circle.fill")
.font(.system(size: 28))
Text(audioPaths.count == 1 ? "Play Audio" : "Play Audio \(index + 1)")
.font(.system(size: 16, weight: .semibold))
}
.foregroundColor(.white)
.frame(maxWidth: .infinity)
.padding(.vertical, 16)
.background(Color(hexString: deck.colorHex))
.cornerRadius(12)
}
.padding(.horizontal, 40)
.padding(.top, 20)
}
}
// Bottom padding to ensure content doesn't hide behind hint
Spacer()
.frame(height: 100)
}
}
.scrollIndicators(.hidden)
}
// Fixed hint overlay at bottom - NOT inside ScrollView
if !showingBack {
VStack(spacing: 0) {
// Gradient fade from transparent to opaque
LinearGradient(
colors: [
Color(UIColor.secondarySystemBackground).opacity(0),
Color(UIColor.secondarySystemBackground)
],
startPoint: .top,
endPoint: .bottom
)
.frame(height: 60)
// Solid opaque section with hint content
VStack(spacing: 8) {
Image(systemName: "hand.tap.fill")
.font(.system(size: 20))
.foregroundColor(.secondary.opacity(0.5))
Text("Tap card to reveal answer")
.font(.system(size: 13))
.foregroundColor(.secondary)
}
.padding(.bottom, 20)
.frame(maxWidth: .infinity)
.background(Color(UIColor.secondarySystemBackground))
}
}
}
.onTapGesture {
withAnimation(.spring(response: 0.3, dampingFraction: 0.7)) {
showingBack.toggle()
}
}
.clipShape(RoundedRectangle(cornerRadius: 16))
.shadow(color: Color.black.opacity(0.05), radius: 10, x: 0, y: 2)
.padding(.horizontal, 20)
}
// Bottom navigation
HStack(spacing: 40) {
Button(action: { previousCard() }) {
Image(systemName: "chevron.left.circle.fill")
.font(.system(size: 44))
.foregroundColor(currentIndex > 0 ? Color(hexString: deck.colorHex) : .gray)
}
.disabled(currentIndex == 0)
Spacer()
Button(action: { nextCard() }) {
Image(systemName: "chevron.right.circle.fill")
.font(.system(size: 44))
.foregroundColor(currentIndex < cards.count - 1 ? Color(hexString: deck.colorHex) : .gray)
}
.disabled(currentIndex >= cards.count - 1)
}
.padding()
.background(Color(UIColor.systemBackground))
}
}
.sheet(item: $selectedLinkedCard) { linkedCard in
LinkedCardDetailView(card: linkedCard)
}
.translationPresentation(isPresented: $showTranslation, text: textToTranslate)
}
private func loadAttributedText(card: CDCard, isBack: Bool) -> NSAttributedString {
if isBack, let rtfData = card.backRTFData,
let attrString = try? NSMutableAttributedString(data: rtfData, options: [.documentType: NSAttributedString.DocumentType.rtf], documentAttributes: nil) {
fixColorsForCurrentMode(attrString)
return attrString
} else if !isBack, let rtfData = card.frontRTFData,
let attrString = try? NSMutableAttributedString(data: rtfData, options: [.documentType: NSAttributedString.DocumentType.rtf], documentAttributes: nil) {
fixColorsForCurrentMode(attrString)
return attrString
}
return NSAttributedString(string: isBack ? card.backText : card.frontText, attributes: [
.foregroundColor: UIColor.label,
.font: UIFont.systemFont(ofSize: 18)
])
}
/// RTF stores hardcoded color values (e.g. black text) which become invisible in dark mode.
/// This replaces non-link text colors with the current label color so text is always visible.
private func fixColorsForCurrentMode(_ attrString: NSMutableAttributedString) {
let fullRange = NSRange(location: 0, length: attrString.length)
attrString.enumerateAttribute(.foregroundColor, in: fullRange, options: []) { value, range, _ in
// Don't change link colors — those are handled by linkTextAttributes
if attrString.attribute(.link, at: range.location, effectiveRange: nil) == nil {
attrString.addAttribute(.foregroundColor, value: UIColor.label, range: range)
}
}
}
private func handleLinkTap(_ url: URL) {
guard url.scheme == "card", let cardID = url.host else { return }
// Search all cards (not just current deck) for the linked card
let request = NSFetchRequest<CDCard>(entityName: "CDCard")
request.predicate = NSPredicate(format: "id == %@", cardID)
request.fetchLimit = 1
if let linkedCard = try? viewContext.fetch(request).first {
selectedLinkedCard = linkedCard
}
}
private func playAudio(path: String) {
let resolved = CDCard.resolvedPath(for: path)
let url = URL(fileURLWithPath: resolved)
do {
audioPlayer = try AVAudioPlayer(contentsOf: url)
audioPlayer?.play()
} catch {
print("Failed to play audio")
}
}
private func nextCard() {
speechHelper.stop()
if currentIndex < cards.count - 1 {
currentIndex += 1
showingBack = false
}
}
private func previousCard() {
speechHelper.stop()
if currentIndex > 0 {
currentIndex -= 1
showingBack = false
}
}
private func toggleShuffle() {
isShuffled.toggle()
if isShuffled {
shuffledIndices = Array(0..<cards.count).shuffled()
currentIndex = 0
showingBack = false
} else {
currentIndex = 0
showingBack = false
}
}
}
// MARK: - Attributed Text Display with Link Support
struct AttributedTextDisplay: UIViewRepresentable {
let attributedText: NSAttributedString
let onLinkTap: (URL) -> Void
func makeUIView(context: Context) -> UITextView {
let textView = UITextView()
textView.isEditable = false
textView.isScrollEnabled = false
textView.backgroundColor = .clear
textView.font = .systemFont(ofSize: 18)
textView.textAlignment = .left
textView.textContainerInset = .zero
textView.textContainer.lineFragmentPadding = 0
textView.delegate = context.coordinator
textView.dataDetectorTypes = []
textView.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
textView.setContentHuggingPriority(.defaultHigh, for: .vertical)
textView.linkTextAttributes = [
.foregroundColor: UIColor.systemBlue,
.underlineStyle: NSUnderlineStyle.single.rawValue
]
return textView
}
func updateUIView(_ uiView: UITextView, context: Context) {
uiView.attributedText = attributedText
// Force layout so the text view reports correct intrinsic size
uiView.invalidateIntrinsicContentSize()
}
func makeCoordinator() -> Coordinator {
Coordinator(onLinkTap: onLinkTap)
}
// Use sizeThatFits to tell SwiftUI the correct height
func sizeThatFits(_ proposal: ProposedViewSize, uiView: UITextView, context: Context) -> CGSize? {
let width = proposal.width ?? 300
let fittingSize = uiView.sizeThatFits(CGSize(width: width, height: CGFloat.greatestFiniteMagnitude))
return CGSize(width: width, height: fittingSize.height)
}
class Coordinator: NSObject, UITextViewDelegate {
let onLinkTap: (URL) -> Void
init(onLinkTap: @escaping (URL) -> Void) {
self.onLinkTap = onLinkTap
}
@available(iOS 17.0, *)
func textView(_ textView: UITextView, primaryActionFor textItem: UITextItem, defaultAction: UIAction) -> UIAction? {
if case .link(let url) = textItem.content {
return UIAction { [weak self] _ in
self?.onLinkTap(url)
}
}
return defaultAction
}
// Fallback for iOS 16 and below
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
onLinkTap(URL)
return false
}
}
}
// MARK: - Linked Card Detail View
struct LinkedCardDetailView: View {
let card: CDCard
@Environment(\.dismiss) private var dismiss
var body: some View {
NavigationStack {
ScrollView {
VStack(alignment: .leading, spacing: 20) {
// Front
VStack(alignment: .leading, spacing: 8) {
Text("FRONT")
.font(.caption)
.foregroundColor(.secondary)
Text(card.frontText)
.font(.body)
ForEach(Array(card.frontImagePathArray.enumerated()), id: \.offset) { _, path in
if let image = CDCard.loadImage(from: path) {
Image(uiImage: image)
.resizable()
.scaledToFit()
.cornerRadius(8)
}
}
}
Divider()
// Back
VStack(alignment: .leading, spacing: 8) {
Text("BACK")
.font(.caption)
.foregroundColor(.secondary)
Text(card.backText)
.font(.body)
ForEach(Array(card.backImagePathArray.enumerated()), id: \.offset) { _, path in
if let image = CDCard.loadImage(from: path) {
Image(uiImage: image)
.resizable()
.scaledToFit()
.cornerRadius(8)
}
}
}
}
.padding()
}
.navigationTitle("Linked Card")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button("Done") { dismiss() }
}
}
}
}
}