perf(phone): move PhoneUtils libphonenumber init off the main thread#1072
Merged
Conversation
…load Stop PhoneVerificationViewModel from reading phoneUtils.defaultCountryLocale eagerly in initialState (forcing main-thread work before libphonenumber is loaded). initialState now uses CountryLocale.Stub; the init coroutine calls ensureLoaded() on the Default dispatcher inside a try/catch (rethrows CancellationException; logs other failures via trace so input observation still starts even if metadata load fails), then dispatches OnCountrySelected with the real locale. Phone-number input observation is extracted to observePhoneNumberInput() and started only after ensureLoaded() completes, so no parse/format ever runs before the metadata is ready.
…bservability PhoneCountryCodeScreenContent was reading phoneUtils.countryLocales directly from a plain @volatile field. Compose does not snapshot-track plain field reads, so the picker rendered empty and never recomposed to recover after the async load. Route the list through ViewModel State (countryLocales: List<CountryLocale>) so it is observed via the existing collectAsStateWithLifecycle() call. The new OnCountryLocalesLoaded event is dispatched in the init coroutine after ensureLoaded() completes, alongside the existing OnCountrySelected dispatch. Also guard the default-locale selection so OnCountrySelected is only dispatched when the loaded default is non-Stub — prevents a +0 / empty-country-code field if ensureLoaded() threw and left defaultCountryLocale == Stub. ContactCoordinator.performSync() gains an ensureLoaded() call before formatting E.164 numbers, guarding against the first sync racing ahead of the app-startup warm-up and using an empty region hint for toE164.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Moves
PhoneUtils' heavy libphonenumber initialization off the main thread to reduce onboarding/startup jank and ANR risk on low-end devices.PhoneUtils(a@Singleton) previously did its expensive work —PhoneNumberUtil.createInstance()(metadata blob load) plus enumerating ~250supportedRegionswith per-region locale/flag lookups — in its constructor. Because it's@Inject lateinitinMainActivity, that ran synchronously on the main thread at startup. This surfaced during triage of an onboarding ANR on a low-end device (TECNO BG6m, 32-bit, 1.87 GB RAM).