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

Support setting/removing Contact/RawContact Photos as part of Insert and Update API calls #119

Closed
9 tasks done
vestrel00 opened this issue Nov 30, 2021 · 2 comments
Closed
9 tasks done
Assignees
Labels
breaking change This may break integration of the library for some consumers enhancement New feature or request refactor This may involve a refactor

Comments

@vestrel00
Copy link
Owner

vestrel00 commented Nov 30, 2021

Problem

As suggested in #116 (comment) (another big thanks to @lau1944 for this brilliant suggestion), the current way photos are set for new contacts that have not yet been inserted into the database is not very intuitive from a consumer point of view. I 100% agree.

Currently, the recommended way is to,

val contactsApi = Contacts(context)
val insertResult = contactsApi
    .insert()
    .rawContact {
        ...
    }
    .commit()

val contact = insertResult.contacts(contactsApi).firstOrNull()
contact?.setPhoto(...)

Instead, or additionally, consumers should be able to do the following,

val insertResult = Contacts(context)
    .insert()
    .rawContact {
        setPhoto(...)
    }
    .commit()

The current functions in contacts.core.util.ContactPhoto.kt and contacts.core.util.RawContactPhoto.kt perform blocking work that touches the database immediately. This is completely different from the functions in contacts.core.util.MutableRawContactData.kt that does not touch the database but use similarly named functions.

For example,

MutableRawContact.setPhoto(...)
MutableRawContact.setName(...)

In the above, setPhoto is a blocking call that touches the DB and should be done asynchronously with permission. On the other hand, setName just modifies the value in memory (no DB operations are performed).

Solution

To allow consumers to set/remove the photo as part of insert and update APIs...

  • Refactor the setPhoto functions to setPhotoDirect.
  • Add a non-parcelized photoData property to MutableContact, MutableRawContact, and NewRawContact. Make sure to support multiple formats; ByteArray, Bitmap, BitmapDrawable, and InputStream.
  • Handle the photo in insert APIs. Make sure this is compatible with allowBlanks.
  • Handle the photo in update APIs. Make sure this is compatible with deleteBlanks.

Other things to do

  • Update sample app to use insert/update APIs for setting/removing photos.
  • Update relevant documentation in code (see TODO in get-set-remove-contact-raw-contact-photo.md)
  • Update cheatsheet
  • Verify that there are no regressions for related API functions!
  • Provide a migration guide as a comment in this issue so that it can be copy-pasted into the release notes.

Notes

  1. This should be done after Create interface for "new entities" for insert APIs and remove nullable ID properties #117.
  2. A similar issue has been created for Contact and RawContact options; Move set Contact/RawContact Options functions to be part of Insert and Update APIs #120.
@vestrel00 vestrel00 added enhancement New feature or request refactor This may involve a refactor labels Nov 30, 2021
@vestrel00 vestrel00 self-assigned this Nov 30, 2021
@vestrel00 vestrel00 changed the title Add a way to get/set/remove Contact/RawContact Photo as part of Insert and Update calls Add a way to set/remove Contact/RawContact Photo as part of Insert and Update calls Nov 30, 2021
@vestrel00 vestrel00 added the breaking change This may break integration of the library for some consumers label Dec 5, 2021
@vestrel00 vestrel00 changed the title Add a way to set/remove Contact/RawContact Photo as part of Insert and Update calls Move set/remove Contact/RawContact Photo functions to be part of Insert and Update calls Dec 19, 2021
@vestrel00 vestrel00 changed the title Move set/remove Contact/RawContact Photo functions to be part of Insert and Update calls Move set/remove Contact/RawContact Photo functions to be part of Insert and Update APIs Dec 19, 2021
@vestrel00 vestrel00 pinned this issue Dec 19, 2021
@vestrel00 vestrel00 moved this from To do to In progress in General maintenance Dec 19, 2021
@vestrel00 vestrel00 moved this from In progress to To do in General maintenance Dec 19, 2021
@vestrel00 vestrel00 unpinned this issue Dec 19, 2021
@vestrel00 vestrel00 pinned this issue Oct 7, 2022
@vestrel00 vestrel00 moved this from To do to In progress in General maintenance Oct 7, 2022
vestrel00 added a commit that referenced this issue Oct 10, 2022
… functions and also prepare for insert and update API usage
@vestrel00 vestrel00 changed the title Move set/remove Contact/RawContact Photo functions to be part of Insert and Update APIs Add support for setting/removing Contact/RawContact Photos as part of Insert and Update API calls Oct 12, 2022
@vestrel00 vestrel00 moved this from In progress to Done in General maintenance Oct 12, 2022
@vestrel00 vestrel00 unpinned this issue Oct 12, 2022
@vestrel00
Copy link
Owner Author

vestrel00 commented Oct 12, 2022

Migration guide (to be included in release notes)

PREVIOUSLY, to set /remove the Contact or RawContact photo, you would use one of these extension functions that immediately commits the changes directly into the database. These can only be used for Contact and RawContacts that are already inserted.

[contact|rawContact].setPhoto(
    contactsApi, 
    [photoInputStream|photoBytes|photoBitmap|photoBitmapDrawable]
)
[contact|rawContact].removePhoto(contactsApi)

NOW, the above functions still exist with a different name and parameter types.

[contact|rawContact].setPhotoDirect(
    contactsApi, 
    PhotoData.from([photoInputStream|photoBytes|photoBitmap|photoBitmapDrawable])
)
[contact|rawContact].removePhotoDirect(contactsApi)

More importantly, you are now also able to set/remove Contact and RawContact photos as part of insert and update API calls!

Contacts(this).insert().rawContact { setPhoto(PhotoData.from(...)) }.commit()
Contacts(this)
    .update()
    .contacts(contact.mutableCopy { setPhoto(PhotoData.from(...)) })
    .contacts(contact.mutableCopy { removePhoto() })
    .rawContacts(rawContact.mutableCopy { setPhoto(PhotoData.from(...)) })
    .rawContacts(rawContact.mutableCopy { removePhoto() })
    .commit()

Read the new documentation for the full guide; https://vestrel00.github.io/contacts-android/other/get-set-remove-contact-raw-contact-photo/

@vestrel00 vestrel00 changed the title Add support for setting/removing Contact/RawContact Photos as part of Insert and Update API calls Support setting/removing Contact/RawContact Photos as part of Insert and Update API calls Nov 29, 2022
@vestrel00
Copy link
Owner Author

This is included in 0.3.0!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change This may break integration of the library for some consumers enhancement New feature or request refactor This may involve a refactor
Projects
Development

No branches or pull requests

1 participant