From d002d2fcfa83a75568079f1d1d5cf30f8c67c1d9 Mon Sep 17 00:00:00 2001 From: Nick Bradbury Date: Fri, 13 Dec 2024 08:20:06 -0500 Subject: [PATCH] Removed LocaleAwareComposable --- .../android/ui/compose/utils/ComposeUtils.kt | 50 ------------------- .../android/ui/mysite/menu/MenuActivity.kt | 11 +--- .../android/ui/mysite/menu/MenuViewModel.kt | 5 -- .../PersonalizationActivity.kt | 9 +--- 4 files changed, 4 insertions(+), 71 deletions(-) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/compose/utils/ComposeUtils.kt b/WordPress/src/main/java/org/wordpress/android/ui/compose/utils/ComposeUtils.kt index 41e13d4aa480..39b35346175d 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/compose/utils/ComposeUtils.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/compose/utils/ComposeUtils.kt @@ -1,18 +1,9 @@ package org.wordpress.android.ui.compose.utils -import android.annotation.SuppressLint -import android.content.res.Configuration import androidx.compose.material.LocalContentAlpha import androidx.compose.material.MaterialTheme import androidx.compose.runtime.Composable -import androidx.compose.runtime.CompositionLocalProvider -import androidx.compose.ui.platform.LocalContext -import androidx.compose.ui.platform.LocalLayoutDirection -import androidx.compose.ui.unit.LayoutDirection import org.wordpress.android.ui.compose.components.ContentAlphaProvider -import org.wordpress.android.util.extensions.isRtl -import org.wordpress.android.util.extensions.primaryLocale -import java.util.Locale /** * Utility function that returns a Composable function that wraps the [content] inside a [ContentAlphaProvider] @@ -26,47 +17,6 @@ fun withFullContentAlpha(content: @Composable () -> Unit): @Composable () -> Uni ) } -/** - * Utility function that wraps the [content] inside a [CompositionLocalProvider] overriding the [LocalContext] - * configuration with the specified [locale] when the specified language should apply. - * Useful to apply a custom language to Compose UIs that do not respond correctly to app language changes. - * @param locale The locale to be used in the [LocalContext] configuration override. - * @param onLocaleChange Callback to be invoked when the locale is overridden, useful to update other app components. - * @param content The Composable function to be rendered with the overridden locale. - */ - -@Composable -@Suppress("DEPRECATION") -@SuppressLint("AppBundleLocaleChanges") -fun LocaleAwareComposable( - locale: Locale = Locale.getDefault(), - onLocaleChange: (Locale) -> Unit = {}, - content: @Composable () -> Unit, -) { - val context = LocalContext.current - val resources = context.resources - val configuration = resources.configuration - - val currentLocale = context.primaryLocale - if (currentLocale != locale) { - val newConfiguration = Configuration(configuration).apply { - setLocale(locale) - } - - val newContext = context.createConfigurationContext(newConfiguration) - val newLayoutDirection = if (newConfiguration.isRtl()) LayoutDirection.Rtl else LayoutDirection.Ltr - onLocaleChange(locale) - CompositionLocalProvider( - LocalContext provides newContext, - LocalLayoutDirection provides newLayoutDirection, - ) { - content() - } - } else { - content() - } -} - /** * Indicates whether the currently selected theme is light. * @return true if the current theme is light diff --git a/WordPress/src/main/java/org/wordpress/android/ui/mysite/menu/MenuActivity.kt b/WordPress/src/main/java/org/wordpress/android/ui/mysite/menu/MenuActivity.kt index 52a5d3071fbf..48f90a947ed5 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/mysite/menu/MenuActivity.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/mysite/menu/MenuActivity.kt @@ -34,11 +34,11 @@ import androidx.compose.material3.SnackbarHostState import androidx.compose.material3.Text import androidx.compose.material3.TopAppBar import androidx.compose.runtime.Composable +import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.DisposableEffect import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue -import androidx.compose.runtime.livedata.observeAsState import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue @@ -59,7 +59,6 @@ import org.wordpress.android.R import org.wordpress.android.ui.ActivityLauncher import org.wordpress.android.ui.ActivityNavigator import org.wordpress.android.ui.compose.theme.AppThemeM3 -import org.wordpress.android.ui.compose.utils.LocaleAwareComposable import org.wordpress.android.ui.compose.utils.uiStringText import org.wordpress.android.ui.mysite.SiteNavigationAction import org.wordpress.android.ui.mysite.items.listitem.ListItemAction @@ -69,7 +68,6 @@ import org.wordpress.android.ui.quickstart.QuickStartMySitePrompts import org.wordpress.android.ui.stats.refresh.utils.StatsLaunchedFrom import org.wordpress.android.ui.utils.ListItemInteraction import org.wordpress.android.ui.utils.UiString -import org.wordpress.android.util.LocaleManager import org.wordpress.android.util.QuickStartUtilsWrapper import org.wordpress.android.util.SnackbarItem import org.wordpress.android.util.SnackbarSequencer @@ -96,12 +94,7 @@ class MenuActivity : AppCompatActivity() { initObservers() setContent { AppThemeM3 { - val userLanguage by viewModel.refreshAppLanguage.observeAsState("") - - LocaleAwareComposable( - locale = LocaleManager.languageLocale(userLanguage), - onLocaleChange = viewModel::setAppLanguage - ) { + CompositionLocalProvider{ viewModel.start(intent.getParcelableExtraCompat(KEY_QUICK_START_EVENT)) MenuScreen( onBackPressed = onBackPressedDispatcher::onBackPressed diff --git a/WordPress/src/main/java/org/wordpress/android/ui/mysite/menu/MenuViewModel.kt b/WordPress/src/main/java/org/wordpress/android/ui/mysite/menu/MenuViewModel.kt index c38dfa402de4..9bcf986e059f 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/mysite/menu/MenuViewModel.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/mysite/menu/MenuViewModel.kt @@ -36,7 +36,6 @@ import org.wordpress.android.util.merge import org.wordpress.android.viewmodel.ContextProvider import org.wordpress.android.viewmodel.Event import org.wordpress.android.viewmodel.ScopedViewModel -import java.util.Locale import javax.inject.Inject import javax.inject.Named @@ -266,10 +265,6 @@ class MenuViewModel @Inject constructor( } } - fun setAppLanguage(locale: Locale) { - // TODO remove this - } - override fun onCleared() { jetpackCapabilitiesUseCase.clear() super.onCleared() diff --git a/WordPress/src/main/java/org/wordpress/android/ui/mysite/personalization/PersonalizationActivity.kt b/WordPress/src/main/java/org/wordpress/android/ui/mysite/personalization/PersonalizationActivity.kt index 207f4e13e400..6edb539a2d1f 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/mysite/personalization/PersonalizationActivity.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/mysite/personalization/PersonalizationActivity.kt @@ -33,6 +33,7 @@ import androidx.compose.material3.TabRow import androidx.compose.material3.Text import androidx.compose.material3.TopAppBar import androidx.compose.runtime.Composable +import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.livedata.observeAsState @@ -54,11 +55,9 @@ import androidx.compose.ui.unit.sp import dagger.hilt.android.AndroidEntryPoint import org.wordpress.android.R import org.wordpress.android.ui.compose.theme.AppThemeM3 -import org.wordpress.android.ui.compose.utils.LocaleAwareComposable import org.wordpress.android.ui.compose.utils.uiStringText import org.wordpress.android.ui.mysite.items.listitem.ListItemAction import org.wordpress.android.ui.utils.UiString -import org.wordpress.android.util.LocaleManager @AndroidEntryPoint class PersonalizationActivity : AppCompatActivity() { @@ -68,11 +67,7 @@ class PersonalizationActivity : AppCompatActivity() { super.onCreate(savedInstanceState) setContent { AppThemeM3 { - val language by viewModel.appLanguage.observeAsState("") - - LocaleAwareComposable( - locale = LocaleManager.languageLocale(language), - ) { + CompositionLocalProvider{ viewModel.start() PersonalizationScreen() }