Skip to content

Commit

Permalink
refactor: normalize stored phone numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Feb 5, 2024
1 parent 4b5aaf4 commit a4bdcb8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/com/bnyro/contacts/repo/DeviceSmsRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.bnyro.contacts.db.obj.SmsData
import com.bnyro.contacts.ext.intValue
import com.bnyro.contacts.ext.longValue
import com.bnyro.contacts.ext.stringValue
import com.bnyro.contacts.util.ContactsHelper
import com.bnyro.contacts.util.PermissionHelper
import com.bnyro.contacts.util.SmsUtil
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -115,7 +116,7 @@ class DeviceSmsRepo : SmsRepository {
contentUri,
arrayOf(Telephony.Sms.THREAD_ID),
"${Telephony.Sms.ADDRESS} = ?",
arrayOf(address),
arrayOf(ContactsHelper.normalizePhoneNumber(address)),
null
)
?.use {
Expand Down
11 changes: 6 additions & 5 deletions app/src/main/java/com/bnyro/contacts/repo/LocalSmsRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.bnyro.contacts.repo
import android.content.Context
import com.bnyro.contacts.db.DatabaseHolder
import com.bnyro.contacts.db.obj.SmsData
import com.bnyro.contacts.util.ContactsHelper
import kotlinx.coroutines.flow.Flow
import kotlin.random.Random

Expand All @@ -15,11 +16,11 @@ class LocalSmsRepo : SmsRepository {
}

override suspend fun getOrCreateThreadId(context: Context, address: String): Long {
DatabaseHolder.Db.localSmsDao().getSmsByAddress(address).firstOrNull()?.let {
return it.threadId
}

return Random.nextLong()
return DatabaseHolder.Db.localSmsDao()
.getSmsByAddress(ContactsHelper.normalizePhoneNumber(address))
.firstOrNull()
?.threadId
?: Random.nextLong()
}

override suspend fun deleteSms(context: Context, id: Long) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,17 @@ fun ContactEditor(
val contactsModel: ContactsModel = viewModel(factory = ContactsModel.Factory)

fun List<ValueWithType>?.fillIfEmpty(): List<ValueWithType> {
return if (this.isNullOrEmpty()) {
listOf(ValueWithType("", 0))
} else {
this
if (this.isNullOrEmpty()) {
return listOf(ValueWithType("", 0))
}

return this
}

fun List<MutableState<ValueWithType>>.clean(): List<ValueWithType> {
return this.filter { it.value.value.isNotBlank() }.map { it.value }
return this.filter { it.value.value.isNotBlank() }.map { it.value }.distinct()
}

fun emptyMutable() = mutableStateOf(ValueWithType("", 0))

var showAdvanced by remember {
mutableStateOf(false)
}
Expand Down Expand Up @@ -192,7 +190,9 @@ fun ContactEditor(
it.accountType = selectedAccount.first
it.accountName = selectedAccount.second
it.websites = websites.clean()
it.numbers = phoneNumber.clean()
it.numbers = phoneNumber.clean().map { number ->
ValueWithType(ContactsHelper.normalizePhoneNumber(number.value), number.type)
}
it.emails = emails.clean()
it.addresses = addresses.clean()
it.events = events.clean()
Expand Down

0 comments on commit a4bdcb8

Please sign in to comment.