Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/src/main/java/one/mixin/android/db/MarketCoinDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ interface MarketCoinDao : BaseDao<MarketCoin> {
@Query("SELECT asset_id FROM market_coins WHERE coin_id = :coinId")
suspend fun findTokenIdsByCoinId(coinId: String): List<String>

@Query("SELECT coin_id FROM market_coins WHERE asset_id = :assetId")
suspend fun findCoinIdByTokenId(assetId: String): String?

@Query("DELETE FROM market_coins WHERE coin_id = :coinId AND asset_id IN (:assetIds)")
suspend fun deleteByCoinIdAndAssetIds(coinId: String, assetIds: List<String>)

@Query("DELETE FROM market_coins WHERE coin_id = :coinId")
suspend fun deleteByCoinId(coinId: String)
}
3 changes: 3 additions & 0 deletions app/src/main/java/one/mixin/android/db/MarketDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,7 @@ interface MarketDao : BaseDao<Market> {

@Query("SELECT m.* FROM markets m LEFT JOIN market_coins mc on mc.coin_id = m.coin_id WHERE mc.asset_id = :assetId")
suspend fun simpleCoinItemByAssetId(assetId: String): CoinItem?

@Query("DELETE FROM markets WHERE coin_id = :coinId")
suspend fun deleteByCoinId(coinId: String)
}
6 changes: 5 additions & 1 deletion app/src/main/java/one/mixin/android/db/MarketFavoredDao.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package one.mixin.android.db

import androidx.room.Dao
import androidx.room.Query
import one.mixin.android.vo.market.MarketFavored

@Dao
interface MarketFavoredDao : BaseDao<MarketFavored>
interface MarketFavoredDao : BaseDao<MarketFavored> {
@Query("DELETE FROM market_favored WHERE coin_id = :coinId")
suspend fun deleteByCoinId(coinId: String)
}
21 changes: 18 additions & 3 deletions app/src/main/java/one/mixin/android/job/RefreshMarketJob.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import one.mixin.android.extension.nowInUtc
import one.mixin.android.vo.market.MarketCoin
import timber.log.Timber

class RefreshMarketJob(private val assetId: String) : BaseJob(
class RefreshMarketJob(private val id: String, private val isCoinId: Boolean = false) : BaseJob(
Params(PRIORITY_UI_HIGH)
.addTags(GROUP).requireNetwork(),
) {
Expand All @@ -15,10 +15,15 @@ class RefreshMarketJob(private val assetId: String) : BaseJob(
const val GROUP = "RefreshMarketJob"
}

override fun onRun() = runBlocking {
val response = routeService.market(assetId)
override fun onRun(): Unit = runBlocking {
val response = routeService.market(id)
if (response.isSuccess && response.data != null) {
response.data?.let { market ->
val localCoinId = if (isCoinId.not()) {
marketCoinDao.findCoinIdByTokenId(id)
} else {
null
}
marketDao.insert(market)
val remoteAssetIds = market.assetIds ?: emptyList()
val localAssetIds = marketCoinDao.findTokenIdsByCoinId(market.coinId)
Expand All @@ -34,7 +39,17 @@ class RefreshMarketJob(private val assetId: String) : BaseJob(
createdAt = nowInUtc()
)
})

if (localCoinId != null && localCoinId != market.coinId) {
marketCoinDao.deleteByCoinId(localCoinId)
marketDao.deleteByCoinId(localCoinId)
marketFavoredDao.deleteByCoinId(localCoinId)
}
}
} else if (response.errorCode == 404 && isCoinId) {
marketCoinDao.deleteByCoinId(id)
marketFavoredDao.deleteByCoinId(id)
marketDao.deleteByCoinId(id)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class MarketDetailsFragment : BaseFragment(R.layout.fragment_details_market) {
savedInstanceState: Bundle?,
) {
super.onViewCreated(view, savedInstanceState)
jobManager.addJobInBackground(RefreshMarketJob(marketItem.coinId))
jobManager.addJobInBackground(RefreshMarketJob(marketItem.coinId, true))
AnalyticsTracker.trackMarketDetail(marketSource)
binding.apply {
titleView.apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ class TransactionsFragment : BaseFragment(R.layout.fragment_transactions), OnSna
}
var market = walletViewModel.findMarketItemByAssetId(asset.assetId)
if (market == null) {
jobManager.addJobInBackground(RefreshMarketJob(asset.assetId))
jobManager.addJobInBackground(RefreshMarketJob(asset.assetId, false))
market = MarketItem(
"", asset.name, asset.symbol, asset.iconUrl, asset.priceUsd,
"", "", "", "", "", runCatching {
Expand Down