Prepopulate find number with local user country code.

This commit is contained in:
Cody Henthorne
2024-02-27 10:35:56 -05:00
committed by Alex Hart
parent 957221e118
commit c10c64a6a6
2 changed files with 25 additions and 4 deletions

View File

@@ -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()
)
}
}
}

View File

@@ -28,9 +28,7 @@ class FindByViewModel(
) : ViewModel() {
private val internalState = mutableStateOf(
FindByState(
mode = mode
)
FindByState.startingState(self = Recipient.self(), mode = mode)
)
val state: State<FindByState> = internalState