From e511fab3583a45475b904ed67b3e3aa5c8f18b3d Mon Sep 17 00:00:00 2001 From: Javier Varona Date: Sun, 18 Mar 2018 22:27:45 -0400 Subject: [PATCH] - Preferred Language Setting is now working --- app/src/main/AndroidManifest.xml | 1 + .../activities/BoardActivity.java | 2 + .../activities/GeneralSettingsActivity.java | 10 +-- .../application/CrystalApplication.java | 20 ++++++ .../crystalwallet/dao/GeneralSettingDao.java | 3 + .../agorise/crystalwallet/enums/Language.java | 49 +++++++++++++ .../fragments/GeneralSettingsFragment.java | 69 +++++++++++++++++-- .../crystalwallet/models/GeneralSetting.java | 5 +- .../service/CrystalWalletService.java | 2 +- .../views/CryptoCoinBalanceViewHolder.java | 2 +- 10 files changed, 149 insertions(+), 14 deletions(-) create mode 100644 app/src/main/java/cy/agorise/crystalwallet/enums/Language.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index fc1b582..e21a87d 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -12,6 +12,7 @@ android:label="Crystal" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" + android:configChanges="locale" android:theme="@style/AppTheme"> diff --git a/app/src/main/java/cy/agorise/crystalwallet/activities/BoardActivity.java b/app/src/main/java/cy/agorise/crystalwallet/activities/BoardActivity.java index 95c7fb4..b4d72c6 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/activities/BoardActivity.java +++ b/app/src/main/java/cy/agorise/crystalwallet/activities/BoardActivity.java @@ -4,6 +4,7 @@ import android.app.ActivityOptions; import android.arch.lifecycle.LiveData; import android.arch.lifecycle.ViewModelProviders; import android.content.Intent; +import android.content.res.Configuration; import android.content.res.Resources; import android.media.MediaPlayer; import android.os.Bundle; @@ -24,6 +25,7 @@ import android.view.animation.LinearInterpolator; import android.widget.ImageView; import java.util.List; +import java.util.Locale; import butterknife.BindColor; import butterknife.BindView; diff --git a/app/src/main/java/cy/agorise/crystalwallet/activities/GeneralSettingsActivity.java b/app/src/main/java/cy/agorise/crystalwallet/activities/GeneralSettingsActivity.java index 33a14d9..771ac7d 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/activities/GeneralSettingsActivity.java +++ b/app/src/main/java/cy/agorise/crystalwallet/activities/GeneralSettingsActivity.java @@ -96,16 +96,16 @@ public class GeneralSettingsActivity extends AppCompatActivity { @OnItemSelected(R.id.spPreferedCountry) void onItemSelected(int position) { if (position != 0) { - GeneralSetting generalSettingCountryCode = this.getSetting(GeneralSetting.SETTING_NAME_PREFERED_COUNTRY); - GeneralSetting generalSettingCurrency = this.getSetting(GeneralSetting.SETTING_NAME_PREFERED_CURRENCY); + GeneralSetting generalSettingCountryCode = this.getSetting(GeneralSetting.SETTING_NAME_PREFERRED_COUNTRY); + GeneralSetting generalSettingCurrency = this.getSetting(GeneralSetting.SETTING_NAME_PREFERRED_CURRENCY); if (generalSettingCountryCode == null){ generalSettingCountryCode = new GeneralSetting(); - generalSettingCountryCode.setName(GeneralSetting.SETTING_NAME_PREFERED_COUNTRY); + generalSettingCountryCode.setName(GeneralSetting.SETTING_NAME_PREFERRED_COUNTRY); } if (generalSettingCurrency == null){ generalSettingCurrency = new GeneralSetting(); - generalSettingCurrency.setName(GeneralSetting.SETTING_NAME_PREFERED_CURRENCY); + generalSettingCurrency.setName(GeneralSetting.SETTING_NAME_PREFERRED_CURRENCY); } String countryCode = countriesMap.get((String) spPreferedCountry.getSelectedItem()); @@ -120,7 +120,7 @@ public class GeneralSettingsActivity extends AppCompatActivity { public void loadSettings(List generalSettings){ for (GeneralSetting generalSetting:generalSettings) { - if (generalSetting.getName().equals(GeneralSetting.SETTING_NAME_PREFERED_COUNTRY)){ + if (generalSetting.getName().equals(GeneralSetting.SETTING_NAME_PREFERRED_COUNTRY)){ String preferedCountryCode = generalSetting.getValue(); spPreferedCountry.setSelection(((ArrayAdapter)spPreferedCountry.getAdapter()).getPosition(countriesMap.get(preferedCountryCode))); } diff --git a/app/src/main/java/cy/agorise/crystalwallet/application/CrystalApplication.java b/app/src/main/java/cy/agorise/crystalwallet/application/CrystalApplication.java index 8a1e16c..bfbab09 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/application/CrystalApplication.java +++ b/app/src/main/java/cy/agorise/crystalwallet/application/CrystalApplication.java @@ -2,14 +2,20 @@ package cy.agorise.crystalwallet.application; import android.app.Application; import android.content.Intent; +import android.content.res.Configuration; +import android.content.res.Resources; +import android.util.DisplayMetrics; import com.idescout.sql.SqlScoutServer; +import java.util.Locale; + import cy.agorise.crystalwallet.dao.CrystalDatabase; import cy.agorise.crystalwallet.enums.CryptoNet; import cy.agorise.crystalwallet.models.BitsharesAsset; import cy.agorise.crystalwallet.models.BitsharesAssetInfo; import cy.agorise.crystalwallet.models.CryptoCurrencyEquivalence; +import cy.agorise.crystalwallet.models.GeneralSetting; import cy.agorise.crystalwallet.network.CryptoNetManager; import cy.agorise.crystalwallet.service.CrystalWalletService; @@ -20,6 +26,8 @@ import cy.agorise.crystalwallet.service.CrystalWalletService; */ public class CrystalApplication extends Application { + private Locale locale = null; + public static String BITSHARES_URL[] = { "wss://de.palmpay.io/ws", // Custom node @@ -78,6 +86,18 @@ public class CrystalApplication extends Application { //Next line is for use the bitshares main net //CryptoNetManager.addCryptoNetURL(CryptoNet.BITSHARES,BITSHARES_URL); + GeneralSetting generalSettingPreferredLanguage = db.generalSettingDao().getSettingByName(GeneralSetting.SETTING_NAME_PREFERRED_LANGUAGE); + + if (generalSettingPreferredLanguage != null) { + Resources resources = getBaseContext().getResources(); + Locale locale = new Locale(generalSettingPreferredLanguage.getValue()); + Locale.setDefault(locale); + DisplayMetrics dm = resources.getDisplayMetrics(); + Configuration configuration = resources.getConfiguration(); + configuration.locale = locale; + resources.updateConfiguration(configuration, dm); + } + Intent intent = new Intent(getApplicationContext(), CrystalWalletService.class); startService(intent); } diff --git a/app/src/main/java/cy/agorise/crystalwallet/dao/GeneralSettingDao.java b/app/src/main/java/cy/agorise/crystalwallet/dao/GeneralSettingDao.java index 0a8dc96..e7c6acb 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/dao/GeneralSettingDao.java +++ b/app/src/main/java/cy/agorise/crystalwallet/dao/GeneralSettingDao.java @@ -24,6 +24,9 @@ public interface GeneralSettingDao { @Query("SELECT * FROM general_setting WHERE name = :name") LiveData getByName(String name); + @Query("SELECT * FROM general_setting WHERE name = :name") + GeneralSetting getSettingByName(String name); + @Insert(onConflict = OnConflictStrategy.REPLACE) public long[] insertGeneralSettings(GeneralSetting... generalSettings); diff --git a/app/src/main/java/cy/agorise/crystalwallet/enums/Language.java b/app/src/main/java/cy/agorise/crystalwallet/enums/Language.java new file mode 100644 index 0000000..492468b --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/enums/Language.java @@ -0,0 +1,49 @@ +package cy.agorise.crystalwallet.enums; + +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; + +/** + * Created by Henry Varona on 16/3/2018. + */ + +public enum Language { + NOTSET("",""),ENGLISH("en","English"),SPANISH("es","Spanish"); + + protected String code; + + protected String label; + + private static Map codeMap = new HashMap(); + static { + for (Language languageEnum : Language.values()) { + codeMap.put(languageEnum.code, languageEnum); + } + } + + Language(String code, String label){ + this.code = code; + this.label = label; + } + + public String getCode(){ + return this.code; + } + + public String getLabel(){ + return this.label; + } + + public String toString(){ + return this.getLabel(); + } + + public static Language getByCode(String code){ + if (codeMap.containsKey(code)) { + return codeMap.get(code); + } else { + return Language.NOTSET; + } + } +} diff --git a/app/src/main/java/cy/agorise/crystalwallet/fragments/GeneralSettingsFragment.java b/app/src/main/java/cy/agorise/crystalwallet/fragments/GeneralSettingsFragment.java index e91e848..ccf99d3 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/fragments/GeneralSettingsFragment.java +++ b/app/src/main/java/cy/agorise/crystalwallet/fragments/GeneralSettingsFragment.java @@ -3,9 +3,14 @@ package cy.agorise.crystalwallet.fragments; import android.arch.lifecycle.LiveData; import android.arch.lifecycle.Observer; import android.arch.lifecycle.ViewModelProviders; +import android.content.Intent; +import android.content.res.Configuration; +import android.content.res.Resources; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; +import android.support.v7.widget.ThemedSpinnerAdapter; +import android.util.DisplayMetrics; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -23,9 +28,13 @@ import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnItemSelected; import cy.agorise.crystalwallet.R; +import cy.agorise.crystalwallet.dao.CrystalDatabase; +import cy.agorise.crystalwallet.enums.Language; import cy.agorise.crystalwallet.models.GeneralSetting; import cy.agorise.crystalwallet.viewmodels.GeneralSettingListViewModel; + + /** * Created by xd on 12/28/17. */ @@ -36,10 +45,15 @@ public class GeneralSettingsFragment extends Fragment { private GeneralSettingListViewModel generalSettingListViewModel; private LiveData> generalSettingListLiveData; + private Boolean spPreferredLanguageInitialized; + @BindView (R.id.spTaxableCountry) Spinner spTaxableCountry; + @BindView (R.id.spPreferredLanguage) + Spinner spPreferredLanguage; public GeneralSettingsFragment() { + this.spPreferredLanguageInitialized = false; // Required empty public constructor } @@ -47,6 +61,8 @@ public class GeneralSettingsFragment extends Fragment { GeneralSettingsFragment fragment = new GeneralSettingsFragment(); Bundle args = new Bundle(); fragment.setArguments(args); + fragment.spPreferredLanguageInitialized = false; + return fragment; } @@ -95,9 +111,17 @@ public class GeneralSettingsFragment extends Fragment { } }); + + return v; } + public void initPreferredLanguage(GeneralSetting preferredLanguageSetting){ + ArrayAdapter preferredLanguageAdapter = new ArrayAdapter(getContext(), android.R.layout.simple_spinner_item, Language.values()); + spPreferredLanguage.setAdapter(preferredLanguageAdapter); + spPreferredLanguage.setSelection(preferredLanguageAdapter.getPosition(Language.getByCode(preferredLanguageSetting.getValue()))); + } + public GeneralSetting getSetting(String name){ for (GeneralSetting generalSetting:this.generalSettingListLiveData.getValue()) { if (generalSetting.getName().equals(name)) { @@ -111,16 +135,16 @@ public class GeneralSettingsFragment extends Fragment { @OnItemSelected(R.id.spTaxableCountry) void onItemSelected(int position) { if (position != 0) { - GeneralSetting generalSettingCountryCode = this.getSetting(GeneralSetting.SETTING_NAME_PREFERED_COUNTRY); - GeneralSetting generalSettingCurrency = this.getSetting(GeneralSetting.SETTING_NAME_PREFERED_CURRENCY); + GeneralSetting generalSettingCountryCode = this.getSetting(GeneralSetting.SETTING_NAME_PREFERRED_COUNTRY); + GeneralSetting generalSettingCurrency = this.getSetting(GeneralSetting.SETTING_NAME_PREFERRED_CURRENCY); if (generalSettingCountryCode == null){ generalSettingCountryCode = new GeneralSetting(); - generalSettingCountryCode.setName(GeneralSetting.SETTING_NAME_PREFERED_COUNTRY); + generalSettingCountryCode.setName(GeneralSetting.SETTING_NAME_PREFERRED_COUNTRY); } if (generalSettingCurrency == null){ generalSettingCurrency = new GeneralSetting(); - generalSettingCurrency.setName(GeneralSetting.SETTING_NAME_PREFERED_CURRENCY); + generalSettingCurrency.setName(GeneralSetting.SETTING_NAME_PREFERRED_CURRENCY); } String countryCode = countriesMap.get((String) spTaxableCountry.getSelectedItem()); @@ -133,11 +157,46 @@ public class GeneralSettingsFragment extends Fragment { } } + @OnItemSelected(R.id.spPreferredLanguage) + void onPreferredLanguageSelected(int position){ + //The first call will be when the spinner gets an adapter attached + if (this.spPreferredLanguageInitialized) { + Language languageSelected = (Language) this.spPreferredLanguage.getSelectedItem(); + GeneralSetting generalSettingPreferredLanguage = this.getSetting(GeneralSetting.SETTING_NAME_PREFERRED_LANGUAGE); + + if (generalSettingPreferredLanguage == null) { + generalSettingPreferredLanguage = new GeneralSetting(); + generalSettingPreferredLanguage.setName(GeneralSetting.SETTING_NAME_PREFERRED_LANGUAGE); + } + + if (!generalSettingPreferredLanguage.getValue().equals(languageSelected.getCode())) { + generalSettingPreferredLanguage.setValue(languageSelected.getCode()); + this.generalSettingListViewModel.saveGeneralSettings(generalSettingPreferredLanguage); + + Resources resources = getContext().getResources(); + Locale locale = new Locale(languageSelected.getCode()); + Locale.setDefault(locale); + DisplayMetrics dm = resources.getDisplayMetrics(); + Configuration configuration = resources.getConfiguration(); + configuration.locale = locale; + resources.updateConfiguration(configuration, dm); + Intent i = getContext().getPackageManager() + .getLaunchIntentForPackage(getContext().getPackageName()); + i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); + startActivity(i); + } + } else { + this.spPreferredLanguageInitialized = true; + } + } + public void loadSettings(List generalSettings){ for (GeneralSetting generalSetting:generalSettings) { - if (generalSetting.getName().equals(GeneralSetting.SETTING_NAME_PREFERED_COUNTRY)){ + if (generalSetting.getName().equals(GeneralSetting.SETTING_NAME_PREFERRED_COUNTRY)){ String preferedCountryCode = generalSetting.getValue(); spTaxableCountry.setSelection(((ArrayAdapter)spTaxableCountry.getAdapter()).getPosition(countriesMap.get(preferedCountryCode))); + } else if (generalSetting.getName().equals(GeneralSetting.SETTING_NAME_PREFERRED_LANGUAGE)){ + initPreferredLanguage(generalSetting); } } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/models/GeneralSetting.java b/app/src/main/java/cy/agorise/crystalwallet/models/GeneralSetting.java index 822a10e..d7ff800 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/models/GeneralSetting.java +++ b/app/src/main/java/cy/agorise/crystalwallet/models/GeneralSetting.java @@ -15,8 +15,9 @@ import cy.agorise.crystalwallet.enums.SeedType; @Entity(tableName = "general_setting") public class GeneralSetting { - public final static String SETTING_NAME_PREFERED_COUNTRY = "PREFERED_COUNTRY"; - public final static String SETTING_NAME_PREFERED_CURRENCY = "PREFERED_CURRENCY"; + public final static String SETTING_NAME_PREFERRED_COUNTRY = "PREFERRED_COUNTRY"; + public final static String SETTING_NAME_PREFERRED_CURRENCY = "PREFERRED_CURRENCY"; + public final static String SETTING_NAME_PREFERRED_LANGUAGE = "PREFERRED_LANGUAGE"; public final static String SETTING_PASSWORD = "PASSWORD"; /** diff --git a/app/src/main/java/cy/agorise/crystalwallet/service/CrystalWalletService.java b/app/src/main/java/cy/agorise/crystalwallet/service/CrystalWalletService.java index 93b89a5..01a80bd 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/service/CrystalWalletService.java +++ b/app/src/main/java/cy/agorise/crystalwallet/service/CrystalWalletService.java @@ -68,7 +68,7 @@ public class CrystalWalletService extends LifecycleService { //getting the preferred currency of the user final LiveData preferredCurrencySetting = - CrystalDatabase.getAppDatabase(service).generalSettingDao().getByName(GeneralSetting.SETTING_NAME_PREFERED_CURRENCY); + CrystalDatabase.getAppDatabase(service).generalSettingDao().getByName(GeneralSetting.SETTING_NAME_PREFERRED_CURRENCY); preferredCurrencySetting.observe(service, new Observer() { @Override diff --git a/app/src/main/java/cy/agorise/crystalwallet/views/CryptoCoinBalanceViewHolder.java b/app/src/main/java/cy/agorise/crystalwallet/views/CryptoCoinBalanceViewHolder.java index aee2bdc..954933b 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/views/CryptoCoinBalanceViewHolder.java +++ b/app/src/main/java/cy/agorise/crystalwallet/views/CryptoCoinBalanceViewHolder.java @@ -69,7 +69,7 @@ public class CryptoCoinBalanceViewHolder extends RecyclerView.ViewHolder { this.clear(); } else { //Retrieves the preferred currency selected by the user - LiveData preferedCurrencySetting = CrystalDatabase.getAppDatabase(this.context).generalSettingDao().getByName(GeneralSetting.SETTING_NAME_PREFERED_CURRENCY); + LiveData preferedCurrencySetting = CrystalDatabase.getAppDatabase(this.context).generalSettingDao().getByName(GeneralSetting.SETTING_NAME_PREFERRED_CURRENCY); //Retrieves the currency of this balance final CryptoCurrency currencyFrom = CrystalDatabase.getAppDatabase(context).cryptoCurrencyDao().getById(balance.getCryptoCurrencyId()); //Sets the name and amount of the balance in the view