-
Notifications
You must be signed in to change notification settings - Fork 72
Description
Description
Currently, the Capacitor Contacts Plugin allows fetching contacts with an image field, which contains the contact's image if available. However, there is no direct way to check whether a contact has an image without also retrieving the image field itself. This can lead to unnecessary memory usage and performance overhead when processing a large number of contacts.
Proposed Solution
Add a new optional field to the contact response, called hasImage. This field should be a boolean that indicates whether a contact has an image available, without returning the actual image data.
This would allow developers to:
- Efficiently determine if a contact has an image.
- Avoid fetching and processing image data unless explicitly required.
Suggested API
Example response:
{
"id": "12345",
"displayName": "John Doe",
"hasImage": true
}Benefits
- Performance: Reduces memory and bandwidth usage by avoiding image data when not needed.
- Efficiency: Provides a lightweight alternative for apps that only need to check the presence of an image.
- Flexibility: Allows developers to decide later whether to fetch the actual image, based on their application's requirements.
Implementation Details
- iOS: Use the CNContactThumbnailImageDataAvailable property in the native code to set the
hasImageflag. - Android: Check the photo_uri column in the ContactsContract.Contacts table to determine the presence of an image and return a corresponding boolean value.
Example Usage
Fetching contacts with hasImage:
import { Contacts } from '@capacitor-community/contacts';
const getContacts = async () => {
const result = await Contacts.getContacts({
fields: ['id', 'displayName', 'hasImage'], // Specify fields to include
});
console.log(result.contacts);
};
getContacts();Conclusion
Adding a hasImage flag would make the plugin more efficient and versatile, particularly for use cases where only the presence of an image is relevant. This enhancement aligns with the plugin's goal of providing optimized contact management.
iOS (Swift) Implementation
let keysToFetch = [
CNContactIdentifierKey as CNKeyDescriptor,
CNContactGivenNameKey as CNKeyDescriptor,
CNContactFamilyNameKey as CNKeyDescriptor,
CNContactThumbnailImageDataAvailableKey as CNKeyDescriptor
]
contactData["hasImage"] = contact.thumbnailImageDataAvailableAndroid (Kotlin) Implementation
contactJson.put("hasImage", photoUri != null)