capawesome-team / capacitor-plugins

⚡️ Community plugins for Capacitor. Supports Android, iOS and the Web.
https://capawesome.io/plugins/
227 stars 34 forks source link

bug: FilePicker on ios - wrong selection order #213

Closed stephan-fischer closed 2 months ago

stephan-fischer commented 3 months ago

Plugin(s)

Version

5.0.0

Platform(s)

Current behavior

if i execute pickImages the end selection is not correct. If i use the ordered param - the selection is always random.

Expected behavior

if i select the images without ordered - it should be sorted by date, if i select with .ordered param, it should have the exact order

Reproduction

-

Steps to reproduce

 const media = await FilePicker.pickImages({
            multiple: true,
            readData: false,
            ordered: true
        });

Other information

The problem is following:

temporaryUrls.append(temporaryUrl)

in FilePicker.swift. Because the async function result.itemProvider.loadFileRepresentation(forTypeIdentifier: UTType.image.identifier, completionHandler: { is randomly finished - means one image could have a longer time etc.

Fix:

// for-in with index:
  for (index, result) in results.enumerated() {
// set count:
        var temporaryUrls = Array<URL?>(repeating: nil, count: results.count)
...

// add index at the right position:
           let temporaryUrl = try self.saveTemporaryFile(url)
                        temporaryUrls[index] = temporaryUrl

// set the final right sorted urls
    let finalUrls = temporaryUrls.compactMap { $0 }
            self.plugin?.handleDocumentPickerResult(urls: finalUrls, error: nil)

Capacitor doctor

-

Before submitting

robingenz commented 3 months ago

Thank you for your request. Would you be willing to create a PR?

stephan-fischer commented 3 months ago

yes, this week i will send a PR 😉