From e4741f59426d677df4de576a12fd826b54a8204c Mon Sep 17 00:00:00 2001 From: Miguel Ruivo Date: Mon, 11 Mar 2019 00:01:44 +0000 Subject: [PATCH] minor improvements --- example/ios/Runner.xcodeproj/project.pbxproj | 4 --- example/lib/main.dart | 9 +++++- ios/Classes/FilePickerPlugin.m | 30 ++++++++++++++------ lib/file_picker.dart | 25 ++++++++++------ 4 files changed, 45 insertions(+), 23 deletions(-) diff --git a/example/ios/Runner.xcodeproj/project.pbxproj b/example/ios/Runner.xcodeproj/project.pbxproj index 4190d80..fe0bc00 100644 --- a/example/ios/Runner.xcodeproj/project.pbxproj +++ b/example/ios/Runner.xcodeproj/project.pbxproj @@ -8,7 +8,6 @@ /* Begin PBXBuildFile section */ 1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */ = {isa = PBXBuildFile; fileRef = 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */; }; - 2D5378261FAA1A9400D5DBA9 /* flutter_assets in Resources */ = {isa = PBXBuildFile; fileRef = 2D5378251FAA1A9400D5DBA9 /* flutter_assets */; }; 3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */; }; 3B80C3941E831B6300D905FE /* App.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3B80C3931E831B6300D905FE /* App.framework */; }; 3B80C3951E831B6300D905FE /* App.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 3B80C3931E831B6300D905FE /* App.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; @@ -42,7 +41,6 @@ /* Begin PBXFileReference section */ 1498D2321E8E86230040F4C2 /* GeneratedPluginRegistrant.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GeneratedPluginRegistrant.h; sourceTree = ""; }; 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GeneratedPluginRegistrant.m; sourceTree = ""; }; - 2D5378251FAA1A9400D5DBA9 /* flutter_assets */ = {isa = PBXFileReference; lastKnownFileType = folder; name = flutter_assets; path = Flutter/flutter_assets; sourceTree = SOURCE_ROOT; }; 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = ""; }; 3B80C3931E831B6300D905FE /* App.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = App.framework; path = Flutter/App.framework; sourceTree = ""; }; 7AFA3C8E1D35360C0083082E /* Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = Release.xcconfig; path = Flutter/Release.xcconfig; sourceTree = ""; }; @@ -77,7 +75,6 @@ 9740EEB11CF90186004384FC /* Flutter */ = { isa = PBXGroup; children = ( - 2D5378251FAA1A9400D5DBA9 /* flutter_assets */, 3B80C3931E831B6300D905FE /* App.framework */, 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */, 9740EEBA1CF902C7004384FC /* Flutter.framework */, @@ -214,7 +211,6 @@ 3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */, 9740EEB41CF90195004384FC /* Debug.xcconfig in Resources */, 97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */, - 2D5378261FAA1A9400D5DBA9 /* flutter_assets in Resources */, 97C146FC1CF9000F007C117D /* Main.storyboard in Resources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/example/lib/main.dart b/example/lib/main.dart index 3e6a0c5..965f940 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -29,7 +29,10 @@ class _FilePickerDemoState extends State { void _openFileExplorer() async { if (_pickingType != FileType.CUSTOM || _hasValidMime) { try { - if (_multiPick) { + if (_pickingType == FileType.DIRECTORY_ONLY) { + _path = await FilePicker.getDirectoryPath(); + print("PATH: " + _path); + } else if (_multiPick) { _path = null; _paths = await FilePicker.getMultiFilePath(fileExtension: _extension); } else { @@ -89,6 +92,10 @@ class _FilePickerDemoState extends State { child: new Text('CUSTOM FORMAT'), value: FileType.CUSTOM, ), + new DropdownMenuItem( + child: new Text('DIRECTORY ONLY'), + value: FileType.DIRECTORY_ONLY, + ), ], onChanged: (value) => setState(() { _pickingType = value; diff --git a/ios/Classes/FilePickerPlugin.m b/ios/Classes/FilePickerPlugin.m index cf0cf3b..e9859d3 100644 --- a/ios/Classes/FilePickerPlugin.m +++ b/ios/Classes/FilePickerPlugin.m @@ -6,7 +6,7 @@ @property (nonatomic) FlutterResult result; @property (nonatomic) UIViewController *viewController; @property (nonatomic) UIImagePickerController *galleryPickerController; -@property (nonatomic) UIDocumentPickerViewController *pickerController; +@property (nonatomic) UIDocumentPickerViewController *documentPickerController; @property (nonatomic) UIDocumentInteractionController *interactionController; @property (nonatomic) MPMediaPickerController *audioPickerController; @property (nonatomic) NSString * fileType; @@ -37,10 +37,11 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { if (_result) { - _result([FlutterError errorWithCode:@"multiple_request" + result([FlutterError errorWithCode:@"multiple_request" message:@"Cancelled by a second request" details:nil]); _result = nil; + return; } _result = result; @@ -56,7 +57,6 @@ [self resolvePickImage]; } else { self.fileType = [FileUtils resolveType:call.method]; - if(self.fileType == nil){ result(FlutterMethodNotImplemented); } else { @@ -70,20 +70,27 @@ - (void)resolvePickDocumentWithMultipleSelection:(BOOL)allowsMultipleSelection { - self.pickerController = [[UIDocumentPickerViewController alloc] + @try{ + self.documentPickerController = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[self.fileType] inMode:UIDocumentPickerModeImport]; + } @catch (NSException * e) { + Log(@"Can't use documents fie picker. Probably due to iOS version being below 11.0 and not having the iCloud entitlement. If so, just make sure to enable it for your app in Xcode. Exception was: %@", e); + _result = nil; + return; + } if (@available(iOS 11.0, *)) { - self.pickerController.allowsMultipleSelection = allowsMultipleSelection; + self.documentPickerController.allowsMultipleSelection = allowsMultipleSelection; } else if(allowsMultipleSelection) { Log(@"Multiple file selection is only supported on iOS 11 and above. Single selection will be used."); } - self.pickerController.delegate = self; - self.pickerController.modalPresentationStyle = UIModalPresentationCurrentContext; + self.documentPickerController.delegate = self; + self.documentPickerController.modalPresentationStyle = UIModalPresentationCurrentContext; self.galleryPickerController.allowsEditing = NO; - [_viewController presentViewController:self.pickerController animated:YES completion:nil]; + + [_viewController presentViewController:self.documentPickerController animated:YES completion:nil]; } - (void) resolvePickImage { @@ -124,7 +131,7 @@ - (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray *)urls{ - [self.pickerController dismissViewControllerAnimated:YES completion:nil]; + [self.documentPickerController dismissViewControllerAnimated:YES completion:nil]; NSArray * result = [FileUtils resolvePath:urls]; if([result count] > 1) { @@ -132,6 +139,7 @@ didPickDocumentsAtURLs:(NSArray *)urls{ } else { _result([result objectAtIndex:0]); } + _result = nil; } @@ -159,9 +167,12 @@ didPickDocumentsAtURLs:(NSArray *)urls{ _result([FlutterError errorWithCode:@"file_picker_error" message:@"Temporary file could not be created" details:nil]); + _result = nil; + return; } _result([pickedVideoUrl != nil ? pickedVideoUrl : pickedImageUrl path]); + _result = nil; } @@ -174,6 +185,7 @@ didPickDocumentsAtURLs:(NSArray *)urls{ Log(@"Couldn't retrieve the audio file path, either is not locally downloaded or the file DRM protected."); } _result([url absoluteString]); + _result = nil; } #pragma mark - Actions canceled diff --git a/lib/file_picker.dart b/lib/file_picker.dart index a3e8d47..02a319c 100644 --- a/lib/file_picker.dart +++ b/lib/file_picker.dart @@ -4,13 +4,7 @@ import 'package:flutter/services.dart'; String _kCustomType = '__CUSTOM_'; -enum FileType { - ANY, - IMAGE, - VIDEO, - AUDIO, - CUSTOM, -} +enum FileType { ANY, IMAGE, VIDEO, AUDIO, CUSTOM, DIRECTORY_ONLY } class FilePicker { static const MethodChannel _channel = const MethodChannel('file_picker'); @@ -29,11 +23,24 @@ class FilePicker { } return result; } on PlatformException catch (e) { - print("[$_tag] Platform exception: " + e.toString()); + print('[$_tag] Platform exception: ' + e.toString()); } catch (e) { print(e.toString()); print( - "[$_tag] Unsupported operation. This probably have happened because [${type.split('_').last}] is an unsupported file type. You may want to try FileType.ALL instead."); + '[$_tag] Unsupported operation. This probably have happened because [${type.split('_').last}] is an unsupported file type. You may want to try FileType.ALL instead.'); + } + return null; + } + + static Future getDirectoryPath() async { + try { + final String result = await _channel.invokeMethod("getDirectoryPath"); + return result; + } on PlatformException catch (e) { + print('[$_tag] Platform exception: ' + e.toString()); + } catch (e) { + print(e.toString()); + print('[$_tag] Unsupported operation.'); } return null; }