copy some logic from monerujo. add new "fresh address" feature

This commit is contained in:
pokkst 2022-10-06 00:39:40 -05:00
parent fd0f756975
commit 9efe44d76a
No known key found for this signature in database
GPG Key ID: 90C2ED85E67A50FF
4 changed files with 54 additions and 9 deletions

View File

@ -8,6 +8,7 @@ import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@ -22,7 +23,11 @@ import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import net.mynero.wallet.R;
import net.mynero.wallet.data.Subaddress;
import net.mynero.wallet.model.TransactionInfo;
import net.mynero.wallet.model.Wallet;
import net.mynero.wallet.model.WalletManager;
import net.mynero.wallet.service.AddressService;
import net.mynero.wallet.service.HistoryService;
import net.mynero.wallet.util.DayNightMode;
import net.mynero.wallet.util.Helper;
import net.mynero.wallet.util.NightmodeHelper;
@ -33,6 +38,9 @@ import java.util.Map;
import timber.log.Timber;
public class ReceiveBottomSheetDialog extends BottomSheetDialogFragment {
private TextView addressTextView = null;
private ImageView addressImageView = null;
private ImageButton copyAddressImageButton = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
@ -42,14 +50,29 @@ public class ReceiveBottomSheetDialog extends BottomSheetDialogFragment {
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ImageView addressImageView = view.findViewById(R.id.monero_qr_imageview);
TextView addressTextView = view.findViewById(R.id.address_textview);
ImageButton copyAddressImageButton = view.findViewById(R.id.copy_address_imagebutton);
addressImageView = view.findViewById(R.id.monero_qr_imageview);
addressTextView = view.findViewById(R.id.address_textview);
copyAddressImageButton = view.findViewById(R.id.copy_address_imagebutton);
ImageView freshAddressImageView = view.findViewById(R.id.fresh_address_imageview);
Wallet wallet = WalletManager.getInstance().getWallet();
AddressService addressService = AddressService.getInstance();
Subaddress addr = AddressService.getInstance().currentSubaddress();
addressTextView.setText(addr.getAddress());
addressImageView.setImageBitmap(generate(addr.getAddress(), 256, 256));
copyAddressImageButton.setOnClickListener(view1 -> Helper.clipBoardCopy(getContext(), "address", addr.getAddress()));
Subaddress addr = addressService.currentSubaddress();
setAddress(addr);
freshAddressImageView.setOnClickListener(view1 -> {
final int maxSubaddresses = addressService.getLastUsedSubaddress() + wallet.getDeviceType().getSubaddressLookahead();
if(wallet.getNumSubaddresses() < maxSubaddresses) {
setAddress(AddressService.getInstance().freshSubaddress());
} else {
Toast.makeText(getContext(), getResources().getString(R.string.max_subaddresses_warning), Toast.LENGTH_SHORT).show();
}
});
}
private void setAddress(Subaddress subaddress) {
addressTextView.setText(subaddress.getAddress());
addressImageView.setImageBitmap(generate(subaddress.getAddress(), 256, 256));
copyAddressImageButton.setOnClickListener(view1 -> Helper.clipBoardCopy(getContext(), "address", subaddress.getAddress()));
}
public Bitmap generate(String text, int width, int height) {

View File

@ -1,5 +1,6 @@
package net.mynero.wallet.service;
import net.mynero.wallet.MoneroApplication;
import net.mynero.wallet.data.Subaddress;
import net.mynero.wallet.model.TransactionInfo;
import net.mynero.wallet.model.Wallet;
@ -14,6 +15,7 @@ import java.util.Locale;
public class AddressService extends ServiceBase {
public static AddressService instance = null;
private int latestAddressIndex = 1;
private int lastUsedSubaddress = 0;
public AddressService(MoneroHandlerThread thread) {
super(thread);
@ -25,9 +27,18 @@ public class AddressService extends ServiceBase {
}
public void refreshAddresses() {
List<TransactionInfo> localTransactionList = new ArrayList<>(HistoryService.getInstance().getHistory());
for (TransactionInfo info : localTransactionList) {
if (info.addressIndex > lastUsedSubaddress)
lastUsedSubaddress = info.addressIndex;
}
latestAddressIndex = WalletManager.getInstance().getWallet().getNumSubaddresses();
}
public int getLastUsedSubaddress() {
return lastUsedSubaddress;
}
public String getPrimaryAddress() {
return WalletManager.getInstance().getWallet().getAddress();
}

View File

@ -15,17 +15,27 @@
<TextView
android:id="@+id/recv_monero_textview"
android:layout_width="match_parent"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="32dp"
android:text="@string/recv_monero"
android:textSize="32sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@id/monero_qr_imageview"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintEnd_toStartOf="@id/fresh_address_imageview"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/fresh_address_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/lb_ic_replay"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/recv_monero_textview"
app:layout_constraintBottom_toBottomOf="@id/recv_monero_textview"
app:tint="@color/oled_textColorPrimary" />
<ImageView
android:id="@+id/monero_qr_imageview"
android:layout_width="256dp"

View File

@ -98,4 +98,5 @@
<string name="outpoint_text">Outpoint: %1$s</string>
<string name="create_wallet_failed">Create wallet failed: %1$s</string>
<string name="wallet_keys_label">Wallet Keys</string>
<string name="max_subaddresses_warning">Max subaddresses. Please receive funds first.</string>
</resources>