Skip to content

Commit

Permalink
Merge branch 'miguelpruivo:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
stakanojmdc authored Aug 16, 2024
2 parents 375a10a + cc26969 commit 36ee60c
Show file tree
Hide file tree
Showing 24 changed files with 118 additions and 126 deletions.
29 changes: 29 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
## 8.1.1
### Web
- Migrate the web example to use the new web bootstrapping, introduced in Flutter 3.22.

## 8.1.0
### General
- Updates the minimum Flutter version to 3.22.0, to support Dart 3.4.

### Web
- Updates the `package:web` dependency to 1.0.0.
- Removes a redundant `FilePickerWeb.platform` static field. Use `FilePicker.platform` instead.

## 8.0.7
### General
- Fixes an issue relating to incorrect registration of platform-specific implementations. [#1555](https://github.com/miguelpruivo/flutter_file_picker/issues/1555).
- Fixes an issue that required users to use `kIsWeb` and `FilePickerWeb` when running on the web.

## 8.0.6
### Android
Updates the minimum Android SDK version to 21, to match the current Flutter minimum Android SDK version. Fixes [#1522](https://github.com/miguelpruivo/flutter_file_picker/issues/1522), [#1528](https://github.com/miguelpruivo/flutter_file_picker/issues/1528), and [#1531](https://github.com/miguelpruivo/flutter_file_picker/issues/1531). Thanks @navaronbracke.

## 8.0.5
### Web
Release picked file data in memory to avoid memory leak

## 8.0.4
### Android
Removes references to Flutter v1 android embedding classes.

## 8.0.3
### Web
Fixes a TypeError with `pickFiles()` when using the HTML renderer.
Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ if (result != null) {
#### Multiple files with extension filter
```dart
FilePickerResult? result = await FilePicker.platform.pickFiles(
allowMultiple: true,
type: FileType.custom,
allowedExtensions: ['jpg', 'pdf', 'doc'],
);
Expand Down Expand Up @@ -160,19 +161,19 @@ For full usage details refer to the **[Wiki](https://github.com/miguelpruivo/flu

## Example App
#### Android
![DemoAndroid](https://github.com/miguelpruivo/flutter_file_picker/blob/master/example/screenshots/example_android.gif)
![DemoAndroid](https://github.com/miguelpruivo/flutter_file_picker/blob/master/example/screenshots/example_android.gif?raw=true)

#### iOS
![DemoMultiFilters](https://github.com/miguelpruivo/flutter_file_picker/blob/master/example/screenshots/example_ios.gif)
![DemoMultiFilters](https://github.com/miguelpruivo/flutter_file_picker/blob/master/example/screenshots/example_ios.gif?raw=true)

#### MacOS
![DemoMacOS](https://github.com/miguelpruivo/flutter_file_picker/blob/master/example/screenshots/example_macos.png)
![DemoMacOS](https://github.com/miguelpruivo/flutter_file_picker/blob/master/example/screenshots/example_macos.png?raw=true)

#### Linux
![DemoLinux](https://github.com/miguelpruivo/flutter_file_picker/blob/master/example/screenshots/example_linux.gif)
![DemoLinux](https://github.com/miguelpruivo/flutter_file_picker/blob/master/example/screenshots/example_linux.gif?raw=true)

#### Windows
![DemoWindows](https://github.com/miguelpruivo/flutter_file_picker/blob/master/example/screenshots/example_windows.gif)
![DemoWindows](https://github.com/miguelpruivo/flutter_file_picker/blob/master/example/screenshots/example_windows.gif?raw=true)

## Getting Started

Expand Down
4 changes: 2 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ rootProject.allprojects {
apply plugin: 'com.android.library'

android {
compileSdkVersion 33
compileSdk 34

defaultConfig {
minSdkVersion 16
minSdk 21
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
lintOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.PluginRegistry;

/**
* FilePickerPlugin
Expand Down Expand Up @@ -116,29 +115,6 @@ public void onActivityStopped(final Activity activity) {
private static boolean withData = false;
private static int compressionQuality;

/**
* Plugin registration.
*/
public static void registerWith(final io.flutter.plugin.common.PluginRegistry.Registrar registrar) {

if (registrar.activity() == null) {
// If a background flutter view tries to register the plugin, there will be no activity from the registrar,
// we stop the registering process immediately because the ImagePicker requires an activity.
return;
}

final Activity activity = registrar.activity();
Application application = null;
if (registrar.context() != null) {
application = (Application) (registrar.context().getApplicationContext());
}

final FilePickerPlugin plugin = new FilePickerPlugin();
plugin.setup(registrar.messenger(), application, activity, registrar, null);

}


@SuppressWarnings("unchecked")
@Override
public void onMethodCall(final MethodCall call, final MethodChannel.Result rawResult) {
Expand Down Expand Up @@ -258,7 +234,6 @@ private void setup(
final BinaryMessenger messenger,
final Application application,
final Activity activity,
final PluginRegistry.Registrar registrar,
final ActivityPluginBinding activityBinding) {

this.activity = activity;
Expand All @@ -278,18 +253,11 @@ public void onCancel(final Object arguments) {
}
});
this.observer = new LifeCycleObserver(activity);
if (registrar != null) {
// V1 embedding setup for activity listeners.
application.registerActivityLifecycleCallbacks(this.observer);
registrar.addActivityResultListener(this.delegate);
registrar.addRequestPermissionsResultListener(this.delegate);
} else {
// V2 embedding setup for activity listeners.
activityBinding.addActivityResultListener(this.delegate);
activityBinding.addRequestPermissionsResultListener(this.delegate);
this.lifecycle = FlutterLifecycleAdapter.getActivityLifecycle(activityBinding);
this.lifecycle.addObserver(this.observer);
}
// V2 embedding setup for activity listeners.
activityBinding.addActivityResultListener(this.delegate);
activityBinding.addRequestPermissionsResultListener(this.delegate);
this.lifecycle = FlutterLifecycleAdapter.getActivityLifecycle(activityBinding);
this.lifecycle.addObserver(this.observer);
}

private void tearDown() {
Expand Down Expand Up @@ -325,7 +293,6 @@ public void onAttachedToActivity(final ActivityPluginBinding binding) {
this.pluginBinding.getBinaryMessenger(),
(Application) this.pluginBinding.getApplicationContext(),
this.activityBinding.getActivity(),
null,
this.activityBinding);
}

Expand Down
6 changes: 3 additions & 3 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
compileSdk 33
compileSdk 34

lintOptions {
disable 'InvalidPackage'
}

defaultConfig {
applicationId "com.mr.flutter.plugin.filepicker.example"
minSdkVersion flutter.minSdkVersion
targetSdk 33
minSdk flutter.minSdkVersion
targetSdk 34
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down
17 changes: 0 additions & 17 deletions example/lib/generated_plugin_registrant.dart

This file was deleted.

10 changes: 1 addition & 9 deletions example/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,8 @@

<title>example</title>
<link rel="manifest" href="manifest.json">
<script src="flutter.js"></script>
</head>
<body>
<!-- This script installs service_worker.js to provide PWA functionality to
application. For more information, see:
https://developers.google.com/web/fundamentals/primers/service-workers -->
<script>
{{flutter_build_config}}
_flutter.loader.load();
</script>
<script src="main.dart.js" type="application/javascript"></script>
<script src="flutter_bootstrap.js" async></script>
</body>
</html>
19 changes: 11 additions & 8 deletions lib/_internal/file_picker_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ class FilePickerWeb extends FilePicker {

final int _readStreamChunkSize = 1000 * 1000; // 1 MB

static final FilePickerWeb platform = FilePickerWeb._();

FilePickerWeb._() {
_target = _ensureInitialized(_kFilePickerInputsDomId);
}

static void registerWith(Registrar registrar) {
FilePicker.platform = platform;
FilePicker.platform = FilePickerWeb._();
}

/// Initializes a DOM container where we can host input elements.
Expand Down Expand Up @@ -169,6 +167,12 @@ class FilePickerWeb extends FilePicker {
_target.children.add(uploadInput);
uploadInput.click();

firstChild = _target.firstChild;
while (firstChild != null) {
_target.removeChild(firstChild);
firstChild = _target.firstChild;
}

final List<PlatformFile>? files = await filesCompleter.future;

return files == null ? null : FilePickerResult(files);
Expand Down Expand Up @@ -212,16 +216,15 @@ class FilePickerWeb extends FilePicker {
if (readerResult == null) {
continue;
}
// TODO: use `isA<JSArrayBuffer>()` when switching to Dart 3.4

// Handle the ArrayBuffer type. This maps to a `ByteBuffer` in Dart.
if (readerResult.instanceOfString('ArrayBuffer')) {
if (readerResult.isA<JSArrayBuffer>()) {
yield (readerResult as JSArrayBuffer).toDart.asUint8List();
start += _readStreamChunkSize;
continue;
}
// TODO: use `isA<JSArray>()` when switching to Dart 3.4
// Handle the Array type.
if (readerResult.instanceOfString('Array')) {

if (readerResult.isA<JSArray>()) {
// Assume this is a List<int>.
yield (readerResult as JSArray).toDart.cast<int>();
start += _readStreamChunkSize;
Expand Down
7 changes: 7 additions & 0 deletions lib/file_picker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,10 @@ library file_picker;
export './src/file_picker.dart';
export './src/platform_file.dart';
export './src/file_picker_result.dart';
export './src/file_picker_macos.dart';
export './src/linux/file_picker_linux.dart';
export './src/file_picker_io.dart';
// Conditional export needed for web to successfully compile,
// as `dart:ffi` is not available on the web.
export './src/windows/file_picker_windows_stub.dart'
if (dart.library.ffi) './src/windows/file_picker_windows.dart';
24 changes: 1 addition & 23 deletions lib/src/file_picker.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';

import 'package:file_picker/src/file_picker_io.dart';
import 'package:file_picker/src/file_picker_macos.dart';
import 'package:file_picker/src/file_picker_result.dart';
import 'package:file_picker/src/linux/file_picker_linux.dart';
import 'package:file_picker/src/windows/stub.dart'
if (dart.library.io) 'package:file_picker/src/windows/file_picker_windows.dart';
import 'package:plugin_platform_interface/plugin_platform_interface.dart';

const String defaultDialogTitle = '';
Expand Down Expand Up @@ -38,7 +32,7 @@ abstract class FilePicker extends PlatformInterface {

static final Object _token = Object();

static FilePicker _instance = FilePicker._setPlatform();
static late FilePicker _instance;

static FilePicker get platform => _instance;

Expand All @@ -47,22 +41,6 @@ abstract class FilePicker extends PlatformInterface {
_instance = instance;
}

factory FilePicker._setPlatform() {
if (Platform.isAndroid || Platform.isIOS) {
return FilePickerIO();
} else if (Platform.isLinux) {
return FilePickerLinux();
} else if (Platform.isWindows) {
return filePickerWithFFI();
} else if (Platform.isMacOS) {
return FilePickerMacOS();
} else {
throw UnimplementedError(
'The current platform "${Platform.operatingSystem}" is not supported by this plugin.',
);
}
}

/// Retrieves the file(s) from the underlying platform
///
/// Default [type] set to [FileType.any] with [allowMultiple] set to `false`.
Expand Down
4 changes: 4 additions & 0 deletions lib/src/file_picker_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ const EventChannel _eventChannel =

/// An implementation of [FilePicker] that uses method channels.
class FilePickerIO extends FilePicker {
static void registerWith() {
FilePicker.platform = FilePickerIO();
}

static const String _tag = 'MethodChannelFilePicker';
static StreamSubscription? _eventSubscription;

Expand Down
6 changes: 5 additions & 1 deletion lib/src/file_picker_macos.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import 'package:file_picker/file_picker.dart';
import 'package:file_picker/src/utils.dart';

class FilePickerMacOS extends FilePicker {
static void registerWith() {
FilePicker.platform = FilePickerMacOS();
}

@override
Future<FilePickerResult?> pickFiles({
String? dialogTitle,
Expand Down Expand Up @@ -131,7 +135,7 @@ class FilePickerMacOS extends FilePicker {
case FileType.media:
return '"avi", "flv", "m4v", "mkv", "mov", "mp4", "mpeg", "webm", "wmv", "bmp", "gif", "jpeg", "jpg", "png"';
case FileType.video:
return '"avi", "flv", "mkv", "mov", "mp4", "mpeg", "webm", "wmv"';
return '"avi", "flv", "mkv", "mov", "mp4", "m4v", "mpeg", "webm", "wmv"';
default:
throw Exception('unknown file type');
}
Expand Down
4 changes: 4 additions & 0 deletions lib/src/linux/file_picker_linux.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import 'package:file_picker/src/platform_file.dart';
import 'package:file_picker/src/utils.dart';

class FilePickerLinux extends FilePicker {
static void registerWith() {
FilePicker.platform = FilePickerLinux();
}

@override
Future<FilePickerResult?> pickFiles({
String? dialogTitle,
Expand Down
12 changes: 8 additions & 4 deletions lib/src/windows/file_picker_windows.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import 'package:file_picker/src/windows/file_picker_windows_ffi_types.dart';
import 'package:path/path.dart';
import 'package:win32/win32.dart';

FilePicker filePickerWithFFI() => FilePickerWindows();

class FilePickerWindows extends FilePicker {
static void registerWith() {
FilePicker.platform = FilePickerWindows();
}

@override
Future<FilePickerResult?> pickFiles({
String? dialogTitle,
Expand Down Expand Up @@ -94,7 +96,9 @@ class FilePickerWindows extends FilePicker {
String? initialDirectory,
}) {
int hr = CoInitializeEx(
nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
nullptr,
COINIT.COINIT_APARTMENTTHREADED | COINIT.COINIT_DISABLE_OLE1DDE,
);

if (!SUCCEEDED(hr)) throw WindowsException(hr);

Expand Down Expand Up @@ -134,7 +138,7 @@ class FilePickerWindows extends FilePicker {
if (!SUCCEEDED(hr)) {
CoUninitialize();

if (hr == HRESULT_FROM_WIN32(ERROR_CANCELLED)) {
if (hr == HRESULT_FROM_WIN32(WIN32_ERROR.ERROR_CANCELLED)) {
return Future.value(null);
}
throw WindowsException(hr);
Expand Down
Loading

0 comments on commit 36ee60c

Please sign in to comment.