Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

[file_selector] Add getDirectoryPaths implementation on macOS #6575

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/file_selector/file_selector_macos/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.9.1

* Adds `getDirectoryPaths` implementation.

## 0.9.0+3

* Changes XTypeGroup initialization from final to const.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2013 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:file_selector_platform_interface/file_selector_platform_interface.dart';
import 'package:flutter/material.dart';

/// Screen that allows the user to select one or more directories using `getDirectoryPaths`,
/// then displays the selected directories in a dialog.
class GetMultipleDirectoriesPage extends StatelessWidget {
/// Default Constructor
const GetMultipleDirectoriesPage({Key? key}) : super(key: key);

Future<void> _getDirectoryPaths(BuildContext context) async {
const String confirmButtonText = 'Choose';
final List<String> directoriesPaths =
await FileSelectorPlatform.instance.getDirectoryPaths(
confirmButtonText: confirmButtonText,
);
if (directoriesPaths.isEmpty) {
// Operation was canceled by the user.
return;
}
await showDialog<void>(
context: context,
builder: (BuildContext context) =>
TextDisplay(directoriesPaths.join('\n')),
);
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Select multiple directories'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
style: ElevatedButton.styleFrom(
// TODO(darrenaustin): Migrate to new API once it lands in stable: https://github.com/flutter/flutter/issues/105724
// ignore: deprecated_member_use
primary: Colors.blue,
// ignore: deprecated_member_use
onPrimary: Colors.white,
),
child: const Text(
'Press to ask user to choose multiple directories'),
onPressed: () => _getDirectoryPaths(context),
),
],
),
),
);
}
}

/// Widget that displays a text file in a dialog.
class TextDisplay extends StatelessWidget {
/// Creates a `TextDisplay`.
const TextDisplay(this.directoryPath, {Key? key}) : super(key: key);

/// The path selected in the dialog.
final String directoryPath;

@override
Widget build(BuildContext context) {
return AlertDialog(
title: const Text('Selected Directories'),
content: Scrollbar(
child: SingleChildScrollView(
child: Text(directoryPath),
),
),
actions: <Widget>[
TextButton(
child: const Text('Close'),
onPressed: () => Navigator.pop(context),
),
],
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ class HomePage extends StatelessWidget {
child: const Text('Open a get directory dialog'),
onPressed: () => Navigator.pushNamed(context, '/directory'),
),
const SizedBox(height: 10),
ElevatedButton(
style: style,
child: const Text('Open a get directories dialog'),
onPressed: () =>
Navigator.pushNamed(context, '/multi-directories'),
),
],
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'package:flutter/material.dart';

import 'get_directory_page.dart';
import 'get_multiple_directories_page.dart';
import 'home_page.dart';
import 'open_image_page.dart';
import 'open_multiple_images_page.dart';
Expand Down Expand Up @@ -36,6 +37,8 @@ class MyApp extends StatelessWidget {
'/open/text': (BuildContext context) => const OpenTextPage(),
'/save/text': (BuildContext context) => SaveTextPage(),
'/directory': (BuildContext context) => const GetDirectoryPage(),
'/multi-directories': (BuildContext context) =>
const GetMultipleDirectoriesPage(),
},
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,13 +241,13 @@ class exampleTests: XCTestCase {
viewProvider: TestViewProvider(),
panelController: panelController)

let returnPath = "/foo/bar"
panelController.openURLs = [URL(fileURLWithPath: returnPath)]
let returnPaths = ["/foo/bar"]
panelController.openURLs = returnPaths.map({ path in URL(fileURLWithPath: path) })

let called = XCTestExpectation()
let call = FlutterMethodCall(methodName: "getDirectoryPath", arguments: [:])
plugin.handle(call) { result in
XCTAssertEqual(result as! String?, returnPath)
XCTAssertEqual(result as! [String]?, returnPaths)
called.fulfill()
}

Expand All @@ -257,8 +257,7 @@ class exampleTests: XCTestCase {
XCTAssertTrue(panel.canChooseDirectories)
// For consistency across platforms, file selection is disabled.
XCTAssertFalse(panel.canChooseFiles)
// The Dart API only allows a single directory to be returned, so users shouldn't be allowed
// to select multiple.

XCTAssertFalse(panel.allowsMultipleSelection)
}
}
Expand All @@ -279,5 +278,46 @@ class exampleTests: XCTestCase {
wait(for: [called], timeout: 0.5)
XCTAssertNotNil(panelController.openPanel)
}


func testGetDirectoryMultiple() throws {
let panelController = TestPanelController()
let plugin = FileSelectorPlugin(
viewProvider: TestViewProvider(),
panelController: panelController)

let returnPaths = ["/foo/bar", "/foo/test"]
panelController.openURLs = returnPaths.map({ path in URL(fileURLWithPath: path) })

let called = XCTestExpectation()
let call = FlutterMethodCall(methodName: "getDirectoryPath", arguments: ["multiple": true])
plugin.handle(call) { result in
XCTAssertEqual(result as! [String]?, returnPaths)
called.fulfill()
}

wait(for: [called], timeout: 0.5)
XCTAssertNotNil(panelController.openPanel)
if let panel = panelController.openPanel {
XCTAssertTrue(panel.canChooseDirectories)
XCTAssertFalse(panel.canChooseFiles)
XCTAssertTrue(panel.allowsMultipleSelection)
}
}

func testGetDirectoryMultipleCancel() throws {
let panelController = TestPanelController()
let plugin = FileSelectorPlugin(
viewProvider: TestViewProvider(),
panelController: panelController)

let called = XCTestExpectation()
let call = FlutterMethodCall(methodName: "getDirectoryPath", arguments: ["multiple": true])
plugin.handle(call) { result in
XCTAssertNil(result)
called.fulfill()
}

wait(for: [called], timeout: 0.5)
XCTAssertNotNil(panelController.openPanel)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ dependencies:
# The example app is bundled with the plugin so we use a path dependency on
# the parent directory to use the current plugin's version.
path: ..
file_selector_platform_interface: ^2.2.0
file_selector_platform_interface: ^2.4.0
flutter:
sdk: flutter

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class FileSelectorMacOS extends FileSelectorPlatform {
'multiple': false,
},
);
return path == null ? null : XFile(path.first);
final String? filePath = _firstOrNull(path);
return filePath == null ? null : XFile(filePath);
}

