Skip to content

Commit

Permalink
Add runtime locale update
Browse files Browse the repository at this point in the history
  • Loading branch information
julienherrero committed Dec 20, 2021
1 parent 184eec5 commit 211d4aa
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 14 deletions.
@@ -1,5 +1,6 @@
package co.zsmb.rainbowcake.demo

import android.content.Context
import co.zsmb.rainbowcake.config.Loggers
import co.zsmb.rainbowcake.config.rainbowCake
import co.zsmb.rainbowcake.dagger.RainbowCakeApplication
Expand All @@ -9,6 +10,7 @@ import co.zsmb.rainbowcake.demo.ui.UIModule
import co.zsmb.rainbowcake.timber.TIMBER
import org.koin.core.context.startKoin
import timber.log.Timber
import java.util.Locale

open class DemoApplication : RainbowCakeApplication() {

Expand All @@ -18,6 +20,8 @@ open class DemoApplication : RainbowCakeApplication() {
injector = DaggerAppComponent.create()
}

override fun getDefaultLanguage(base: Context): Locale = Locale.ENGLISH

override fun onCreate() {
super.onCreate()

Expand Down
Expand Up @@ -12,7 +12,16 @@ import co.zsmb.rainbowcake.demo.ui.koin.KoinFragment
import co.zsmb.rainbowcake.demo.ui.sharedvmpager.SharedVMPagerFragment
import co.zsmb.rainbowcake.demo.ui.viewbinding.ViewBindingSampleFragment
import co.zsmb.rainbowcake.navigation.navigator
import kotlinx.android.synthetic.main.fragment_example.*
import com.akexorcist.localizationactivity.ui.LocalizationActivity
import kotlinx.android.synthetic.main.fragment_example.bottomSheetExampleDemoButton
import kotlinx.android.synthetic.main.fragment_example.customAnimationDemoButton
import kotlinx.android.synthetic.main.fragment_example.dialogExampleDemoButton
import kotlinx.android.synthetic.main.fragment_example.koinExampleDemoButton
import kotlinx.android.synthetic.main.fragment_example.localeExampleDemoButton
import kotlinx.android.synthetic.main.fragment_example.localeExampleDemoTextView
import kotlinx.android.synthetic.main.fragment_example.sharedVMDemoButton
import kotlinx.android.synthetic.main.fragment_example.viewBindingExampleDemoButton
import java.util.Locale

class ExampleFragment : RainbowCakeFragment<ExampleViewState, ExampleViewModel>() {

Expand All @@ -27,11 +36,13 @@ class ExampleFragment : RainbowCakeFragment<ExampleViewState, ExampleViewModel>(
}

customAnimationDemoButton.setOnClickListener {
navigator?.add(FooFragment(),
enterAnim = R.anim.slide_in_right,
exitAnim = R.anim.slide_out_left,
popEnterAnim = R.anim.slide_in_left,
popExitAnim = R.anim.slide_out_right)
navigator?.add(
FooFragment(),
enterAnim = R.anim.slide_in_right,
exitAnim = R.anim.slide_out_left,
popEnterAnim = R.anim.slide_in_left,
popExitAnim = R.anim.slide_out_right
)
}

viewBindingExampleDemoButton.setOnClickListener {
Expand All @@ -49,10 +60,21 @@ class ExampleFragment : RainbowCakeFragment<ExampleViewState, ExampleViewModel>(
dialogExampleDemoButton.setOnClickListener {
ExampleDialogFragment().show(requireFragmentManager(), "DialogFragment")
}
}

override fun render(viewState: ExampleViewState) {
localeExampleDemoButton.setOnClickListener {
with(activity as LocalizationActivity) {
if (getCurrentLanguage() == Locale.ENGLISH) {
setLanguage(Locale.FRENCH)
} else {
setLanguage(Locale.ENGLISH)
}
}
}

val language = (activity as LocalizationActivity).getCurrentLanguage().displayLanguage
localeExampleDemoTextView.text = getString(R.string.locale_text, language)
}

override fun render(viewState: ExampleViewState) {
}
}
27 changes: 26 additions & 1 deletion demo/src/main/res/layout/fragment_example.xml
@@ -1,5 +1,6 @@
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/exampleFragmentRoot"
android:layout_width="match_parent"
android:layout_height="match_parent">
Expand Down Expand Up @@ -83,9 +84,33 @@
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:text="DialogFragment"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintBottom_toTopOf="@+id/localeExampleDemoButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/bottomSheetExampleDemoButton" />

<Button
android:id="@+id/localeExampleDemoButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:text="@string/locale_button"
app:layout_constraintBottom_toTopOf="@+id/localeExampleDemoTextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/dialogExampleDemoButton" />

<TextView
android:id="@+id/localeExampleDemoTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/localeExampleDemoButton"
tools:text="en_EN" />

</androidx.constraintlayout.widget.ConstraintLayout>
4 changes: 4 additions & 0 deletions demo/src/main/res/values-fr/strings.xml
@@ -0,0 +1,4 @@
<resources>
<string name="locale_button">Changer la langue</string>
<string name="locale_text">Langue actuelle: %s</string>
</resources>
4 changes: 3 additions & 1 deletion demo/src/main/res/values/strings.xml
@@ -1,3 +1,5 @@
<resources>
<string name="app_name">RainbowCake</string>
<string name="app_name" translatable="false">RainbowCake</string>
<string name="locale_button">Change locale</string>
<string name="locale_text">Current locale: %s</string>
</resources>
3 changes: 3 additions & 0 deletions rainbow-cake-core/build.gradle
Expand Up @@ -16,6 +16,9 @@ dependencies {
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"

// Localization
api 'com.akexorcist:localization:1.2.10'

// Testing
androidTestImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test:runner:1.4.0'
Expand Down
Expand Up @@ -4,11 +4,12 @@ import android.os.Bundle
import androidx.annotation.CallSuper
import androidx.appcompat.app.AppCompatActivity
import co.zsmb.rainbowcake.internal.logging.log
import com.akexorcist.localizationactivity.ui.LocalizationActivity

/**
* Base class for Activities that connects them to the appropriate ViewModel instances.
*/
public abstract class RainbowCakeActivity<VS : Any, VM : RainbowCakeViewModel<VS>> : AppCompatActivity() {
public abstract class RainbowCakeActivity<VS : Any, VM : RainbowCakeViewModel<VS>> : LocalizationActivity() {

private val logTag: String by lazy(mode = LazyThreadSafetyMode.NONE) { "RainbowCakeActivity ($this)" }

Expand Down
@@ -1,13 +1,13 @@
package co.zsmb.rainbowcake.dagger

import android.app.Application
import androidx.annotation.CallSuper
import com.akexorcist.localizationactivity.ui.LocalizationApplication

/**
* Base class for applications built on this architecture, primarily
* used for DI integration
*/
public abstract class RainbowCakeApplication : Application() {
public abstract class RainbowCakeApplication : LocalizationApplication() {

/**
* The injector used by the [getViewModelFromFactory] methods.
Expand All @@ -24,5 +24,4 @@ public abstract class RainbowCakeApplication : Application() {
* This method should create a Dagger injector set the [injector] property.
*/
protected abstract fun setupInjector()

}

0 comments on commit 211d4aa

Please sign in to comment.