Files
Spanish/Conjuga/SharedModels/Sources/SharedModels/BookChapter.swift
T
Trey T ac84b22977 Books — fix infinite render loop via value-based navigation
Opening a book chapter froze the app in an infinite render loop. Root
cause: the books screens used the eager `NavigationLink { destination }`
form inside `List`/`LazyVStack`. That form keeps the destination view
structurally parented to the source row, so `BookReaderView`'s ScrollView
got trapped inside a `List` row — a row sizes to intrinsic height, a
ScrollView has none, so the two never converge and re-measure forever.

Switch the whole books navigation chain to value-based navigation:
- practiceHomeView, BookLibraryView, BookChapterListView use
  NavigationLink(value:).
- PracticeView's NavigationStack declares the BooksRoute, Book, and
  BookChapter destinations once, at the stack root (mixing eager and
  value-based pushes in one path caused pushed screens to pop back).
- BookReaderView is built from just a BookChapter; it resolves its Book
  by slug via @Query.

Also:
- BookChapter gains a stored paragraphCount so the chapter list no longer
  decodes the full paragraph JSON on every render (bookDataVersion -> 6
  to re-seed).
- BookSpeechController builds its AVSpeechSynthesizer lazily.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-18 20:08:36 -05:00

45 lines
1.4 KiB
Swift

import Foundation
import SwiftData
/// One chapter of a `Book`. Spanish + English paragraphs are stored as JSON-
/// encoded `[String]` so SwiftData doesn't have to manage variable-length
/// arrays directly.
@Model
public final class BookChapter {
@Attribute(.unique) public var id: String = "" // "<bookSlug>-ch<number>"
public var bookSlug: String = ""
public var number: Int = 0
public var title: String = ""
/// Spanish paragraph count, stored at seed time so chapter lists can show
/// it without decoding the full `paragraphsESJSON` blob on every render.
public var paragraphCount: Int = 0
public var paragraphsESJSON: Data = Data()
public var paragraphsENJSON: Data = Data()
public init(
id: String,
bookSlug: String,
number: Int,
title: String,
paragraphCount: Int,
paragraphsESJSON: Data,
paragraphsENJSON: Data
) {
self.id = id
self.bookSlug = bookSlug
self.number = number
self.title = title
self.paragraphCount = paragraphCount
self.paragraphsESJSON = paragraphsESJSON
self.paragraphsENJSON = paragraphsENJSON
}
public func paragraphsES() -> [String] {
(try? JSONDecoder().decode([String].self, from: paragraphsESJSON)) ?? []
}
public func paragraphsEN() -> [String] {
(try? JSONDecoder().decode([String].self, from: paragraphsENJSON)) ?? []
}
}