From 2501d7c985d6f71c18335a8c6d94386e0a90f438 Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 10 Feb 2022 16:55:49 +0100 Subject: [PATCH] make normalizedNumber nullable --- .../commons/helpers/SimpleContactsHelper.kt | 7 ++++--- .../com/simplemobiletools/commons/models/PhoneNumber.kt | 2 +- .../com/simplemobiletools/commons/models/SimpleContact.kt | 8 ++++---- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/helpers/SimpleContactsHelper.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/helpers/SimpleContactsHelper.kt index c154fd218..4f4e7f9ae 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/helpers/SimpleContactsHelper.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/helpers/SimpleContactsHelper.kt @@ -44,8 +44,9 @@ class SimpleContactsHelper(val context: Context) { } allContacts = allContacts.filter { it.name.isNotEmpty() }.distinctBy { - val startIndex = Math.max(0, it.phoneNumbers.first().normalizedNumber.length - 9) - it.phoneNumbers.first().normalizedNumber.substring(startIndex) + val normalizedNumber = it.phoneNumbers.first().normalizedNumber ?: it.phoneNumbers.first().value.normalizePhoneNumber() + val startIndex = Math.max(0, normalizedNumber.length - 9) + normalizedNumber.substring(startIndex) }.distinctBy { it.rawId }.toMutableList() as ArrayList // if there are duplicate contacts with the same name, while the first one has phone numbers 1234 and 4567, second one has only 4567, @@ -58,7 +59,7 @@ class SimpleContactsHelper(val context: Context) { if (contacts.any { it.phoneNumbers.size == 1 } && contacts.any { it.phoneNumbers.size > 1 }) { val multipleNumbersContact = contacts.first() contacts.subList(1, contacts.size).forEach { contact -> - if (contact.phoneNumbers.all { multipleNumbersContact.doesContainPhoneNumber(it.normalizedNumber) }) { + if (contact.phoneNumbers.all { it.normalizedNumber != null && multipleNumbersContact.doesContainPhoneNumber(it.normalizedNumber!!) }) { val contactToRemove = allContacts.firstOrNull { it.rawId == contact.rawId } if (contactToRemove != null) { contactsToRemove.add(contactToRemove) diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/models/PhoneNumber.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/models/PhoneNumber.kt index 3a4d07a1b..0bb0cf465 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/models/PhoneNumber.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/models/PhoneNumber.kt @@ -1,3 +1,3 @@ package com.simplemobiletools.commons.models -data class PhoneNumber(var value: String, var type: Int, var label: String, var normalizedNumber: String) +data class PhoneNumber(var value: String, var type: Int, var label: String, var normalizedNumber: String?) diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/models/SimpleContact.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/models/SimpleContact.kt index c07aee1ab..68a4261d9 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/models/SimpleContact.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/models/SimpleContact.kt @@ -31,11 +31,11 @@ data class SimpleContact( return if (text.isNotEmpty()) { val normalizedText = text.normalizePhoneNumber() if (normalizedText.isEmpty()) { - phoneNumbers.map { it.normalizedNumber }.any { phoneNumber -> + phoneNumbers.mapNotNull { it.normalizedNumber }.any { phoneNumber -> phoneNumber.contains(text) } } else { - phoneNumbers.map { it.normalizedNumber }.any { phoneNumber -> + phoneNumbers.mapNotNull { it.normalizedNumber }.any { phoneNumber -> PhoneNumberUtils.compare(phoneNumber.normalizePhoneNumber(), normalizedText) || phoneNumber.contains(text) || phoneNumber.normalizePhoneNumber().contains(normalizedText) || @@ -51,11 +51,11 @@ data class SimpleContact( return if (text.isNotEmpty()) { val normalizedText = text.normalizePhoneNumber() if (normalizedText.isEmpty()) { - phoneNumbers.map { it.normalizedNumber }.any { phoneNumber -> + phoneNumbers.mapNotNull { it.normalizedNumber }.any { phoneNumber -> phoneNumber == text } } else { - phoneNumbers.map { it.normalizedNumber }.any { phoneNumber -> + phoneNumbers.mapNotNull { it.normalizedNumber }.any { phoneNumber -> PhoneNumberUtils.compare(phoneNumber.normalizePhoneNumber(), normalizedText) || phoneNumber == text || phoneNumber.normalizePhoneNumber() == normalizedText ||