109 lines
3.1 KiB
Dart
109 lines
3.1 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:MakerBook/utils/encrypt.dart';
|
|
import 'package:MakerBook/utils/global.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
//import 'package:vocsy_epub_viewer/epub_viewer.dart';
|
|
|
|
class EpubViewerPage extends StatefulWidget {
|
|
final int book_id;
|
|
|
|
const EpubViewerPage({super.key, required this.book_id});
|
|
|
|
@override
|
|
_EpubViewerPageState createState() => _EpubViewerPageState();
|
|
}
|
|
|
|
class _EpubViewerPageState extends State<EpubViewerPage> {
|
|
|
|
String? lastLocation;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_prepareTempEpub();
|
|
|
|
// 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(widget.book_id, lastLocation);
|
|
Navigator.pop(context);
|
|
});*/
|
|
|
|
//openLocalEpub();
|
|
}
|
|
|
|
Future<void> openLocalEpub() async {
|
|
final tempDir = await getTemporaryDirectory();
|
|
final path = '${tempDir.path}/test.epub';
|
|
|
|
final data = await rootBundle.load('assets/sample.epub');
|
|
final file = File(path);
|
|
await file.writeAsBytes(data.buffer.asUint8List());
|
|
|
|
// Config obrigatória
|
|
/*VocsyEpub.setConfig(
|
|
themeColor: Colors.blue,
|
|
identifier: 'test-book',
|
|
scrollDirection: EpubScrollDirection.VERTICAL,
|
|
allowSharing: false,
|
|
enableTts: false,
|
|
);
|
|
|
|
VocsyEpub.open(path);*/
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
super.dispose();
|
|
}
|
|
|
|
// 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');
|
|
|
|
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'];
|
|
|
|
// Salva o arquivo descriptografado no diretório temporário
|
|
await tempFile.writeAsBytes(unprotectedBytes);
|
|
|
|
lastLocation = await getLastLocation(widget.book_id);
|
|
|
|
// 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,
|
|
);*/
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return const Center(child: CircularProgressIndicator());
|
|
}
|
|
|
|
} |