Skip to content

Commit

Permalink
Migrate to file_selector, related to #558
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Dec 20, 2023
1 parent 56fc29f commit 5f5d339
Show file tree
Hide file tree
Showing 31 changed files with 148 additions and 212 deletions.
2 changes: 1 addition & 1 deletion app/lib/actions/export.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:butterfly/api/save_data.dart';
import 'package:butterfly/api/save.dart';
import 'package:butterfly/bloc/document_bloc.dart';
import 'package:butterfly_api/butterfly_api.dart';
import 'package:flutter/material.dart';
Expand Down
30 changes: 14 additions & 16 deletions app/lib/api/open.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'dart:async';
import 'dart:io';

import 'package:butterfly/visualizer/asset.dart';
import 'package:butterfly_api/butterfly_api.dart';
import 'package:file_picker/file_picker.dart';
import 'package:file_selector/file_selector.dart' as fs;
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
Expand All @@ -18,21 +18,19 @@ Future<bool> openHelp(List<String> pageLocation, [String? fragment]) {
mode: LaunchMode.externalApplication);
}

Future<(Uint8List?, String?)> openSupported(
[List<String>? fileExtension]) async {
final isMobile = !kIsWeb && (Platform.isAndroid || Platform.isIOS);
final files = await FilePicker.platform.pickFiles(
type: isMobile ? FileType.any : FileType.custom,
allowedExtensions: isMobile
? null
: (fileExtension ??
AssetFileType.values.expand((e) => e.getFileExtensions()).toList()),
allowMultiple: false,
withData: true,
Future<(Uint8List?, String?)> importFile(BuildContext context,
[List<AssetFileType>? types]) async {
final result = await fs.openFile(
acceptedTypeGroups: (types ?? AssetFileType.values)
.map((e) => fs.XTypeGroup(
label: e.getLocalizedName(context),
extensions: e.getFileExtensions(),
mimeTypes: [e.getMime()],
))
.toList(),
);
if (files?.files.isEmpty ?? true) return (null, null);
var e = files!.files.first;
return (e.bytes, e.extension);
final data = await result?.readAsBytes();
return (data, result?.name.split('.').lastOrNull);
}

Future<void> openFile(
Expand Down
22 changes: 16 additions & 6 deletions app/lib/api/save_data.dart → app/lib/api/save.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import 'package:butterfly/api/save_data_stub.dart'
if (dart.library.io) 'package:butterfly/api/save_data_io.dart'
if (dart.library.js) 'package:butterfly/api/save_data_html.dart'
as save_data;
import 'package:file_selector/file_selector.dart' as fs;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
Expand All @@ -11,8 +8,21 @@ Future<void> exportFile(
List<int> bytes,
String fileExtension,
String mimeType,
) async =>
save_data.exportFile(context, bytes, fileExtension, mimeType);
) async {
final result = await fs.getSaveLocation(
acceptedTypeGroups: [
fs.XTypeGroup(
label: AppLocalizations.of(context).export,
extensions: [fileExtension],
mimeTypes: [mimeType],
),
],
);
if (result == null) return;
final file = fs.XFile.fromData(Uint8List.fromList(bytes),
mimeType: mimeType, name: 'output.$fileExtension');
await file.saveTo(result.path);
}

Future<void> exportSvg(BuildContext context, String data) =>
exportFile(context, data.codeUnits, 'svg', 'image/svg');
Expand Down
20 changes: 0 additions & 20 deletions app/lib/api/save_data_html.dart

This file was deleted.

60 changes: 0 additions & 60 deletions app/lib/api/save_data_io.dart

This file was deleted.

10 changes: 0 additions & 10 deletions app/lib/api/save_data_stub.dart

This file was deleted.

3 changes: 0 additions & 3 deletions app/lib/dialogs/background/dialog.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import 'dart:io';
import 'dart:ui' as ui;

import 'package:butterfly/api/open.dart';
import 'package:butterfly/bloc/document_bloc.dart';
import 'package:butterfly/dialogs/texture.dart';
import 'package:butterfly/visualizer/preset.dart';
import 'package:butterfly_api/butterfly_api.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
Expand Down
17 changes: 7 additions & 10 deletions app/lib/dialogs/background/general.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,13 @@ class _GeneralBackgroundPropertiesView extends StatelessWidget {
onTap: () async {
final state = context.read<DocumentBloc>().state;
if (state is! DocumentLoaded) return;
final files = await FilePicker.platform.pickFiles(
type: FileType.image, allowMultiple: false, withData: true);
if (files?.files.isEmpty ?? true) return;
final e = files!.files.first;
var content = e.bytes ?? Uint8List(0);
if (!kIsWeb) {
content = await File(e.path ?? '').readAsBytes();
}
final dataPath = Uri.dataFromBytes(content).toString();
final codec = await ui.instantiateImageCodec(content);
final (result, _) = await importFile(
context,
[AssetFileType.image],
);
if (result == null) return;
final dataPath = Uri.dataFromBytes(result).toString();
final codec = await ui.instantiateImageCodec(result);
final frame = await codec.getNextFrame();
final image = frame.image;
final width = image.width.toDouble(),
Expand Down
2 changes: 1 addition & 1 deletion app/lib/dialogs/collaboration/dialog.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:butterfly/api/save_data.dart';
import 'package:butterfly/api/save.dart';
import 'package:butterfly/bloc/document_bloc.dart';
import 'package:butterfly/cubits/current_index.dart';
import 'package:butterfly/services/network.dart';
Expand Down
2 changes: 1 addition & 1 deletion app/lib/dialogs/export/image.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'dart:ui' as ui;

import 'package:butterfly/api/save_data.dart';
import 'package:butterfly/api/save.dart';
import 'package:butterfly/bloc/document_bloc.dart';
import 'package:butterfly_api/butterfly_api.dart';
import 'package:flutter/foundation.dart';
Expand Down
2 changes: 1 addition & 1 deletion app/lib/dialogs/export/svg.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:material_leap/material_leap.dart';
import 'package:phosphor_flutter/phosphor_flutter.dart';

import '../../api/save_data.dart';
import '../../api/save.dart';

class SvgExportDialog extends StatefulWidget {
final double x, y;
Expand Down
2 changes: 1 addition & 1 deletion app/lib/dialogs/packs/color_pick.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:butterfly/api/save_data.dart';
import 'package:butterfly/api/save.dart';
import 'package:butterfly/bloc/document_bloc.dart';
import 'package:butterfly_api/butterfly_api.dart';
import 'package:collection/collection.dart';
Expand Down
5 changes: 3 additions & 2 deletions app/lib/dialogs/packs/dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:material_leap/material_leap.dart';
import 'package:phosphor_flutter/phosphor_flutter.dart';

import '../../api/save_data.dart';
import '../../api/save.dart';
import '../../widgets/remote_button.dart';
import 'pack.dart';

Expand Down Expand Up @@ -337,7 +337,8 @@ class _PacksDialogState extends State<PacksDialog>
PhosphorIconsLight.arrowSquareIn),
onTap: () async {
Navigator.of(ctx).pop();
final (data, _) = await openSupported(['bfly']);
final (data, _) = await importFile(
context, [AssetFileType.note]);
if (data == null) return;
final pack = NoteData.fromData(data);
final metadata = pack.getMetadata();
Expand Down
57 changes: 10 additions & 47 deletions app/lib/handlers/asset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,17 @@ Future<void> showImportAssetWizard(ImportType type, BuildContext context,
position: position, advanced: advanced);
}

Future<void> importWithDialog(
AssetFileType assetType, {
FileType type = FileType.any,
List<String>? allowedExtensions,
}) async {
final files = await FilePicker.platform.pickFiles(
type: type,
allowedExtensions: allowedExtensions,
allowMultiple: false,
withData: true,
);
if (files?.files.isEmpty ?? true) return;
final e = files!.files.first;
var content = e.bytes ?? Uint8List(0);
if (!kIsWeb) {
content = await File(e.path ?? '').readAsBytes();
}
return importAsset(assetType, content);
Future<void> importWithDialog(AssetFileType type) async {
final (result, _) = await importFile(context, [type]);
if (result == null) return;
return importAsset(type, result);
}

if (!type.isAvailable()) return;

switch (type) {
case ImportType.image:
return importWithDialog(
AssetFileType.image,
type: FileType.image,
);
return importWithDialog(AssetFileType.image);
case ImportType.camera:
final content = await showDialog<Uint8List>(
context: context,
Expand All @@ -64,34 +47,14 @@ Future<void> showImportAssetWizard(ImportType type, BuildContext context,
if (content == null) return;
return importAsset(AssetFileType.image, content);
case ImportType.svg:
return importWithDialog(
AssetFileType.svg,
type: FileType.custom,
allowedExtensions: ['svg'],
);
return importWithDialog(AssetFileType.svg);
case ImportType.pdf:
return importWithDialog(
AssetFileType.pdf,
type: FileType.custom,
allowedExtensions: ['pdf'],
);
return importWithDialog(AssetFileType.pdf);
case ImportType.document:
return importWithDialog(
AssetFileType.note,
type: FileType.custom,
allowedExtensions: ['bfly'],
);
return importWithDialog(AssetFileType.note);
case ImportType.markdown:
return importWithDialog(
AssetFileType.markdown,
type: FileType.custom,
allowedExtensions: ['md', 'markdown'],
);
return importWithDialog(AssetFileType.markdown);
case ImportType.xopp:
return importWithDialog(
AssetFileType.xopp,
type: FileType.custom,
allowedExtensions: ['xopp'],
);
return importWithDialog(AssetFileType.xopp);
}
}
5 changes: 2 additions & 3 deletions app/lib/handlers/handler.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:math';

import 'package:animations/animations.dart';
import 'package:butterfly/api/open.dart';
import 'package:butterfly/bloc/document_bloc.dart';
import 'package:butterfly/cubits/settings.dart';
import 'package:butterfly/cubits/transform.dart';
Expand All @@ -19,7 +19,6 @@ import 'package:butterfly/visualizer/tool.dart';
import 'package:butterfly_api/butterfly_api.dart';
import 'package:butterfly_api/butterfly_text.dart' as text;
import 'package:collection/collection.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
Expand All @@ -34,7 +33,7 @@ import 'package:share_plus/share_plus.dart';

import '../actions/paste.dart';
import '../actions/select.dart';
import '../api/save_data.dart';
import '../api/save.dart';
import '../cubits/current_index.dart';
import '../dialogs/import/camera.dart';
import '../dialogs/name.dart';
Expand Down
2 changes: 1 addition & 1 deletion app/lib/services/export.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:butterfly/api/save_data.dart';
import 'package:butterfly/api/save.dart';
import 'package:butterfly/bloc/document_bloc.dart';
import 'package:butterfly/cubits/current_index.dart';
import 'package:butterfly/helpers/element.dart';
Expand Down
2 changes: 1 addition & 1 deletion app/lib/services/import.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import 'package:pdf/pdf.dart';
import 'package:printing/printing.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

import '../api/save_data.dart';
import '../api/save.dart';
import '../cubits/current_index.dart';
import '../cubits/settings.dart';
import '../dialogs/error.dart';
Expand Down
Loading

0 comments on commit 5f5d339

Please sign in to comment.