Skip to content

Commit

Permalink
Add documentation on ContactColumn
Browse files Browse the repository at this point in the history
  • Loading branch information
alexstyl committed Oct 24, 2021
1 parent 386eddd commit 78c0ec3
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 14 deletions.
6 changes: 3 additions & 3 deletions library/src/main/java/com/alexstyl/contactstore/Contact.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import android.provider.ContactsContract.PhoneticNameStyle
import com.alexstyl.contactstore.ContactColumn.Events
import com.alexstyl.contactstore.ContactColumn.GroupMemberships
import com.alexstyl.contactstore.ContactColumn.Image
import com.alexstyl.contactstore.ContactColumn.LinkedAccountColumn
import com.alexstyl.contactstore.ContactColumn.LinkedAccountValues
import com.alexstyl.contactstore.ContactColumn.Mails
import com.alexstyl.contactstore.ContactColumn.Names
import com.alexstyl.contactstore.ContactColumn.Nickname
Expand Down Expand Up @@ -124,7 +124,7 @@ interface Contact {
val webAddresses: List<LabeledValue<WebAddress>>

/**
* Requires : [ContactColumn.LinkedAccountColumn]
* Requires : [ContactColumn.LinkedAccountValues]
*/
val linkedAccountValues: List<LinkedAccountValue>

Expand All @@ -151,7 +151,7 @@ fun Contact.containsColumn(column: ContactColumn): Boolean {
}

fun Contact.containsLinkedAccountColumns(): Boolean {
return columns.any { it is LinkedAccountColumn }
return columns.any { it is LinkedAccountValues }
}

/**
Expand Down
55 changes: 54 additions & 1 deletion library/src/main/java/com/alexstyl/contactstore/ContactColumn.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,71 @@
package com.alexstyl.contactstore

sealed class ContactColumn {
/**
* A column that will populate the [Contact.phones] field of all queried contacts when requested.
*/
object Phones : ContactColumn()

/**
* A column that will populate the [Contact.mails] field of all queried contacts when requested.
*/
object Mails : ContactColumn()

/**
* A column that will populate the [Contact.note] field of all queried contacts when requested.
*/
object Note : ContactColumn()

/**
* A column that will populate the [Contact.events] field of all queried contacts when requested.
*
*/
object Events : ContactColumn()

/**
* A column that will populate the [Contact.postalAddresses] field of all queried contacts when requested.
*/
object PostalAddresses : ContactColumn()

/**
* A column that will populate the [Contact.imageData] field of all queried contacts when requested.
*
* @see Contact.imageData
*/
object Image : ContactColumn()

/**
* A column that will populate the [Contact.prefix], [Contact.firstName],[Contact.middleName],[Contact.lastName], [Contact.suffix] fields of all queried contacts when requested.
*/
object Names : ContactColumn()

/**
* A column that will populate the [Contact.nickname] field of all queried contacts when requested.
*/
object Nickname : ContactColumn()

/**
* A column that will populate the [Contact.webAddresses] field of all queried contacts when requested.
*/
object WebAddresses : ContactColumn()

/**
* A column that will populate the [Contact.organization] and [Contact.jobTitle] fields of all queried contacts when requested.
*/
object Organization : ContactColumn()

/**
* A column that will populate the [Contact.groups] field of all queried contacts when requested.
*/
object GroupMemberships : ContactColumn()
data class LinkedAccountColumn(val packageName: String) : ContactColumn()

/**
* A column that will populate the [Contact.linkedAccountValues] field of all queried contacts when requested.
*
* Each 3rd party app specifies a unique account type when syncing web contacts into the device.
* See [SyncColumns.ACCOUNT_TYPE][android.provider.ContactsContract.SyncColumns.ACCOUNT_TYPE] for more details.
*/
data class LinkedAccountValues(val accountType: String) : ContactColumn()
}

fun standardColumns(): List<ContactColumn> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ internal class ContactQueries(
}

private fun buildSelectionArgs(columnsToFetch: List<ContactColumn>): Array<String> {
val linkedAccountColumns = columnsToFetch.filterIsInstance<LinkedAccountColumn>()
val linkedAccountColumns = columnsToFetch.filterIsInstance<LinkedAccountValues>()
val standardColumns = columnsToFetch - linkedAccountColumns
return standardColumns.map { column ->
when (column) {
Expand All @@ -424,28 +424,26 @@ internal class ContactQueries(
Organization -> OrganizationColumns.CONTENT_ITEM_TYPE
Nickname -> NicknameColumns.CONTENT_ITEM_TYPE
GroupMemberships -> GroupColumns.CONTENT_ITEM_TYPE
is LinkedAccountColumn ->
is LinkedAccountValues ->
error("Tried to map a LinkedAccountColumn as standard column")
}
}.toTypedArray() + linkedAccountColumns.map {
it.packageName
}.toTypedArray()
}.toTypedArray() + linkedAccountColumns.map { it.accountType }.toTypedArray()
}

private fun buildColumnsToFetchSelection(
forContactId: Long,
columnsToFetch: List<ContactColumn>
): String {
val columnsQuery = buildString {
val linkedAccountColumns = columnsToFetch.filterIsInstance<LinkedAccountColumn>()
val linkedAccountColumns = columnsToFetch.filterIsInstance<LinkedAccountValues>()
val standardColumns = columnsToFetch - linkedAccountColumns

if (standardColumns.isNotEmpty()) {
append(
" ${Data.MIMETYPE} IN ${
valueIn(standardColumns.map { column ->
when (column) {
is LinkedAccountColumn ->
is LinkedAccountValues ->
error("Tried to map a LinkedAccountColumn as standard column")
else -> "?"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.alexstyl.contactstore

data class LinkedAccountValue(
val id: Long,
val accountType: String,
val mimetype: String,
val data1: String,
val data2: String,
Expand All @@ -18,5 +19,4 @@ data class LinkedAccountValue(
val data13: String,
val data14: String,
val data15: String,
val accountType: String,
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.alexstyl.contactstore

import com.alexstyl.contactstore.ContactColumn.LinkedAccountColumn
import com.alexstyl.contactstore.ContactColumn.LinkedAccountValues
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

Expand Down Expand Up @@ -31,7 +31,7 @@ internal class RequiredReadColumnA<T>(
private val value: T
) {
operator fun getValue(contact: Contact, property: KProperty<*>): T {
return if (contact.columns.any { it is LinkedAccountColumn }) {
return if (contact.columns.any { it is LinkedAccountValues }) {
value
} else {
error(
Expand Down

0 comments on commit 78c0ec3

Please sign in to comment.