Updated on 2026-08-14
This commit is contained in:
parent
d8f9a85560
commit
1c82ef2c7b
12 changed files with 332 additions and 57 deletions
|
|
@ -14,6 +14,7 @@ import android.os.Build
|
|||
import android.os.Bundle
|
||||
import android.text.Html
|
||||
import android.view.Gravity
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.widget.Toast
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
|
|
@ -24,7 +25,8 @@ import com.tangem.App
|
|||
import com.tangem.Constant
|
||||
import com.tangem.data.Blockchain
|
||||
import com.tangem.data.dp.PrefsManager
|
||||
import com.tangem.data.network.ServerApiCommon
|
||||
import com.tangem.server_android.PayIdResponse
|
||||
import com.tangem.server_android.Result
|
||||
import com.tangem.server_android.ServerApiTangem
|
||||
import com.tangem.server_android.model.CardVerifyAndGetInfo
|
||||
import com.tangem.tangem_card.data.TangemCard
|
||||
|
|
@ -49,11 +51,13 @@ import com.tangem.ui.navigation.NavigationResultListener
|
|||
import com.tangem.util.LOG
|
||||
import com.tangem.util.UtilHelper
|
||||
import com.tangem.wallet.*
|
||||
import kotlinx.android.synthetic.main.dialog_pay_id.view.*
|
||||
import kotlinx.android.synthetic.main.fr_loaded_wallet.*
|
||||
import kotlinx.android.synthetic.main.layout_btn_details.*
|
||||
import kotlinx.android.synthetic.main.layout_tangem_card.*
|
||||
import org.greenrobot.eventbus.EventBus
|
||||
import org.greenrobot.eventbus.Subscribe
|
||||
import retrofit2.HttpException
|
||||
import java.io.InputStream
|
||||
import java.util.*
|
||||
import kotlin.concurrent.timerTask
|
||||
|
|
@ -62,6 +66,7 @@ class LoadedWalletFragment : BaseFragment(), NavigationResultListener, NfcAdapte
|
|||
CardProtocol.Notifications, SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
companion object {
|
||||
val TAG: String = LoadedWalletFragment::class.java.simpleName
|
||||
const val PAY_ID_TANGEM = "\$payid.tangem.com"
|
||||
}
|
||||
|
||||
override val layoutId = R.layout.fr_loaded_wallet
|
||||
|
|
@ -69,7 +74,6 @@ class LoadedWalletFragment : BaseFragment(), NavigationResultListener, NfcAdapte
|
|||
private lateinit var viewModel: LoadedWalletViewModel
|
||||
private lateinit var ctx: TangemContext
|
||||
private lateinit var mpSecondScanSound: MediaPlayer
|
||||
private var serverApiCommon: ServerApiCommon = ServerApiCommon()
|
||||
private var serverApiTangem: ServerApiTangem = ServerApiTangem()
|
||||
private var lastTag: Tag? = null
|
||||
private var lastReadSuccess = true
|
||||
|
|
@ -111,6 +115,10 @@ class LoadedWalletFragment : BaseFragment(), NavigationResultListener, NfcAdapte
|
|||
srl.isRefreshing = true
|
||||
}
|
||||
|
||||
private var payId: String? = null
|
||||
private lateinit var btnLoadItems: MutableList<CharSequence>
|
||||
|
||||
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
|
@ -127,6 +135,8 @@ class LoadedWalletFragment : BaseFragment(), NavigationResultListener, NfcAdapte
|
|||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
|
||||
viewModel = ViewModelProviders.of(this).get(LoadedWalletViewModel::class.java)
|
||||
|
||||
mpSecondScanSound = MediaPlayer.create(activity, R.raw.scan_card_sound)
|
||||
|
||||
val engine = CoinEngineFactory.create(ctx)
|
||||
|
|
@ -139,6 +149,37 @@ class LoadedWalletFragment : BaseFragment(), NavigationResultListener, NfcAdapte
|
|||
btnExtract.backgroundTintList = inactiveColor
|
||||
|
||||
tvWallet.text = ctx.coinData.wallet
|
||||
btnLoadItems = mutableListOf<CharSequence>(
|
||||
getString(R.string.loaded_wallet_load_via_app),
|
||||
getString(R.string.loaded_wallet_load_via_share_address),
|
||||
getString(R.string.loaded_wallet_load_via_qr)
|
||||
)
|
||||
if (ctx.blockchain == Blockchain.Ripple) {
|
||||
viewModel.getPayId(
|
||||
Util.byteArrayToHexString(ctx.card?.cid!!),
|
||||
Util.byteArrayToHexString(ctx.card?.cardPublicKey!!))
|
||||
.observe(
|
||||
viewLifecycleOwner, Observer<Result<PayIdResponse>> { result ->
|
||||
when (result) {
|
||||
is Result.Success -> {
|
||||
payId = result.data.payId
|
||||
btnLoadItems.add(result.data.payId)
|
||||
}
|
||||
is Result.Failure -> {
|
||||
(result.error as? HttpException)?.let {
|
||||
if (it.code() == 404) {
|
||||
val createPayId = getString(R.string.loaded_wallet_create_pay_id)
|
||||
payId = createPayId
|
||||
btnLoadItems.add(createPayId)
|
||||
}
|
||||
}
|
||||
ctx.error = result.error?.localizedMessage
|
||||
refresh()
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
// set listeners
|
||||
srl.setOnRefreshListener { refresh() }
|
||||
|
|
@ -149,16 +190,17 @@ class LoadedWalletFragment : BaseFragment(), NavigationResultListener, NfcAdapte
|
|||
|
||||
btnCopy.setOnClickListener { doShareWallet(false) }
|
||||
|
||||
|
||||
|
||||
if (Util.bytesToHex(ctx.card?.cid)?.startsWith("10") == true) {
|
||||
btnLoad?.visibility = View.GONE
|
||||
}
|
||||
|
||||
btnLoad.setOnClickListener {
|
||||
val items = arrayOf<CharSequence>(getString(R.string.loaded_wallet_load_via_app), getString(R.string.loaded_wallet_load_via_share_address), getString(R.string.loaded_wallet_load_via_qr))//, getString(R.string.via_cryptonit), getString(R.string.via_kraken))
|
||||
val cw = android.view.ContextThemeWrapper(activity, R.style.AlertDialogTheme)
|
||||
val dialog = AlertDialog.Builder(cw).setItems(items
|
||||
val dialog = AlertDialog.Builder(cw).setItems(btnLoadItems.toTypedArray()
|
||||
) { _, which ->
|
||||
when (items[which]) {
|
||||
when (btnLoadItems[which]) {
|
||||
getString(R.string.loaded_wallet_load_via_app) -> {
|
||||
try {
|
||||
val intent = Intent(Intent.ACTION_VIEW, engine.shareWalletUriEx)
|
||||
|
|
@ -176,22 +218,14 @@ class LoadedWalletFragment : BaseFragment(), NavigationResultListener, NfcAdapte
|
|||
getString(R.string.loaded_wallet_load_via_qr) -> {
|
||||
ShowQRCodeDialog.show(activity as AppCompatActivity?, engine.shareWalletUriEx.toString())
|
||||
}
|
||||
|
||||
getString(R.string.loaded_wallet_load_via_cryptonit) -> {
|
||||
navigateForResult(
|
||||
Constant.REQUEST_CODE_RECEIVE_TRANSACTION,
|
||||
R.id.action_loadedWalletFragment_to_prepareCryptonitWithdrawalFragment,
|
||||
Bundle().apply { ctx.saveToBundle(this) }
|
||||
)
|
||||
payId -> {
|
||||
if (payId == getString(R.string.loaded_wallet_create_pay_id)) {
|
||||
createPayIdDialog()
|
||||
} else {
|
||||
payId?.let { copyText(it) }
|
||||
}
|
||||
}
|
||||
|
||||
getString(R.string.loaded_wallet_load_via_kraken) -> {
|
||||
navigateForResult(
|
||||
Constant.REQUEST_CODE_RECEIVE_TRANSACTION,
|
||||
R.id.action_loadedWalletFragment_to_prepareKrakenWithdrawalFragment,
|
||||
Bundle().apply { ctx.saveToBundle(this) }
|
||||
)
|
||||
}
|
||||
else -> {
|
||||
}
|
||||
}
|
||||
|
|
@ -297,10 +331,9 @@ class LoadedWalletFragment : BaseFragment(), NavigationResultListener, NfcAdapte
|
|||
startVerify(lastTag)
|
||||
|
||||
|
||||
viewModel = ViewModelProviders.of(this).get(LoadedWalletViewModel::class.java)
|
||||
|
||||
// set rate info to CoinData
|
||||
viewModel.getRateInfo().observe(this, Observer<Float> { rate ->
|
||||
viewModel.getRateInfo().observe(viewLifecycleOwner, Observer<Float> { rate ->
|
||||
ctx.coinData.rate = rate
|
||||
ctx.coinData.rateAlter = rate
|
||||
updateViews()
|
||||
|
|
@ -308,6 +341,54 @@ class LoadedWalletFragment : BaseFragment(), NavigationResultListener, NfcAdapte
|
|||
viewModel.requestRateInfo(ctx)
|
||||
}
|
||||
|
||||
private fun createPayIdDialog() {
|
||||
val dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_pay_id, null);
|
||||
val alertDialogBuilderUserInput = AlertDialog.Builder(context);
|
||||
alertDialogBuilderUserInput.setView(dialogView);
|
||||
alertDialogBuilderUserInput
|
||||
.setCancelable(true)
|
||||
.setPositiveButton(getString(R.string.create_pay_id_create)) { dialogBox, _ ->
|
||||
if (dialogView.etPayId.text.isNullOrBlank()) {
|
||||
Toast.makeText(context, getString(R.string.create_pay_id_empty), Toast.LENGTH_LONG).show()
|
||||
return@setPositiveButton
|
||||
}
|
||||
|
||||
dialogBox.cancel()
|
||||
createPayId("${dialogView.etPayId.text}$PAY_ID_TANGEM")
|
||||
}
|
||||
|
||||
.setNegativeButton(getString(R.string.general_cancel)){ dialogBox, _ ->
|
||||
dialogBox.cancel()
|
||||
}
|
||||
|
||||
alertDialogBuilderUserInput.create().show()
|
||||
}
|
||||
|
||||
private fun createPayId(payId: String) {
|
||||
viewModel.setPayId(
|
||||
Util.byteArrayToHexString(ctx.card!!.cid!!),
|
||||
Util.byteArrayToHexString(ctx.card!!.cardPublicKey!!),
|
||||
payId,
|
||||
ctx.coinData.wallet,
|
||||
ctx.blockchain.id
|
||||
)
|
||||
.observe(viewLifecycleOwner, Observer { result ->
|
||||
when (result) {
|
||||
is Result.Success -> {
|
||||
this.payId = payId
|
||||
if (btnLoadItems.last() == getString(R.string.loaded_wallet_create_pay_id)) {
|
||||
btnLoadItems.removeAt(btnLoadItems.lastIndex)
|
||||
}
|
||||
btnLoadItems.add(payId)
|
||||
Toast.makeText(context, getString(R.string.create_pay_id_success), Toast.LENGTH_LONG).show()
|
||||
}
|
||||
is Result.Failure -> {
|
||||
Toast.makeText(context, getString(R.string.create_pay_id_error), Toast.LENGTH_LONG).show()
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
override fun onPause() {
|
||||
super.onPause()
|
||||
if (timerHideErrorAndMessage != null) {
|
||||
|
|
@ -836,7 +917,7 @@ class LoadedWalletFragment : BaseFragment(), NavigationResultListener, NfcAdapte
|
|||
val chooser = Intent.createChooser(intent, getString(R.string.loaded_wallet_chooser_share))
|
||||
|
||||
// verify the intent will resolve to at least one activity
|
||||
if (intent.resolveActivity(activity!!.packageManager) != null) {
|
||||
if (intent.resolveActivity(requireActivity().packageManager) != null) {
|
||||
startActivity(chooser)
|
||||
}
|
||||
} else {
|
||||
|
|
@ -845,11 +926,14 @@ class LoadedWalletFragment : BaseFragment(), NavigationResultListener, NfcAdapte
|
|||
Toast.makeText(activity, R.string.loaded_wallet_toast_copied, Toast.LENGTH_LONG).show()
|
||||
}
|
||||
} else {
|
||||
val txtShare = ctx.coinData.wallet
|
||||
val clipboard = activity?.getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
|
||||
clipboard.primaryClip = ClipData.newPlainText(txtShare, txtShare)
|
||||
Toast.makeText(activity, R.string.loaded_wallet_toast_copied, Toast.LENGTH_LONG).show()
|
||||
copyText(ctx.coinData.wallet)
|
||||
}
|
||||
}
|
||||
|
||||
private fun copyText(text: String) {
|
||||
val clipboard = activity?.getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
|
||||
clipboard.primaryClip = ClipData.newPlainText(text, text)
|
||||
Toast.makeText(activity, R.string.loaded_wallet_toast_copied, Toast.LENGTH_LONG).show()
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,15 +1,18 @@
|
|||
package com.tangem.ui.fragment.wallet
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.*
|
||||
import com.tangem.data.network.ServerApiCommon
|
||||
import com.tangem.data.network.ServerApiCommon.RateInfoListener
|
||||
import com.tangem.data.network.model.RateInfoResponse
|
||||
import com.tangem.server_android.PayIdResponse
|
||||
import com.tangem.server_android.PayIdService
|
||||
import com.tangem.server_android.Result
|
||||
import com.tangem.server_android.SetPayIdResponse
|
||||
import com.tangem.wallet.TangemContext
|
||||
|
||||
class LoadedWalletViewModel : ViewModel() {
|
||||
private var serverApiCommon: ServerApiCommon = ServerApiCommon()
|
||||
private val serverApiCommon: ServerApiCommon = ServerApiCommon()
|
||||
private val payIdService = PayIdService()
|
||||
private lateinit var ctx: TangemContext
|
||||
private val rate: MutableLiveData<Float> by lazy {
|
||||
MutableLiveData<Float>().also {
|
||||
|
|
@ -17,6 +20,8 @@ class LoadedWalletViewModel : ViewModel() {
|
|||
}
|
||||
}
|
||||
|
||||
private val payId: LiveData<Result<PayIdResponse>> = MutableLiveData()
|
||||
|
||||
fun getRateInfo(): LiveData<Float> {
|
||||
return rate
|
||||
}
|
||||
|
|
@ -28,7 +33,7 @@ class LoadedWalletViewModel : ViewModel() {
|
|||
}
|
||||
|
||||
private fun loadRateInfo() {
|
||||
serverApiCommon.setRateInfoListener(object: RateInfoListener{
|
||||
serverApiCommon.setRateInfoListener(object : RateInfoListener {
|
||||
override fun onSuccess(rateInfoResponse: RateInfoResponse?) {
|
||||
rate.value = rateInfoResponse!!.data!!.quote!!.usd!!.price
|
||||
}
|
||||
|
|
@ -38,4 +43,31 @@ class LoadedWalletViewModel : ViewModel() {
|
|||
}
|
||||
})
|
||||
}
|
||||
|
||||
fun getPayId(cardId: String, publicKey: String): LiveData<Result<PayIdResponse>> {
|
||||
return liveData(viewModelScope.coroutineContext) {
|
||||
emit(
|
||||
payIdService.getPayId(
|
||||
cardId,
|
||||
publicKey
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun setPayId(
|
||||
cardId: String,
|
||||
publicKey: String,
|
||||
payId: String,
|
||||
address: String,
|
||||
network: String
|
||||
): LiveData<Result<SetPayIdResponse>> {
|
||||
return liveData(viewModelScope.coroutineContext) {
|
||||
emit(
|
||||
payIdService.setPayId(
|
||||
cardId, publicKey, payId, address, network
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue