From 9551961025e54f1e94814c0b1cf490c1d3faa18c Mon Sep 17 00:00:00 2001 From: pokkst Date: Sat, 2 Dec 2023 13:28:49 -0600 Subject: [PATCH 1/6] Add address label editing --- .../wallet/adapter/SubaddressAdapter.java | 6 ++ .../EditAddressLabelBottomSheetDialog.java | 71 ++++++++++++++++++ .../fragment/receive/ReceiveFragment.java | 28 ++++++- ...bottom_sheet_dialog_edit_address_label.xml | 73 +++++++++++++++++++ app/src/main/res/values/strings.xml | 3 + 5 files changed, 180 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/net/mynero/wallet/fragment/dialog/EditAddressLabelBottomSheetDialog.java create mode 100644 app/src/main/res/layout/bottom_sheet_dialog_edit_address_label.xml diff --git a/app/src/main/java/net/mynero/wallet/adapter/SubaddressAdapter.java b/app/src/main/java/net/mynero/wallet/adapter/SubaddressAdapter.java index 377c0ee..eab52fa 100644 --- a/app/src/main/java/net/mynero/wallet/adapter/SubaddressAdapter.java +++ b/app/src/main/java/net/mynero/wallet/adapter/SubaddressAdapter.java @@ -78,6 +78,8 @@ public class SubaddressAdapter extends RecyclerView.Adapter listener.onSubaddressSelected(subaddress)); + itemView.setOnLongClickListener(v -> { + listener.onSubaddressEditLabel(subaddress); + return true; + }); } } } diff --git a/app/src/main/java/net/mynero/wallet/fragment/dialog/EditAddressLabelBottomSheetDialog.java b/app/src/main/java/net/mynero/wallet/fragment/dialog/EditAddressLabelBottomSheetDialog.java new file mode 100644 index 0000000..f99ae20 --- /dev/null +++ b/app/src/main/java/net/mynero/wallet/fragment/dialog/EditAddressLabelBottomSheetDialog.java @@ -0,0 +1,71 @@ +package net.mynero.wallet.fragment.dialog; + +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; +import android.widget.EditText; +import android.widget.ImageButton; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import com.google.android.material.bottomsheet.BottomSheetDialogFragment; + +import net.mynero.wallet.R; +import net.mynero.wallet.model.Wallet; +import net.mynero.wallet.model.WalletManager; +import net.mynero.wallet.service.AddressService; +import net.mynero.wallet.util.Helper; + +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; + +public class EditAddressLabelBottomSheetDialog extends BottomSheetDialogFragment { + public LabelListener listener = null; + public int addressIndex = 0; + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + return inflater.inflate(R.layout.bottom_sheet_dialog_edit_address_label, null); + } + + @Override + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + Wallet wallet = WalletManager.getInstance().getWallet(); + AddressService addressService = AddressService.getInstance(); + ImageButton pasteButton = view.findViewById(R.id.paste_password_imagebutton); + EditText labelEditText = view.findViewById(R.id.wallet_password_edittext); + Button saveLabelButton = view.findViewById(R.id.unlock_wallet_button); + + boolean isDate = false; + try { + new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss", Locale.US).parse(wallet.getSubaddressLabel(addressIndex)); + isDate = true; + } catch (ParseException ignored) { } + + labelEditText.setText(isDate ? null : wallet.getSubaddressLabel(addressIndex)); + pasteButton.setOnClickListener(view1 -> labelEditText.setText(Helper.getClipBoardText(view.getContext()))); + saveLabelButton.setOnClickListener(view1 -> { + String label = labelEditText.getText().toString(); + if(addressService.getLatestAddressIndex() == addressIndex) { + addressService.freshSubaddress(); + } + wallet.setSubaddressLabel(addressIndex, label); + wallet.store(); + if(listener != null) { + listener.onDismiss(); + } + dismiss(); + }); + } + + public interface LabelListener { + void onDismiss(); + } +} \ No newline at end of file diff --git a/app/src/main/java/net/mynero/wallet/fragment/receive/ReceiveFragment.java b/app/src/main/java/net/mynero/wallet/fragment/receive/ReceiveFragment.java index 05202d5..479e9e5 100644 --- a/app/src/main/java/net/mynero/wallet/fragment/receive/ReceiveFragment.java +++ b/app/src/main/java/net/mynero/wallet/fragment/receive/ReceiveFragment.java @@ -26,6 +26,7 @@ import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import net.mynero.wallet.R; import net.mynero.wallet.adapter.SubaddressAdapter; import net.mynero.wallet.data.Subaddress; +import net.mynero.wallet.fragment.dialog.EditAddressLabelBottomSheetDialog; import net.mynero.wallet.util.DayNightMode; import net.mynero.wallet.util.Helper; import net.mynero.wallet.util.NightmodeHelper; @@ -70,7 +71,17 @@ public class ReceiveFragment extends Fragment { } private void bindObservers(View view) { - SubaddressAdapter adapter = new SubaddressAdapter(mViewModel::selectAddress); + SubaddressAdapter adapter = new SubaddressAdapter(new SubaddressAdapter.SubaddressAdapterListener() { + @Override + public void onSubaddressSelected(Subaddress subaddress) { + mViewModel.selectAddress(subaddress); + } + + @Override + public void onSubaddressEditLabel(Subaddress subaddress) { + editAddressLabel(subaddress); + } + }); RecyclerView recyclerView = view.findViewById(R.id.address_list_recyclerview); recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); recyclerView.setAdapter(adapter); @@ -78,6 +89,13 @@ public class ReceiveFragment extends Fragment { mViewModel.addresses.observe(getViewLifecycleOwner(), adapter::submitList); } + private void editAddressLabel(Subaddress subaddress) { + EditAddressLabelBottomSheetDialog dialog = new EditAddressLabelBottomSheetDialog(); + dialog.addressIndex = subaddress.getAddressIndex(); + dialog.listener = () -> mViewModel.init(); + dialog.show(getParentFragmentManager(), "edit_address_dialog"); + } + private void setAddress(Subaddress subaddress) { final String label = subaddress.getDisplayLabel(); final String address = getContext().getString(R.string.subbaddress_info_subtitle, @@ -86,6 +104,14 @@ public class ReceiveFragment extends Fragment { addressTextView.setText(subaddress.getAddress()); addressImageView.setImageBitmap(generate(subaddress.getAddress(), 256, 256)); copyAddressImageButton.setOnClickListener(view1 -> Helper.clipBoardCopy(getContext(), "address", subaddress.getAddress())); + addressLabelTextView.setOnLongClickListener(v -> { + editAddressLabel(subaddress); + return true; + }); + addressTextView.setOnLongClickListener(v -> { + editAddressLabel(subaddress); + return true; + }); } private Bitmap generate(String text, int width, int height) { diff --git a/app/src/main/res/layout/bottom_sheet_dialog_edit_address_label.xml b/app/src/main/res/layout/bottom_sheet_dialog_edit_address_label.xml new file mode 100644 index 0000000..a37c300 --- /dev/null +++ b/app/src/main/res/layout/bottom_sheet_dialog_edit_address_label.xml @@ -0,0 +1,73 @@ + + + + + + + + + + + + +