Option to remember and reuse selected fiat (#967)

This commit is contained in:
m2049r 2024-11-08 12:01:35 +01:00 committed by GitHub
parent a3c0ca7ebe
commit 989d52b33d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 83 additions and 10 deletions

View File

@ -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 {

View File

@ -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<String> 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();
}

View File

@ -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);
}
}
}

View File

@ -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);
}

View File

@ -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

View File

@ -461,6 +461,7 @@
<string name="preferred_nightmode" translatable="false">preferred_nightmode</string>
<string name="preferred_locale" translatable="false">preferred_locale</string>
<string name="preferred_lock" translatable="false">preferred_lock</string>
<string name="preferred_stickyfiat" translatable="false">preferred_stickyfiat</string>
<string name="about_info" translatable="false">about_info</string>
<string name="privacy_info" translatable="false">privacy_info</string>
<string name="credits_info" translatable="false">credits_info</string>
@ -510,4 +511,8 @@
<string name="open_wallet_sidekick_missing">Please connect Sidekick device</string>
<string name="fab_restore_sidekick">Restore from Sidekick</string>
<string name="sidekick_connected">Sidekick Connected</string>
<string name="setting_stickyfiat">Sticky Fiat</string>
<string name="setting_stickyfiat_summary">Remember last used fiat currency</string>
<string name="preferred_stickyfiat_pref" translatable="false">preferred_stickyfiat_pref</string>
</resources>

View File

@ -24,6 +24,11 @@
android:defaultValue="false"
android:key="@string/preferred_lock"
android:title="@string/setting_lock" />
<SwitchPreference
android:defaultValue="false"
android:key="@string/preferred_stickyfiat"
android:summary="@string/setting_stickyfiat_summary"
android:title="@string/setting_stickyfiat" />
</PreferenceCategory>
<PreferenceCategory app:title="@string/title_info">
<Preference