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

Option to remember and reuse selected fiat #967

Merged
merged 1 commit into from
Nov 8, 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
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 4 additions & 7 deletions app/src/main/java/com/m2049r/xmrwallet/WalletFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
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;

Expand Down Expand Up @@ -131,6 +132,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
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);
Expand Down Expand Up @@ -182,6 +184,7 @@ public void onDismissedBySwipeRight(RecyclerView recyclerView, int[] reverseSort
sCurrency.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
StickyFiatHelper.setPreferredFiatSymbol(requireContext(), (String) sCurrency.getSelectedItem());
refreshBalance();
}

Expand Down Expand Up @@ -315,13 +318,7 @@ public void exchange(final ExchangeRate exchangeRate) {
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();
}
Expand Down
62 changes: 62 additions & 0 deletions app/src/main/java/com/m2049r/xmrwallet/util/StickyFiatHelper.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
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;
Expand Down Expand Up @@ -181,7 +182,8 @@ protected void setCurrencyAdapter(Spinner spinner, List<String> currencies) {
}

void setInitialSpinnerSelections(Spinner baseSpinner, Spinner quoteSpinner) {
baseSpinner.setSelection(0, true);
//baseSpinner.setSelection(0, true);
StickyFiatHelper.setPreferredCurrencyPosition(baseSpinner);
quoteSpinner.setSelection(0, true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
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;
Expand Down Expand Up @@ -174,6 +175,7 @@ protected void onFinishInflate() {
pbExchange = findViewById(R.id.pbExchange);

setCurrencyAdapter(sCurrencyA);
StickyFiatHelper.setPreferredCurrencyPosition(sCurrencyA);
setCurrencyAdapter(sCurrencyB);

// make progress circle gray
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
5 changes: 5 additions & 0 deletions app/src/main/res/xml/root_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down