diff --git a/lib/shared/widgets/read_view.dart b/lib/shared/widgets/read_view.dart index b3e394ca..6df9c6cb 100644 --- a/lib/shared/widgets/read_view.dart +++ b/lib/shared/widgets/read_view.dart @@ -3,6 +3,7 @@ import 'dart:io'; import 'package:flutter/material.dart'; import 'package:hub/features/backend/index.dart'; import 'package:hub/flutter_flow/index.dart'; +import 'package:hub/shared/utils/dialog_util.dart'; import 'package:hub/shared/widgets.dart'; import 'package:path_provider/path_provider.dart'; import 'package:pdfx/pdfx.dart'; @@ -44,58 +45,78 @@ class ReadView extends StatefulWidget { class ReadViewState extends State { Future _initializePdf() async { - final file = await downloadPdf(widget.url); - final Future document = DocumentType.openFile(file.path); - return ReadViewController(document: document); + try { + final file = await downloadPdf(widget.url); + final Future document = DocumentType.openFile(file.path); + return ReadViewController(document: document); + } catch (e) { + logError('Erro ao baixar o PDF', e); + return Future.error(e); + } } Future downloadPdf(String url) async { - final response = await http.get(Uri.parse(url)); - if (response.statusCode == 200) { - final bytes = response.bodyBytes; - final dir = await getTemporaryDirectory(); - final file = File('${dir.path}/downloaded.pdf'); - await file.writeAsBytes(bytes); - return file; - } else { - throw Exception('Falha ao baixar o PDF'); + try { + final response = await http.get(Uri.parse(url)); + if (response.statusCode == 200) { + final bytes = response.bodyBytes; + final dir = await getTemporaryDirectory(); + final file = File('${dir.path}/downloaded.pdf'); + await file.writeAsBytes(bytes); + return file; + } else { + throw Exception('Falha ao baixar o PDF'); + } + } catch (e) { + logError('Erro ao baixar o PDF', e); + rethrow; } } + void logError(String message, dynamic error) { + log('$message: $error'); + DialogUtil.error(context, message); + } + @override Widget build(BuildContext context) { return Stack( children: [ - _buildPDFViewer(), - buildShareButton(), + _buildPDFViewer(context), + buildShareButton(context), ], ); } - Positioned buildShareButton() { + Positioned buildShareButton(BuildContext context) { + final theme = FlutterFlowTheme.of(context); return Positioned( bottom: 10, right: 10, child: IconButton( icon: Icon( Icons.share, - color: Colors.black, + color: theme.primaryText, ), - color: Colors.black, + color: theme.primaryText, onPressed: onShare, ), ); } void onShare() async { - final Uri uri = Uri.parse(widget.url); - final response = await http.get(uri); - if (response.statusCode == 200) { - final XFile xfile = XFile.fromData(response.bodyBytes, - name: '${widget.title}.pdf', mimeType: 'application/pdf'); - await Share.shareXFiles([xfile], text: 'Confira este PDF!'); - } else { - log('Erro ao baixar o arquivo: ${response.statusCode}'); + try { + final Uri uri = Uri.parse(widget.url); + final response = await http.get(uri); + if (response.statusCode == 200) { + final XFile xfile = XFile.fromData(response.bodyBytes, + name: '${widget.title}.pdf', mimeType: 'application/pdf'); + await Share.shareXFiles([xfile], text: 'Confira este PDF!'); + } else { + throw Exception('Erro ao compartilhar o arquivo: ${response.statusCode}'); + } + } catch (e) { + logError('Erro ao compartilhar o arquivo', e); } } @@ -112,12 +133,19 @@ class ReadViewState extends State { ); } - Widget _buildPDFViewer() => Padding( + Widget _buildPDFViewer(BuildContext context) => Padding( padding: EdgeInsets.all(10), child: FutureBuilder( future: _initializePdf(), builder: (context, snapshot) { if (!snapshot.hasData) return buildLoadingIndicator(context); + if (snapshot.error != null) { + return buildLoadingIndicator(context); + } + if (snapshot.connectionState == ConnectionState.waiting) { + return buildLoadingIndicator(context); + } + return PdfView( controller: snapshot.data!, renderer: (PdfPage page) => page.render( @@ -134,6 +162,4 @@ class ReadViewState extends State { ); }), ); - - // Widget get progressIndicator => LoadingUtil.buildLoadingIndicator(context); }