Skip to content

Commit

Permalink
dapp push in history
Browse files Browse the repository at this point in the history
  • Loading branch information
polstianka committed Oct 18, 2024
1 parent 88ab78d commit f67e194
Show file tree
Hide file tree
Showing 59 changed files with 732 additions and 363 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.os.Parcelable
import android.util.Log
import com.tonapps.blockchain.ton.TONOpCode
import com.tonapps.blockchain.ton.extensions.loadOpCode
import com.tonapps.blockchain.ton.extensions.parseCell
import com.tonapps.blockchain.ton.extensions.safeParseCell
import com.tonapps.blockchain.ton.extensions.storeOpCode
import com.tonapps.blockchain.ton.extensions.toTlb
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import androidx.collection.ArrayMap
import androidx.core.net.toUri
import com.tonapps.blockchain.ton.extensions.toRawAddress
import com.tonapps.blockchain.ton.extensions.toUserFriendly
import com.tonapps.extensions.map
import com.tonapps.extensions.withoutQuery
import com.tonapps.wallet.api.API
import com.tonapps.wallet.data.dapps.entities.AppConnectEntity
import com.tonapps.wallet.data.dapps.entities.AppEntity
import com.tonapps.wallet.data.dapps.entities.AppPushEntity
import com.tonapps.wallet.data.dapps.source.DatabaseSource
import com.tonapps.wallet.data.rn.RNLegacy
import com.tonapps.wallet.data.rn.data.RNTC
Expand All @@ -37,6 +39,7 @@ class DAppsRepository(
context: Context,
private val scope: CoroutineScope,
private val rnLegacy: RNLegacy,
private val api: API,
) {

private val _connectionsFlow = MutableStateFlow<List<AppConnectEntity>?>(null)
Expand Down Expand Up @@ -73,6 +76,22 @@ class DAppsRepository(
}.flowOn(Dispatchers.IO).launchIn(scope)
}

suspend fun getPushes(tonProof: String, accountId: String): List<AppPushEntity> {
val pushes = api.getPushFromApps(tonProof, accountId).map { AppPushEntity.Body(it) }
if (pushes.isEmpty()) {
return emptyList()
}
val apps = getApps(pushes.map { it.dappUrl })

val list = mutableListOf<AppPushEntity>()
for (push in pushes) {
val app = apps.firstOrNull { it.url == push.dappUrl } ?: continue
list.add(AppPushEntity(app, push))
}

return list.toList()
}

