Skip to content

Commit

Permalink
Removed LocaleAwareComposable
Browse files Browse the repository at this point in the history
  • Loading branch information
nbradbury committed Dec 13, 2024
1 parent e71c2eb commit d002d2f
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 71 deletions.
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -266,10 +265,6 @@ class MenuViewModel @Inject constructor(
}
}

fun setAppLanguage(locale: Locale) {
// TODO remove this
}

override fun onCleared() {
jetpackCapabilitiesUseCase.clear()
super.onCleared()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand All @@ -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()
}
Expand Down

0 comments on commit d002d2f

Please sign in to comment.