Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
SeniorZhai committed Mar 7, 2025
1 parent 1bfcb09 commit bd004ac
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 24 deletions.
5 changes: 4 additions & 1 deletion app/src/main/java/one/mixin/android/db/web3/Web3ChainDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ import one.mixin.android.db.web3.vo.Web3Chain
import one.mixin.android.vo.Chain

@Dao
interface Web3ChainDao : BaseDao<Web3Chain>
interface Web3ChainDao : BaseDao<Web3Chain> {
@Query("SELECT chain_id FROM chains WHERE chain_id = :chainId LIMIT 1")
suspend fun chainExists(chainId: String): String
}
36 changes: 20 additions & 16 deletions app/src/main/java/one/mixin/android/job/RefreshWeb3Job.kt
Original file line number Diff line number Diff line change
Expand Up @@ -170,28 +170,32 @@ class RefreshWeb3Job : BaseJob(
private suspend fun fetchChain(chainIds: List<String>) {
chainIds.forEach { chainId ->
try {
val response = tokenService.getChainById(chainId)
if (response.isSuccess) {
val chain = response.data
if (chain != null) {
web3ChainDao.insert(
Web3Chain(
chainId = chain.chainId,
name = chain.name,
symbol = chain.symbol,
iconUrl = chain.iconUrl,
threshold = chain.threshold,
if (web3ChainDao.chainExists(chainId) == null) {
val response = tokenService.getChainById(chainId)
if (response.isSuccess) {
val chain = response.data
if (chain != null) {
web3ChainDao.insert(
Web3Chain(
chainId = chain.chainId,
name = chain.name,
symbol = chain.symbol,
iconUrl = chain.iconUrl,
threshold = chain.threshold,
)
)
)
Timber.d("Successfully inserted ${chain?.name} chains into database")
Timber.d("Successfully inserted ${chain.name} chain into database")
} else {
Timber.d("No chain found for chainId: $chainId")
}
} else {
Timber.d("No chains found")
Timber.e("Failed to fetch chain $chainId: ${response.errorCode} - ${response.errorDescription}")
}
} else {
Timber.e("Failed to fetch chains: ${response.errorCode} - ${response.errorDescription}")
Timber.d("Chain $chainId already exists in local database, skipping fetch")
}
} catch (e: Exception) {
Timber.e(e, "Exception occurred while fetching chains")
Timber.e(e, "Exception occurred while fetching chain $chainId")
}
}
}
Expand Down
73 changes: 71 additions & 2 deletions app/src/main/java/one/mixin/android/job/RefreshWeb3TokenJob.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ package one.mixin.android.job

import com.birbit.android.jobqueue.Params
import kotlinx.coroutines.runBlocking
import one.mixin.android.Constants.RouteConfig.ROUTE_BOT_USER_ID
import one.mixin.android.db.web3.vo.Web3Chain
import one.mixin.android.ui.wallet.fiatmoney.requestRouteAPI
import timber.log.Timber

class RefreshWeb3TokenJob(
private val walletId: String,
private val assetId: String
private val assetId: String?
) : BaseJob(Params(PRIORITY_UI_HIGH).requireNetwork().setGroupId(GROUP)) {
companion object {
private const val serialVersionUID = 1L
Expand All @@ -16,9 +19,75 @@ class RefreshWeb3TokenJob(
override fun onRun(): Unit =
runBlocking {
try {
// Todo refresh
if (assetId == null) {
fetchWalletAssets(walletId)
} else {
// todo
}
} catch (e: Exception) {
Timber.e(e)
}
}

private suspend fun fetchWalletAssets(walletId: String) {
requestRouteAPI(
invokeNetwork = {
routeService.getWalletAssets(walletId)
},
successBlock = { response ->
val assets = response.data
if (assets != null && assets.isNotEmpty()) {
Timber.d("Fetched ${assets.size} assets for wallet ${walletId}")
if (assets.isNotEmpty()) {
web3TokenDao.insertList(assets)
fetchChain(assets.map { it.chainId }.distinct())
Timber.d("Inserted ${assets.size} tokens into database")
}
} else {
Timber.d("No assets found for wallet ${walletId}")
}
},
failureBlock = { response ->
Timber.e("Failed to fetch assets for wallet ${walletId}: ${response.errorCode} - ${response.errorDescription}")
false
},
requestSession = {
userService.fetchSessionsSuspend(listOf(ROUTE_BOT_USER_ID))
},
defaultErrorHandle = {}
)
}

private suspend fun fetchChain(chainIds: List<String>) {
chainIds.forEach { chainId ->
try {
if (web3ChainDao.chainExists(chainId) == null) {
val response = tokenService.getChainById(chainId)
if (response.isSuccess) {
val chain = response.data
if (chain != null) {
web3ChainDao.insert(
Web3Chain(
chainId = chain.chainId,
name = chain.name,
symbol = chain.symbol,
iconUrl = chain.iconUrl,
threshold = chain.threshold,
)
)
Timber.d("Successfully inserted ${chain.name} chain into database")
} else {
Timber.d("No chain found for chainId: $chainId")
}
} else {
Timber.e("Failed to fetch chain $chainId: ${response.errorCode} - ${response.errorDescription}")
}
} else {
Timber.d("Chain $chainId already exists in local database, skipping fetch")
}
} catch (e: Exception) {
Timber.e(e, "Exception occurred while fetching chain $chainId")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import one.mixin.android.extension.numberFormat8
import one.mixin.android.extension.putBoolean
import one.mixin.android.job.MixinJobManager
import one.mixin.android.job.RefreshWeb3Job
import one.mixin.android.job.RefreshWeb3TokenJob
import one.mixin.android.job.RefreshWeb3TransactionJob
import one.mixin.android.session.Session
import one.mixin.android.ui.address.TransferDestinationInputFragment
Expand Down Expand Up @@ -238,6 +239,9 @@ class ClassicWalletFragment : BaseFragment(R.layout.fragment_privacy_wallet), He

fun update() {
jobManager.addJobInBackground(RefreshWeb3TransactionJob())
if (walletId.isEmpty().not()) {
jobManager.addJobInBackground(RefreshWeb3TokenJob(walletId = walletId, assetId = null))
}
}

override fun onStop() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,9 @@ class WalletFragment : BaseFragment(R.layout.fragment_wallet) {
closeMenu()
}

override fun onHiddenChanged(hidden: Boolean) {
super.onHiddenChanged(hidden)
if (!hidden) {
if (classicWalletFragment.isVisible) classicWalletFragment.update()
}
override fun onResume() {
super.onResume()
if (classicWalletFragment.isVisible) classicWalletFragment.update()
}

private fun closeMenu() {
Expand Down

0 comments on commit bd004ac

Please sign in to comment.