From a8678114adf97647d336768f32f14adf70892d60 Mon Sep 17 00:00:00 2001 From: pokkst Date: Wed, 16 Nov 2022 13:27:14 -0600 Subject: [PATCH] Implement street mode --- .../wallet/adapter/CoinsInfoAdapter.java | 7 ++- .../dialog/SendBottomSheetDialog.java | 9 ++-- .../wallet/fragment/home/HomeFragment.java | 19 ++++---- .../fragment/settings/SettingsFragment.java | 8 ++++ .../transaction/TransactionFragment.java | 8 +++- .../net/mynero/wallet/model/BalanceInfo.java | 48 +++++++++++++++++++ .../mynero/wallet/service/BalanceService.java | 14 +++--- .../net/mynero/wallet/util/Constants.java | 3 ++ app/src/main/res/layout/fragment_settings.xml | 23 ++++++++- app/src/main/res/values/strings.xml | 1 + 10 files changed, 114 insertions(+), 26 deletions(-) create mode 100644 app/src/main/java/net/mynero/wallet/model/BalanceInfo.java diff --git a/app/src/main/java/net/mynero/wallet/adapter/CoinsInfoAdapter.java b/app/src/main/java/net/mynero/wallet/adapter/CoinsInfoAdapter.java index 69e1a60..44124d7 100644 --- a/app/src/main/java/net/mynero/wallet/adapter/CoinsInfoAdapter.java +++ b/app/src/main/java/net/mynero/wallet/adapter/CoinsInfoAdapter.java @@ -27,6 +27,8 @@ import androidx.recyclerview.widget.RecyclerView; import net.mynero.wallet.R; import net.mynero.wallet.model.CoinsInfo; import net.mynero.wallet.model.Wallet; +import net.mynero.wallet.service.PrefService; +import net.mynero.wallet.util.Constants; import java.util.ArrayList; import java.util.List; @@ -102,8 +104,9 @@ public class CoinsInfoAdapter extends RecyclerView.Adapter _sendingMax = new MutableLiveData<>(false); @@ -136,11 +134,10 @@ public class SendBottomSheetDialog extends BottomSheetDialogFragment { } private void bindObservers() { - - BalanceService.getInstance().balance.observe(getViewLifecycleOwner(), balance -> { - createButton.setEnabled(balance != 0); + BalanceService.getInstance().balanceInfo.observe(getViewLifecycleOwner(), balanceInfo -> { + createButton.setEnabled(balanceInfo.getRawUnlocked() != 0); if(!isChurning) { - sendMaxButton.setEnabled(balance != 0); + sendMaxButton.setEnabled(balanceInfo.getRawUnlocked() != 0); } }); diff --git a/app/src/main/java/net/mynero/wallet/fragment/home/HomeFragment.java b/app/src/main/java/net/mynero/wallet/fragment/home/HomeFragment.java index ee2ef4d..4c763a9 100644 --- a/app/src/main/java/net/mynero/wallet/fragment/home/HomeFragment.java +++ b/app/src/main/java/net/mynero/wallet/fragment/home/HomeFragment.java @@ -32,6 +32,7 @@ import net.mynero.wallet.model.WalletManager; import net.mynero.wallet.service.BalanceService; import net.mynero.wallet.service.BlockchainService; import net.mynero.wallet.service.HistoryService; +import net.mynero.wallet.util.Constants; import java.util.Collections; @@ -91,16 +92,16 @@ public class HomeFragment extends Fragment implements TransactionInfoAdapter.TxI BlockchainService blockchainService = BlockchainService.getInstance(); if (balanceService != null) { - balanceService.balance.observe(getViewLifecycleOwner(), balance -> { - unlockedBalanceTextView.setText(getString(R.string.wallet_balance_text, Wallet.getDisplayAmount(balance))); - }); + balanceService.balanceInfo.observe(getViewLifecycleOwner(), balanceInfo -> { + if(balanceInfo != null) { + unlockedBalanceTextView.setText(getString(R.string.wallet_balance_text, balanceInfo.getUnlockedDisplay())); - balanceService.lockedBalance.observe(getViewLifecycleOwner(), lockedBalance -> { - if (lockedBalance == 0) { - lockedBalanceTextView.setVisibility(View.INVISIBLE); - } else { - lockedBalanceTextView.setText(getString(R.string.wallet_locked_balance_text, Wallet.getDisplayAmount(lockedBalance))); - lockedBalanceTextView.setVisibility(View.VISIBLE); + if (balanceInfo.getLockedDisplay().equals(Constants.STREET_MODE_BALANCE) || balanceInfo.isLockedBalanceZero()) { + lockedBalanceTextView.setVisibility(View.INVISIBLE); + } else { + lockedBalanceTextView.setText(getString(R.string.wallet_locked_balance_text, balanceInfo.getLockedDisplay())); + lockedBalanceTextView.setVisibility(View.VISIBLE); + } } }); } diff --git a/app/src/main/java/net/mynero/wallet/fragment/settings/SettingsFragment.java b/app/src/main/java/net/mynero/wallet/fragment/settings/SettingsFragment.java index c32bfc7..172e765 100644 --- a/app/src/main/java/net/mynero/wallet/fragment/settings/SettingsFragment.java +++ b/app/src/main/java/net/mynero/wallet/fragment/settings/SettingsFragment.java @@ -33,6 +33,7 @@ import net.mynero.wallet.fragment.dialog.NodeSelectionBottomSheetDialog; import net.mynero.wallet.fragment.dialog.PasswordBottomSheetDialog; import net.mynero.wallet.model.Wallet; import net.mynero.wallet.model.WalletManager; +import net.mynero.wallet.service.BalanceService; import net.mynero.wallet.service.BlockchainService; import net.mynero.wallet.service.PrefService; import net.mynero.wallet.util.Constants; @@ -97,6 +98,7 @@ public class SettingsFragment extends Fragment implements PasswordBottomSheetDia selectNodeButton = view.findViewById(R.id.select_node_button); SwitchCompat nightModeSwitch = view.findViewById(R.id.day_night_switch); + SwitchCompat streetModeSwitch = view.findViewById(R.id.street_mode_switch); SwitchCompat torSwitch = view.findViewById(R.id.tor_switch); ConstraintLayout proxySettingsLayout = view.findViewById(R.id.wallet_proxy_settings_layout); walletProxyAddressEditText = view.findViewById(R.id.wallet_proxy_address_edittext); @@ -111,6 +113,12 @@ public class SettingsFragment extends Fragment implements PasswordBottomSheetDia } }); + streetModeSwitch.setChecked(PrefService.getInstance().getBoolean(Constants.PREF_STREET_MODE, false)); + streetModeSwitch.setOnCheckedChangeListener((compoundButton, b) -> { + PrefService.getInstance().edit().putBoolean(Constants.PREF_STREET_MODE, b).apply(); + BalanceService.getInstance().refreshBalance(); + }); + boolean usesProxy = PrefService.getInstance().getBoolean(Constants.PREF_USES_TOR, false); String proxy = PrefService.getInstance().getString(Constants.PREF_PROXY, ""); if (proxy.contains(":")) { diff --git a/app/src/main/java/net/mynero/wallet/fragment/transaction/TransactionFragment.java b/app/src/main/java/net/mynero/wallet/fragment/transaction/TransactionFragment.java index b906a6e..8a7c6b0 100644 --- a/app/src/main/java/net/mynero/wallet/fragment/transaction/TransactionFragment.java +++ b/app/src/main/java/net/mynero/wallet/fragment/transaction/TransactionFragment.java @@ -18,6 +18,8 @@ import androidx.lifecycle.ViewModelProvider; import net.mynero.wallet.R; import net.mynero.wallet.model.TransactionInfo; +import net.mynero.wallet.model.Wallet; +import net.mynero.wallet.service.PrefService; import net.mynero.wallet.util.Constants; import net.mynero.wallet.util.Helper; @@ -100,12 +102,14 @@ public class TransactionFragment extends Fragment { Context ctx = getContext(); if(ctx != null) { + boolean streetModeEnabled = PrefService.getInstance().getBoolean(Constants.PREF_STREET_MODE, false); + String balanceString = streetModeEnabled ? Constants.STREET_MODE_BALANCE : Wallet.getDisplayAmount(transactionInfo.amount); if (transactionInfo.direction == TransactionInfo.Direction.Direction_In) { txAmountTextView.setTextColor(ContextCompat.getColor(ctx, R.color.oled_positiveColor)); - txAmountTextView.setText(getString(R.string.tx_list_amount_positive, Helper.getDisplayAmount(transactionInfo.amount))); + txAmountTextView.setText(getString(R.string.tx_list_amount_positive, balanceString)); } else { txAmountTextView.setTextColor(ContextCompat.getColor(ctx, R.color.oled_negativeColor)); - txAmountTextView.setText(getString(R.string.tx_list_amount_negative, Helper.getDisplayAmount(transactionInfo.amount))); + txAmountTextView.setText(getString(R.string.tx_list_amount_negative, balanceString)); } } }); diff --git a/app/src/main/java/net/mynero/wallet/model/BalanceInfo.java b/app/src/main/java/net/mynero/wallet/model/BalanceInfo.java new file mode 100644 index 0000000..a815c7b --- /dev/null +++ b/app/src/main/java/net/mynero/wallet/model/BalanceInfo.java @@ -0,0 +1,48 @@ +package net.mynero.wallet.model; + +import net.mynero.wallet.service.PrefService; +import net.mynero.wallet.util.Constants; + +public class BalanceInfo { + private final long rawUnlocked; + private final long rawLocked; + + public BalanceInfo(long rawUnlocked, long rawLocked) { + this.rawUnlocked = rawUnlocked; + this.rawLocked = rawLocked; + } + + public long getRawLocked() { + return rawLocked; + } + + public long getRawUnlocked() { + return rawUnlocked; + } + + public boolean isUnlockedBalanceZero() { + return rawUnlocked == 0; + } + + public boolean isLockedBalanceZero() { + return rawLocked == 0; + } + + public String getUnlockedDisplay() { + boolean streetModeEnabled = PrefService.getInstance().getBoolean(Constants.PREF_STREET_MODE, false); + if(streetModeEnabled) { + return Constants.STREET_MODE_BALANCE; + } else { + return Wallet.getDisplayAmount(rawUnlocked); + } + } + + public String getLockedDisplay() { + boolean streetModeEnabled = PrefService.getInstance().getBoolean(Constants.PREF_STREET_MODE, false); + if(streetModeEnabled) { + return Constants.STREET_MODE_BALANCE; + } else { + return Wallet.getDisplayAmount(rawLocked); + } + } +} \ No newline at end of file diff --git a/app/src/main/java/net/mynero/wallet/service/BalanceService.java b/app/src/main/java/net/mynero/wallet/service/BalanceService.java index ac4a9fb..8025eb9 100644 --- a/app/src/main/java/net/mynero/wallet/service/BalanceService.java +++ b/app/src/main/java/net/mynero/wallet/service/BalanceService.java @@ -3,14 +3,15 @@ package net.mynero.wallet.service; import androidx.lifecycle.LiveData; import androidx.lifecycle.MutableLiveData; +import net.mynero.wallet.model.BalanceInfo; +import net.mynero.wallet.model.Wallet; import net.mynero.wallet.model.WalletManager; +import net.mynero.wallet.util.Constants; public class BalanceService extends ServiceBase { public static BalanceService instance = null; - private final MutableLiveData _balance = new MutableLiveData<>(0L); - private final MutableLiveData _lockedBalance = new MutableLiveData<>(0L); - public LiveData balance = _balance; - public LiveData lockedBalance = _lockedBalance; + private final MutableLiveData _balanceInfo = new MutableLiveData<>(null); + public LiveData balanceInfo = _balanceInfo; public BalanceService(MoneroHandlerThread thread) { super(thread); @@ -22,8 +23,9 @@ public class BalanceService extends ServiceBase { } public void refreshBalance() { - _balance.postValue(getUnlockedBalanceRaw()); - _lockedBalance.postValue(getLockedBalanceRaw()); + long rawUnlocked = getUnlockedBalanceRaw(); + long rawLocked = getLockedBalanceRaw(); + _balanceInfo.postValue(new BalanceInfo(rawUnlocked, rawLocked)); } public long getUnlockedBalanceRaw() { diff --git a/app/src/main/java/net/mynero/wallet/util/Constants.java b/app/src/main/java/net/mynero/wallet/util/Constants.java index 2c285a1..3d68df7 100644 --- a/app/src/main/java/net/mynero/wallet/util/Constants.java +++ b/app/src/main/java/net/mynero/wallet/util/Constants.java @@ -10,9 +10,12 @@ public class Constants { public static final String PREF_NODE_2 = "pref_node_2"; public static final String PREF_CUSTOM_NODES = "pref_custom_nodes"; public static final String PREF_USES_OFFSET = "pref_uses_offset"; + public static final String PREF_STREET_MODE = "pref_street_mode"; public static final String URI_PREFIX = "monero:"; public static final String URI_ARG_AMOUNT = "tx_amount"; public static final String URI_ARG_AMOUNT2 = "amount"; public static final String NAV_ARG_TXINFO = "nav_arg_txinfo"; + + public static final String STREET_MODE_BALANCE = "#.############"; } diff --git a/app/src/main/res/layout/fragment_settings.xml b/app/src/main/res/layout/fragment_settings.xml index ae615fa..6cee5c6 100644 --- a/app/src/main/res/layout/fragment_settings.xml +++ b/app/src/main/res/layout/fragment_settings.xml @@ -112,6 +112,27 @@ app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toBottomOf="@id/appearance_settings_textview" /> + + + + + app:layout_constraintTop_toBottomOf="@id/street_mode_switch" />