This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[file_selector_platform_interface] Add platform interface for new file_selector plugin #2995
Merged
ditman
merged 14 commits into
flutter:master
from
jasonpanelli:file_selector_platform_interface
Sep 25, 2020
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
2548d8d
Add file_selector_platform_interface
jasonpanelli 73c4e72
Update pubspec.yaml
jasonpanelli a8a6192
Update Method Channels, add tests for method channels and XFile
jasonpanelli d7afd5b
Update year
jasonpanelli f3cb665
Add more XFile tests, fix dart io XFile saveTo(..)
jasonpanelli 6bf7c99
Update copyrights
jasonpanelli b3a65a3
Add XTypeGroup tests and remove document/* as a web wild card
jasonpanelli 2430dbd
Update License
jasonpanelli 185b59c
Update License (Flutter Authors)
jasonpanelli 7c947e1
Change to version 1.0.0, rename unsupported.dart, fix references to "…
jasonpanelli 942c9d2
Update openRead(..) function
jasonpanelli 2a3f0ba
Run flutter format
jasonpanelli 70b864f
Merge branch 'master' into file_selector_platform_interface
ditman ea07c3f
Add codeowners for the file_selector plugin.
ditman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
packages/file_selector/file_selector_platform_interface/CHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## 1.0.0 | ||
|
||
* Initial release. |
25 changes: 25 additions & 0 deletions
25
packages/file_selector/file_selector_platform_interface/LICENSE
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Copyright 2020 The Flutter Authors. All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without modification, | ||
are permitted provided that the following conditions are met: | ||
|
||
* Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
* Redistributions in binary form must reproduce the above | ||
copyright notice, this list of conditions and the following | ||
disclaimer in the documentation and/or other materials provided | ||
with the distribution. | ||
* Neither the name of Google Inc. nor the names of its | ||
contributors may be used to endorse or promote products derived | ||
from this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | ||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | ||
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 changes: 26 additions & 0 deletions
26
packages/file_selector/file_selector_platform_interface/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# file_selector_platform_interface | ||
|
||
A common platform interface for the `file_selector` plugin. | ||
|
||
This interface allows platform-specific implementations of the `file_selector` | ||
plugin, as well as the plugin itself, to ensure they are supporting the | ||
same interface. | ||
|
||
# Usage | ||
|
||
To implement a new platform-specific implementation of `file_selector`, extend | ||
[`FileSelectorPlatform`][2] with an implementation that performs the | ||
platform-specific behavior, and when you register your plugin, set the default | ||
`FileSelectorPlatform` by calling | ||
`FileSelectorPlatform.instance = MyPlatformFileSelector()`. | ||
|
||
# Note on breaking changes | ||
|
||
Strongly prefer non-breaking changes (such as adding a method to the interface) | ||
over breaking changes for this package. | ||
|
||
See https://flutter.dev/go/platform-interface-breaking-changes for a discussion | ||
on why a less-clean interface is preferable to a breaking change. | ||
|
||
[1]: ../file_selector | ||
[2]: lib/file_selector_platform_interface.dart |
2 changes: 2 additions & 0 deletions
2
.../file_selector/file_selector_platform_interface/lib/file_selector_platform_interface.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export 'src/platform_interface/file_selector_interface.dart'; | ||
export 'src/types/types.dart'; |
93 changes: 93 additions & 0 deletions
93
...file_selector_platform_interface/lib/src/method_channel/method_channel_file_selector.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright 2020 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'package:flutter/services.dart'; | ||
|
||
import 'package:file_selector_platform_interface/file_selector_platform_interface.dart'; | ||
import 'package:meta/meta.dart'; | ||
|
||
const MethodChannel _channel = | ||
MethodChannel('plugins.flutter.io/file_selector'); | ||
|
||
/// An implementation of [FileSelectorPlatform] that uses method channels. | ||
class MethodChannelFileSelector extends FileSelectorPlatform { | ||
/// The MethodChannel that is being used by this implementation of the plugin. | ||
@visibleForTesting | ||
MethodChannel get channel => _channel; | ||
|
||
/// Load a file from user's computer and return it as an XFile | ||
@override | ||
Future<XFile> openFile({ | ||
List<XTypeGroup> acceptedTypeGroups, | ||
String initialDirectory, | ||
String confirmButtonText, | ||
}) async { | ||
final List<String> path = await _channel.invokeListMethod<String>( | ||
'openFile', | ||
<String, dynamic>{ | ||
'acceptedTypeGroups': | ||
acceptedTypeGroups?.map((group) => group.toJSON())?.toList(), | ||
'initialDirectory': initialDirectory, | ||
'confirmButtonText': confirmButtonText, | ||
'multiple': false, | ||
}, | ||
); | ||
return path == null ? null : XFile(path?.first); | ||
} | ||
|
||
/// Load multiple files from user's computer and return it as an XFile | ||
@override | ||
Future<List<XFile>> openFiles({ | ||
List<XTypeGroup> acceptedTypeGroups, | ||
String initialDirectory, | ||
String confirmButtonText, | ||
}) async { | ||
final List<String> pathList = await _channel.invokeListMethod<String>( | ||
'openFile', | ||
<String, dynamic>{ | ||
'acceptedTypeGroups': | ||
acceptedTypeGroups?.map((group) => group.toJSON())?.toList(), | ||
'initialDirectory': initialDirectory, | ||
'confirmButtonText': confirmButtonText, | ||
'multiple': true, | ||
}, | ||
); | ||
return pathList?.map((path) => XFile(path))?.toList() ?? []; | ||
} | ||
|
||
/// Gets the path from a save dialog | ||
@override | ||
Future<String> getSavePath({ | ||
List<XTypeGroup> acceptedTypeGroups, | ||
String initialDirectory, | ||
String suggestedName, | ||
String confirmButtonText, | ||
}) async { | ||
return _channel.invokeMethod<String>( | ||
'getSavePath', | ||
<String, dynamic>{ | ||
'acceptedTypeGroups': | ||
acceptedTypeGroups?.map((group) => group.toJSON())?.toList(), | ||
'initialDirectory': initialDirectory, | ||
'suggestedName': suggestedName, | ||
'confirmButtonText': confirmButtonText, | ||
}, | ||
); | ||
} | ||
|
||
/// Gets a directory path from a dialog | ||
@override | ||
Future<String> getDirectoryPath({ | ||
String initialDirectory, | ||
String confirmButtonText, | ||
}) async { | ||
return _channel.invokeMethod<String>( | ||
'getDirectoryPath', | ||
<String, dynamic>{ | ||
'initialDirectory': initialDirectory, | ||
'confirmButtonText': confirmButtonText, | ||
}, | ||
); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
.../file_selector_platform_interface/lib/src/platform_interface/file_selector_interface.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright 2020 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'dart:async'; | ||
|
||
import 'package:file_selector_platform_interface/file_selector_platform_interface.dart'; | ||
import 'package:plugin_platform_interface/plugin_platform_interface.dart'; | ||
|
||
import '../method_channel/method_channel_file_selector.dart'; | ||
|
||
/// The interface that implementations of file_selector must implement. | ||
/// | ||
/// Platform implementations should extend this class rather than implement it as `file_selector` | ||
/// does not consider newly added methods to be breaking changes. Extending this class | ||
/// (using `extends`) ensures that the subclass will get the default implementation, while | ||
/// platform implementations that `implements` this interface will be broken by newly added | ||
/// [FileSelectorPlatform] methods. | ||
abstract class FileSelectorPlatform extends PlatformInterface { | ||
/// Constructs a FileSelectorPlatform. | ||
FileSelectorPlatform() : super(token: _token); | ||
|
||
static final Object _token = Object(); | ||
|
||
static FileSelectorPlatform _instance = MethodChannelFileSelector(); | ||
|
||
/// The default instance of [FileSelectorPlatform] to use. | ||
/// | ||
/// Defaults to [MethodChannelFileSelector]. | ||
static FileSelectorPlatform get instance => _instance; | ||
|
||
/// Platform-specific plugins should set this with their own platform-specific | ||
/// class that extends [FileSelectorPlatform] when they register themselves. | ||
static set instance(FileSelectorPlatform instance) { | ||
PlatformInterface.verifyToken(instance, _token); | ||
_instance = instance; | ||
} | ||
|
||
/// Open file dialog for loading files and return a file path | ||
Future<XFile> openFile({ | ||
List<XTypeGroup> acceptedTypeGroups, | ||
String initialDirectory, | ||
String confirmButtonText, | ||
}) { | ||
throw UnimplementedError('openFile() has not been implemented.'); | ||
} | ||
|
||
/// Open file dialog for loading files and return a list of file paths | ||
Future<List<XFile>> openFiles({ | ||
List<XTypeGroup> acceptedTypeGroups, | ||
String initialDirectory, | ||
String confirmButtonText, | ||
}) { | ||
throw UnimplementedError('openFiles() has not been implemented.'); | ||
} | ||
|
||
/// Open file dialog for saving files and return a file path at which to save | ||
Future<String> getSavePath({ | ||
List<XTypeGroup> acceptedTypeGroups, | ||
String initialDirectory, | ||
String suggestedName, | ||
String confirmButtonText, | ||
}) { | ||
throw UnimplementedError('getSavePath() has not been implemented.'); | ||
} | ||
|
||
/// Open file dialog for loading directories and return a directory path | ||
Future<String> getDirectoryPath({ | ||
String initialDirectory, | ||
String confirmButtonText, | ||
}) { | ||
throw UnimplementedError('getDirectoryPath() has not been implemented.'); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/file_selector/file_selector_platform_interface/lib/src/types/types.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export 'x_file/x_file.dart'; | ||
|
||
export 'x_type_group/x_type_group.dart'; |
82 changes: 82 additions & 0 deletions
82
packages/file_selector/file_selector_platform_interface/lib/src/types/x_file/base.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import 'dart:convert'; | ||
import 'dart:typed_data'; | ||
|
||
/// The interface for a XFile. | ||
/// | ||
/// A XFile is a container that wraps the path of a selected | ||
/// file by the user and (in some platforms, like web) the bytes | ||
/// with the contents of the file. | ||
/// | ||
/// This class is a very limited subset of dart:io [File], so all | ||
/// the methods should seem familiar. | ||
abstract class XFileBase { | ||
/// Construct a XFile | ||
XFileBase(String path); | ||
|
||
/// Save the XFile at the indicated file path. | ||
void saveTo(String path) async { | ||
throw UnimplementedError('saveTo has not been implemented.'); | ||
} | ||
|
||
/// Get the path of the picked file. | ||
/// | ||
/// This should only be used as a backwards-compatibility clutch | ||
/// for mobile apps, or cosmetic reasons only (to show the user | ||
/// the path they've picked). | ||
/// | ||
/// Accessing the data contained in the picked file by its path | ||
/// is platform-dependant (and won't work on web), so use the | ||
/// byte getters in the XFile instance instead. | ||
String get path { | ||
throw UnimplementedError('.path has not been implemented.'); | ||
} | ||
|
||
/// The name of the file as it was selected by the user in their device. | ||
/// | ||
/// Use only for cosmetic reasons, do not try to use this as a path. | ||
String get name { | ||
throw UnimplementedError('.name has not been implemented.'); | ||
} | ||
|
||
/// For web, it may be necessary for a file to know its MIME type. | ||
String get mimeType { | ||
throw UnimplementedError('.mimeType has not been implemented.'); | ||
} | ||
|
||
/// Get the length of the file. Returns a `Future<int>` that completes with the length in bytes. | ||
Future<int> length() { | ||
throw UnimplementedError('.length() has not been implemented.'); | ||
} | ||
|
||
/// Synchronously read the entire file contents as a string using the given [Encoding]. | ||
/// | ||
/// By default, `encoding` is [utf8]. | ||
/// | ||
/// Throws Exception if the operation fails. | ||
Future<String> readAsString({Encoding encoding = utf8}) { | ||
throw UnimplementedError('readAsString() has not been implemented.'); | ||
} | ||
|
||
/// Synchronously read the entire file contents as a list of bytes. | ||
/// | ||
/// Throws Exception if the operation fails. | ||
Future<Uint8List> readAsBytes() { | ||
throw UnimplementedError('readAsBytes() has not been implemented.'); | ||
} | ||
|
||
/// Create a new independent [Stream] for the contents of this file. | ||
/// | ||
/// If `start` is present, the file will be read from byte-offset `start`. Otherwise from the beginning (index 0). | ||
/// | ||
/// If `end` is present, only up to byte-index `end` will be read. Otherwise, until end of file. | ||
/// | ||
/// In order to make sure that system resources are freed, the stream must be read to completion or the subscription on the stream must be cancelled. | ||
Stream<Uint8List> openRead([int start, int end]) { | ||
throw UnimplementedError('openRead() has not been implemented.'); | ||
} | ||
|
||
/// Get the last-modified time for the XFile | ||
Future<DateTime> lastModified() { | ||
throw UnimplementedError('openRead() has not been implemented.'); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.