0.4.4.6: Improve coin freezing

This commit is contained in:
pokkst 2023-09-18 09:13:31 -05:00
parent 71dcdfdb99
commit 4bf9bf6ee2
No known key found for this signature in database
GPG Key ID: 90C2ED85E67A50FF
7 changed files with 73 additions and 11 deletions

View File

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

View File

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

View File

@ -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<CoinsInfoAdapter.View
outpointTextView.setText(itemView.getResources().getString(R.string.outpoint_text, coinsInfo.getHash() + ":" + coinsInfo.getLocalOutputIndex()));
if (selected) {
itemView.setBackgroundTintList(ContextCompat.getColorStateList(itemView.getContext(), R.color.oled_colorSecondary));
} else if(coinsInfo.isFrozen()) {
} else if(coinsInfo.isFrozen() || UTXOService.instance.isCoinFrozen(coinsInfo)) {
itemView.setBackgroundTintList(ContextCompat.getColorStateList(itemView.getContext(), R.color.oled_frozen_utxo));
} else if (!coinsInfo.isUnlocked()) {
itemView.setBackgroundTintList(ContextCompat.getColorStateList(itemView.getContext(), R.color.oled_locked_utxo));

View File

@ -127,7 +127,7 @@ public class UtxosFragment extends Fragment implements CoinsInfoAdapter.CoinsInf
boolean frozenExists = false, unfrozenExists = false, bothExist = false;
for(CoinsInfo selectedUtxo : adapter.getSelectedUtxos().values()) {
if(selectedUtxo.isFrozen())
if(selectedUtxo.isFrozen() || UTXOService.getInstance().isCoinFrozen(selectedUtxo))
frozenExists = true;
else {
unfrozenExists = true;

View File

@ -4,6 +4,7 @@ import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import net.mynero.wallet.model.BalanceInfo;
import net.mynero.wallet.model.CoinsInfo;
import net.mynero.wallet.model.Wallet;
import net.mynero.wallet.model.WalletManager;
import net.mynero.wallet.util.Constants;
@ -29,11 +30,23 @@ public class BalanceService extends ServiceBase {
}
public long getUnlockedBalanceRaw() {
return WalletManager.getInstance().getWallet().getUnlockedBalance();
long unlocked = 0;
for(CoinsInfo coinsInfo : UTXOService.getInstance().getUtxos()) {
if(!coinsInfo.isSpent() && !coinsInfo.isFrozen() && coinsInfo.isUnlocked() && !UTXOService.getInstance().isCoinFrozen(coinsInfo)) {
unlocked += coinsInfo.getAmount();
}
}
return unlocked;
}
public long getTotalBalanceRaw() {
return WalletManager.getInstance().getWallet().getBalance();
long total = 0;
for(CoinsInfo coinsInfo : UTXOService.getInstance().getUtxos()) {
if(!coinsInfo.isSpent() && !coinsInfo.isFrozen() && !UTXOService.getInstance().isCoinFrozen(coinsInfo)) {
total += coinsInfo.getAmount();
}
}
return total;
}
public long getLockedBalanceRaw() {

View File

@ -10,6 +10,10 @@ import net.mynero.wallet.model.CoinsInfo;
import net.mynero.wallet.model.PendingTransaction;
import net.mynero.wallet.model.Wallet;
import net.mynero.wallet.model.WalletManager;
import net.mynero.wallet.util.Constants;
import org.json.JSONArray;
import org.json.JSONException;
import java.util.ArrayList;
import java.util.Collections;
@ -20,10 +24,16 @@ public class UTXOService extends ServiceBase {
public static UTXOService instance = null;
private final MutableLiveData<List<CoinsInfo>> _utxos = new MutableLiveData<>();
public LiveData<List<CoinsInfo>> utxos = _utxos;
private ArrayList<String> 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<CoinsInfo> getUtxos() {
List<CoinsInfo> value = utxos.getValue();
return value != null ? value : List.of();
}
private List<CoinsInfo> getUtxosInternal() {
return WalletManager.getInstance().getWallet().getCoins().getAll();
}
public void toggleFrozen(HashMap<String, CoinsInfo> selectedCoins) {
Coins coins = WalletManager.getInstance().getWallet().getCoins();
ArrayList<String> 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<String> frozenCoinsCopy = new ArrayList<>(frozenCoins);
for(String pubKey : frozenCoinsCopy) {
jsonArray.put(pubKey);
}
prefService.edit().putString(Constants.PREF_FROZEN_COINS, jsonArray.toString()).apply();
}
public ArrayList<String> 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());

View File

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