106 lines
3.0 KiB
Dart
106 lines
3.0 KiB
Dart
import 'dart:io';
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:advance_pdf_viewer/advance_pdf_viewer.dart';
|
|
import 'package:decpisos/funcoes_gerais.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:share/share.dart';
|
|
|
|
class PDFViewerScreenController extends GetxController{
|
|
var loading = true.obs;
|
|
var loadPDF = false.obs;
|
|
var visibleRefresh = false.obs;
|
|
var error = false.obs;
|
|
PDFDocument doc;
|
|
File _filePDF;
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
this._loadPDF();
|
|
}
|
|
|
|
Future<void> _loadPDF() async {
|
|
var file = await this._createFileOfPdfUrl();
|
|
if(file != null && await file.exists()){
|
|
this.doc = await PDFDocument.fromFile(file);
|
|
//this.visibleRefresh.value = true;
|
|
}
|
|
else{
|
|
if(await FuncoesGerais.checkInternetConnection() == false){
|
|
this.error.value = true;
|
|
}
|
|
else{
|
|
this.doc = await PDFDocument.fromURL(FuncoesGerais.urlPDF);
|
|
}
|
|
|
|
}
|
|
|
|
this.loading.value = false;
|
|
}
|
|
|
|
Future<void> refreshPDF() async {
|
|
this.loading.value = true;
|
|
try {
|
|
await this._filePDF.delete();
|
|
this._loadPDF();
|
|
} catch (e) {
|
|
Get.rawSnackbar(message: 'Não possível atualizar o PDF. Tente novamente');
|
|
this.loading.value = false;
|
|
}
|
|
}
|
|
|
|
Future<void> sharePDF() async {
|
|
//Share.shareFiles(['${directory.path}/image.jpg'], text: 'Great picture');
|
|
this.loadPDF.value = true;
|
|
var file = await this._createFileOfPdfUrl();
|
|
try {
|
|
Share.shareFiles([file.path], text: 'DecCard').then((value) => this.loadPDF.value = false);
|
|
} catch (e) {
|
|
this.loadPDF.value = false;
|
|
Get.rawSnackbar(message: 'Não foi possível compartilhar o PDF');
|
|
}
|
|
|
|
}
|
|
|
|
Future<File> _createFileOfPdfUrl() async {
|
|
final url = FuncoesGerais.urlPDF;
|
|
File file;
|
|
|
|
final filename = url.substring(url.lastIndexOf("/") + 1);
|
|
String dir = (await getApplicationDocumentsDirectory()).path;
|
|
file = new File('$dir/$filename');
|
|
if(!await file.exists()){
|
|
if(await FuncoesGerais.checkInternetConnection()){
|
|
var request = await HttpClient().getUrl(Uri.parse(url));
|
|
var response = await request.close();
|
|
var bytes = await consolidateHttpClientResponseBytes(response);
|
|
await file.writeAsBytes(bytes);
|
|
}
|
|
else{
|
|
file = null;
|
|
}
|
|
}
|
|
this._filePDF = file;
|
|
if(this._filePDF != null) this.visibleRefresh.value = await this._filePDF.exists();
|
|
|
|
return file;
|
|
}
|
|
|
|
Widget buildError(){
|
|
return Container(
|
|
width: double.maxFinite,
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
children: [
|
|
Text('Não foi possível carregar o PDF'),
|
|
SizedBox(height: 10.0,),
|
|
RaisedButton(onPressed: this.refreshPDF,child: Text('Atualizar'),)
|
|
],
|
|
),
|
|
);
|
|
}
|
|
} |