Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(get-medias/sort): add option to customize sort on getMedias #37

Merged
merged 4 commits into from
Feb 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions ios/Plugin.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,13 @@
"${PODS_ROOT}/Target Support Files/Pods-PluginTests/Pods-PluginTests-frameworks.sh",
"${BUILT_PRODUCTS_DIR}/Capacitor/Capacitor.framework",
"${BUILT_PRODUCTS_DIR}/CapacitorCordova/Cordova.framework",
"${BUILT_PRODUCTS_DIR}/SDWebImage/SDWebImage.framework",
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Capacitor.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Cordova.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SDWebImage.framework",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
Expand Down
34 changes: 33 additions & 1 deletion ios/Plugin/MediaPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,39 @@ public class MediaPlugin: CAPPlugin {

let options = PHFetchOptions()
options.fetchLimit = quantity
options.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]

// Set sort
var sortDescriptors = [] as [NSSortDescriptor];

// Handle when sort is string
if call.getString("sort") != nil {
print("sort is string");
let key = call.getString("sort") ?? "creationDate"
sortDescriptors.append(NSSortDescriptor(key: key, ascending: false))
}
// Handle when sort is an array
else if let sortArray = call.getArray("sort") as? [[String: Any]] {
print("sort is array");
for object in sortArray {

// Should have at least key for array value
if let key = object["key"] as? String {
print("sort is array: has good key");
let ascending = object["ascending"] as? Bool ?? false
sortDescriptors.append(NSSortDescriptor(key: key, ascending: ascending))
}
}
}

// Check if sort descriptors is empty
// it can happen because of validations inside the previous if, in this case, set a default value
if sortDescriptors.isEmpty {
print("sort descriptors is empty");
sortDescriptors.append(NSSortDescriptor(key: "creationDate", ascending: false))
}

// Set sort descriptors
options.sortDescriptors = sortDescriptors

if albumId != nil {
let albumFetchResult = PHAssetCollection.fetchAssetCollections(withLocalIdentifiers: [albumId!], options: nil)
Expand Down
19 changes: 19 additions & 0 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,25 @@ export interface MediaFetchOptions {
* Which album identifier to query in (get identifier with getAlbums())
*/
albumIdentifier?: string;
/**
* Sort order of returned assets by field and ascending/descending
*/
sort?: string | MediaSort[];
}

export interface MediaSort {
key:
stewones marked this conversation as resolved.
Show resolved Hide resolved
| 'meidaType'
| 'mediaSubtypes'
| 'sourceType'
| 'pixelWidth'
| 'pixelHeight'
| 'creationDate'
| 'modificationDate'
| 'duration'
| 'isFavorite'
| 'hasAdjustments';
ascending: boolean;
}

export interface MediaResponse {
Expand Down