Skip to content

Commit

Permalink
feat(get-medias/sort): add option to customize sort on getMedias (#37)
Browse files Browse the repository at this point in the history
* feat(get-medias/sort): add option to customize sort on getMedias

* refactor(get-medias/sort): make search accept either a string or an array

* refactor(get-medias/sort): use MediaField type instead of string

* chore(clean): remove print
  • Loading branch information
matheusdavidson authored Feb 18, 2023
1 parent 9a846f0 commit 187b253
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
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
30 changes: 29 additions & 1 deletion ios/Plugin/MediaPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,35 @@ 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 {
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]] {
for object in sortArray {

// Should have at least key for array value
if let key = object["key"] as? String {
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 {
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
21 changes: 21 additions & 0 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,29 @@ 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?: MediaField | MediaSort[];
}

export interface MediaSort {
key: MediaField;
ascending: boolean;
}

export type MediaField =
| 'meidaType'
| 'mediaSubtypes'
| 'sourceType'
| 'pixelWidth'
| 'pixelHeight'
| 'creationDate'
| 'modificationDate'
| 'duration'
| 'isFavorite'
| 'hasAdjustments';

export interface MediaResponse {
medias: MediaAsset[];
}
Expand Down

0 comments on commit 187b253

Please sign in to comment.