From 989d52b33de200253f1ea3bc2d7d91b8764079fd Mon Sep 17 00:00:00 2001 From: m2049r Date: Fri, 8 Nov 2024 12:01:35 +0100 Subject: [PATCH] Option to remember and reuse selected fiat (#967) --- app/build.gradle | 4 +- .../com/m2049r/xmrwallet/WalletFragment.java | 11 ++-- .../xmrwallet/util/StickyFiatHelper.java | 62 +++++++++++++++++++ .../xmrwallet/widget/ExchangeEditText.java | 4 +- .../m2049r/xmrwallet/widget/ExchangeView.java | 2 + app/src/main/res/values/strings.xml | 5 ++ app/src/main/res/xml/root_preferences.xml | 5 ++ 7 files changed, 83 insertions(+), 10 deletions(-) create mode 100644 app/src/main/java/com/m2049r/xmrwallet/util/StickyFiatHelper.java diff --git a/app/build.gradle b/app/build.gradle index 05db577b..bb2c4baf 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -8,8 +8,8 @@ android { compileSdk 35 minSdkVersion 21 targetSdkVersion 35 - versionCode 4104 - versionName "4.1.4 'Exolix'" + versionCode 4105 + versionName "4.1.5 'Exolix'" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" externalNativeBuild { cmake { diff --git a/app/src/main/java/com/m2049r/xmrwallet/WalletFragment.java b/app/src/main/java/com/m2049r/xmrwallet/WalletFragment.java index 2a4421ae..c499ed91 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/WalletFragment.java +++ b/app/src/main/java/com/m2049r/xmrwallet/WalletFragment.java @@ -51,6 +51,7 @@ import com.m2049r.xmrwallet.service.exchange.api.ExchangeCallback; import com.m2049r.xmrwallet.service.exchange.api.ExchangeRate; import com.m2049r.xmrwallet.util.Helper; import com.m2049r.xmrwallet.util.ServiceHelper; +import com.m2049r.xmrwallet.util.StickyFiatHelper; import com.m2049r.xmrwallet.util.ThemeHelper; import com.m2049r.xmrwallet.widget.Toolbar; @@ -131,6 +132,7 @@ public class WalletFragment extends Fragment ArrayAdapter spinnerAdapter = new ArrayAdapter<>(requireContext(), R.layout.item_spinner_balance, currencies); spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); sCurrency.setAdapter(spinnerAdapter); + StickyFiatHelper.setPreferredCurrencyPosition(sCurrency); bSend = view.findViewById(R.id.bSend); bReceive = view.findViewById(R.id.bReceive); @@ -182,6 +184,7 @@ public class WalletFragment extends Fragment sCurrency.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView parentView, View selectedItemView, int position, long id) { + StickyFiatHelper.setPreferredFiatSymbol(requireContext(), (String) sCurrency.getSelectedItem()); refreshBalance(); } @@ -315,13 +318,7 @@ public class WalletFragment extends Fragment balanceCurrency = Helper.BASE_CRYPTO; balanceRate = 1.0; } else { - int spinnerPosition = ((ArrayAdapter) sCurrency.getAdapter()).getPosition(exchangeRate.getQuoteCurrency()); - if (spinnerPosition < 0) { // requested currency not in list - Timber.e("Requested currency not in list %s", exchangeRate.getQuoteCurrency()); - sCurrency.setSelection(0, true); - } else { - sCurrency.setSelection(spinnerPosition, true); - } + StickyFiatHelper.setCurrencyPosition(sCurrency, exchangeRate.getQuoteCurrency()); balanceCurrency = exchangeRate.getQuoteCurrency(); balanceRate = exchangeRate.getRate(); } diff --git a/app/src/main/java/com/m2049r/xmrwallet/util/StickyFiatHelper.java b/app/src/main/java/com/m2049r/xmrwallet/util/StickyFiatHelper.java new file mode 100644 index 00000000..e8665132 --- /dev/null +++ b/app/src/main/java/com/m2049r/xmrwallet/util/StickyFiatHelper.java @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2024 m2049r + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.m2049r.xmrwallet.util; + +import android.content.Context; +import android.widget.ArrayAdapter; +import android.widget.Spinner; + +import androidx.preference.PreferenceManager; + +import com.m2049r.xmrwallet.R; + +public class StickyFiatHelper { + + public static String getPreferredFiatSymbol(Context ctx) { + if (PreferenceManager.getDefaultSharedPreferences(ctx) + .getBoolean(ctx.getString(R.string.preferred_stickyfiat), false)) { + return PreferenceManager.getDefaultSharedPreferences(ctx) + .getString(ctx.getString(R.string.preferred_stickyfiat_pref), Helper.BASE_CRYPTO); + } + return null; + } + + public static void setPreferredFiatSymbol(Context ctx, String symbol) { + if (PreferenceManager.getDefaultSharedPreferences(ctx) + .getBoolean(ctx.getString(R.string.preferred_stickyfiat), false)) { + PreferenceManager.getDefaultSharedPreferences(ctx).edit() + .putString(ctx.getString(R.string.preferred_stickyfiat_pref), symbol).apply(); + } + } + + + public static void setPreferredCurrencyPosition(Spinner spinner) { + String stickyFiat = StickyFiatHelper.getPreferredFiatSymbol(spinner.getContext()); + if (stickyFiat != null) { + StickyFiatHelper.setCurrencyPosition(spinner, stickyFiat); + } + } + + public static void setCurrencyPosition(Spinner spinner, String symbol) { + int spinnerPosition = ((ArrayAdapter) spinner.getAdapter()).getPosition(symbol); + if (spinnerPosition < 0) { // requested currency not in list + spinner.setSelection(0, true); + } else { + spinner.setSelection(spinnerPosition, true); + } + } +} diff --git a/app/src/main/java/com/m2049r/xmrwallet/widget/ExchangeEditText.java b/app/src/main/java/com/m2049r/xmrwallet/widget/ExchangeEditText.java index be32e270..34caddd1 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/widget/ExchangeEditText.java +++ b/app/src/main/java/com/m2049r/xmrwallet/widget/ExchangeEditText.java @@ -41,6 +41,7 @@ import com.m2049r.xmrwallet.service.exchange.api.ExchangeCallback; import com.m2049r.xmrwallet.service.exchange.api.ExchangeRate; import com.m2049r.xmrwallet.util.Helper; import com.m2049r.xmrwallet.util.ServiceHelper; +import com.m2049r.xmrwallet.util.StickyFiatHelper; import com.m2049r.xmrwallet.util.ThemeHelper; import java.util.ArrayList; @@ -181,7 +182,8 @@ public class ExchangeEditText extends LinearLayout { } void setInitialSpinnerSelections(Spinner baseSpinner, Spinner quoteSpinner) { - baseSpinner.setSelection(0, true); + //baseSpinner.setSelection(0, true); + StickyFiatHelper.setPreferredCurrencyPosition(baseSpinner); quoteSpinner.setSelection(0, true); } diff --git a/app/src/main/java/com/m2049r/xmrwallet/widget/ExchangeView.java b/app/src/main/java/com/m2049r/xmrwallet/widget/ExchangeView.java index a574f328..174ff58a 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/widget/ExchangeView.java +++ b/app/src/main/java/com/m2049r/xmrwallet/widget/ExchangeView.java @@ -44,6 +44,7 @@ import com.m2049r.xmrwallet.service.exchange.api.ExchangeCallback; import com.m2049r.xmrwallet.service.exchange.api.ExchangeRate; import com.m2049r.xmrwallet.util.Helper; import com.m2049r.xmrwallet.util.ServiceHelper; +import com.m2049r.xmrwallet.util.StickyFiatHelper; import com.m2049r.xmrwallet.util.ThemeHelper; import java.util.ArrayList; @@ -174,6 +175,7 @@ public class ExchangeView extends LinearLayout { pbExchange = findViewById(R.id.pbExchange); setCurrencyAdapter(sCurrencyA); + StickyFiatHelper.setPreferredCurrencyPosition(sCurrencyA); setCurrencyAdapter(sCurrencyB); // make progress circle gray diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8d2e7c84..783b2955 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -461,6 +461,7 @@ preferred_nightmode preferred_locale preferred_lock + preferred_stickyfiat about_info privacy_info credits_info @@ -510,4 +511,8 @@ Please connect Sidekick device Restore from Sidekick Sidekick Connected + + Sticky Fiat + Remember last used fiat currency + preferred_stickyfiat_pref diff --git a/app/src/main/res/xml/root_preferences.xml b/app/src/main/res/xml/root_preferences.xml index f5949b50..d8600268 100644 --- a/app/src/main/res/xml/root_preferences.xml +++ b/app/src/main/res/xml/root_preferences.xml @@ -24,6 +24,11 @@ android:defaultValue="false" android:key="@string/preferred_lock" android:title="@string/setting_lock" /> +