Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run saveDocument() in an isolate #2078

Open
patricknicolosi opened this issue Sep 17, 2024 · 2 comments
Open

Run saveDocument() in an isolate #2078

patricknicolosi opened this issue Sep 17, 2024 · 2 comments
Labels
pdf viewer PDF viewer component waiting for customer response Cannot make further progress until the customer responds.

Comments

@patricknicolosi
Copy link

patricknicolosi commented Sep 17, 2024

Use case

The saveDocument function takes a long time to execute for more complex documents, it would be convenient to find a way to execute this function in an Isolate.

Currently, it is not possible to pass PdfViewerController to an isolate to run saveDocument() separately

Currently code stucks to show loading in showLoadingDialog.

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:gym_manager_document_filler/helpers/ftp_helper.dart';
import 'package:gym_manager_document_filler/screens/home/home_screen.dart';
import 'package:gym_manager_document_filler/utils/assets_utils.dart';
import 'package:gym_manager_document_filler/utils/ui_utils.dart';
import 'package:lucide_icons_flutter/lucide_icons.dart';
import 'package:path_provider/path_provider.dart';
import 'package:syncfusion_flutter_pdfviewer/pdfviewer.dart';

class PdfViewer extends StatelessWidget {
  final String documentAssetPath;
  PdfViewer(this.documentAssetPath, {super.key});

  final PdfViewerController _pdfViewerController = PdfViewerController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: FloatingActionButton.extended(
          icon: const Icon(LucideIcons.save),
          label: const Text("Salva"),
          onPressed: () async {
            await _exportPdf(
                context: context,
                fileName: AssetsUtils.formatAssetsName(
                    documentAssetPath.split('/')[2]));
          }),
      body: SafeArea(
        child: Stack(
          children: [
            SfPdfViewer.asset(
              documentAssetPath,
              controller: _pdfViewerController,
              canShowPageLoadingIndicator: true,
              initialZoomLevel: 0.6,
            ),
            Padding(
              padding: const EdgeInsets.all(50),
              child: IconButton.filledTonal(
                  icon: const Icon(LucideIcons.chevronLeft),
                  onPressed: () {
                    Navigator.pop(context);
                  }),
            )
          ],
        ),
      ),
    );
  }

  Future _exportPdf(
      {required BuildContext context, required String fileName}) async {
    UiUtils.showLoadingDialog(context: context);
    await Future.delayed(const Duration(seconds: 1));
    try {
      File filledPdf = await _createPdfFile(fileName: fileName);
      await FtpHelper.getIstance()
          .upload(fileName: "$fileName.pdf", file: filledPdf);
      filledPdf.deleteSync();
      _showSuccessUpload(context);
    } catch (e) {
      Navigator.pop(context);
      _showErrorUpload(context, e);
      return;
    }
  }

  Future<File> _createPdfFile({required String fileName}) async {
    try {
      Directory dir = await getApplicationSupportDirectory();
      File filledPdf = File("${dir.path}/$fileName.pdf");
      filledPdf.writeAsBytesSync(await _pdfViewerController.saveDocument());
      return filledPdf;
    } catch (e) {
      throw Exception(e);
    }
  }

  void _showErrorUpload(BuildContext context, Object e) {
    ScaffoldMessenger.of(context).showSnackBar(SnackBar(
      backgroundColor: Colors.red,
      content: Text(
        e.toString(),
        style: const TextStyle(color: Colors.white),
      ),
      behavior: SnackBarBehavior.floating,
      margin: const EdgeInsets.all(10),
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
    ));
    Navigator.of(context).pushAndRemoveUntil(
        MaterialPageRoute(builder: (context) => const HomeScreen()),
        (Route<dynamic> route) => false);
  }

  void _showSuccessUpload(BuildContext context) {
    ScaffoldMessenger.of(context).showSnackBar(SnackBar(
      backgroundColor: Colors.green,
      content: const Text(
        "Modulo salvato con successo",
        style: TextStyle(color: Colors.white),
      ),
      behavior: SnackBarBehavior.floating,
      margin: const EdgeInsets.all(10),
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
    ));
    Navigator.of(context).pushAndRemoveUntil(
        MaterialPageRoute(builder: (context) => const HomeScreen()),
        (Route<dynamic> route) => false);
  }
}
@patricknicolosi patricknicolosi changed the title Call saveDocument() in an isolate Run saveDocument() in an isolate Sep 17, 2024
@VijayakumarMariappan VijayakumarMariappan added pdf viewer PDF viewer component open Open labels Sep 18, 2024
@Deepak1799
Copy link
Collaborator

Deepak1799 commented Jan 21, 2025

Currently, the Syncfusion Flutter PDF Viewer does not support saving documents in a separate isolate. This is because isolates are only supported on Dart's native platform and not on the web platform. To maintain consistent functionality across all platforms, we have not provided this feature.
Reference: Concurrency | Dart

Please confirm us whether you are using the SfPdfViewer widget on the Web platform.

@Deepak1799 Deepak1799 added waiting for customer response Cannot make further progress until the customer responds. and removed open Open labels Jan 21, 2025
@patricknicolosi
Copy link
Author

I'm running this code on Android Phone

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pdf viewer PDF viewer component waiting for customer response Cannot make further progress until the customer responds.
Projects
None yet
Development

No branches or pull requests

3 participants