Skip to content

Commit

Permalink
Merge pull request #560 from LinwoodDev/feature/file-selector
Browse files Browse the repository at this point in the history
Migrate to file_selector
  • Loading branch information
CodeDoctorDE authored Dec 21, 2023
2 parents 37dd3ca + 780bce3 commit 39dc5ef
Show file tree
Hide file tree
Showing 43 changed files with 459 additions and 475 deletions.
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ body:
options:
- Android
- Windows
- MacOS
- Linux
- Web
validations:
Expand Down
2 changes: 1 addition & 1 deletion FLUTTER_VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.16.4
3.16.5
2 changes: 1 addition & 1 deletion api/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ dependencies:
json_annotation: ^4.8.1
replay_bloc: ^0.2.5
rxdart: ^0.27.7
xml: ^6.4.2
xml: ^6.5.0
dev_dependencies:
test: ^1.25.0
json_serializable: ^6.7.1
Expand Down
27 changes: 14 additions & 13 deletions app/android/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ GEM
specs:
CFPropertyList (3.0.6)
rexml
addressable (2.8.5)
addressable (2.8.6)
public_suffix (>= 2.0.2, < 6.0)
artifactory (3.0.15)
atomos (0.1.3)
aws-eventstream (1.3.0)
aws-partitions (1.862.0)
aws-partitions (1.870.0)
aws-sdk-core (3.190.0)
aws-eventstream (~> 1, >= 1.3.0)
aws-partitions (~> 1, >= 1.651.0)
aws-sigv4 (~> 1.8)
jmespath (~> 1, >= 1.6.1)
aws-sdk-kms (1.74.0)
aws-sdk-kms (1.75.0)
aws-sdk-core (~> 3, >= 3.188.0)
aws-sigv4 (~> 1.1)
aws-sdk-s3 (1.141.0)
Expand All @@ -35,7 +35,7 @@ GEM
domain_name (0.6.20231109)
dotenv (2.8.1)
emoji_regex (3.2.3)
excon (0.105.0)
excon (0.108.0)
faraday (1.10.3)
faraday-em_http (~> 1.0)
faraday-em_synchrony (~> 1.0)
Expand Down Expand Up @@ -106,7 +106,7 @@ GEM
xcpretty (~> 0.3.0)
xcpretty-travis-formatter (>= 0.0.3)
gh_inspector (1.1.3)
google-apis-androidpublisher_v3 (0.53.0)
google-apis-androidpublisher_v3 (0.54.0)
google-apis-core (>= 0.11.0, < 2.a)
google-apis-core (0.11.2)
addressable (~> 2.5, >= 2.5.1)
Expand All @@ -123,11 +123,11 @@ GEM
google-apis-core (>= 0.11.0, < 2.a)
google-apis-storage_v1 (0.29.0)
google-apis-core (>= 0.11.0, < 2.a)
google-cloud-core (1.6.0)
google-cloud-env (~> 1.0)
google-cloud-core (1.6.1)
google-cloud-env (>= 1.0, < 3.a)
google-cloud-errors (~> 1.0)
google-cloud-env (1.6.0)
faraday (>= 0.17.3, < 3.0)
google-cloud-env (2.1.0)
faraday (>= 1.0, < 3.a)
google-cloud-errors (1.3.1)
google-cloud-storage (1.45.0)
addressable (~> 2.8)
Expand All @@ -137,8 +137,9 @@ GEM
google-cloud-core (~> 1.6)
googleauth (>= 0.16.2, < 2.a)
mini_mime (~> 1.0)
googleauth (1.8.1)
faraday (>= 0.17.3, < 3.a)
googleauth (1.9.1)
faraday (>= 1.0, < 3.a)
google-cloud-env (~> 2.1)
jwt (>= 1.4, < 3.0)
multi_json (~> 1.11)
os (>= 0.9, < 2.0)
Expand All @@ -148,7 +149,7 @@ GEM
domain_name (~> 0.5)
httpclient (2.8.3)
jmespath (1.6.2)
json (2.7.0)
json (2.7.1)
jwt (2.7.1)
mini_magick (4.12.0)
mini_mime (1.1.5)
Expand Down Expand Up @@ -186,7 +187,7 @@ GEM
unicode-display_width (>= 1.1.1, < 3)
trailblazer-option (0.1.2)
tty-cursor (0.7.1)
tty-screen (0.8.1)
tty-screen (0.8.2)
tty-spinner (0.9.3)
tty-cursor (~> 0.7)
uber (0.1.0)
Expand Down
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
40 changes: 24 additions & 16 deletions app/lib/api/open.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
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';
import 'package:url_launcher/url_launcher.dart';

Future<bool> openReleaseNotes() {
return launchUrl(
Uri(
scheme: 'https',
host: 'go.linwood.dev',
pathSegments: ['butterfly', '2.0']),
mode: LaunchMode.externalApplication);
}

Future<bool> openHelp(List<String> pageLocation, [String? fragment]) {
return launchUrl(
Uri(
Expand All @@ -18,21 +27,20 @@ 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)
.where((e) => e.getFileExtensions().isNotEmpty)
.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
10 changes: 0 additions & 10 deletions app/lib/api/open_release_notes.dart

This file was deleted.

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
Loading

0 comments on commit 39dc5ef

Please sign in to comment.