openBook method

Future<bool> openBook(
  1. Uint8List data
)

Load an EPUB from bytes.

Implementation

Future<bool> openBook(Uint8List data) async {
  try {
    _loadingError = null;
    _isLoaded = false;
    notifyListeners();

    // Parse EPUB
    _book = await EpubReader.readBook(data);
    if (_book == null) {
      _loadingError = 'Failed to parse EPUB file';
      notifyListeners();
      return false;
    }

    // Extract images
    _imageData = EpubImageExtractor.extractImages(_book!);

    // Parse CSS styles
    _cssParser = EpubCssParser();
    _cssParser!.parseFromBook(_book!);

    // Parse content
    final parser = EpubContentParser(_book!);
    final result = parser.parse();

    _tableOfContents = result.tableOfContents;
    _flatChapters = result.flatChapters;
    _paragraphs = result.paragraphs;

    // Initial position
    _currentPosition = ReadingPosition.initial(
      totalParagraphs: _paragraphs.length,
    );

    _isLoaded = true;
    notifyListeners();

    debugPrint('📚 Loaded: ${_book?.Title}');
    return true;
  } catch (e, stack) {
    _loadingError = e.toString();
    debugPrint('❌ Error: $e\n$stack');
    notifyListeners();
    return false;
  }
}