Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -562,10 +562,6 @@ class TokenRepository

fun getPendingDisplays() = safeSnapshotDao.getPendingDisplays()

suspend fun pendingDeposits(
asset: String,
) = tokenService.pendingDeposits(asset)

suspend fun clearAllPendingDeposits() = safeSnapshotDao.clearAllPendingDeposits()

suspend fun clearPendingDepositsByAssetId(assetId: String) =
Expand Down
39 changes: 39 additions & 0 deletions app/src/main/java/one/mixin/android/repository/Web3Repository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import one.mixin.android.crypto.CryptoWalletHelper
import one.mixin.android.db.TokenDao
import one.mixin.android.db.property.Web3PropertyHelper
import one.mixin.android.db.web3.Web3AddressDao
import one.mixin.android.db.web3.Web3ChainDao
import one.mixin.android.db.web3.Web3TokenDao
import one.mixin.android.db.web3.Web3TokensExtraDao
import one.mixin.android.db.web3.Web3TransactionDao
Expand All @@ -38,11 +39,13 @@ class Web3Repository
@Inject
constructor(
@ApplicationContext private val context: Context,
val tokenService: TokenService,
val routeService: RouteService,
val web3TokenDao: Web3TokenDao,
val web3TransactionDao: Web3TransactionDao,
val web3TokensExtraDao: Web3TokensExtraDao,
val web3AddressDao: Web3AddressDao,
val web3ChainDao: Web3ChainDao,
val web3WalletDao: Web3WalletDao,
val tokenRepository: TokenRepository,
val userRepository: UserRepository
Expand Down Expand Up @@ -211,6 +214,42 @@ constructor(

suspend fun getWalletByDestination(destination: String) = web3AddressDao.getWalletByDestination(destination)

suspend fun getWalletAddresses(walletId: String) = routeService.getWalletAddresses(walletId)

suspend fun getWalletAssets(walletId: String) = routeService.getWalletAssets(walletId)

suspend fun insertWalletAddresses(addresses: List<Web3Address>) {
web3AddressDao.insertList(addresses)
}

suspend fun insertWalletAssets(assets: List<Web3Token>) {
web3TokenDao.insertList(assets)
}

suspend fun updateBalanceToZeroForMissingAssets(walletId: String, assetIds: List<String>) {
web3TokenDao.updateBalanceToZeroForMissingAssets(walletId, assetIds)
}

suspend fun updateAllBalancesToZero(walletId: String) {
web3TokenDao.updateAllBalancesToZero(walletId)
}

suspend fun insertTokensExtra(extras: List<Web3TokensExtra>) {
web3TokensExtraDao.insertList(extras)
}

suspend fun findTokensExtraByAssetId(assetId: String, walletId: String): Web3TokensExtra? {
return web3TokensExtraDao.findByAssetId(assetId, walletId)
}

suspend fun chainExists(chainId: String) = web3ChainDao.chainExists(chainId)

suspend fun insertChain(chain: one.mixin.android.db.web3.vo.Web3Chain) {
web3ChainDao.insert(chain)
}

suspend fun getChainById(chainId: String) = tokenService.getChainById(chainId)

// Only deposit display
suspend fun getTokenByWalletAndAssetId(walletId: String, assetId: String): Web3TokenItem? {
val localToken = web3TokenDao.web3TokenItemById(walletId, assetId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ import one.mixin.android.util.isIcapAddress
import one.mixin.android.util.rxpermission.RxPermissions
import one.mixin.android.util.viewBinding
import one.mixin.android.vo.Address
import one.mixin.android.vo.WalletCategory
import one.mixin.android.vo.WithdrawalMemoPossibility
import one.mixin.android.vo.safe.TokenItem
import timber.log.Timber
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package one.mixin.android.ui.common.refresh

import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import one.mixin.android.api.handleMixinResponse
import one.mixin.android.ui.wallet.WalletViewModel
import one.mixin.android.util.reportException
import one.mixin.android.vo.safe.toSnapshot
import timber.log.Timber

object PendingDepositRefreshHelper {

fun startRefreshData(
fragment: Fragment,
walletViewModel: WalletViewModel,
refreshJob: Job?,
onPendingDepositUpdated: (() -> Unit)? = null
): Job? {
refreshJob?.cancel()
return fragment.lifecycleScope.launch(Dispatchers.IO) {
refreshPendingDepositData(
walletViewModel,
onPendingDepositUpdated
)
}
}

fun cancelRefreshData(refreshJob: Job?): Job? {
refreshJob?.cancel()
return null
}

private suspend fun refreshPendingDepositData(
walletViewModel: WalletViewModel,
onPendingDepositUpdated: (() -> Unit)? = null
) {
try {
while (true) {
handleMixinResponse(
invokeNetwork = { walletViewModel.allPendingDeposit() },
exceptionBlock = { e ->
reportException(e)
false
},
successBlock = {
val pendingDeposits = it.data ?: emptyList()
val destinationTags = walletViewModel.findDepositEntryDestinations()
pendingDeposits
.filter { pd ->
destinationTags.any { dt ->
dt.destination == pd.destination && (dt.tag.isNullOrBlank() || dt.tag == pd.tag)
}
}
.map { pd -> pd.toSnapshot() }.let { snapshots ->
// If there are no pending deposit snapshots belonging to the current user, clear all pending deposits
if (snapshots.isEmpty()) {
walletViewModel.clearAllPendingDeposits()
return@let
}
snapshots.map { it.assetId }.distinct().forEach {
walletViewModel.findOrSyncAsset(it)
}
walletViewModel.insertPendingDeposit(snapshots)
onPendingDepositUpdated?.invoke()
}
},
)

delay(10_000)
}
} catch (e: Exception) {
Timber.e(e, "Error refreshing pending deposits")
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package one.mixin.android.ui.common
package one.mixin.android.ui.common.refresh

import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
Expand All @@ -12,7 +13,7 @@ import one.mixin.android.ui.home.web3.Web3ViewModel
import one.mixin.android.web3.js.Web3Signer
import timber.log.Timber

object PendingTransactionRefreshHelper {
object PendingWeb3TransactionRefreshHelper {

fun startRefreshData(
fragment: Fragment,
Expand All @@ -22,7 +23,7 @@ object PendingTransactionRefreshHelper {
onTransactionStatusUpdated: ((hash: String, newStatus: String) -> Unit)? = null
): Job? {
refreshJob?.cancel()
return fragment.lifecycleScope.launch {
return fragment.lifecycleScope.launch(Dispatchers.IO) {
refreshTransactionData(
Web3Signer.currentWalletId,
web3ViewModel,
Expand Down Expand Up @@ -74,7 +75,7 @@ object PendingTransactionRefreshHelper {
}
}
}
delay(5_000)
delay(10_000)
}
}
} catch (e: Exception) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package one.mixin.android.ui.common.refresh

import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import one.mixin.android.ui.home.web3.Web3ViewModel
import one.mixin.android.util.reportException
import timber.log.Timber

object WalletRefreshHelper {

fun startRefreshData(
fragment: Fragment,
web3ViewModel: Web3ViewModel,
walletId: String?,
refreshJob: Job?,
onWalletUpdated: (() -> Unit)? = null
): Job? {
refreshJob?.cancel()
return fragment.lifecycleScope.launch(Dispatchers.IO) {
refreshWalletData(
web3ViewModel,
walletId,
onWalletUpdated
)
}
}

fun cancelRefreshData(refreshJob: Job?): Job? {
refreshJob?.cancel()
return null
}

private suspend fun refreshWalletData(
web3ViewModel: Web3ViewModel,
walletId: String?,
onWalletUpdated: (() -> Unit)? = null
) {
try {
while (true) {
if (walletId == null) {
val allWallets = web3ViewModel.getAllWallets()
if (allWallets.isEmpty()) {
Timber.w("No wallets found, stopping refresh")
break
}

Timber.d("Found ${allWallets.size} wallets to refresh")

for (wallet in allWallets) {
try {
web3ViewModel.refreshWalletAddresses(wallet.id)

web3ViewModel.refreshWalletAssets(wallet.id)

Timber.d("Successfully refreshed wallet: ${wallet.id}")
} catch (e: Exception) {
Timber.e(e, "Error refreshing wallet: ${wallet.id}")
reportException(e)
}
}

onWalletUpdated?.invoke()
Timber.d("Successfully refreshed all ${allWallets.size} wallets")
} else {
val wallet = web3ViewModel.findWalletById(walletId)
if (wallet == null) {
Timber.w("Wallet not found: $walletId, stopping refresh")
break
}

try {
web3ViewModel.refreshWalletAddresses(walletId)

web3ViewModel.refreshWalletAssets(walletId)

onWalletUpdated?.invoke()
Timber.d("Successfully refreshed wallet: $walletId")

} catch (e: Exception) {
Timber.e(e, "Error refreshing wallet: $walletId")
reportException(e)
}
}

delay(10_000) // 10 seconds refresh interval
}
} catch (e: Exception) {
Timber.e(e, "Error in wallet refresh loop for walletId: $walletId")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ import one.mixin.android.extension.isStickerSupport
import one.mixin.android.extension.isVideo
import one.mixin.android.extension.isWebp
import one.mixin.android.extension.lateOneHours
import one.mixin.android.extension.navTo
import one.mixin.android.extension.networkConnected
import one.mixin.android.extension.nowInUtc
import one.mixin.android.extension.openAsUrlOrWeb
Expand Down Expand Up @@ -167,11 +166,9 @@ import one.mixin.android.ui.call.GroupUsersBottomSheetDialogFragment.Companion.G
import one.mixin.android.ui.common.GroupBottomSheetDialogFragment
import one.mixin.android.ui.common.LinkFragment
import one.mixin.android.ui.common.UserBottomSheetDialogFragment
import one.mixin.android.ui.common.biometric.buildEmptyTransferBiometricItem
import one.mixin.android.ui.common.message.ChatRoomHelper
import one.mixin.android.ui.common.profile.ProfileBottomSheetDialogFragment
import one.mixin.android.ui.common.showUserBottom
import one.mixin.android.ui.conversation.adapter.GalleryCallback
import one.mixin.android.ui.conversation.adapter.MentionAdapter
import one.mixin.android.ui.conversation.adapter.MentionAdapter.OnUserClickListener
import one.mixin.android.ui.conversation.adapter.Menu
Expand Down Expand Up @@ -205,9 +202,8 @@ import one.mixin.android.ui.sticker.StickerPreviewBottomSheetFragment
import one.mixin.android.ui.tip.TipActivity
import one.mixin.android.ui.tip.TipType
import one.mixin.android.ui.wallet.AssetListBottomSheetDialogFragment
import one.mixin.android.ui.wallet.AssetListBottomSheetDialogFragment.Companion.TYPE_FROM_TRANSFER
import one.mixin.android.ui.wallet.AssetListBottomSheetDialogFragment.Companion.ASSET_PREFERENCE
import one.mixin.android.ui.wallet.InputFragment
import one.mixin.android.ui.wallet.AssetListBottomSheetDialogFragment.Companion.TYPE_FROM_TRANSFER
import one.mixin.android.ui.wallet.TransactionFragment
import one.mixin.android.ui.wallet.WalletActivity
import one.mixin.android.ui.web.WebActivity
Expand Down Expand Up @@ -238,7 +234,6 @@ import one.mixin.android.vo.MessageItem
import one.mixin.android.vo.MessageStatus
import one.mixin.android.vo.ParticipantRole
import one.mixin.android.vo.PinMessageData
import one.mixin.android.vo.Plan
import one.mixin.android.vo.Sticker
import one.mixin.android.vo.TranscriptData
import one.mixin.android.vo.TranscriptMessage
Expand Down Expand Up @@ -282,11 +277,9 @@ import one.mixin.android.widget.ChatControlView
import one.mixin.android.widget.CircleProgress.Companion.STATUS_PLAY
import one.mixin.android.widget.ContentEditText
import one.mixin.android.widget.DraggableRecyclerView
import one.mixin.android.widget.DraggableRecyclerView.Companion.FLING_DOWN
import one.mixin.android.widget.LinearSmoothScrollerCustom
import one.mixin.android.widget.MixinHeadersDecoration
import one.mixin.android.widget.buildBottomSheetView
import one.mixin.android.widget.gallery.internal.entity.Item
import one.mixin.android.widget.gallery.ui.GalleryActivity.Companion.IS_VIDEO
import one.mixin.android.widget.keyboard.KeyboardLayout.OnKeyboardHiddenListener
import one.mixin.android.widget.keyboard.KeyboardLayout.OnKeyboardShownListener
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ import one.mixin.android.vo.AppCardData
import one.mixin.android.vo.MessageCategory
import one.mixin.android.vo.MessageItem
import one.mixin.android.vo.MessageStatus
import one.mixin.android.vo.Plan
import one.mixin.android.vo.User
import one.mixin.android.vo.create
import one.mixin.android.vo.isAppCard
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package one.mixin.android.ui.conversation.chathistory.holder

import android.graphics.ColorFilter
import android.view.View
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import one.mixin.android.api.request.TransferRequest
import one.mixin.android.api.response.PaymentStatus
import one.mixin.android.extension.defaultSharedPreferences
import one.mixin.android.extension.isUUID
import one.mixin.android.extension.navTo
import one.mixin.android.extension.nowInUtc
import one.mixin.android.extension.putString
import one.mixin.android.pay.parseExternalTransferUri
Expand All @@ -30,7 +29,6 @@ import one.mixin.android.ui.conversation.link.CollectionBottomSheetDialogFragmen
import one.mixin.android.ui.wallet.AssetListBottomSheetDialogFragment
import one.mixin.android.ui.wallet.AssetListBottomSheetDialogFragment.Companion.ASSET_PREFERENCE
import one.mixin.android.ui.wallet.AssetListBottomSheetDialogFragment.Companion.TYPE_FROM_TRANSFER
import one.mixin.android.ui.wallet.InputFragment
import one.mixin.android.ui.wallet.NetworkFee
import one.mixin.android.ui.wallet.WalletActivity
import one.mixin.android.ui.wallet.transfer.TransferBottomSheetDialogFragment
Expand Down
Loading
Loading