From e2a453478c4d214d774ba1bdd94533c3a9e29497 Mon Sep 17 00:00:00 2001 From: Javier Varona Date: Wed, 21 Mar 2018 22:21:23 -0400 Subject: [PATCH] - Added timezone setting functionality --- .../fragments/GeneralSettingsFragment.java | 95 ++++++++++++++----- .../crystalwallet/models/GeneralSetting.java | 1 + .../GeneralSettingListViewModel.java | 4 + .../crystalwallet/views/TimeZoneAdapter.java | 73 ++++++++++++++ .../views/TransactionViewHolder.java | 18 +++- .../res/layout/fragment_general_settings.xml | 4 +- .../res/layout/time_zone_spinner_item.xml | 24 +++++ 7 files changed, 190 insertions(+), 29 deletions(-) create mode 100644 app/src/main/java/cy/agorise/crystalwallet/views/TimeZoneAdapter.java create mode 100644 app/src/main/res/layout/time_zone_spinner_item.xml 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 ccf99d3..f67ab5d 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/fragments/GeneralSettingsFragment.java +++ b/app/src/main/java/cy/agorise/crystalwallet/fragments/GeneralSettingsFragment.java @@ -18,11 +18,14 @@ import android.widget.ArrayAdapter; import android.widget.Spinner; import java.util.ArrayList; +import java.util.Calendar; import java.util.Collections; import java.util.Currency; import java.util.HashMap; import java.util.List; import java.util.Locale; +import java.util.TimeZone; +import java.util.concurrent.TimeUnit; import butterknife.BindView; import butterknife.ButterKnife; @@ -32,7 +35,7 @@ import cy.agorise.crystalwallet.dao.CrystalDatabase; import cy.agorise.crystalwallet.enums.Language; import cy.agorise.crystalwallet.models.GeneralSetting; import cy.agorise.crystalwallet.viewmodels.GeneralSettingListViewModel; - +import cy.agorise.crystalwallet.views.TimeZoneAdapter; /** @@ -46,14 +49,18 @@ public class GeneralSettingsFragment extends Fragment { private LiveData> generalSettingListLiveData; private Boolean spPreferredLanguageInitialized; + private Boolean spTimeZoneInitialized; @BindView (R.id.spTaxableCountry) Spinner spTaxableCountry; @BindView (R.id.spPreferredLanguage) Spinner spPreferredLanguage; + @BindView (R.id.spDisplayDateTime) + Spinner spDisplayDateTime; public GeneralSettingsFragment() { this.spPreferredLanguageInitialized = false; + this.spTimeZoneInitialized = false; // Required empty public constructor } @@ -62,7 +69,7 @@ public class GeneralSettingsFragment extends Fragment { Bundle args = new Bundle(); fragment.setArguments(args); fragment.spPreferredLanguageInitialized = false; - + fragment.spTimeZoneInitialized = false; return fragment; } @@ -81,7 +88,22 @@ public class GeneralSettingsFragment extends Fragment { generalSettingListViewModel = ViewModelProviders.of(this).get(GeneralSettingListViewModel.class); generalSettingListLiveData = generalSettingListViewModel.getGeneralSettingList(); - // Initializes the countries spinner + + + //Observes the general settings data + generalSettingListLiveData.observe(this, new Observer>() { + @Override + public void onChanged(@Nullable List generalSettings) { + loadSettings(generalSettings); + } + }); + + + + return v; + } + + public void initPreferredCountry(GeneralSetting preferredCountrySetting){ countriesMap = new HashMap(); String[] countryCodeList = Locale.getISOCountries(); ArrayList countryAndCurrencyList = new ArrayList(); @@ -103,23 +125,31 @@ public class GeneralSettingsFragment extends Fragment { ArrayAdapter countryAdapter = new ArrayAdapter(this.getContext(), android.R.layout.simple_spinner_item, countryAndCurrencyList); spTaxableCountry.setAdapter(countryAdapter); - //Observes the general settings data - generalSettingListLiveData.observe(this, new Observer>() { - @Override - public void onChanged(@Nullable List generalSettings) { - loadSettings(generalSettings); - } - }); - - - - return v; + if (preferredCountrySetting != null) { + String preferedCountryCode = preferredCountrySetting.getValue(); + spTaxableCountry.setSelection(((ArrayAdapter) spTaxableCountry.getAdapter()).getPosition(countriesMap.get(preferedCountryCode))); + } } 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()))); + if (preferredLanguageSetting != null) { + spPreferredLanguage.setSelection(preferredLanguageAdapter.getPosition(Language.getByCode(preferredLanguageSetting.getValue()))); + } + } + + public void initDateTimeFormat(GeneralSetting dateTimeFormatSetting){ + TimeZoneAdapter timeZoneAdapter; + if (spDisplayDateTime.getAdapter() == null) { + timeZoneAdapter = new TimeZoneAdapter(getContext(), android.R.layout.simple_spinner_dropdown_item); + spDisplayDateTime.setAdapter(timeZoneAdapter); + } else { + timeZoneAdapter = (TimeZoneAdapter) spDisplayDateTime.getAdapter(); + } + if (dateTimeFormatSetting != null) { + spDisplayDateTime.setSelection(timeZoneAdapter.getPosition(dateTimeFormatSetting.getValue())); + } } public GeneralSetting getSetting(String name){ @@ -157,6 +187,27 @@ public class GeneralSettingsFragment extends Fragment { } } + @OnItemSelected(R.id.spDisplayDateTime) + void onTimeZoneSelected(int position){ + //The first call will be when the spinner gets an adapter attached + if (this.spTimeZoneInitialized) { + String timeZoneIdSelected = (String) this.spDisplayDateTime.getSelectedItem(); + GeneralSetting generalSettingTimeZone = this.getSetting(GeneralSetting.SETTING_NAME_TIME_ZONE); + + if (generalSettingTimeZone == null) { + generalSettingTimeZone = new GeneralSetting(); + generalSettingTimeZone.setName(GeneralSetting.SETTING_NAME_TIME_ZONE); + } + + if ((generalSettingTimeZone.getValue() == null)||(!generalSettingTimeZone.getValue().equals(timeZoneIdSelected))) { + generalSettingTimeZone.setValue(timeZoneIdSelected); + this.generalSettingListViewModel.saveGeneralSettings(generalSettingTimeZone); + } + } else { + this.spTimeZoneInitialized = true; + } + } + @OnItemSelected(R.id.spPreferredLanguage) void onPreferredLanguageSelected(int position){ //The first call will be when the spinner gets an adapter attached @@ -169,7 +220,7 @@ public class GeneralSettingsFragment extends Fragment { generalSettingPreferredLanguage.setName(GeneralSetting.SETTING_NAME_PREFERRED_LANGUAGE); } - if (!generalSettingPreferredLanguage.getValue().equals(languageSelected.getCode())) { + if ((generalSettingPreferredLanguage.getValue() == null)||(!generalSettingPreferredLanguage.getValue().equals(languageSelected.getCode()))) { generalSettingPreferredLanguage.setValue(languageSelected.getCode()); this.generalSettingListViewModel.saveGeneralSettings(generalSettingPreferredLanguage); @@ -191,13 +242,9 @@ public class GeneralSettingsFragment extends Fragment { } public void loadSettings(List generalSettings){ - for (GeneralSetting generalSetting:generalSettings) { - 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); - } - } + + initPreferredCountry(getSetting(GeneralSetting.SETTING_NAME_PREFERRED_COUNTRY)); + initPreferredLanguage(getSetting(GeneralSetting.SETTING_NAME_PREFERRED_LANGUAGE)); + initDateTimeFormat(getSetting(GeneralSetting.SETTING_NAME_TIME_ZONE)); } } 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 d7ff800..b990e68 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/models/GeneralSetting.java +++ b/app/src/main/java/cy/agorise/crystalwallet/models/GeneralSetting.java @@ -18,6 +18,7 @@ public class GeneralSetting { 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_NAME_TIME_ZONE = "TIME_ZONE"; public final static String SETTING_PASSWORD = "PASSWORD"; /** diff --git a/app/src/main/java/cy/agorise/crystalwallet/viewmodels/GeneralSettingListViewModel.java b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/GeneralSettingListViewModel.java index 492c1b3..ccea8d7 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/viewmodels/GeneralSettingListViewModel.java +++ b/app/src/main/java/cy/agorise/crystalwallet/viewmodels/GeneralSettingListViewModel.java @@ -37,4 +37,8 @@ public class GeneralSettingListViewModel extends AndroidViewModel { public void saveGeneralSettings(GeneralSetting... generalSettings){ this.db.generalSettingDao().insertGeneralSettings(generalSettings); } + + public GeneralSetting getGeneralSettingByName(String name){ + return this.db.generalSettingDao().getSettingByName(name); + } } diff --git a/app/src/main/java/cy/agorise/crystalwallet/views/TimeZoneAdapter.java b/app/src/main/java/cy/agorise/crystalwallet/views/TimeZoneAdapter.java new file mode 100644 index 0000000..e6c8e69 --- /dev/null +++ b/app/src/main/java/cy/agorise/crystalwallet/views/TimeZoneAdapter.java @@ -0,0 +1,73 @@ +package cy.agorise.crystalwallet.views; + +import android.content.Context; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.TextView; + +import java.util.Calendar; +import java.util.TimeZone; +import java.util.concurrent.TimeUnit; + +import cy.agorise.crystalwallet.R; + +/** + * Created by Henry Varona on 21/3/2018. + */ + +public class TimeZoneAdapter extends ArrayAdapter { + + LayoutInflater inflater; + String[] timeZoneIds; + + public TimeZoneAdapter(@NonNull Context context, int resource) { + super(context, resource); + + this.inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); + this.timeZoneIds = TimeZone.getAvailableIDs(); + } + + @Override + public int getCount() { + return this.timeZoneIds.length; + } + + @Override + public int getPosition(@Nullable String item) { + for (int i=0;i 0?"+"+hours:""+hours); + + View timeZoneItemView = this.inflater.inflate(R.layout.time_zone_spinner_item,null,true); + + TextView tvTimeZoneLabel = (TextView) timeZoneItemView.findViewById(R.id.tvTimeZoneLabel); + tvTimeZoneLabel.setText(String.format("%s (GMT%s:%02d)", nextTimeZone.getID(), hoursString, minutes)); + return timeZoneItemView; + } +} diff --git a/app/src/main/java/cy/agorise/crystalwallet/views/TransactionViewHolder.java b/app/src/main/java/cy/agorise/crystalwallet/views/TransactionViewHolder.java index ded083f..ad10bc9 100644 --- a/app/src/main/java/cy/agorise/crystalwallet/views/TransactionViewHolder.java +++ b/app/src/main/java/cy/agorise/crystalwallet/views/TransactionViewHolder.java @@ -23,8 +23,10 @@ import cy.agorise.crystalwallet.activities.CryptoCoinTransactionReceiptActivity; import cy.agorise.crystalwallet.models.CryptoCoinTransaction; import cy.agorise.crystalwallet.models.CryptoCurrency; import cy.agorise.crystalwallet.models.CryptoNetAccount; +import cy.agorise.crystalwallet.models.GeneralSetting; import cy.agorise.crystalwallet.viewmodels.CryptoCurrencyViewModel; import cy.agorise.crystalwallet.viewmodels.CryptoNetAccountViewModel; +import cy.agorise.crystalwallet.viewmodels.GeneralSettingListViewModel; /** * Created by Henry Varona on 17/9/2017. @@ -122,13 +124,23 @@ public class TransactionViewHolder extends RecyclerView.ViewHolder { String amountString = String.format("%.2f",transaction.getAmount()/Math.pow(10,cryptoCurrency.getPrecision())); + GeneralSettingListViewModel generalSettingListViewModel = ViewModelProviders.of(this.fragment).get(GeneralSettingListViewModel.class); + GeneralSetting timeZoneSetting = generalSettingListViewModel.getGeneralSettingByName(GeneralSetting.SETTING_NAME_TIME_ZONE); + + TimeZone userTimeZone; + if (timeZoneSetting != null){ + userTimeZone = TimeZone.getTimeZone(timeZoneSetting.getValue()); + } else { + userTimeZone = TimeZone.getTimeZone("cet"); + } + DateFormat dateFormat = new SimpleDateFormat("dd MMM"); - dateFormat.setTimeZone(TimeZone.getTimeZone("cet")); + dateFormat.setTimeZone(userTimeZone); DateFormat hourFormat = new SimpleDateFormat("HH:mm:ss"); - hourFormat.setTimeZone(TimeZone.getTimeZone("cet")); + hourFormat.setTimeZone(userTimeZone); tvTransactionDate.setText(dateFormat.format(transaction.getDate())); - tvTransactionHour.setText(hourFormat.format(transaction.getDate())+" CET"); + tvTransactionHour.setText(hourFormat.format(transaction.getDate())); tvFrom.setText(transaction.getFrom()); tvTo.setText(transaction.getTo()); diff --git a/app/src/main/res/layout/fragment_general_settings.xml b/app/src/main/res/layout/fragment_general_settings.xml index 257d0f2..7e30113 100644 --- a/app/src/main/res/layout/fragment_general_settings.xml +++ b/app/src/main/res/layout/fragment_general_settings.xml @@ -112,9 +112,9 @@ android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="8dp" - app:layout_constraintTop_toBottomOf="@id/tvDisplayDateTime" + app:layout_constraintEnd_toEndOf="@id/spPreferredLanguage" app:layout_constraintStart_toStartOf="@id/spPreferredLanguage" - app:layout_constraintEnd_toEndOf="@id/spPreferredLanguage"/> + app:layout_constraintTop_toBottomOf="@id/tvDisplayDateTime" /> + + + + + + + + + \ No newline at end of file