Skip to content

Commit

Permalink
Remove strong types from ContactPredicates
Browse files Browse the repository at this point in the history
  • Loading branch information
alexstyl committed Feb 19, 2022
1 parent ca57d81 commit 6fd710f
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,29 @@ public class TestContactStore(
StoredContact(
contactId = current.size.toLong(),
isStarred = contact.isStarred,
prefix = contact.takeIfContains(ContactColumn.Names) { contact.prefix }.orEmpty(),
firstName = contact.takeIfContains(ContactColumn.Names) { contact.firstName }.orEmpty(),
middleName = contact.takeIfContains(ContactColumn.Names) { contact.middleName }.orEmpty(),
lastName = contact.takeIfContains(ContactColumn.Names) { contact.lastName }.orEmpty(),
suffix = contact.takeIfContains(ContactColumn.Names) { contact.suffix }.orEmpty(),
phoneticMiddleName = contact.takeIfContains(ContactColumn.Names) { contact.phoneticMiddleName }.orEmpty(),
phoneticFirstName = contact.takeIfContains(ContactColumn.Names) { contact.phoneticFirstName }.orEmpty(),
phoneticLastName = contact.takeIfContains(ContactColumn.Names) { contact.phoneticLastName }.orEmpty(),
prefix = contact.takeIfContains(ContactColumn.Names) { contact.prefix }
.orEmpty(),
firstName = contact.takeIfContains(ContactColumn.Names) { contact.firstName }
.orEmpty(),
middleName = contact.takeIfContains(ContactColumn.Names) { contact.middleName }
.orEmpty(),
lastName = contact.takeIfContains(ContactColumn.Names) { contact.lastName }
.orEmpty(),
suffix = contact.takeIfContains(ContactColumn.Names) { contact.suffix }
.orEmpty(),
phoneticMiddleName = contact.takeIfContains(ContactColumn.Names) { contact.phoneticMiddleName }
.orEmpty(),
phoneticFirstName = contact.takeIfContains(ContactColumn.Names) { contact.phoneticFirstName }
.orEmpty(),
phoneticLastName = contact.takeIfContains(ContactColumn.Names) { contact.phoneticLastName }
.orEmpty(),
phoneticNameStyle = contact.takeIfContains(ContactColumn.Names) { contact.phoneticNameStyle }
?: ContactsContract.PhoneticNameStyle.UNDEFINED,
imageData = contact.takeIfContains(ContactColumn.Image) { contact.imageData },
organization = contact.takeIfContains(ContactColumn.Organization) { contact.organization }.orEmpty(),
jobTitle = contact.takeIfContains(ContactColumn.Organization) { contact.jobTitle }.orEmpty(),
organization = contact.takeIfContains(ContactColumn.Organization) { contact.organization }
.orEmpty(),
jobTitle = contact.takeIfContains(ContactColumn.Organization) { contact.jobTitle }
.orEmpty(),
webAddresses = contact.takeIfContains(ContactColumn.WebAddresses) { contact.webAddresses }
.orEmpty(),
phones = contact.takeIfContains(ContactColumn.Phones) { contact.phones }
Expand All @@ -145,7 +155,8 @@ public class TestContactStore(
postalAddresses = contact.takeIfContains(ContactColumn.PostalAddresses) { contact.postalAddresses }
.orEmpty(),
note = contact.takeIfContains(ContactColumn.Note) { contact.note },
nickname = contact.takeIfContains(ContactColumn.Nickname) { contact.nickname }.orEmpty(),
nickname = contact.takeIfContains(ContactColumn.Nickname) { contact.nickname }
.orEmpty(),
groups = contact
.takeIfContains(ContactColumn.GroupMemberships) { contact.groups }
.orEmpty(),
Expand Down Expand Up @@ -270,7 +281,7 @@ public class TestContactStore(
return when (predicate) {
is ContactPredicate.ContactLookup -> matchesContact(predicate, contact)
is ContactPredicate.MailLookup -> {
val query = predicate.mailAddress.raw
val query = predicate.mailAddress
return contact.mails.any { it.value.raw.startsWith(query, ignoreCase = true) }
}
is ContactPredicate.NameLookup -> matchesName(predicate, contact)
Expand Down Expand Up @@ -316,7 +327,7 @@ public class TestContactStore(
predicate: ContactPredicate.PhoneLookup,
contact: StoredContact
): Boolean {
val query = predicate.phoneNumber.raw
val query = predicate.phoneNumber
return contact.phones.any { it.value.raw.startsWith(query, ignoreCase = true) }
}

Expand All @@ -343,11 +354,26 @@ public class TestContactStore(
columns = columnsToFetch,
isStarred = it.isStarred,

firstName = valueIfPresent(columnsToFetch, ContactColumn.Names) { it.firstName }.orEmpty(),
lastName = valueIfPresent(columnsToFetch, ContactColumn.Names) { it.lastName }.orEmpty(),
prefix = valueIfPresent(columnsToFetch, ContactColumn.Names) { it.prefix }.orEmpty(),
middleName = valueIfPresent(columnsToFetch, ContactColumn.Names) { it.middleName }.orEmpty(),
suffix = valueIfPresent(columnsToFetch, ContactColumn.Names) { it.suffix }.orEmpty(),
firstName = valueIfPresent(
columnsToFetch,
ContactColumn.Names
) { it.firstName }.orEmpty(),
lastName = valueIfPresent(
columnsToFetch,
ContactColumn.Names
) { it.lastName }.orEmpty(),
prefix = valueIfPresent(
columnsToFetch,
ContactColumn.Names
) { it.prefix }.orEmpty(),
middleName = valueIfPresent(
columnsToFetch,
ContactColumn.Names
) { it.middleName }.orEmpty(),
suffix = valueIfPresent(
columnsToFetch,
ContactColumn.Names
) { it.suffix }.orEmpty(),
phoneticFirstName = valueIfPresent(
columnsToFetch,
ContactColumn.Names
Expand Down Expand Up @@ -397,7 +423,10 @@ public class TestContactStore(
ContactColumn.PostalAddresses
) { it.postalAddresses }.orEmpty(),
note = valueIfPresent(columnsToFetch, ContactColumn.Note) { it.note },
nickname = valueIfPresent(columnsToFetch, ContactColumn.Nickname) { it.nickname }.orEmpty(),
nickname = valueIfPresent(
columnsToFetch,
ContactColumn.Nickname
) { it.nickname }.orEmpty(),
groups = valueIfPresent(
columnsToFetch,
ContactColumn.GroupMemberships
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal class PredicateTestContactStoreTest {
)

val actual = store.fetchContacts(
predicate = PhoneLookup(PhoneNumber("55"))
predicate = PhoneLookup("55")
).first()

assertThat(actual).containsOnly(CONTACT)
Expand Down Expand Up @@ -57,7 +57,7 @@ internal class PredicateTestContactStoreTest {
)

val actual = store.fetchContacts(
predicate = ContactLookup(inContactIds = listOf(0L))
predicate = ContactLookup(contactId = 0L)
).first()

assertThat(actual).containsOnly(CONTACT)
Expand All @@ -72,7 +72,7 @@ internal class PredicateTestContactStoreTest {
)

val actual = store.fetchContacts(
predicate = MailLookup(MailAddress("hi"))
predicate = MailLookup("hi")
).first()

assertThat(actual).containsOnly(CONTACT)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.alexstyl.contactstore

import com.alexstyl.contactstore.ContactPredicate.MailLookup
import com.alexstyl.contactstore.ContactPredicate.NameLookup
import com.alexstyl.contactstore.ContactPredicate.PhoneLookup
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
Expand Down Expand Up @@ -31,9 +33,7 @@ internal class ContactStoreLookupTest : ContactStoreTestBase() {

@Test
fun lookupByPhoneNumber(): Unit = runBlocking {
val actual = store.fetchContacts(
ContactPredicate.PhoneLookup(PhoneNumber("555"))
).first()
val actual = store.fetchContacts(PhoneLookup("555")).first()
val expected = listOf(
paoloMelendez()
)
Expand All @@ -42,9 +42,7 @@ internal class ContactStoreLookupTest : ContactStoreTestBase() {

@Test
fun lookupByMail(): Unit = runBlocking {
val actual = store.fetchContacts(
ContactPredicate.MailLookup(MailAddress("hi@mail.com"))
).first()
val actual = store.fetchContacts(MailLookup("hi@mail.com")).first()
val expected = listOf(kimClay())

assertThat(actual, equalTo(expected))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@
package com.alexstyl.contactstore

public sealed class ContactPredicate {
public data class PhoneLookup(val phoneNumber: PhoneNumber) : ContactPredicate()
public data class MailLookup(val mailAddress: MailAddress) : ContactPredicate()
public companion object {
@Suppress("FunctionName")
@Deprecated(
"Use a string directly instead. This signature is going away in 1.0.0",
ReplaceWith("PhoneLookup(phoneNumber.raw)")
)
public fun PhoneLookup(phoneNumber: PhoneNumber): ContactPredicate {
return PhoneLookup(phoneNumber.raw)
}

@Suppress("FunctionName")
@Deprecated(
"Use a string directly instead. This signature is going away in 1.0.0",
ReplaceWith("MailLookup(mailAddress.raw)")
)
public fun MailLookup(mailAddress: MailAddress): ContactPredicate {
return MailLookup(mailAddress.raw)
}
}

/**
* Performs a contact lookup by trying to match the given string against each contact's phone numbers.
*/
public data class PhoneLookup(val phoneNumber: String) : ContactPredicate()

/**
* Performs a contact lookup by trying to match the given string against each contact's mail addresses.
*/
public data class MailLookup(val mailAddress: String) : ContactPredicate()

/**
* Performs a contact lookup by trying to match the given string against various parts of the contact name.
Expand Down
16 changes: 4 additions & 12 deletions library/src/main/java/com/alexstyl/contactstore/ContactQueries.kt
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ internal class ContactQueries(
}

private fun lookupFromMail(
mailAddress: MailAddress,
mailAddress: String,
displayNameStyle: DisplayNameStyle
): Flow<List<PartialContact>> {
return contentResolver.runQueryFlow(
contentUri = EmailColumns.CONTENT_FILTER_URI.buildUpon()
.appendEncodedPath(mailAddress.raw)
.appendEncodedPath(mailAddress)
.build(),
projection = FilterQuery.projection(displayNameStyle),
sortOrder = FilterQuery.sortOrder(displayNameStyle)
Expand All @@ -161,12 +161,12 @@ internal class ContactQueries(
}

private fun lookupFromPhone(
phoneNumber: PhoneNumber,
phoneNumber: String,
displayNameStyle: DisplayNameStyle
): Flow<List<PartialContact>> {
return contentResolver.runQueryFlow(
contentUri = ContactsContract.PhoneLookup.CONTENT_FILTER_URI.buildUpon()
.appendEncodedPath(phoneNumber.raw)
.appendEncodedPath(phoneNumber)
.build(),
projection = arrayOf(
PHONE_LOOKUP_CONTACT_ID,
Expand Down Expand Up @@ -724,11 +724,3 @@ internal class ContactQueries(
}
}
}

internal fun Boolean.toBoolInt(): String {
return if (this) {
"1"
} else {
"0"
}
}

0 comments on commit 6fd710f

Please sign in to comment.