@override
Expand Down Expand Up @@ -79,13 +80,30 @@ class FileSelectorMacOS extends FileSelectorPlatform {
String? initialDirectory,
String? confirmButtonText,
}) async {
return _channel.invokeMethod<String>(
final List<String>? pathList = await _channel.invokeListMethod<String>(
'getDirectoryPath',
<String, dynamic>{
'initialDirectory': initialDirectory,
'confirmButtonText': confirmButtonText,
},
);
return _firstOrNull(pathList);
}

@override
Future<List<String>> getDirectoryPaths({
String? initialDirectory,
String? confirmButtonText,
}) async {
final List<String>? pathList = await _channel.invokeListMethod<String>(
'getDirectoryPath',
<String, dynamic>{
'initialDirectory': initialDirectory,
'confirmButtonText': confirmButtonText,
'multiple': true,
},
);
return pathList ?? <String>[];
}

// Converts the type group list into a flat list of all allowed types, since
Expand Down Expand Up @@ -126,4 +144,8 @@ class FileSelectorMacOS extends FileSelectorPlatform {

return allowedTypes;
}

String? _firstOrNull(List<String>? list) {
return (list?.isEmpty ?? true) ? null : list?.first;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,7 @@ public class FileSelectorPlugin: NSObject, FlutterPlugin {
configure(panel: panel, with: arguments)
configure(openPanel: panel, with: arguments, choosingDirectory: choosingDirectory)
panelController.display(panel, for: viewProvider.view?.window) { (selection: [URL]?) in
if (choosingDirectory) {
result(selection?.first?.path)
} else {
result(selection?.map({ item in item.path }))
}
result(selection?.map({ item in item.path }))
}
case saveMethod:
let panel = NSSavePanel()
Expand Down
4 changes: 2 additions & 2 deletions packages/file_selector/file_selector_macos/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: file_selector_macos
description: macOS implementation of the file_selector plugin.
repository: https://github.com/flutter/plugins/tree/main/packages/file_selector/file_selector_macos
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+file_selector%22
version: 0.9.0+3
version: 0.9.1

environment:
sdk: ">=2.12.0 <3.0.0"
Expand All @@ -18,7 +18,7 @@ flutter:

dependencies:
cross_file: ^0.3.1
file_selector_platform_interface: ^2.2.0
file_selector_platform_interface: ^2.4.0
flutter:
sdk: flutter

Expand Down
Loading