suspend fun getConnections(
accountId: String,
testnet: Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ data class AppEntity(
val empty: Boolean
): Parcelable {

val id: String
get() = url.toString()

val host: String
get() = url.host ?: "unknown"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.tonapps.wallet.data.dapps.entities

import android.net.Uri
import androidx.core.net.toUri
import com.tonapps.extensions.getLongCompat
import org.json.JSONObject

data class AppPushEntity(
val from: AppEntity,
val body: Body
) {

val timestamp: Long
get() = body.dateCreate

val iconUrl: String
get() = from.iconUrl

val title: String
get() = body.title

val message: String
get() = body.message

val url: Uri
get() = body.dappUrl

val deeplink: String
get() = body.link

data class Body(
val dappUrl: Uri,
val title: String,
val message: String,
val link: String,
val account: String,
val dateCreate: Long
) {

constructor(json: JSONObject) : this(
dappUrl = json.getString("dapp_url").toUri(),
title = json.getString("title"),
message = json.getString("message"),
link = json.getString("link"),
account = json.getString("account"),
dateCreate = json.getLongCompat("date_create")
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ internal class DatabaseSource(
val notFoundApps = urls.filter { url -> apps.none { it.url == url } }
if (notFoundApps.isNotEmpty()) {
for (url in notFoundApps) {
val domain = url.host ?: "test.ton"
val domain = url.host ?: "unknown"
apps.add(AppEntity(
url = url,
name = domain.split(".").firstOrNull() ?: domain,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,11 @@ class EventsRepository(
accountId: String,
testnet: Boolean,
beforeLt: Long? = null,
limit: Int = 12
): AccountEvents? = withContext(Dispatchers.IO) {
try {
if (beforeLt != null) {
remoteDataSource.get(accountId, testnet, beforeLt)
remoteDataSource.get(accountId, testnet, beforeLt, limit)
} else {
val events = remoteDataSource.get(accountId, testnet)?.also {
localDataSource.setCache(cacheKey(accountId, testnet), it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.tonapps.tonkeeper.api

import android.util.Log
import com.squareup.moshi.adapter
import com.tonapps.blockchain.ton.extensions.equalsAddress
import com.tonapps.icu.Coins
import com.tonapps.blockchain.ton.extensions.toUserFriendly
import com.tonapps.extensions.ifPunycodeToUnicode
Expand All @@ -12,6 +13,7 @@ import com.tonapps.extensions.short12
import com.tonapps.extensions.short6
import com.tonapps.extensions.short8
import com.tonapps.tonkeeperx.R
import com.tonapps.wallet.data.account.entities.WalletEntity
import io.tonapi.infrastructure.Serializer
import io.tonapi.models.AccountAddress
import io.tonapi.models.AccountEvent
Expand All @@ -38,6 +40,23 @@ fun TokenRates.to(toCurrency: String, value: Float): Float {
return price.toFloat() * value
}

fun AccountEvent.isOutTransfer(wallet: WalletEntity): Boolean {
return actions.any { it.isOutTransfer(wallet) }
}

val Action.isTransfer: Boolean
get() {
return type == Action.Type.tonTransfer || type == Action.Type.jettonTransfer || type == Action.Type.nftItemTransfer
}

fun Action.isOutTransfer(wallet: WalletEntity): Boolean {
if (!isTransfer) {
return false
}
val sender = tonTransfer?.sender ?: jettonTransfer?.sender ?: nftItemTransfer?.sender ?: return false
return sender.address.equalsAddress(wallet.accountId)
}

/*val MessageConsequences.totalFees: Long
get() {
// = event.extra // trace.transaction.totalFees
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ data class WalletPurchaseMethodEntity(
val config: ConfigEntity,
): Parcelable {

@IgnoredOnParcel
val useCustomTabs: Boolean
get() = true // method.useCustomTabs

@IgnoredOnParcel
val successUrlPattern: SuccessUrlPattern?
get() = method.successUrlPattern
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class HistoryHelper(
private data class ActionDateSection(
val date: Long,
val dateFormat: String,
val events: MutableList<HistoryItem.Event>
val events: MutableList<HistoryItem>
) {

fun get(): List<HistoryItem> {
Expand All @@ -116,9 +116,9 @@ class HistoryHelper(
return "UQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJKZ".equalsAddress(account.address)
}

private fun sort(list: List<HistoryItem>): List<HistoryItem.Event> {
private fun sort(list: List<HistoryItem>): List<HistoryItem> {
return list
.filterIsInstance<HistoryItem.Event>()
.filter { it is HistoryItem.Event || it is HistoryItem.App }
.distinctBy { it.uniqueId }
.sortedWith { a, b ->
(b.timestampForSort - a.timestampForSort).toInt()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ class HistoryItemDecoration: RecyclerView.ItemDecoration() {
) {
super.getItemOffsets(outRect, view, parent, state)
val position = parent.getChildAdapterPosition(view)
if (position == 0) {
return
}

if (parent.isLayoutRequested) {
parent.doOnLayout {
getItemOffsets(outRect, view, parent, state)
}
return
}
/*if (position == 0) {
return
}*/

val holder = parent.findViewHolderForAdapterPosition(position) ?: return
val item = (holder as? BaseListHolder<*>)?.item ?: return

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.tonapps.tonkeeper.core.history.list.item

import android.content.Context
import android.net.Uri
import android.os.Parcel
import android.os.Parcelable
import androidx.core.net.toUri
import com.tonapps.blockchain.ton.extensions.toUserFriendly
import com.tonapps.extensions.ifPunycodeToUnicode
import com.tonapps.extensions.locale
import com.tonapps.extensions.readBooleanCompat
import com.tonapps.extensions.readCharSequenceCompat
import com.tonapps.extensions.readEnum
Expand All @@ -15,10 +18,12 @@ import com.tonapps.extensions.writeEnum
import com.tonapps.tonkeeper.core.history.ActionType
import com.tonapps.tonkeeper.core.history.recipient
import com.tonapps.tonkeeper.core.history.sender
import com.tonapps.tonkeeper.helper.DateHelper
import com.tonapps.uikit.list.BaseListItem
import com.tonapps.uikit.list.ListCell
import com.tonapps.wallet.data.account.entities.WalletEntity
import com.tonapps.wallet.data.collectibles.entities.NftEntity
import com.tonapps.wallet.data.dapps.entities.AppPushEntity
import io.tonapi.models.AccountAddress
import io.tonapi.models.EncryptedComment
import kotlinx.parcelize.IgnoredOnParcel
Expand Down Expand Up @@ -127,18 +132,30 @@ sealed class HistoryItem(
val title: String,
val body: String,
val date: String,
val host: String,
val url: Uri,
val timestamp: Long,
val deepLink: String,
val wallet: WalletEntity,
): HistoryItem(TYPE_APP) {


constructor(context: Context, wallet: WalletEntity, push: AppPushEntity) : this(
iconUri = push.iconUrl.toUri(),
title = push.title,
body = push.message,
date = DateHelper.formattedDate(push.timestamp, context.locale),
url = push.url,
timestamp = push.timestamp,
deepLink = push.deeplink,
wallet = wallet
)

constructor(parcel: Parcel) : this(
iconUri = parcel.readParcelableCompat()!!,
title = parcel.readString()!!,
body = parcel.readString()!!,
date = parcel.readString()!!,
host = parcel.readString()!!,
url = parcel.readParcelableCompat()!!,
timestamp = parcel.readLong(),
deepLink = parcel.readString()!!,
wallet = parcel.readParcelableCompat()!!
Expand All @@ -149,7 +166,7 @@ sealed class HistoryItem(
dest.writeString(title)
dest.writeString(body)
dest.writeString(date)
dest.writeString(host)
dest.writeParcelable(url, flags)
dest.writeLong(timestamp)
dest.writeString(deepLink)
dest.writeParcelable(wallet, flags)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@ import androidx.browser.customtabs.CustomTabColorSchemeParams
import androidx.browser.customtabs.CustomTabsIntent
import com.tonapps.extensions.activity
import com.tonapps.extensions.locale
import com.tonapps.tonkeeper.core.entities.WalletPurchaseMethodEntity
import com.tonapps.tonkeeper.extensions.showToast
import com.tonapps.uikit.color.backgroundPageColor
import com.tonapps.uikit.color.textPrimaryColor
import com.tonapps.wallet.localization.Localization

object BrowserHelper {

fun openPurchase(context: Context, method: WalletPurchaseMethodEntity) {
context.activity?.let {
open(it, method.uri)
}
}

fun open(context: Context, url: String) {
context.activity?.let {
open(it, url)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.tonapps.tonkeeper.koin

import androidx.lifecycle.viewmodel.viewModelFactory
import org.koin.dsl.module
import com.tonapps.tonkeeper.ui.screen.wallet.main.WalletViewModel
import com.tonapps.tonkeeper.ui.screen.settings.main.SettingsViewModel
Expand All @@ -21,7 +20,7 @@ import com.tonapps.tonkeeper.ui.screen.battery.settings.BatterySettingsViewModel
import com.tonapps.tonkeeper.ui.screen.battery.refill.BatteryRefillViewModel
import com.tonapps.tonkeeper.ui.screen.battery.recharge.BatteryRechargeViewModel
import com.tonapps.tonkeeper.ui.screen.send.contacts.main.SendContactsViewModel
import com.tonapps.tonkeeper.ui.screen.purchase.main.PurchaseViewModel
import com.tonapps.tonkeeper.ui.screen.purchase.PurchaseViewModel
import com.tonapps.tonkeeper.ui.screen.nft.NftViewModel
import com.tonapps.tonkeeper.ui.screen.send.contacts.add.AddContactViewModel
import com.tonapps.tonkeeper.ui.screen.send.contacts.edit.EditContactViewModel
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package com.tonapps.tonkeeper.manager.tonconnect.bridge

import android.util.Log
import com.google.firebase.crashlytics.FirebaseCrashlytics
import com.tonapps.blockchain.ton.proof.TONProof
import com.tonapps.extensions.base64
import com.tonapps.extensions.optStringCompat
import com.tonapps.security.CryptoBox
import com.tonapps.security.hex
import com.tonapps.tonkeeper.manager.tonconnect.bridge.model.BridgeError
import com.tonapps.tonkeeper.manager.tonconnect.bridge.model.BridgeEvent
import com.tonapps.wallet.api.API
import com.tonapps.wallet.data.account.entities.WalletEntity
import com.tonapps.wallet.data.dapps.entities.AppConnectEntity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
Expand Down

This file was deleted.

Loading

0 comments on commit f67e194

Please sign in to comment.