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: Add hasImage field #138

Open
stephan-fischer opened this issue Dec 9, 2024 · 0 comments
Open

Feat: Add hasImage field #138

stephan-fischer opened this issue Dec 9, 2024 · 0 comments

Comments

@stephan-fischer
Copy link

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 hasImage flag.
  • 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.thumbnailImageDataAvailable

Android (Kotlin) Implementation

 contactJson.put("hasImage", photoUri != null)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant