B3nedikt / reword

Reword is a android library to update the texts of views when the apps texts have changed due to a language change or an update of the apps string resources.
Apache License 2.0
31 stars 7 forks source link

Can you support Android's compose UI #38

Open cye10 opened 4 months ago

cye10 commented 4 months ago

Is there an existing method that supports compose.

B3nedikt commented 4 months ago

I would say what this library does is nearly the same as a recomposition in compose, except that it only works for strings. So you could achieve the exact same behavior by simply triggering a recomposition.

    private val recomposeToggleState: MutableState<Boolean> = mutableStateOf(false)

    @Composable
    fun YourView() {

        Text(text = stringResource(R.string.a_string))

        LaunchedEffect(recomposeToggleState.value) {}
    }

    fun recompose() {
        recomposeToggleState.value = !recomposeToggleState.value
    }

I am not sure if this is helpful in your case though, what would be your use case for using Reword with compose?

cye10 commented 4 months ago

At present, we have a large amount of compose code. The above code is modified by modifying the source location. Can we only make individual modifications to each location to achieve this.

Del-S commented 4 months ago

So since Compose only updates Composables that changed I had the issue with this as well where only parts of the app were reworded. I bypassed it by showing loading content instead of content of the app for a few milliseconds after the update in the MainActivity.

class MainActivity : AppCompatActivity() {

  private val restringRecompose: MutableState<Boolean> = mutableStateOf(false)

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    collectStringUpdates()
    setContent {
        AppTheme {
            // Shows loading instead of the app content (MainScreen).
            if (restringRecompose.value) {
                                LoadingLayout()
            } else {
                MainScreen()       
            }

            // Hides loading after delay
            LaunchedEffect(key1 = restringRecompose.value) {
                delay(500) // Set the delay you want to show to the user. If there is none then it just blinks the loading layout.
                if (restringRecompose.value) {
                    restringRecompose.value = false
                }
            }
        }
    }

    private fun collectStringUpdates() {
        vm.srings.collect {
            // Triggers Restring and loading
            Restring.putStrings(dictionary.locale, dictionary.texts)
            restringRecompose.value = true
        }
    }
  }

I think I might use this intead of Reword library.