Commit c8bca6e

derdilla <82763757+NobodyForNothing@users.noreply.github.com>
2023-10-12 15:48:39
remove file_saver dependency that can be replaced by dart:io File object
Signed-off-by: derdilla <82763757+NobodyForNothing@users.noreply.github.com>
1 parent bd2b7e5
Changed files (10)
android/app/src/main/kotlin/com/derdilla/blood_pressure_app/StorageProvider.kt
@@ -18,15 +18,18 @@ class StorageProvider(private var context: Context,
 
     override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
          when (call.method) {
-             "shareFile" -> shareFile(call.argument<String>("path")!!, call.argument<String>("mimeType")!!)
+             "shareFile" -> shareFile(call.argument<String>("path")!!,
+                 call.argument<String>("mimeType")!!, call.argument<String>("name"))
         }
     }
 
     /**
      * Show the share sheet for saving a file.
+     *
+     * @param name Used to make the name of the shared file different from the original name.
      */
-    private fun shareFile(path: String, mimeType: String) {
-        val uri = sharableUriFromPath(path)
+    private fun shareFile(path: String, mimeType: String, name: String?) {
+        val uri = sharableUriFromPath(path, name)
 
         val sendIntent: Intent = Intent().apply {
             action = Intent.ACTION_SEND
@@ -39,9 +42,9 @@ class StorageProvider(private var context: Context,
     }
 
 
-    private fun sharableUriFromPath(path: String): Uri {
+    private fun sharableUriFromPath(path: String, name: String?): Uri {
         val initialFile = File(path)
-        val sharablePath = File(shareFolder, initialFile.name)
+        val sharablePath = File(shareFolder, name ?: initialFile.name)
         if (sharablePath.exists()) sharablePath.delete()
         initialFile.copyTo(sharablePath)
         return getUriForFile(context, "com.derdilla.bloodPressureApp.share", sharablePath)
lib/model/export_import.dart
@@ -3,6 +3,7 @@ import 'dart:convert';
 import 'dart:io';
 import 'dart:typed_data';
 
+import 'package:blood_pressure_app/model/blood_pressure.dart';
 import 'package:blood_pressure_app/model/blood_pressure_analyzer.dart';
 import 'package:blood_pressure_app/model/export_options.dart';
 import 'package:blood_pressure_app/model/storage/export_csv_settings_store.dart';
@@ -12,7 +13,6 @@ import 'package:blood_pressure_app/model/storage/settings_store.dart';
 import 'package:blood_pressure_app/platform_integration/platform_client.dart';
 import 'package:csv/csv.dart';
 import 'package:file_picker/file_picker.dart';
-import 'package:file_saver/file_saver.dart';
 import 'package:flutter/material.dart';
 import 'package:flutter_gen/gen_l10n/app_localizations.dart';
 import 'package:intl/intl.dart';
@@ -23,8 +23,6 @@ import 'package:pdf/widgets.dart' as pw;
 import 'package:provider/provider.dart';
 import 'package:sqflite/sqflite.dart';
 
-import 'blood_pressure.dart';
-
 extension PdfCompatability on Color {
   PdfColor toPdfColor() => PdfColor(red / 256, green / 256, blue / 256, opacity);
 }
@@ -279,21 +277,23 @@ class Exporter {
   }
 
   Future<void> export() async {
-    var fileContents = await ExportFileCreator(settings, exportSettings, csvExportSettings, pdfExportSettings,
+    final fileContents = await ExportFileCreator(settings, exportSettings, csvExportSettings, pdfExportSettings,
         localizations, theme, exportColumnsConfig).createFile(data);
-    String filename = 'blood_press_${DateTime.now().toIso8601String()}';
-    String ext = exportSettings.exportFormat.name;
-    String path = await FileSaver.instance.saveFile(name: filename, ext: ext, bytes: fileContents);
+    final filename = 'blood_press_${DateTime.now().toIso8601String()}';
+    final ext = exportSettings.exportFormat.name;
+
+    final file = File(join(Directory.systemTemp.path, '$filename.$ext'));
+    file.writeAsBytesSync(fileContents);
 
     assert(Platform.isAndroid || Platform.isIOS);
     if (exportSettings.defaultExportDir.isNotEmpty) {
       JSaver.instance.save(
-          fromPath: path,
+          fromPath: file.path,
           androidPathOptions: AndroidPathOptions(toDefaultDirectory: true)
       );
       messenger.showSnackBar(SnackBar(content: Text(localizations.success(exportSettings.defaultExportDir))));
     } else {
-      PlatformClient.shareFile(path, 'text/csv');
+      PlatformClient.shareFile(file.path, 'text/csv', '$filename.$ext');
     }
   }
 
lib/platform_integration/platform_client.dart
@@ -23,13 +23,16 @@ class PlatformClient {
   ///
   /// The [mimeType] can be any string but should generally follow the `*/*` pattern. All official mime types can be
   /// found here: https://mimetype.io/all-types
+  ///
+  /// When [name] is set to a non-null value the file will be shared with this name instead of the original file name.
   /// 
   /// The returned value indicates whether a [PlatformException] was thrown.
-  static Future<bool> shareFile(String path, String mimeType) async {
+  static Future<bool> shareFile(String path, String mimeType, [String? name]) async {
     try {
       await _platformChannel.invokeMethod('shareFile', {
         'path': path,
-        'mimeType': mimeType
+        'mimeType': mimeType,
+        'name': name
       });
       return true;
     } on PlatformException {
linux/flutter/generated_plugin_registrant.cc
@@ -6,13 +6,9 @@
 
 #include "generated_plugin_registrant.h"
 
-#include <file_saver/file_saver_plugin.h>
 #include <url_launcher_linux/url_launcher_plugin.h>
 
 void fl_register_plugins(FlPluginRegistry* registry) {
-  g_autoptr(FlPluginRegistrar) file_saver_registrar =
-      fl_plugin_registry_get_registrar_for_plugin(registry, "FileSaverPlugin");
-  file_saver_plugin_register_with_registrar(file_saver_registrar);
   g_autoptr(FlPluginRegistrar) url_launcher_linux_registrar =
       fl_plugin_registry_get_registrar_for_plugin(registry, "UrlLauncherPlugin");
   url_launcher_plugin_register_with_registrar(url_launcher_linux_registrar);
linux/flutter/generated_plugins.cmake
@@ -3,7 +3,6 @@
 #
 
 list(APPEND FLUTTER_PLUGIN_LIST
-  file_saver
   url_launcher_linux
 )
 
macos/Flutter/GeneratedPluginRegistrant.swift
@@ -5,17 +5,13 @@
 import FlutterMacOS
 import Foundation
 
-import file_saver
 import package_info_plus
-import path_provider_foundation
 import shared_preferences_foundation
 import sqflite
 import url_launcher_macos
 
 func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
-  FileSaverPlugin.register(with: registry.registrar(forPlugin: "FileSaverPlugin"))
   FLTPackageInfoPlusPlugin.register(with: registry.registrar(forPlugin: "FLTPackageInfoPlusPlugin"))
-  PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
   SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
   SqflitePlugin.register(with: registry.registrar(forPlugin: "SqflitePlugin"))
   UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
windows/flutter/generated_plugin_registrant.cc
@@ -6,12 +6,9 @@
 
 #include "generated_plugin_registrant.h"
 
-#include <file_saver/file_saver_plugin.h>
 #include <url_launcher_windows/url_launcher_windows.h>
 
 void RegisterPlugins(flutter::PluginRegistry* registry) {
-  FileSaverPluginRegisterWithRegistrar(
-      registry->GetRegistrarForPlugin("FileSaverPlugin"));
   UrlLauncherWindowsRegisterWithRegistrar(
       registry->GetRegistrarForPlugin("UrlLauncherWindows"));
 }
windows/flutter/generated_plugins.cmake
@@ -3,7 +3,6 @@
 #
 
 list(APPEND FLUTTER_PLUGIN_LIST
-  file_saver
   url_launcher_windows
 )
 
pubspec.lock
@@ -201,14 +201,6 @@ packages:
       url: "https://pub.dev"
     source: hosted
     version: "5.5.0"
-  file_saver:
-    dependency: "direct main"
-    description:
-      name: file_saver
-      sha256: "591d25e750e3a4b654f7b0293abc2ed857242f82ca7334051b2a8ceeb369dac8"
-      url: "https://pub.dev"
-    source: hosted
-    version: "0.2.8"
   fixnum:
     dependency: transitive
     description:
@@ -437,30 +429,6 @@ packages:
       url: "https://pub.dev"
     source: hosted
     version: "1.0.1"
-  path_provider:
-    dependency: transitive
-    description:
-      name: path_provider
-      sha256: a1aa8aaa2542a6bc57e381f132af822420216c80d4781f7aa085ca3229208aaa
-      url: "https://pub.dev"
-    source: hosted
-    version: "2.1.1"
-  path_provider_android:
-    dependency: transitive
-    description:
-      name: path_provider_android
-      sha256: "6b8b19bd80da4f11ce91b2d1fb931f3006911477cec227cce23d3253d80df3f1"
-      url: "https://pub.dev"
-    source: hosted
-    version: "2.2.0"
-  path_provider_foundation:
-    dependency: transitive
-    description:
-      name: path_provider_foundation
-      sha256: "19314d595120f82aca0ba62787d58dde2cc6b5df7d2f0daf72489e38d1b57f2d"
-      url: "https://pub.dev"
-    source: hosted
-    version: "2.3.1"
   path_provider_linux:
     dependency: transitive
     description:
pubspec.yaml
@@ -23,15 +23,14 @@ dependencies:
   shared_preferences: ^2.1.1  # BSD-3-Clause
   pdf: ^3.10.4
   package_info_plus: ^4.0.2
-
-  # can become one custom dependency
-  file_saver: ^0.2.1  # BSD-3-Clause
-  file_picker: ^5.2.11  # MIT
-  jsaver: ^1.2.0
   function_tree: ^0.9.0
   badges: ^3.1.1
   flutter_markdown: ^0.6.17
   collection: ^1.17.1
+
+  # can become one custom dependency
+  file_picker: ^5.2.11  # MIT
+  jsaver: ^1.2.0
   restart_app: ^1.2.1
 
 dev_dependencies: