Skip to content

Commit bfc9937

Browse files
committed
Implement isDirectoryAccessSupported
1 parent 61cbaa1 commit bfc9937

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

android/src/main/kotlin/codeux/design/filepicker/file_picker_writable/FilePickerWritablePlugin.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ class FilePickerWritablePlugin : FlutterPlugin, MethodCallHandler,
115115
?: throw FilePickerException("Expected argument 'path'")
116116
impl.openFilePickerForCreate(result, path)
117117
}
118+
"isDirectoryAccessSupported" -> {
119+
result.success(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
120+
}
118121
"openDirectoryPicker" -> {
119122
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
120123
val initialDirUri = call.argument<String>("initialDirUri")

ios/Classes/SwiftFilePickerWritablePlugin.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ public class SwiftFilePickerWritablePlugin: NSObject, FlutterPlugin {
6969
throw FilePickerError.invalidArguments(message: "Expected 'args'")
7070
}
7171
openFilePickerForCreate(path: path, result: result)
72+
case "isDirectoryAccessSupported":
73+
result(true)
7274
case "openDirectoryPicker":
7375
guard let args = call.arguments as? Dictionary<String, Any> else {
7476
throw FilePickerError.invalidArguments(message: "Expected 'args'")

lib/src/file_picker_writable.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,28 @@ class FilePickerWritable {
247247
});
248248
}
249249

250+
/// See if the the directory picker and directory tree access is supported on
251+
/// the current platform. If this returns `false` then [openDirectory],
252+
/// [getDirectory], and [resolveRelativePath] will fail with an exception.
253+
Future<bool> isDirectoryAccessSupported() async {
254+
_logger.finest('isDirectoryAccessSupported()');
255+
final result =
256+
await _channel.invokeMethod<bool>('isDirectoryAccessSupported');
257+
if (result == null) {
258+
throw StateError('Error while checking if directory access is supported');
259+
}
260+
return result;
261+
}
262+
250263
/// Shows a directory picker so the user can select a directory.
251264
///
252265
/// [initialDirUri] is the URI indicating where the picker should start by
253266
/// default. This is only honored on a best-effort basis and even then is not
254267
/// supported on all systems. It can be a [FileInfo.uri] or a
255268
/// [DirectoryInfo.uri].
269+
///
270+
/// An exception will be thrown if invoked on a system that does not support
271+
/// directory access, i.e. if [isDirectoryAccessSupported] returns `false`.
256272
Future<DirectoryInfo?> openDirectory({String? initialDirUri}) async {
257273
_logger.finest('openDirectoryPicker()');
258274
final result = await _channel.invokeMapMethod<String, String>(
@@ -296,6 +312,9 @@ class FilePickerWritable {
296312
///
297313
/// [rootIdentifier] should be a [DirectoryInfo.identifier] obtained from
298314
/// [pickDirectory]. [fileIdentifier] should be a [FileInfo.identifier].
315+
///
316+
/// An exception will be thrown if invoked on a system that does not support
317+
/// directory access, i.e. if [isDirectoryAccessSupported] returns `false`.
299318
Future<DirectoryInfo> getDirectory({
300319
required String rootIdentifier,
301320
required String fileIdentifier,
@@ -316,6 +335,9 @@ class FilePickerWritable {
316335
///
317336
/// [directoryIdentifier] should be a [DirectoryInfo.identifier] obtained from
318337
/// [pickDirectory] or [getDirectory].
338+
///
339+
/// An exception will be thrown if invoked on a system that does not support
340+
/// directory access, i.e. if [isDirectoryAccessSupported] returns `false`.
319341
Future<EntityInfo> resolveRelativePath({
320342
required String directoryIdentifier,
321343
required String relativePath,

0 commit comments

Comments
 (0)