Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [#274] fixing locale on android 14 and setup test using junit & mockk #282

Merged
merged 5 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ android {
buildConfig true
viewBinding true
}
bundle {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andhikayuana is this a complex concept. Not familiar with it. Could you briefly explain ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is related with android app bundle concept. in short, when the users download the app from playstore it will only download the specific architecture, specific language
more info https://developer.android.com/guide/app-bundle/configure-base#disable_config_apks

language {
enableSplit = false
}
}
assetPacks = [":install_time_asset_pack",":fast_follow_asset_pack_01"]
signingConfigs {
release {
Expand Down Expand Up @@ -365,6 +370,10 @@ dependencies {

// Get the latest version from https://mvnrepository.com/artifact/com.appsflyer/af-android-sdk
implementation 'com.appsflyer:af-android-sdk:6.15.1'

//test
testImplementation "io.mockk:mockk:1.13.13"
testImplementation 'junit:junit:4.13.2'
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class IntroLanguageResource {
private val EUR: String = "EUR"
private val RMB: String = "RMB"
private val JPY: String = "JPY"
fun loadResources() : Array<IntroLanguage>{
return arrayOf<IntroLanguage> (
fun loadResources(): Array<IntroLanguage> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this your linter or just your notice/fix?...I love linter/autoformatters

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, I just using reformat code from Android Studio

return arrayOf<IntroLanguage>(
IntroLanguage(
Language.ENGLISH.code,
Language.ENGLISH.title,
Expand Down Expand Up @@ -115,4 +115,8 @@ class IntroLanguageResource {
)
)
}

fun findLanguageIndex(language: Language): Int {
return loadResources().map { intro -> intro.lang }.indexOf(language)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
Expand All @@ -14,13 +15,12 @@
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.appsflyer.AppsFlyerLib;
import com.breadwallet.entities.IntroLanguageResource;
import com.breadwallet.entities.Language;
import com.breadwallet.presenter.activities.SetPinActivity;
import com.breadwallet.presenter.entities.PartnerNames;
import com.breadwallet.tools.adapter.CountryLanguageAdapter;
import com.breadwallet.tools.util.LocaleHelper;
import com.google.android.material.snackbar.Snackbar;
//import com.breadwallet.BuildConfig;
import com.breadwallet.R;
import com.breadwallet.presenter.activities.BreadActivity;
Expand Down Expand Up @@ -78,6 +78,12 @@ protected void onCreate(Bundle savedInstanceState) {
countryLanguageAdapter = new CountryLanguageAdapter(this, introLanguageResource.loadResources());
listLangRecyclerView.setAdapter(countryLanguageAdapter);

Language currentLanguage = LocaleHelper.Companion.getInstance().getCurrentLocale();
int currentIndex = introLanguageResource.findLanguageIndex(currentLanguage);
countryLanguageAdapter.updateCenterPosition(currentIndex);
new Handler().post(() -> listLangRecyclerView.scrollToPosition(currentIndex));


listLangRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/com/breadwallet/tools/util/LocaleHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.breadwallet.tools.util
import android.content.Context
import android.content.SharedPreferences
import android.os.Build
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.os.LocaleListCompat
import androidx.preference.PreferenceManager
import com.breadwallet.entities.Language
import java.util.*
Expand Down Expand Up @@ -34,6 +36,8 @@ class LocaleHelper private constructor() {
Locale.setDefault(locale)
val config = context.resources.configuration
config.setLocale(locale)
val localeList = LocaleListCompat.forLanguageTags(language.code)
AppCompatDelegate.setApplicationLocales(localeList)
return context.createConfigurationContext(config)
}

Expand Down
53 changes: 53 additions & 0 deletions app/src/test/java/com/breadwallet/tools/util/LocaleHelperTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.breadwallet.tools.util

import android.content.Context
import com.breadwallet.entities.Language
import io.mockk.mockk
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test

class LocaleHelperTest {

@Test
fun `given LocaleHelper instance, then should validate default value`() {
val context: Context = mockk(relaxed = true)

LocaleHelper.init(context)

val currentLocale = LocaleHelper.instance.currentLocale

assertTrue(currentLocale == Language.ENGLISH)
assertEquals("en", currentLocale.code)
assertEquals("English", currentLocale.title)
assertEquals("Select language", currentLocale.desc)
}

@Test
fun `getLocale invoked, should return Locale object`() {
val localeIndonesian = LocaleHelper.getLocale(Language.INDONESIAN)

assertEquals("id", localeIndonesian.language)

val localeChineseSimplified = LocaleHelper.getLocale(Language.CHINESE_SIMPLIFIED)

assertEquals("zh", localeChineseSimplified.language)
assertEquals("CN", localeChineseSimplified.country)
}

@Test
fun `setLocaleIfNeeded invoked, should update current locale`() {
val context: Context = mockk(relaxed = true)
LocaleHelper.init(context)

val currentLocale = LocaleHelper.instance.currentLocale
assertTrue(currentLocale == Language.ENGLISH)

var changed = LocaleHelper.instance.setLocaleIfNeeded(Language.INDONESIAN)
assertTrue(changed)

changed = LocaleHelper.instance.setLocaleIfNeeded(Language.INDONESIAN)
assertFalse(changed)
}
}