exportToFile method

Future<void> exportToFile(
  1. DateTime start,
  2. DateTime end
)

Implementation

Future<void> exportToFile(DateTime start, DateTime end) async {
  final content = await hiveRepository.export(start, end);
  final fileName = '${workerDepId}_${worker.key.dep}_${worker.key.name}_'
      '${standardFormat.format(start)}_'
      '${standardFormat.format(end)}.ais_json';
  //
  // > for web - just download
  //
  if (kIsWeb) {
    final blob = html.Blob(
      <String>[content],
      'text/json',
      'native',
    );
    html.AnchorElement(href: html.Url.createObjectUrlFromBlob(blob))
      ..setAttribute('download', fileName)
      ..click();
  } else {
    //
    // > save and try to share
    //
    final filePath = await getSafePath([fileName]);
    if (filePath == null) {
      showNotification(tr().errorSave);
    } else {
      File(filePath).writeAsStringSync(content);
      try {
        await Share.shareXFiles([XFile(filePath)]);
        // ignore: avoid_catching_errors
      } on UnimplementedError {
        showNotification(
          tr().fileSavedTo + filePath,
          duration: const Duration(seconds: 10),
        );
      } on MissingPluginException {
        showNotification(
          tr().fileSavedTo + filePath,
          duration: const Duration(seconds: 10),
        );
      }
    }
  }
}