Skip to content

Commit

Permalink
feat(file-picker): add limit option for iOS (#108)
Browse files Browse the repository at this point in the history
* feat(file-picker): Create the option to define a limit for the number of files picked by user #17

Update FilePicker.swift, FilePickerPlugin.swift and README.md

* changeset

* bug(file-picker): pickFiles on Safari web does not bring up file picker on first call #14

* Apply suggestions from code review

Co-authored-by: Robin Genz <[email protected]>

* Revert "bug(file-picker): pickFiles on Safari web does not bring up file picker on first call #14"

This reverts commit b8682fa.

* docgen definitions update

* prettier

---------

Co-authored-by: Robin Genz <[email protected]>
  • Loading branch information
stephan-fischer and robingenz authored Dec 30, 2023
1 parent 1377baa commit 777fff5
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/heavy-dancers-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@capawesome/capacitor-file-picker': minor
---

feat(ios): add `limit` option
11 changes: 6 additions & 5 deletions packages/file-picker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,12 @@ Remove all listeners for this plugin.

#### PickMediaOptions

| Prop | Type | Description | Default |
| --------------------- | -------------------- | ---------------------------------------------------------------------------------------------------------------------------------------- | ------------------ |
| **`multiple`** | <code>boolean</code> | Whether multiple files may be selected. | <code>false</code> |
| **`readData`** | <code>boolean</code> | Whether to read the file data. | <code>false</code> |
| **`skipTranscoding`** | <code>boolean</code> | Whether to avoid transcoding, if possible. On iOS, for example, HEIC images are automatically transcoded to JPEG. Only available on iOS. | <code>false</code> |
| Prop | Type | Description | Default |
| --------------------- | -------------------- | ---------------------------------------------------------------------------------------------------------------------------------------- | -------------------------- |
| **`multiple`** | <code>boolean</code> | Whether multiple files may be selected. | <code>false</code> |
| **`readData`** | <code>boolean</code> | Whether to read the file data. | <code>false</code> |
| **`skipTranscoding`** | <code>boolean</code> | Whether to avoid transcoding, if possible. On iOS, for example, HEIC images are automatically transcoded to JPEG. Only available on iOS. | <code>false</code> |
| **`limit`** | <code>number</code> | The maximum number of files that the user can select. This option is ignored if `multiple` is set to `false`. Only available on iOS. | <code>0 (unlimited)</code> |


#### PluginListenerHandle
Expand Down
12 changes: 6 additions & 6 deletions packages/file-picker/ios/Plugin/FilePicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ import MobileCoreServices
}
}

public func openImagePicker(multiple: Bool, skipTranscoding: Bool) {
public func openImagePicker(multiple: Bool, skipTranscoding: Bool, limit: Int?) {
DispatchQueue.main.async {
if #available(iOS 14, *) {
var configuration = PHPickerConfiguration(photoLibrary: PHPhotoLibrary.shared())
configuration.selectionLimit = multiple ? 0 : 1
configuration.selectionLimit = multiple ? (limit ?? 0) : 1
configuration.filter = .images
configuration.preferredAssetRepresentationMode = skipTranscoding ? .current : .automatic
let picker = PHPickerViewController(configuration: configuration)
Expand All @@ -60,11 +60,11 @@ import MobileCoreServices
}
}

public func openMediaPicker(multiple: Bool, skipTranscoding: Bool) {
public func openMediaPicker(multiple: Bool, skipTranscoding: Bool, limit: Int?) {
DispatchQueue.main.async {
if #available(iOS 14, *) {
var configuration = PHPickerConfiguration(photoLibrary: PHPhotoLibrary.shared())
configuration.selectionLimit = multiple ? 0 : 1
configuration.selectionLimit = multiple ? (limit ?? 0) : 1
configuration.preferredAssetRepresentationMode = skipTranscoding ? .current : .automatic
let picker = PHPickerViewController(configuration: configuration)
picker.delegate = self
Expand All @@ -80,11 +80,11 @@ import MobileCoreServices
}
}

public func openVideoPicker(multiple: Bool, skipTranscoding: Bool) {
public func openVideoPicker(multiple: Bool, skipTranscoding: Bool, limit: Int?) {
DispatchQueue.main.async {
if #available(iOS 14, *) {
var configuration = PHPickerConfiguration(photoLibrary: PHPhotoLibrary.shared())
configuration.selectionLimit = multiple ? 0 : 1
configuration.selectionLimit = multiple ? (limit ?? 0) : 1
configuration.filter = .videos
configuration.preferredAssetRepresentationMode = skipTranscoding ? .current : .automatic
let picker = PHPickerViewController(configuration: configuration)
Expand Down
9 changes: 6 additions & 3 deletions packages/file-picker/ios/Plugin/FilePickerPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,29 @@ public class FilePickerPlugin: CAPPlugin {

let multiple = call.getBool("multiple", false)
let skipTranscoding = call.getBool("skipTranscoding", false)
let limit = call.getInt("limit")

implementation?.openImagePicker(multiple: multiple, skipTranscoding: skipTranscoding)
implementation?.openImagePicker(multiple: multiple, skipTranscoding: skipTranscoding, limit: limit)
}

@objc func pickMedia(_ call: CAPPluginCall) {
savedCall = call

let multiple = call.getBool("multiple", false)
let skipTranscoding = call.getBool("skipTranscoding", false)
let limit = call.getInt("limit")

implementation?.openMediaPicker(multiple: multiple, skipTranscoding: skipTranscoding)
implementation?.openMediaPicker(multiple: multiple, skipTranscoding: skipTranscoding, limit: limit)
}

@objc func pickVideos(_ call: CAPPluginCall) {
savedCall = call

let multiple = call.getBool("multiple", false)
let skipTranscoding = call.getBool("skipTranscoding", false)
let limit = call.getInt("limit")

implementation?.openVideoPicker(multiple: multiple, skipTranscoding: skipTranscoding)
implementation?.openVideoPicker(multiple: multiple, skipTranscoding: skipTranscoding, limit: limit)
}

@objc func notifyPickerDismissedListener() {
Expand Down
12 changes: 12 additions & 0 deletions packages/file-picker/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,18 @@ export interface PickMediaOptions {
* @see https://developer.apple.com/documentation/photokit/phpickerconfiguration/assetrepresentationmode/current
*/
skipTranscoding?: boolean;
/**
* The maximum number of files that the user can select.
*
* This option is ignored if `multiple` is set to `false`.
*
* Only available on iOS.
*
* @default 0 (unlimited)
*
* @example 4
*/
limit?: number;
}

/**
Expand Down

0 comments on commit 777fff5

Please sign in to comment.