Display generated PDF on expense screen
This commit is contained in:
		@@ -1,8 +1,14 @@
 | 
			
		||||
import 'dart:io';
 | 
			
		||||
import 'dart:typed_data';
 | 
			
		||||
 | 
			
		||||
import 'package:alert_dialog/alert_dialog.dart';
 | 
			
		||||
import 'package:flutter/material.dart';
 | 
			
		||||
import 'package:flutter_hooks/flutter_hooks.dart';
 | 
			
		||||
import 'package:flutter_pdfview/flutter_pdfview.dart';
 | 
			
		||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
 | 
			
		||||
import 'package:logging/logging.dart';
 | 
			
		||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
 | 
			
		||||
import 'package:scanbot_sdk/scanbot_sdk.dart';
 | 
			
		||||
import 'package:scanbot_sdk/scanbot_sdk_ui_v2.dart';
 | 
			
		||||
 | 
			
		||||
import '../../services/storage/expenses.dart';
 | 
			
		||||
@@ -10,7 +16,8 @@ import '../../services/storage/expenses.dart';
 | 
			
		||||
part 'scan_screen.g.dart';
 | 
			
		||||
 | 
			
		||||
@riverpod
 | 
			
		||||
Future<String?> _scanDocument(Ref ref) async {
 | 
			
		||||
Future<Uint8List?> _scanDocument(Ref ref) async {
 | 
			
		||||
  DocumentDataExtractionResult d;
 | 
			
		||||
  var configuration = DocumentScanningFlow(
 | 
			
		||||
    appearance: DocumentFlowAppearanceConfiguration(
 | 
			
		||||
      statusBarMode: StatusBarMode.DARK,
 | 
			
		||||
@@ -23,7 +30,19 @@ Future<String?> _scanDocument(Ref ref) async {
 | 
			
		||||
  );
 | 
			
		||||
  var documentResult = await ScanbotSdkUiV2.startDocumentScanner(configuration);
 | 
			
		||||
 | 
			
		||||
  return documentResult.data?.pdfURI;
 | 
			
		||||
  if (documentResult.status != OperationStatus.OK) {
 | 
			
		||||
    throw Exception("Scanner failed with status ${documentResult.status}");
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Convert result to PDF
 | 
			
		||||
  var result = await ScanbotSdk.document.createPDFForDocument(
 | 
			
		||||
    PDFFromDocumentParams(
 | 
			
		||||
      documentID: documentResult.data!.uuid,
 | 
			
		||||
      pdfConfiguration: PdfConfiguration(),
 | 
			
		||||
    ),
 | 
			
		||||
  );
 | 
			
		||||
  final pdfPath = result.pdfFileUri.replaceFirst("file://", "");
 | 
			
		||||
  return File(pdfPath).readAsBytes();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
class ScanScreen extends HookConsumerWidget {
 | 
			
		||||
@@ -43,7 +62,7 @@ class ScanScreen extends HookConsumerWidget {
 | 
			
		||||
    // Perform a switch-case on the result to handle loading/error states
 | 
			
		||||
    return switch (boredSuggestion) {
 | 
			
		||||
      AsyncData(:final value) when value != null => ExpenseEditor(
 | 
			
		||||
        filePath: value,
 | 
			
		||||
        file: value,
 | 
			
		||||
        onFinished: (e) {},
 | 
			
		||||
      ),
 | 
			
		||||
 | 
			
		||||
@@ -97,18 +116,31 @@ class ScanErrorScreen extends StatelessWidget {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
class ExpenseEditor extends HookWidget {
 | 
			
		||||
  final String filePath;
 | 
			
		||||
  final Uint8List file;
 | 
			
		||||
  final Function(Expense) onFinished;
 | 
			
		||||
 | 
			
		||||
  const ExpenseEditor({
 | 
			
		||||
    super.key,
 | 
			
		||||
    required this.filePath,
 | 
			
		||||
    required this.file,
 | 
			
		||||
    required this.onFinished,
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  @override
 | 
			
		||||
  Widget build(BuildContext context) {
 | 
			
		||||
    // TODO: implement build
 | 
			
		||||
    throw UnimplementedError();
 | 
			
		||||
    return ListView(
 | 
			
		||||
      children: [
 | 
			
		||||
        SizedBox(
 | 
			
		||||
          height: 200,
 | 
			
		||||
          child: PDFView(
 | 
			
		||||
            pdfData: file,
 | 
			
		||||
            onError: (e) {
 | 
			
		||||
              Logger.root.warning("Failed to render PDF $e");
 | 
			
		||||
              alert(context, content: Text("Failed to render PDF $e"));
 | 
			
		||||
            },
 | 
			
		||||
            fitPolicy: FitPolicy.BOTH,
 | 
			
		||||
          ),
 | 
			
		||||
        ),
 | 
			
		||||
      ],
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user