Skip to content

Commit

Permalink
Closes #312 Add extensions for getting all data kinds of a Contact or…
Browse files Browse the repository at this point in the history
… RawContact as a list
  • Loading branch information
vestrel00 committed Sep 5, 2023
1 parent 25b9705 commit 5271a69
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 21 deletions.
17 changes: 17 additions & 0 deletions core/src/main/java/contacts/core/util/ExistingContactData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package contacts.core.util

import contacts.core.entities.ExistingContactEntity
import contacts.core.entities.ExistingDataEntity

/**
* Sequence of all data kinds (e.g. addresses, emails, events, etc) of all of this
* [ExistingContactEntity.rawContacts].
*/
fun ExistingContactEntity.data(): Sequence<ExistingDataEntity> = sequence {
yieldAll(rawContacts.flatMap { it.data() })
}

/**
* Same as [ExistingContactEntity.data] but as a [List].
*/
fun ExistingContactEntity.dataList(): List<ExistingDataEntity> = data().toList()
36 changes: 36 additions & 0 deletions core/src/main/java/contacts/core/util/ExistingRawContactData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package contacts.core.util

import contacts.core.entities.ExistingDataEntity
import contacts.core.entities.ExistingRawContactEntity

/**
* Sequence of all data kinds (e.g. addresses, emails, events, etc) of this
* [ExistingRawContactEntity].
*/
fun ExistingRawContactEntity.data(): Sequence<ExistingDataEntity> = sequence {
yieldAll(addresses.filterIsInstance(ExistingDataEntity::class.java))
yieldAll(emails.filterIsInstance(ExistingDataEntity::class.java))
yieldAll(events.filterIsInstance(ExistingDataEntity::class.java))
// Group memberships are implicitly read-only.
yieldAll(ims.filterIsInstance(ExistingDataEntity::class.java))
(name as? ExistingDataEntity)?.also { yield(it) }
(nickname as? ExistingDataEntity)?.also { yield(it) }
(note as? ExistingDataEntity)?.also { yield(it) }
(organization as? ExistingDataEntity)?.also { yield(it) }
yieldAll(phones.filterIsInstance(ExistingDataEntity::class.java))
// Photo is implicitly read-only.
yieldAll(relations.filterIsInstance(ExistingDataEntity::class.java))
(sipAddress as? ExistingDataEntity)?.also { yield(it) }
yieldAll(websites.filterIsInstance(ExistingDataEntity::class.java))

yieldAll(
customDataEntities.values
.flatMap { it.entities }
.filterIsInstance(ExistingDataEntity::class.java)
)
}

/**
* Same as [ExistingRawContactEntity.data] but as a [List].
*/
fun ExistingRawContactEntity.dataList(): List<ExistingDataEntity> = data().toList()
71 changes: 50 additions & 21 deletions core/src/main/java/contacts/core/util/NewRawContactData.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
package contacts.core.util

import android.accounts.Account
import contacts.core.entities.*
import contacts.core.entities.NewAddress
import contacts.core.entities.NewCustomDataEntity
import contacts.core.entities.NewDataEntity
import contacts.core.entities.NewEmail
import contacts.core.entities.NewEvent
import contacts.core.entities.NewGroupMembership
import contacts.core.entities.NewIm
import contacts.core.entities.NewName
import contacts.core.entities.NewNickname
import contacts.core.entities.NewNote
import contacts.core.entities.NewOptions
import contacts.core.entities.NewOrganization
import contacts.core.entities.NewPhone
import contacts.core.entities.NewRawContact
import contacts.core.entities.NewRelation
import contacts.core.entities.NewSipAddress
import contacts.core.entities.NewWebsite
import contacts.core.entities.removeAll
import contacts.core.redactedCopyOrThis

/**
Expand Down Expand Up @@ -357,6 +374,37 @@ fun NewRawContact.removeAllWebsites() {
websites.clear()
}

/**
* Sequence of all data kinds (e.g. addresses, emails, events, etc) of this [NewRawContact].
*/
fun NewRawContact.data(): Sequence<NewDataEntity> = sequence {
yieldAll(addresses)
yieldAll(emails)
yieldAll(events)
// Group memberships are implicitly read-only.
yieldAll(ims)
name?.also { yield(it) }
nickname?.also { yield(it) }
note?.also { yield(it) }
organization?.also { yield(it) }
yieldAll(phones)
// Photo is implicitly read-only.
yieldAll(relations)
sipAddress?.also { yield(it) }
yieldAll(websites)

yieldAll(
customDataEntities.values
.flatMap { it.entities }
.filterIsInstance(NewCustomDataEntity::class.java)
)
}

/**
* Same as [NewRawContact.data] but as a [List].
*/
fun NewRawContact.dataList(): List<NewDataEntity> = data().toList()

/**
* Sets the value of all [NewDataEntity.isReadOnly] (including any custom data) to [readOnly].
*
Expand All @@ -375,24 +423,5 @@ fun NewRawContact.removeAllWebsites() {
* added/set.
*/
fun NewRawContact.setDataAsReadOnly(readOnly: Boolean) {
addresses.forEach { it.isReadOnly = readOnly }
emails.forEach { it.isReadOnly = readOnly }
events.forEach { it.isReadOnly = readOnly }
// Group memberships are implicitly read-only.
ims.forEach { it.isReadOnly = readOnly }
name?.isReadOnly = readOnly
nickname?.isReadOnly = readOnly
note?.isReadOnly = readOnly
organization?.isReadOnly = readOnly
phones.forEach { it.isReadOnly = readOnly }
// Photo is implicitly read-only.
relations.forEach { it.isReadOnly = readOnly }
sipAddress?.isReadOnly = readOnly
websites.forEach { it.isReadOnly = readOnly }

customDataEntities.values.flatMap { it.entities }.forEach {
if (it is NewCustomDataEntity) {
it.isReadOnly = readOnly
}
}
data().forEach { it.isReadOnly = readOnly }
}
7 changes: 7 additions & 0 deletions docs/entities/about-api-entities.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,13 @@ Log.d(
Each Contact may have more than one of the following data if the Contact is made up of 2 or more
RawContacts; name, nickname, note, organization, sip address.

To get the list of all data kinds of a particular Contact or RawContact,

```kotlin
contact.dataList()
rawContact.dataList()
```

> ℹ️ For more info on how to easily aggregate data from all RawContacts in a Contact, read
[Convenience functions](./../other/convenience-functions.md).

Expand Down
20 changes: 20 additions & 0 deletions docs/other/convenience-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,26 @@ added to is redacted.

> ℹ️ For more info, read [Redact entities and API input and output in production](./../entities/redact-apis-and-entities.md).
## Getting all data of a Contact or RawContact

There may be instances where you want to get all available data of a Contact or RawContact instance.
In such cases, you can use extensions in `ExistingContactData.kt`, `ExistingRawContactData.kt`, and
`NewRawContactData.kt`.

To get a sequence of all data kinds of a Contact or RawContact,

```kotlin
contact.data()
rawContact.data()
```

To get it as a list,

```kotlin
contact.dataList()
rawContact.dataList()
```

## Getting the parent Contact of a RawContact or Data

Using the `Query` API, it is easy to get the parent Contact of a RawContact or Data,
Expand Down

0 comments on commit 5271a69

Please sign in to comment.