expo-media-library: filter Query by media subtype (screenshots, Live Photos, ...)
Benny Neugebauer
The legacy API can filter assets by media subtype on iOS: getAssetsAsync({ mediaSubtypes: ['screenshot'] }), added in https://github.com/expo/expo/pull/36756. The new Query API has no equivalent: AssetField only covers creationTime, modificationTime, mediaType, width, height, duration and isFavorite.
The new API can read the subtypes of a single asset with asset.getMediaSubtypes(), but it can't ask for "all screenshots" or "all Live Photos". The only way is to call getMediaSubtypes() on every asset in the library, which is slow for large libraries. PhotoKit supports the filter natively with a predicate on mediaSubtypes, which is what the legacy implementation does.
Use case: my app lets people sort their photos by type (screenshots, Live Photos, portraits, panoramas, slo-mo videos). Today I need the legacy API for that one call, while everything else uses the new API.
Proposal: support subtypes in Query, for example:
new Query().within(AssetField.MEDIA_SUBTYPES, [MediaSubtype.SCREENSHOT, MediaSubtype.LIVE_PHOTO]).exeForMetadata()
It would match assets that have any of the given subtypes ((mediaSubtypes & mask) != 0), like the legacy option does. On Android it could match nothing or throw, the same as the legacy option, which is iOS only.
Wiktor Smaga
Hey, I've just merged a PR for this: https://github.com/expo/expo/pull/51515
It's not released yet, but here's how you can use it:
const screenshots = await new Query().contains(AssetField.MEDIA_SUBTYPES, MediaSubtype.SCREENSHOT).exeForMetadata();
Currently, it supports filtering by one subtype per query. Do you have a use case for matching multiple subtypes at once? If so, let me know and I can follow up with
Query.containsAny()
.Wiktor Smaga
Hey, thanks for the feature request! I'll take care of it, and I think we should even be able to backport it to SDK 58!