Convert ReceiveFragment to Kotlin

This commit is contained in:
pokkst 2023-12-06 15:43:28 -06:00
parent ec7827a847
commit f2755fbf83
No known key found for this signature in database
GPG Key ID: EC4FAAA66859FAA4
3 changed files with 151 additions and 154 deletions

View File

@ -19,7 +19,7 @@ import java.util.regex.Pattern
class Subaddress( class Subaddress(
private val accountIndex: Int, private val accountIndex: Int,
@JvmField val addressIndex: Int, val addressIndex: Int,
@JvmField val address: String, @JvmField val address: String,
val label: String val label: String
) : Comparable<Subaddress> { ) : Comparable<Subaddress> {

View File

@ -1,138 +1,144 @@
package net.mynero.wallet.fragment.receive; package net.mynero.wallet.fragment.receive
import android.graphics.Bitmap; import android.graphics.Bitmap
import android.os.Bundle; import android.os.Bundle
import android.view.LayoutInflater; import android.view.LayoutInflater
import android.view.View; import android.view.View
import android.view.ViewGroup; import android.view.ViewGroup
import android.widget.ImageButton; import android.widget.ImageButton
import android.widget.ImageView; import android.widget.ImageView
import android.widget.TextView; import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
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 net.mynero.wallet.R
import net.mynero.wallet.adapter.SubaddressAdapter
import net.mynero.wallet.adapter.SubaddressAdapter.SubaddressAdapterListener
import net.mynero.wallet.data.Subaddress
import net.mynero.wallet.fragment.dialog.EditAddressLabelBottomSheetDialog
import net.mynero.wallet.fragment.dialog.EditAddressLabelBottomSheetDialog.LabelListener
import net.mynero.wallet.util.Helper.clipBoardCopy
import timber.log.Timber
import java.nio.charset.StandardCharsets
import java.util.EnumMap
import androidx.annotation.NonNull; class ReceiveFragment : Fragment() {
import androidx.annotation.Nullable; private var addressTextView: TextView? = null
import androidx.fragment.app.Fragment; private var addressLabelTextView: TextView? = null
import androidx.lifecycle.ViewModelProvider; private var addressImageView: ImageView? = null
import androidx.recyclerview.widget.LinearLayoutManager; private var copyAddressImageButton: ImageButton? = null
import androidx.recyclerview.widget.RecyclerView; private var mViewModel: ReceiveViewModel? = null
override fun onCreateView(
import com.google.zxing.BarcodeFormat; inflater: LayoutInflater, container: ViewGroup?,
import com.google.zxing.EncodeHintType; savedInstanceState: Bundle?
import com.google.zxing.WriterException; ): View? {
import com.google.zxing.common.BitMatrix; return inflater.inflate(R.layout.fragment_receive, container, false)
import com.google.zxing.qrcode.QRCodeWriter;
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.Helper;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import timber.log.Timber;
public class ReceiveFragment extends Fragment {
private TextView addressTextView = null;
private TextView addressLabelTextView = null;
private ImageView addressImageView = null;
private ImageButton copyAddressImageButton = null;
private ReceiveViewModel mViewModel;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_receive, container, false);
} }
@Override override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState)
super.onViewCreated(view, savedInstanceState); mViewModel = ViewModelProvider(this)[ReceiveViewModel::class.java]
mViewModel = new ViewModelProvider(this).get(ReceiveViewModel.class); addressImageView = view.findViewById(R.id.monero_qr_imageview)
addressImageView = view.findViewById(R.id.monero_qr_imageview); addressTextView = view.findViewById(R.id.address_textview)
addressTextView = view.findViewById(R.id.address_textview); addressLabelTextView = view.findViewById(R.id.address_label_textview)
addressLabelTextView = view.findViewById(R.id.address_label_textview); copyAddressImageButton = view.findViewById(R.id.copy_address_imagebutton)
copyAddressImageButton = view.findViewById(R.id.copy_address_imagebutton); bindListeners(view)
bindListeners(view); bindObservers(view)
bindObservers(view); mViewModel?.init()
mViewModel.init();
} }
private void bindListeners(View view) { private fun bindListeners(view: View) {
ImageButton freshAddressImageView = view.findViewById(R.id.fresh_address_imageview); val freshAddressImageView = view.findViewById<ImageButton>(R.id.fresh_address_imageview)
freshAddressImageView.setOnClickListener(view1 -> { freshAddressImageView.setOnClickListener { mViewModel?.freshSubaddress }
mViewModel.getFreshSubaddress();
});
} }
private void bindObservers(View view) { private fun bindObservers(view: View) {
SubaddressAdapter adapter = new SubaddressAdapter(new SubaddressAdapter.SubaddressAdapterListener() { val adapter = SubaddressAdapter(object : SubaddressAdapterListener {
@Override override fun onSubaddressSelected(subaddress: Subaddress?) {
public void onSubaddressSelected(Subaddress subaddress) { mViewModel?.selectAddress(subaddress)
mViewModel.selectAddress(subaddress);
} }
@Override override fun onSubaddressEditLabel(subaddress: Subaddress?) {
public void onSubaddressEditLabel(Subaddress subaddress) { editAddressLabel(subaddress)
editAddressLabel(subaddress);
} }
}); })
RecyclerView recyclerView = view.findViewById(R.id.address_list_recyclerview); val recyclerView = view.findViewById<RecyclerView>(R.id.address_list_recyclerview)
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); recyclerView.layoutManager = LinearLayoutManager(activity)
recyclerView.setAdapter(adapter); recyclerView.adapter = adapter
mViewModel.address.observe(getViewLifecycleOwner(), this::setAddress); mViewModel?.address?.observe(viewLifecycleOwner) { subaddress: Subaddress? ->
mViewModel.addresses.observe(getViewLifecycleOwner(), adapter::submitList); setAddress(
subaddress
)
}
mViewModel?.addresses?.observe(viewLifecycleOwner) { dataSet: List<Subaddress> ->
adapter.submitList(
dataSet
)
}
} }
private void editAddressLabel(Subaddress subaddress) { private fun editAddressLabel(subaddress: Subaddress?) {
EditAddressLabelBottomSheetDialog dialog = new EditAddressLabelBottomSheetDialog(); val dialog = EditAddressLabelBottomSheetDialog()
dialog.addressIndex = subaddress.addressIndex; dialog.addressIndex = subaddress?.addressIndex ?: return
dialog.listener = () -> mViewModel.init(); dialog.listener = object : LabelListener {
dialog.show(getParentFragmentManager(), "edit_address_dialog"); override fun onDismiss() {
mViewModel?.init()
}
}
dialog.show(parentFragmentManager, "edit_address_dialog")
} }
private void setAddress(Subaddress subaddress) { private fun setAddress(subaddress: Subaddress?) {
final String label = subaddress.getDisplayLabel(); val label = subaddress?.displayLabel
final String address = getContext().getString(R.string.subbaddress_info_subtitle, val address = context?.getString(
subaddress.addressIndex, subaddress.getSquashedAddress()); R.string.subbaddress_info_subtitle,
addressLabelTextView.setText(label.isEmpty() ? address : label); subaddress?.addressIndex, subaddress?.squashedAddress
addressTextView.setText(subaddress.address); )
addressImageView.setImageBitmap(generate(subaddress.address, 256, 256)); addressLabelTextView?.text = if (label?.isEmpty() == true) address else label
copyAddressImageButton.setOnClickListener(view1 -> Helper.clipBoardCopy(getContext(), "address", subaddress.address)); addressTextView?.text = subaddress?.address
addressLabelTextView.setOnLongClickListener(v -> { addressImageView?.setImageBitmap(subaddress?.address?.let { generate(it, 256, 256) })
editAddressLabel(subaddress); copyAddressImageButton?.setOnClickListener {
return true; clipBoardCopy(
}); context, "address", subaddress?.address
addressTextView.setOnLongClickListener(v -> { )
editAddressLabel(subaddress); }
return true; addressLabelTextView?.setOnLongClickListener {
}); editAddressLabel(subaddress)
true
}
addressTextView?.setOnLongClickListener {
editAddressLabel(subaddress)
true
}
} }
private Bitmap generate(String text, int width, int height) { private fun generate(text: String, width: Int, height: Int): Bitmap? {
if ((width <= 0) || (height <= 0)) return null; if (width <= 0 || height <= 0) return null
Map<EncodeHintType, Object> hints = new HashMap<>(); val hints: MutableMap<EncodeHintType, Any?> = EnumMap(com.google.zxing.EncodeHintType::class.java)
hints.put(EncodeHintType.CHARACTER_SET, StandardCharsets.UTF_8); hints[EncodeHintType.CHARACTER_SET] = StandardCharsets.UTF_8
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); hints[EncodeHintType.ERROR_CORRECTION] = ErrorCorrectionLevel.M
try { try {
BitMatrix bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints); val bitMatrix = QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints)
int[] pixels = new int[width * height]; val pixels = IntArray(width * height)
for (int i = 0; i < height; i++) { for (i in 0 until height) {
for (int j = 0; j < width; j++) { for (j in 0 until width) {
if (bitMatrix.get(j, i)) { if (bitMatrix[j, i]) {
pixels[i * width + j] = 0xffffffff; pixels[i * width + j] = -0x1
} else { } else {
pixels[i * height + j] = getResources().getColor(R.color.oled_colorBackground); pixels[i * height + j] = resources.getColor(R.color.oled_colorBackground)
} }
} }
} }
return Bitmap.createBitmap(pixels, 0, width, width, height, Bitmap.Config.RGB_565); return Bitmap.createBitmap(pixels, 0, width, width, height, Bitmap.Config.RGB_565)
} catch (WriterException ex) { } catch (ex: WriterException) {
Timber.e(ex); Timber.e(ex)
} }
return null; return null
} }
} }

