feat: refactor receive fragment

It's now possible to rename latest address without generating a new one
This commit is contained in:
- 2024-09-18 16:37:12 +02:00
parent 8a769d0e23
commit f0ba6f5ce5
8 changed files with 44 additions and 57 deletions

View file

@ -15,6 +15,7 @@
*/
package net.mynero.wallet.adapter
import android.graphics.Typeface
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
@ -26,19 +27,19 @@ import net.mynero.wallet.service.PrefService
import net.mynero.wallet.util.Constants
import net.mynero.wallet.util.Helper
class SubaddressAdapter(val listener: SubaddressAdapterListener?) :
RecyclerView.Adapter<SubaddressAdapter.ViewHolder>() {
private var localDataSet: List<Subaddress>
class SubaddressAdapter(
private var addresses: List<Subaddress>,
private var selectedAddress: Subaddress?,
val listener: SubaddressAdapterListener?
): RecyclerView.Adapter<SubaddressAdapter.ViewHolder>() {
/**
* Initialize the dataset of the Adapter.
*/
init {
localDataSet = ArrayList()
fun submitAddresses(addresses: List<Subaddress>) {
this.addresses = addresses
notifyDataSetChanged()
}
fun submitList(dataSet: List<Subaddress>) {
localDataSet = dataSet
fun submitSelectedAddress(selectedAddress: Subaddress?) {
this.selectedAddress = selectedAddress
notifyDataSetChanged()
}
@ -52,13 +53,13 @@ class SubaddressAdapter(val listener: SubaddressAdapterListener?) :
// Replace the contents of a view (invoked by the layout manager)
override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
val subaddress = localDataSet[position]
viewHolder.bind(subaddress)
val address = addresses[position]
viewHolder.bind(address, selectedAddress != null && address == selectedAddress)
}
// Return the size of your dataset (invoked by the layout manager)
override fun getItemCount(): Int {
return localDataSet.size
return addresses.size
}
interface SubaddressAdapterListener {
@ -71,17 +72,20 @@ class SubaddressAdapter(val listener: SubaddressAdapterListener?) :
* (custom ViewHolder).
*/
class ViewHolder(view: View, val listener: SubaddressAdapterListener?) :
RecyclerView.ViewHolder(
view
) {
RecyclerView.ViewHolder(view) {
fun bind(subaddress: Subaddress) {
fun bind(subaddress: Subaddress, isSelected: Boolean) {
val addressTextView =
itemView.findViewById<TextView>(R.id.address_item_address_textview)
val addressLabelTextView = itemView.findViewById<TextView>(R.id.address_label_textview)
val addressAmountTextView =
itemView.findViewById<TextView>(R.id.address_amount_textview)
addressTextView.text = subaddress.address
if (isSelected) {
addressTextView.setTypeface(null, Typeface.BOLD)
} else {
addressTextView.setTypeface(null, Typeface.NORMAL)
}
val label = subaddress.displayLabel
val address = itemView.context.getString(
R.string.subbaddress_info_subtitle,

View file

@ -10,7 +10,6 @@ import android.widget.ImageButton
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import net.mynero.wallet.R
import net.mynero.wallet.model.WalletManager
import net.mynero.wallet.service.AddressService
import net.mynero.wallet.util.Helper.getClipBoardText
import java.text.ParseException
import java.text.SimpleDateFormat
@ -30,7 +29,6 @@ class EditAddressLabelBottomSheetDialog : BottomSheetDialogFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val wallet = WalletManager.instance?.wallet
val addressService = AddressService.instance
val pasteButton = view.findViewById<ImageButton>(R.id.paste_password_imagebutton)
val labelEditText = view.findViewById<EditText>(R.id.wallet_password_edittext)
val saveLabelButton = view.findViewById<Button>(R.id.unlock_wallet_button)
@ -50,9 +48,6 @@ class EditAddressLabelBottomSheetDialog : BottomSheetDialogFragment() {
pasteButton.setOnClickListener { labelEditText.setText(getClipBoardText(view.context)) }
saveLabelButton.setOnClickListener {
val label = labelEditText.text.toString()
if (addressService?.latestAddressIndex == addressIndex) {
addressService.freshSubaddress()
}
wallet?.setSubaddressLabel(addressIndex, label)
wallet?.store()
if (listener != null) {

View file

@ -17,7 +17,6 @@ import androidx.recyclerview.widget.RecyclerView
import com.google.zxing.BarcodeFormat
import com.google.zxing.EncodeHintType
import com.google.zxing.WriterException
import com.google.zxing.qrcode.QRCodeWriter
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel
import com.journeyapps.barcodescanner.BarcodeEncoder
import net.mynero.wallet.R
@ -36,6 +35,7 @@ class ReceiveFragment : Fragment() {
private var addressImageView: ImageView? = null
private var copyAddressImageButton: ImageButton? = null
private var mViewModel: ReceiveViewModel? = null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
@ -61,7 +61,7 @@ class ReceiveFragment : Fragment() {
}
private fun bindObservers(view: View) {
val adapter = SubaddressAdapter(object : SubaddressAdapterListener {
val subaddressAdapterListener = object : SubaddressAdapterListener {
override fun onSubaddressSelected(subaddress: Subaddress?) {
mViewModel?.selectAddress(subaddress)
}
@ -69,19 +69,18 @@ class ReceiveFragment : Fragment() {
override fun onSubaddressEditLabel(subaddress: Subaddress?) {
editAddressLabel(subaddress)
}
})
}
val adapter = SubaddressAdapter(emptyList(), null, subaddressAdapterListener)
val recyclerView = view.findViewById<RecyclerView>(R.id.address_list_recyclerview)
recyclerView.layoutManager = LinearLayoutManager(activity)
recyclerView.adapter = adapter
mViewModel?.address?.observe(viewLifecycleOwner) { subaddress: Subaddress? ->
setAddress(
subaddress
)
mViewModel?.address?.observe(viewLifecycleOwner) { address: Subaddress? ->
setAddress(address)
adapter.submitSelectedAddress(address)
}
mViewModel?.addresses?.observe(viewLifecycleOwner) { dataSet: List<Subaddress> ->
adapter.submitList(
dataSet
)
mViewModel?.addresses?.observe(viewLifecycleOwner) { addresses: List<Subaddress> ->
// We want newer addresses addresses to be shown first
adapter.submitAddresses(addresses.reversed())
}
}

View file

@ -11,23 +11,25 @@ import java.util.Collections
class ReceiveViewModel : ViewModel() {
private val _address = MutableLiveData<Subaddress?>()
private val _addresses = MutableLiveData<List<Subaddress>>()
var address: LiveData<Subaddress?> = _address
var addresses: LiveData<List<Subaddress>> = _addresses
val address: LiveData<Subaddress?> = _address
val addresses: LiveData<List<Subaddress>> = _addresses
fun init() {
_address.value = AddressService.instance?.currentSubaddress()
_addresses.value = subaddresses
_address.value = addresses.value?.lastOrNull()
}
private val subaddresses: List<Subaddress>
get() {
val wallet = WalletManager.instance?.wallet
val subaddresses = ArrayList<Subaddress>()
val addressesSize = AddressService.instance?.latestAddressIndex ?: 0
for (i in addressesSize - 1 downTo 0) {
val numAddresses = AddressService.instance?.numAddresses ?: 1
for (i in 0 until numAddresses) {
wallet?.getSubaddressObject(i)?.let { subaddresses.add(it) }
}
return Collections.unmodifiableList(subaddresses)
}
val freshSubaddress: Unit
get() {
_address.value = AddressService.instance?.freshSubaddress()

View file

@ -19,9 +19,6 @@ import android.util.Log
import android.util.Pair
import net.mynero.wallet.data.Subaddress
import java.io.File
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
class Wallet {
var isSynchronized = false
@ -418,16 +415,6 @@ class Wallet {
get() = getNumSubaddresses(accountIndex)
private external fun getNumSubaddresses(accountIndex: Int): Int
val newSubaddress: String
get() = getNewSubaddress(accountIndex)
private fun getNewSubaddress(accountIndex: Int): String {
val timeStamp = SimpleDateFormat("yyyy-MM-dd-HH:mm:ss", Locale.US).format(Date())
addSubaddress(accountIndex, timeStamp)
val subaddress = getLastSubaddress(accountIndex)
Log.d("Wallet.kt", "${(getNumSubaddresses(accountIndex) - 1)}: $subaddress")
return subaddress
}
external fun addSubaddress(accountIndex: Int, label: String?)
private fun getLastSubaddress(accountIndex: Int): String {

View file

@ -7,7 +7,7 @@ import java.util.Date
import java.util.Locale
class AddressService(thread: MoneroHandlerThread) : ServiceBase(thread) {
var latestAddressIndex = 1
var numAddresses = 1
private set
init {
@ -15,7 +15,7 @@ class AddressService(thread: MoneroHandlerThread) : ServiceBase(thread) {
}
fun refreshAddresses() {
WalletManager.instance?.wallet?.numSubaddresses?.let { latestAddressIndex = it }
WalletManager.instance?.wallet?.numSubaddresses?.let { numAddresses = it }
}
fun freshSubaddress(): Subaddress? {
@ -24,12 +24,12 @@ class AddressService(thread: MoneroHandlerThread) : ServiceBase(thread) {
wallet?.addSubaddress(wallet.getAccountIndex(), timeStamp)
refreshAddresses()
wallet?.store()
return wallet?.getSubaddressObject(latestAddressIndex)
return wallet?.getSubaddressObject(numAddresses - 1)
}
fun currentSubaddress(): Subaddress? {
val wallet = WalletManager.instance?.wallet
return wallet?.getSubaddressObject(latestAddressIndex)
return wallet?.getSubaddressObject(numAddresses - 1)
}
companion object {

View file

@ -109,7 +109,7 @@
android:layout_marginBottom="8dp"
android:ellipsize="middle"
android:singleLine="true"
android:text="@string/previous_addresses"
android:text="@string/all_addresses"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@id/address_list_recyclerview"

View file

@ -127,7 +127,7 @@
<string name="trusted_daemon">Trusted daemon</string>
<string name="use_bundled_tor">Let Mysu start and manage a Tor daemon</string>
<string name="subbaddress_info_subtitle" translatable="false">#%1$d: %2$s</string>
<string name="previous_addresses">Previous addresses</string>
<string name="all_addresses">All addresses</string>
<string name="donate_label">Donate to Mysu</string>
<string name="donating_label">Donating to Mysu. Thank you!</string>
<string name="transactions">Transactions</string>