From 4bf9bf6ee2990e2e9be14c07939fb721f5f93ca4 Mon Sep 17 00:00:00 2001 From: pokkst Date: Mon, 18 Sep 2023 09:13:31 -0500 Subject: [PATCH] 0.4.4.6: Improve coin freezing --- app/build.gradle | 4 +- .../java/net/mynero/wallet/MainActivity.java | 2 +- .../wallet/adapter/CoinsInfoAdapter.java | 3 +- .../wallet/fragment/utxos/UtxosFragment.java | 2 +- .../mynero/wallet/service/BalanceService.java | 17 +++++- .../mynero/wallet/service/UTXOService.java | 55 +++++++++++++++++-- .../net/mynero/wallet/util/Constants.java | 1 + 7 files changed, 73 insertions(+), 11 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 9639933..ad5e657 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -9,8 +9,8 @@ android { applicationId "net.mynero.wallet" minSdkVersion 21 targetSdkVersion 34 - versionCode 40405 - versionName "0.4.4.5 'Fluorine Fermi'" + versionCode 40406 + versionName "0.4.4.6 'Fluorine Fermi'" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" externalNativeBuild { cmake { diff --git a/app/src/main/java/net/mynero/wallet/MainActivity.java b/app/src/main/java/net/mynero/wallet/MainActivity.java index 856fc05..0735a03 100644 --- a/app/src/main/java/net/mynero/wallet/MainActivity.java +++ b/app/src/main/java/net/mynero/wallet/MainActivity.java @@ -109,11 +109,11 @@ public class MainActivity extends AppCompatActivity implements MoneroHandlerThre @Override public void onRefresh() { + this.utxoService.refreshUtxos(); this.historyService.refreshHistory(); this.balanceService.refreshBalance(); this.blockchainService.refreshBlockchain(); this.addressService.refreshAddresses(); - this.utxoService.refreshUtxos(); } @Override 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 3005222..fc12a54 100644 --- a/app/src/main/java/net/mynero/wallet/adapter/CoinsInfoAdapter.java +++ b/app/src/main/java/net/mynero/wallet/adapter/CoinsInfoAdapter.java @@ -29,6 +29,7 @@ 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.service.UTXOService; import net.mynero.wallet.util.Constants; import java.util.ArrayList; @@ -150,7 +151,7 @@ public class CoinsInfoAdapter extends RecyclerView.Adapter> _utxos = new MutableLiveData<>(); public LiveData> utxos = _utxos; + private ArrayList frozenCoins = new ArrayList<>(); public UTXOService(MoneroHandlerThread thread) { super(thread); instance = this; + try { + this.loadFrozenCoins(); + } catch (JSONException e) { + throw new RuntimeException(e); + } } public static UTXOService getInstance() { @@ -31,19 +41,56 @@ public class UTXOService extends ServiceBase { } public void refreshUtxos() { - _utxos.postValue(getUtxos()); + _utxos.postValue(getUtxosInternal()); } public List getUtxos() { + List value = utxos.getValue(); + return value != null ? value : List.of(); + } + + private List getUtxosInternal() { return WalletManager.getInstance().getWallet().getCoins().getAll(); } public void toggleFrozen(HashMap selectedCoins) { - Coins coins = WalletManager.getInstance().getWallet().getCoins(); + ArrayList frozenCoinsCopy = new ArrayList<>(frozenCoins); for(CoinsInfo coin : selectedCoins.values()) { - coins.setFrozen(coin.getPubKey(), !coin.isFrozen()); + if(frozenCoinsCopy.contains(coin.getPubKey())) { + frozenCoinsCopy.remove(coin.getPubKey()); + } else { + frozenCoinsCopy.add(coin.getPubKey()); + } } + this.frozenCoins = frozenCoinsCopy; + this.saveFrozenCoins(); refreshUtxos(); + BalanceService.getInstance().refreshBalance(); + } + + public boolean isCoinFrozen(CoinsInfo coinsInfo) { + return frozenCoins.contains(coinsInfo.getPubKey()); + } + + private void loadFrozenCoins() throws JSONException { + PrefService prefService = PrefService.getInstance(); + String frozenCoinsArrayString = prefService.getString(Constants.PREF_FROZEN_COINS, "[]"); + JSONArray frozenCoinsArray = new JSONArray(frozenCoinsArrayString); + for(int i = 0; i < frozenCoinsArray.length(); i++) { + String pubKey = frozenCoinsArray.getString(i); + frozenCoins.add(pubKey); + } + this.refreshUtxos(); + } + + private void saveFrozenCoins() { + PrefService prefService = PrefService.getInstance(); + JSONArray jsonArray = new JSONArray(); + ArrayList frozenCoinsCopy = new ArrayList<>(frozenCoins); + for(String pubKey : frozenCoinsCopy) { + jsonArray.put(pubKey); + } + prefService.edit().putString(Constants.PREF_FROZEN_COINS, jsonArray.toString()).apply(); } public ArrayList selectUtxos(long amount, boolean sendAll) throws Exception { @@ -56,7 +103,7 @@ public class UTXOService extends ServiceBase { Collections.sort(utxos); //loop through each utxo for (CoinsInfo coinsInfo : utxos) { - if (!coinsInfo.isSpent() && coinsInfo.isUnlocked() && !coinsInfo.isFrozen()) { //filter out spent, locked, and frozen outputs + if (!coinsInfo.isSpent() && coinsInfo.isUnlocked() && !coinsInfo.isFrozen() && !frozenCoins.contains(coinsInfo.getPubKey())) { //filter out spent, locked, and frozen outputs if (sendAll) { // if send all, add all utxos and set amount to send all selectedUtxos.add(coinsInfo.getKeyImage()); 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 b9bcb14..323fc5d 100644 --- a/app/src/main/java/net/mynero/wallet/util/Constants.java +++ b/app/src/main/java/net/mynero/wallet/util/Constants.java @@ -12,6 +12,7 @@ public class Constants { public static final String PREF_STREET_MODE = "pref_street_mode"; public static final String PREF_MONEROCHAN = "pref_monerochan"; public static final String PREF_DONATE_PER_TX = "pref_donate_per_tx"; + public static final String PREF_FROZEN_COINS = "pref_frozen_coins"; public static final String URI_PREFIX = "monero:"; public static final String URI_ARG_AMOUNT = "tx_amount";