MakerBook/books_viewer/lib/widgets/epub_viewer2.dart

120 lines
3.8 KiB
Dart

import 'dart:convert';
import 'dart:io';
import 'package:MakerBook/utils/encrypt.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:vocsy_epub_viewer/epub_viewer.dart';
class EpubViewer2Page extends StatefulWidget {
final int book_id;
const EpubViewer2Page({super.key, required this.book_id});
@override
_EpubViewer2PageState createState() => _EpubViewer2PageState();
}
class _EpubViewer2PageState extends State<EpubViewer2Page> {
String? lastLocation;
String? epubPath;
@override
void initState() {
super.initState();
_prepareTempEpub();
//_initializeEpubController();
// Registra um listener para capturar o lastLocation quando o leitor informar
VocsyEpub.locatorStream.listen((locator) async {
print('Localização atual: $locator');
// Armazena o locator como um Map<String, dynamic> em vez de string diretamente
lastLocation = locator;
await _saveLastLocation();
});
}
@override
void dispose() {
super.dispose();
}
// Função para salvar a última posição de leitura
Future<void> _saveLastLocation() async {
if (lastLocation == null) return;
final directory = await getApplicationDocumentsDirectory();
final file = File('${directory.path}/${widget.book_id}.epub');
// Recarrega o conteúdo protegido atual, atualiza o lastLocation, e salva novamente
String protectedContent = await file.readAsString();
Map<String, dynamic> fileData = unprotectFile(protectedContent);
String updatedProtectedContent = await protectFile(fileData['epubBytes'], lastLocation!);
await file.writeAsString(updatedProtectedContent);
Navigator.pop(context);
}
// Prepara o arquivo temporário para o leitor EPUB
Future<void> _prepareTempEpub_bkp() async {
final directoryReal = await getApplicationDocumentsDirectory();
final file = File('${directoryReal.path}/${widget.book_id}.epub');
final directory = await getTemporaryDirectory();
final tempFile = File('${directory.path}/${widget.book_id}_temp.epub');
// Lê o conteúdo protegido e remove a proteção
String protectedContent = await file.readAsString();
Map<String, dynamic> fileData = unprotectFile(protectedContent);
List<int> unprotectedBytes = fileData['epubBytes'];
lastLocation = fileData['lastLocation']; // Recupera o lastLocation
// Salva o arquivo descriptografado no diretório temporário
await tempFile.writeAsBytes(unprotectedBytes);
// Configuração do leitor
VocsyEpub.setConfig(
themeColor: Theme.of(context).primaryColor,
identifier: widget.book_id.toString(),
scrollDirection: EpubScrollDirection.ALLDIRECTIONS,
allowSharing: false,
enableTts: false,
);
// Abrindo o arquivo EPUB e usando o lastLocation se estiver disponível
VocsyEpub.open(
tempFile.path,
lastLocation: lastLocation != null ? EpubLocator.fromJson(jsonDecode(lastLocation!)) : null,
);
}
// Prepara o arquivo temporário para o leitor EPUB
Future<void> _prepareTempEpub() async {
final directoryReal = await getApplicationDocumentsDirectory();
final file = File('${directoryReal.path}/${widget.book_id}.epub');
// Configuração do leitor
VocsyEpub.setConfig(
themeColor: Theme.of(context).primaryColor,
identifier: widget.book_id.toString(),
scrollDirection: EpubScrollDirection.ALLDIRECTIONS,
allowSharing: false,
enableTts: false,
);
// Abrindo o arquivo EPUB e usando o lastLocation se estiver disponível
VocsyEpub.open(
file.path,
lastLocation: lastLocation != null ? EpubLocator.fromJson(jsonDecode(lastLocation!)) : null,
);
}
@override
Widget build(BuildContext context) {
return const Center(child: CircularProgressIndicator());
}
}