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.7k
[image_picker] Migrate iOS to Pigeon and improve state handling #5285
Merged
fluttergithubbot
merged 10 commits into
flutter:main
from
stuartmorgan-g:image-picker-ios-pigeon
Apr 22, 2022
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8961c9b
Switch to internal copy of method channel implementation
stuartmorgan-g 761d926
Remove Android-only functions
stuartmorgan-g 99f75e0
Temporarily point to local pigeon
stuartmorgan-g 9950255
Add Pigeon API, and update Dart to use it
stuartmorgan-g ebb4d84
Update ObjC implementation to use Pigeon
stuartmorgan-g 8fd936a
Autoformat generated Pigeon code
stuartmorgan-g c4e2963
Reference Pigeon 3.0.2, not yet published
stuartmorgan-g f429c60
Version bump
stuartmorgan-g 49e175c
Standardize Dart helper names
stuartmorgan-g fe9980c
Review comments
stuartmorgan-g 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
Next
Next commit
Switch to internal copy of method channel implementation
- Loading branch information
commit 8961c9bd909bf3cd60acd3df7a83832380cac17f
There are no files selected for viewing
280 changes: 280 additions & 0 deletions
280
packages/image_picker/image_picker_ios/lib/image_picker_ios.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,280 @@ | ||
| // 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 'dart:async'; | ||
|
|
||
| import 'package:flutter/foundation.dart'; | ||
| import 'package:flutter/services.dart'; | ||
|
|
||
| import 'package:image_picker_platform_interface/image_picker_platform_interface.dart'; | ||
|
|
||
| const MethodChannel _channel = MethodChannel('plugins.flutter.io/image_picker'); | ||
|
|
||
| /// An implementation of [ImagePickerPlatform] for iOS. | ||
| class ImagePickerIOS extends ImagePickerPlatform { | ||
| /// The MethodChannel that is being used by this implementation of the plugin. | ||
| @visibleForTesting | ||
| MethodChannel get channel => _channel; | ||
|
|
||
| @override | ||
| Future<PickedFile?> pickImage({ | ||
| required ImageSource source, | ||
| double? maxWidth, | ||
| double? maxHeight, | ||
| int? imageQuality, | ||
| CameraDevice preferredCameraDevice = CameraDevice.rear, | ||
| }) async { | ||
| final String? path = await _getImagePath( | ||
| source: source, | ||
| maxWidth: maxWidth, | ||
| maxHeight: maxHeight, | ||
| imageQuality: imageQuality, | ||
| preferredCameraDevice: preferredCameraDevice, | ||
| ); | ||
| return path != null ? PickedFile(path) : null; | ||
| } | ||
|
|
||
| @override | ||
| Future<List<PickedFile>?> pickMultiImage({ | ||
| double? maxWidth, | ||
| double? maxHeight, | ||
| int? imageQuality, | ||
| }) async { | ||
| final List<dynamic>? paths = await _getMultiImagePath( | ||
| maxWidth: maxWidth, | ||
| maxHeight: maxHeight, | ||
| imageQuality: imageQuality, | ||
| ); | ||
| if (paths == null) { | ||
| return null; | ||
| } | ||
|
|
||
| return paths.map((dynamic path) => PickedFile(path as String)).toList(); | ||
| } | ||
|
|
||
| Future<List<dynamic>?> _getMultiImagePath({ | ||
| double? maxWidth, | ||
| double? maxHeight, | ||
| int? imageQuality, | ||
| }) { | ||
| if (imageQuality != null && (imageQuality < 0 || imageQuality > 100)) { | ||
| throw ArgumentError.value( | ||
| imageQuality, 'imageQuality', 'must be between 0 and 100'); | ||
| } | ||
|
|
||
| if (maxWidth != null && maxWidth < 0) { | ||
| throw ArgumentError.value(maxWidth, 'maxWidth', 'cannot be negative'); | ||
| } | ||
|
|
||
| if (maxHeight != null && maxHeight < 0) { | ||
| throw ArgumentError.value(maxHeight, 'maxHeight', 'cannot be negative'); | ||
| } | ||
|
|
||
| return _channel.invokeMethod<List<dynamic>?>( | ||
| 'pickMultiImage', | ||
| <String, dynamic>{ | ||
| 'maxWidth': maxWidth, | ||
| 'maxHeight': maxHeight, | ||
| 'imageQuality': imageQuality, | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| Future<String?> _getImagePath({ | ||
| required ImageSource source, | ||
| double? maxWidth, | ||
| double? maxHeight, | ||
| int? imageQuality, | ||
| CameraDevice preferredCameraDevice = CameraDevice.rear, | ||
| }) { | ||
| if (imageQuality != null && (imageQuality < 0 || imageQuality > 100)) { | ||
| throw ArgumentError.value( | ||
| imageQuality, 'imageQuality', 'must be between 0 and 100'); | ||
| } | ||
|
|
||
| if (maxWidth != null && maxWidth < 0) { | ||
| throw ArgumentError.value(maxWidth, 'maxWidth', 'cannot be negative'); | ||
| } | ||
|
|
||
| if (maxHeight != null && maxHeight < 0) { | ||
| throw ArgumentError.value(maxHeight, 'maxHeight', 'cannot be negative'); | ||
| } | ||
|
|
||
| return _channel.invokeMethod<String>( | ||
| 'pickImage', | ||
| <String, dynamic>{ | ||
| 'source': source.index, | ||
| 'maxWidth': maxWidth, | ||
| 'maxHeight': maxHeight, | ||
| 'imageQuality': imageQuality, | ||
| 'cameraDevice': preferredCameraDevice.index | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| @override | ||
| Future<PickedFile?> pickVideo({ | ||
| required ImageSource source, | ||
| CameraDevice preferredCameraDevice = CameraDevice.rear, | ||
| Duration? maxDuration, | ||
| }) async { | ||
| final String? path = await _getVideoPath( | ||
| source: source, | ||
| maxDuration: maxDuration, | ||
| preferredCameraDevice: preferredCameraDevice, | ||
| ); | ||
| return path != null ? PickedFile(path) : null; | ||
| } | ||
|
|
||
| Future<String?> _getVideoPath({ | ||
| required ImageSource source, | ||
| CameraDevice preferredCameraDevice = CameraDevice.rear, | ||
| Duration? maxDuration, | ||
| }) { | ||
| return _channel.invokeMethod<String>( | ||
| 'pickVideo', | ||
| <String, dynamic>{ | ||
| 'source': source.index, | ||
| 'maxDuration': maxDuration?.inSeconds, | ||
| 'cameraDevice': preferredCameraDevice.index | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| @override | ||
| Future<LostData> retrieveLostData() async { | ||
| final Map<String, dynamic>? result = | ||
| await _channel.invokeMapMethod<String, dynamic>('retrieve'); | ||
|
|
||
| if (result == null) { | ||
| return LostData.empty(); | ||
| } | ||
|
|
||
| assert(result.containsKey('path') != result.containsKey('errorCode')); | ||
|
|
||
| final String? type = result['type'] as String?; | ||
| assert(type == kTypeImage || type == kTypeVideo); | ||
|
|
||
| RetrieveType? retrieveType; | ||
| if (type == kTypeImage) { | ||
| retrieveType = RetrieveType.image; | ||
| } else if (type == kTypeVideo) { | ||
| retrieveType = RetrieveType.video; | ||
| } | ||
|
|
||
| PlatformException? exception; | ||
| if (result.containsKey('errorCode')) { | ||
| exception = PlatformException( | ||
| code: result['errorCode']! as String, | ||
| message: result['errorMessage'] as String?); | ||
| } | ||
|
|
||
| final String? path = result['path'] as String?; | ||
|
|
||
| return LostData( | ||
| file: path != null ? PickedFile(path) : null, | ||
| exception: exception, | ||
| type: retrieveType, | ||
| ); | ||
| } | ||
|
|
||
| @override | ||
| Future<XFile?> getImage({ | ||
| required ImageSource source, | ||
| double? maxWidth, | ||
| double? maxHeight, | ||
| int? imageQuality, | ||
| CameraDevice preferredCameraDevice = CameraDevice.rear, | ||
| }) async { | ||
| final String? path = await _getImagePath( | ||
| source: source, | ||
| maxWidth: maxWidth, | ||
| maxHeight: maxHeight, | ||
| imageQuality: imageQuality, | ||
| preferredCameraDevice: preferredCameraDevice, | ||
| ); | ||
| return path != null ? XFile(path) : null; | ||
| } | ||
|
|
||
| @override | ||
| Future<List<XFile>?> getMultiImage({ | ||
| double? maxWidth, | ||
| double? maxHeight, | ||
| int? imageQuality, | ||
| }) async { | ||
| final List<dynamic>? paths = await _getMultiImagePath( | ||
| maxWidth: maxWidth, | ||
| maxHeight: maxHeight, | ||
| imageQuality: imageQuality, | ||
| ); | ||
| if (paths == null) { | ||
| return null; | ||
| } | ||
|
|
||
| return paths.map((dynamic path) => XFile(path as String)).toList(); | ||
| } | ||
|
|
||
| @override | ||
| Future<XFile?> getVideo({ | ||
| required ImageSource source, | ||
| CameraDevice preferredCameraDevice = CameraDevice.rear, | ||
| Duration? maxDuration, | ||
| }) async { | ||
| final String? path = await _getVideoPath( | ||
| source: source, | ||
| maxDuration: maxDuration, | ||
| preferredCameraDevice: preferredCameraDevice, | ||
| ); | ||
| return path != null ? XFile(path) : null; | ||
| } | ||
|
|
||
| @override | ||
| Future<LostDataResponse> getLostData() async { | ||
| List<XFile>? pickedFileList; | ||
|
|
||
| final Map<String, dynamic>? result = | ||
| await _channel.invokeMapMethod<String, dynamic>('retrieve'); | ||
|
|
||
| if (result == null) { | ||
| return LostDataResponse.empty(); | ||
| } | ||
|
|
||
| assert(result.containsKey('path') != result.containsKey('errorCode')); | ||
|
|
||
| final String? type = result['type'] as String?; | ||
| assert(type == kTypeImage || type == kTypeVideo); | ||
|
|
||
| RetrieveType? retrieveType; | ||
| if (type == kTypeImage) { | ||
| retrieveType = RetrieveType.image; | ||
| } else if (type == kTypeVideo) { | ||
| retrieveType = RetrieveType.video; | ||
| } | ||
|
|
||
| PlatformException? exception; | ||
| if (result.containsKey('errorCode')) { | ||
| exception = PlatformException( | ||
| code: result['errorCode']! as String, | ||
| message: result['errorMessage'] as String?); | ||
| } | ||
|
|
||
| final String? path = result['path'] as String?; | ||
|
|
||
| final List<String>? pathList = | ||
| (result['pathList'] as List<dynamic>?)?.cast<String>(); | ||
| if (pathList != null) { | ||
| pickedFileList = <XFile>[]; | ||
| for (final String path in pathList) { | ||
| pickedFileList.add(XFile(path)); | ||
| } | ||
| } | ||
|
|
||
| return LostDataResponse( | ||
| file: path != null ? XFile(path) : null, | ||
| exception: exception, | ||
| type: retrieveType, | ||
| files: pickedFileList, | ||
| ); | ||
| } | ||
| } | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: naming is a bit confusing since this says "getVideoPath" but is calling "pickVideo"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's some weird legacy naming in this plugin from the way things were named when the platform interface was created, and then when the new cross-file version were implemented. (This is just a copy from the shared method channel.)
I'll give it a better name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Standardized the helpers as
_<host API name>AsPath. Still not great, but consistent, and it makes it more clear what the distinction is between the helper (returns paths) and the public variants (returnXFileorPickedFile).