View File

@ -1,49 +1,40 @@
package net.mynero.wallet.fragment.receive; package net.mynero.wallet.fragment.receive
import androidx.lifecycle.LiveData; import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel; import androidx.lifecycle.ViewModel
import net.mynero.wallet.data.Subaddress
import net.mynero.wallet.model.WalletManager
import net.mynero.wallet.service.AddressService
import java.util.Collections
import net.mynero.wallet.data.Subaddress; class ReceiveViewModel : ViewModel() {
import net.mynero.wallet.model.Wallet; private val _address = MutableLiveData<Subaddress?>()
import net.mynero.wallet.model.WalletManager; private val _addresses = MutableLiveData<List<Subaddress>>()
import net.mynero.wallet.service.AddressService; var address: LiveData<Subaddress?> = _address
var addresses: LiveData<List<Subaddress>> = _addresses
import java.util.ArrayList; fun init() {
import java.util.Collections; _address.value = AddressService.instance?.currentSubaddress()
import java.util.List; _addresses.value = subaddresses
public class ReceiveViewModel extends ViewModel {
private final MutableLiveData<Subaddress> _address = new MutableLiveData<>();
private final MutableLiveData<List<Subaddress>> _addresses = new MutableLiveData<>();
public LiveData<Subaddress> address = _address;
public LiveData<List<Subaddress>> addresses = _addresses;
public void init() {
if (AddressService.instance != null) {
_address.setValue(AddressService.instance.currentSubaddress());
}
_addresses.setValue(getSubaddresses());
} }
private List<Subaddress> getSubaddresses() { private val subaddresses: List<Subaddress>
Wallet wallet = WalletManager.getInstance().getWallet(); get() {
ArrayList<Subaddress> subaddresses = new ArrayList<>(); val wallet = WalletManager.instance?.wallet
int addressesSize = AddressService.instance != null ? AddressService.instance.getLatestAddressIndex() : 0; val subaddresses = ArrayList<Subaddress>()
for (int i = addressesSize - 1; i >= 0; i--) { val addressesSize = AddressService.instance?.latestAddressIndex ?: 0
subaddresses.add(wallet.getSubaddressObject(i)); for (i in addressesSize - 1 downTo 0) {
wallet?.getSubaddressObject(i)?.let { subaddresses.add(it) }
}
return Collections.unmodifiableList(subaddresses)
} }
return Collections.unmodifiableList(subaddresses); val freshSubaddress: Unit
} get() {
_address.value = AddressService.instance?.freshSubaddress()
public void getFreshSubaddress() { _addresses.value = subaddresses
if (AddressService.instance != null) {
_address.setValue(AddressService.instance.freshSubaddress());
} }
_addresses.setValue(getSubaddresses());
}
public void selectAddress(Subaddress subaddress) { fun selectAddress(subaddress: Subaddress?) {
_address.setValue(subaddress); _address.value = subaddress
} }
} }