Skip to content

Commit

Permalink
bug fixeds
Browse files Browse the repository at this point in the history
  • Loading branch information
polstianka committed Oct 14, 2024
1 parent 65e44d7 commit e62218c
Show file tree
Hide file tree
Showing 19 changed files with 213 additions and 68 deletions.
8 changes: 0 additions & 8 deletions apps/wallet/api/src/main/java/com/tonapps/wallet/api/API.kt
Original file line number Diff line number Diff line change
Expand Up @@ -505,14 +505,6 @@ class API(
}
}

fun batteryApplyPromoCode(token: String, testnet: Boolean, code: String): Boolean {
return withRetry {
battery(testnet).promoCodeBatteryPurchase(token, PromoCodeBatteryPurchaseRequest(
promoCode = code
)).success
} ?: false
}

fun batteryVerifyPurchasePromo(testnet: Boolean, code: String): Boolean {
return withRetry {
battery(testnet).verifyPurchasePromo(code)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ data class SignRequestEntity(

fun setNetwork(network: TonNetwork) = apply { this.network = network }

fun setTestnet(testnet: Boolean) = setNetwork(if (testnet) TonNetwork.TESTNET else TonNetwork.MAINNET)

fun addMessage(message: RawMessageEntity) = apply { messages.add(message) }

fun build(): SignRequestEntity {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.flow.take
import kotlinx.coroutines.flow.timeout
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withTimeout
import kotlinx.coroutines.withTimeoutOrNull
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.time.Duration.Companion.seconds
Expand All @@ -45,9 +47,7 @@ class BillingManager(
.build()

private val _productsFlow = MutableStateFlow<List<ProductDetails>?>(null)
val productsFlow = _productsFlow.asStateFlow().filterNotNull().filter {
it.isNotEmpty()
}
val productsFlow = _productsFlow.asStateFlow().filterNotNull()

private val _purchasesUpdatedFlow = MutableEffectFlow<PurchasesUpdated>()
val purchasesUpdatedFlow = _purchasesUpdatedFlow.shareIn(scope, SharingStarted.Lazily, 0).distinctUntilChanged()
Expand All @@ -63,12 +63,37 @@ class BillingManager(
client.consumePurchase(params)
}

suspend fun getProducts(
suspend fun loadProducts(
productIds: List<String>,
productType: String = ProductType.INAPP
) = billingClient.ready { client ->
) {
val products = withTimeoutOrNull(5.seconds) {
getProducts(productIds, productType)
} ?: emptyList()

_productsFlow.value = products
}

fun setEmptyProducts() {
_productsFlow.value = emptyList()
}

private suspend fun getProductDetails(client: BillingClient, params: QueryProductDetailsParams): List<ProductDetails> = suspendCancellableCoroutine { continuation ->
client.queryProductDetailsAsync(params) { billingResult, productDetailsList ->
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
continuation.resume(productDetailsList)
} else {
continuation.resumeWithException(BillingException(billingResult))
}
}
}

private suspend fun getProducts(
productIds: List<String>,
productType: String = ProductType.INAPP
): List<ProductDetails> = billingClient.ready { client ->
if (productIds.isEmpty()) {
return@ready
return@ready emptyList()
}

val productList = productIds.map { productId ->
Expand All @@ -82,17 +107,7 @@ class BillingManager(
.setProductList(productList)
.build()

client.queryProductDetailsAsync(params) { billingResult, productDetailsList ->
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
_productsFlow.value = productDetailsList
} else {
_productsFlow.value = emptyList() // In case of an error
}
}
}

fun setEmptyProducts() {
_productsFlow.value = emptyList()
getProductDetails(client, params)
}

private suspend fun getPendingPurchases(client: BillingClient): List<Purchase> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class HistoryHelper(
if (burnAddress.equalsAddress(account.address) || (account.name != null && burnAddress == account.name)) {
return true
}
return "UQCNzZIsoe75gjl8KIwUJW1Fawt-7IbsFwd0ubGIFkig159E".equalsAddress(account.address)
return "UQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJKZ".equalsAddress(account.address)
}

private fun sort(list: List<HistoryItem>): List<HistoryItem.Event> {
Expand Down Expand Up @@ -410,7 +410,17 @@ class HistoryHelper(

if (action.jettonSwap != null) {
val jettonSwap = action.jettonSwap!!
val jettonPreview = jettonSwap.jettonPreview!!
val jettonPreview = jettonSwap.jettonPreview ?: return createUnknown(
index = index,
txId = txId,
action = action,
date = date,
timestamp = timestamp,
simplePreview = simplePreview,
dateDetails = dateDetails,
isScam = isScam,
wallet = wallet
)
val token = jettonSwap.jettonPreview!!.address
val amount = Coins.ofNano(jettonSwap.amount, jettonPreview.decimals)
val tokenIn = jettonSwap.tokenIn
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ data class DeepLink(

// fix for bad tg scheme
url = url.replace("tg:resolve", "tg://resolve")
url = url.replace("\\u0026", "&")

return Uri.parse(url)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tonapps.tonkeeper.deeplink

import android.net.Uri
import android.util.Log
import androidx.core.net.toUri
import com.tonapps.blockchain.ton.extensions.safePublicKey
import com.tonapps.extensions.hostOrNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.tonapps.tonkeeper.ui.screen.battery.recharge.list.Item
import com.tonapps.tonkeeper.ui.screen.battery.refill.entity.PromoState
import com.tonapps.tonkeeper.ui.screen.send.main.state.SendDestination
import com.tonapps.tonkeeper.ui.screen.send.transaction.SendTransactionScreen
import com.tonapps.tonkeeperx.BuildConfig
import com.tonapps.uikit.list.ListCell
import com.tonapps.wallet.api.API
import com.tonapps.wallet.api.entity.TokenEntity
Expand Down Expand Up @@ -176,7 +177,7 @@ class BatteryRechargeViewModel(

uiItems.addAll(uiItemsPacks(packs, selectedPackType, customAmount))

if (!api.config.batteryPromoDisable) {
if (BuildConfig.DEBUG || !api.config.batteryPromoDisable) {
uiItems.add(Item.Space)
uiItems.add(Item.Promo(promoState))
}
Expand Down Expand Up @@ -519,11 +520,13 @@ class BatteryRechargeViewModel(
}
promoStateFlow.tryEmit(PromoState.Loading())
try {
api.batteryVerifyPurchasePromo(wallet.testnet, promo)
val token = accountRepository.requestTonProofToken(wallet) ?: throw IllegalStateException("proof token is null")
batteryRepository.setAppliedPromo(wallet.testnet, promo)
api.batteryApplyPromoCode(token, wallet.testnet, promo)
promoStateFlow.tryEmit(PromoState.Applied(promo))
if (api.batteryVerifyPurchasePromo(wallet.testnet, promo)) {
batteryRepository.setAppliedPromo(wallet.testnet, promo)
promoStateFlow.tryEmit(PromoState.Applied(promo))
} else {
throw IllegalStateException("promo code is invalid")
}

} catch (_: Exception) {
batteryRepository.setAppliedPromo(wallet.testnet, null)
promoStateFlow.tryEmit(PromoState.Error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class BatteryRefillViewModel(
uiItems.add(uiItemBattery(batteryBalance, api.config))
uiItems.add(Item.Space)

if (!api.config.batteryPromoDisable) {
if (BuildConfig.DEBUG || !api.config.batteryPromoDisable) {
uiItems.add(Item.Promo(promoState))
uiItems.add(Item.Space)
}
Expand Down Expand Up @@ -137,7 +137,11 @@ class BatteryRefillViewModel(

init {
viewModelScope.launch(Dispatchers.IO) {
billingManager.getProducts(api.config.iapPackages.map { it.productId })
if (environment.isGooglePlayAvailable) {
billingManager.loadProducts(api.config.iapPackages.map { it.productId })
} else {
billingManager.setEmptyProducts()
}

val appliedPromo = batteryRepository.getAppliedPromo(wallet.testnet)

Expand Down Expand Up @@ -296,11 +300,12 @@ class BatteryRefillViewModel(
if (isInitial) {
delay(2000)
}
val token = accountRepository.requestTonProofToken(wallet) ?: throw IllegalStateException("proof token is null")
api.batteryVerifyPurchasePromo(wallet.testnet, promo)
api.batteryApplyPromoCode(token, wallet.testnet, promo)
batteryRepository.setAppliedPromo(wallet.testnet, promo)
_promoStateFlow.value = PromoState.Applied(promo)
if (api.batteryVerifyPurchasePromo(wallet.testnet, promo)) {
batteryRepository.setAppliedPromo(wallet.testnet, promo)
_promoStateFlow.value = PromoState.Applied(promo)
} else {
throw IllegalStateException("promo code is invalid")
}
} catch (_: Exception) {
batteryRepository.setAppliedPromo(wallet.testnet, null)
_promoStateFlow.value = PromoState.Error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import com.tonapps.blockchain.ton.extensions.equalsAddress
import com.tonapps.blockchain.ton.extensions.toWalletAddress
import com.tonapps.extensions.getParcelableCompat
import com.tonapps.extensions.short4
import com.tonapps.extensions.toUriOrNull
import com.tonapps.tonkeeper.deeplink.DeepLink
import com.tonapps.tonkeeper.deeplink.DeepLinkRoute
import com.tonapps.tonkeeper.extensions.copyWithToast
import com.tonapps.tonkeeper.extensions.showToast
import com.tonapps.tonkeeper.extensions.toastLoading
Expand All @@ -23,6 +26,7 @@ import com.tonapps.tonkeeper.popup.ActionSheet
import com.tonapps.tonkeeper.ui.base.WalletContextScreen
import com.tonapps.tonkeeper.ui.screen.browser.dapp.DAppArgs
import com.tonapps.tonkeeper.ui.screen.browser.dapp.DAppScreen
import com.tonapps.tonkeeper.ui.screen.root.RootViewModel
import com.tonapps.tonkeeper.ui.screen.send.main.SendScreen
import com.tonapps.tonkeeperx.R
import com.tonapps.uikit.color.accentBlueColor
Expand All @@ -39,6 +43,7 @@ import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import org.koin.androidx.viewmodel.ext.android.activityViewModel
import org.koin.core.parameter.parametersOf
import uikit.base.BaseFragment
import uikit.dialog.alert.AlertDialog
Expand All @@ -61,6 +66,8 @@ class NftScreen(wallet: WalletEntity): WalletContextScreen(R.layout.fragment_nft
private val isCanSend: Boolean
get() = !wallet.isWatchOnly && !nftEntity.inSale && nftEntity.ownerAddress.equalsAddress(wallet.address)

private val rootViewModel: RootViewModel by activityViewModel()

override val viewModel: NftViewModel by walletViewModel { parametersOf(nftEntity) }

private val verificationIcon: Drawable by lazy {
Expand Down Expand Up @@ -220,7 +227,12 @@ class NftScreen(wallet: WalletEntity): WalletContextScreen(R.layout.fragment_nft
}

private fun mustOpenButtonDApp(url: String) {
navigation?.add(DAppScreen.newInstance(wallet, url = url.toUri()))
if (url.startsWith("ton:")) {
val uri = url.toUriOrNull() ?: return
rootViewModel.processDeepLink(uri, false, null, false)
} else {
navigation?.add(DAppScreen.newInstance(wallet, url = url.toUri()))
}
finish()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.content.res.Configuration
import android.net.Uri
import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.view.View
import androidx.biometric.BiometricPrompt
import androidx.core.app.ActivityCompat
Expand All @@ -13,7 +14,10 @@ import androidx.core.view.WindowInsetsCompat
import androidx.core.view.updatePadding
import com.google.android.play.core.appupdate.AppUpdateManager
import com.google.android.play.core.appupdate.AppUpdateManagerFactory
import com.tonapps.blockchain.ton.extensions.base64
import com.tonapps.extensions.currentTimeSeconds
import com.tonapps.extensions.toUriOrNull
import com.tonapps.icu.Coins
import com.tonapps.tonkeeper.App
import com.tonapps.tonkeeper.deeplink.DeepLink
import com.tonapps.tonkeeper.extensions.isDarkMode
Expand All @@ -34,6 +38,8 @@ import com.tonapps.tonkeeperx.R
import com.tonapps.wallet.api.entity.TokenEntity
import com.tonapps.wallet.data.account.entities.WalletEntity
import com.tonapps.wallet.data.core.Theme
import com.tonapps.wallet.data.core.entity.RawMessageEntity
import com.tonapps.wallet.data.core.entity.SignRequestEntity
import com.tonapps.wallet.data.passcode.LockScreen
import com.tonapps.wallet.data.passcode.PasscodeBiometric
import com.tonapps.wallet.data.passcode.PasscodeManager
Expand All @@ -43,6 +49,7 @@ import com.tonapps.wallet.data.settings.SettingsRepository
import com.tonapps.wallet.localization.Localization
import org.koin.android.ext.android.inject
import org.koin.androidx.viewmodel.ext.android.viewModel
import org.ton.cell.Cell
import uikit.base.BaseFragment
import uikit.dialog.alert.AlertDialog
import uikit.extensions.collectFlow
Expand Down Expand Up @@ -200,7 +207,8 @@ class RootActivity: BaseWalletActivity() {
tokenAddress = event.jettonAddress ?: TokenEntity.TON.address,
amountNano = event.amount ?: 0L,
text = event.text,
wallet = event.wallet
wallet = event.wallet,
bin = event.bin
)
is RootEvent.CloseCurrentTonConnect -> closeCurrentTonConnect {}
else -> { }
Expand All @@ -211,14 +219,48 @@ class RootActivity: BaseWalletActivity() {
removeByClass(runnable, SendTransactionScreen::class.java, TonConnectScreen::class.java)
}

private fun openSign(
wallet: WalletEntity,
targetAddress: String,
amountNano: Long,
bin: Cell
) {

val request = SignRequestEntity.Builder()
.setFrom(wallet.contract.address)
.setValidUntil(currentTimeSeconds())
.addMessage(RawMessageEntity(
addressValue = targetAddress,
amount = amountNano,
stateInitValue = null,
payloadValue = bin.base64()
))
.setTestnet(wallet.testnet)
.build()

val screen = SendTransactionScreen.newInstance(wallet, request)
add(screen)
}

private fun openSend(
wallet: WalletEntity,
targetAddress: String? = null,
tokenAddress: String = TokenEntity.TON.address,
amountNano: Long = 0,
text: String? = null,
nftAddress: String? = null
nftAddress: String? = null,
bin: Cell? = null
) {
if (bin != null && 0 >= amountNano) {
toast(Localization.invalid_link)
return
}

if (targetAddress != null && amountNano > 0 && bin != null) {
openSign(wallet, targetAddress, amountNano, bin)
return
}

val fragment = supportFragmentManager.findFragment<SendScreen>()
if (fragment == null) {
add(
Expand All @@ -229,6 +271,7 @@ class RootActivity: BaseWalletActivity() {
amountNano = amountNano,
text = text,
nftAddress = nftAddress,
bin = bin
)
)
} else {
Expand All @@ -237,6 +280,7 @@ class RootActivity: BaseWalletActivity() {
tokenAddress = tokenAddress,
amountNano = amountNano,
text = text,
bin = bin
)
}
}
Expand Down Expand Up @@ -320,9 +364,7 @@ class RootActivity: BaseWalletActivity() {
}

private fun openTelegramLink(uri: Uri) {
val intent = Intent(Intent.ACTION_VIEW, uri)
intent.`package` = "org.telegram.messenger"
if (!safeStartActivity(intent)) {
if (!safeStartActivity(Intent(Intent.ACTION_VIEW, uri))) {
BrowserHelper.open(this, uri)
}
}
Expand Down
Loading

0 comments on commit e62218c

Please sign in to comment.