From c10c64a6a6ab1890ff51111879f077caec602237 Mon Sep 17 00:00:00 2001 From: Cody Henthorne Date: Tue, 27 Feb 2024 10:35:56 -0500 Subject: [PATCH] Prepopulate find number with local user country code. --- .../recipients/ui/findby/FindByState.kt | 25 ++++++++++++++++++- .../recipients/ui/findby/FindByViewModel.kt | 4 +-- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/recipients/ui/findby/FindByState.kt b/app/src/main/java/org/thoughtcrime/securesms/recipients/ui/findby/FindByState.kt index de79b9f213..d701b2f7bf 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/recipients/ui/findby/FindByState.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/recipients/ui/findby/FindByState.kt @@ -5,9 +5,15 @@ package org.thoughtcrime.securesms.recipients.ui.findby +import com.google.i18n.phonenumbers.NumberParseException import com.google.i18n.phonenumbers.PhoneNumberUtil +import org.signal.core.util.orNull +import org.thoughtcrime.securesms.recipients.Recipient import org.thoughtcrime.securesms.registration.util.CountryPrefix +/** + * State for driving find by number/username screen. + */ data class FindByState( val mode: FindByMode, val userEntry: String = "", @@ -17,4 +23,21 @@ data class FindByState( val selectedCountryPrefix: CountryPrefix = supportedCountryPrefixes.first(), val countryPrefixSearchEntry: String = "", val isLookupInProgress: Boolean = false -) +) { + companion object { + fun startingState(self: Recipient, mode: FindByMode): FindByState { + val countryCode: Int = try { + PhoneNumberUtil.getInstance() + .parse(self.e164.orNull(), null) + .countryCode + } catch (e: NumberParseException) { + -1 + } + + val state = FindByState(mode = mode) + return state.copy( + selectedCountryPrefix = state.supportedCountryPrefixes.firstOrNull { it.digits == countryCode } ?: state.supportedCountryPrefixes.first() + ) + } + } +} diff --git a/app/src/main/java/org/thoughtcrime/securesms/recipients/ui/findby/FindByViewModel.kt b/app/src/main/java/org/thoughtcrime/securesms/recipients/ui/findby/FindByViewModel.kt index 8637b1910b..fdc392efcb 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/recipients/ui/findby/FindByViewModel.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/recipients/ui/findby/FindByViewModel.kt @@ -28,9 +28,7 @@ class FindByViewModel( ) : ViewModel() { private val internalState = mutableStateOf( - FindByState( - mode = mode - ) + FindByState.startingState(self = Recipient.self(), mode = mode) ) val state: State = internalState