From e6985ea6bb12b7be91670a71bea960aacc9c88be Mon Sep 17 00:00:00 2001 From: Crossle Song Date: Thu, 18 Jul 2024 12:05:02 +0400 Subject: [PATCH] update refresh snapshots --- .../mixin/android/job/RefreshSnapshotsJob.kt | 36 +++++++++++-------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/one/mixin/android/job/RefreshSnapshotsJob.kt b/app/src/main/java/one/mixin/android/job/RefreshSnapshotsJob.kt index 4ec281cb76..46f05fb3ff 100644 --- a/app/src/main/java/one/mixin/android/job/RefreshSnapshotsJob.kt +++ b/app/src/main/java/one/mixin/android/job/RefreshSnapshotsJob.kt @@ -8,31 +8,37 @@ import one.mixin.android.ui.wallet.BaseTransactionsFragment.Companion.LIMIT import one.mixin.android.vo.safe.SafeSnapshot import org.threeten.bp.Instant -class RefreshSnapshotsJob : BaseJob(Params(PRIORITY_BACKGROUND).singleInstanceBy(GROUP).requireNetwork()) { +class RefreshSnapshotsJob : BaseJob(Params(PRIORITY_BACKGROUND).singleInstanceBy(GROUP).requireNetwork().persist()) { companion object { private const val serialVersionUID = 2L private val TIME_ZERO: String = Instant.ofEpochMilli(0).toString() const val GROUP = "RefreshSnapshotsJob" } - override fun onRun() = + override fun onRun(): Unit = runBlocking { val startPosition = PropertyHelper.findValueByKey(PREF_SNAPSHOT_OFFSET, TIME_ZERO) - val response = tokenService.getSnapshots(offset = startPosition, limit = LIMIT) - if (response.isSuccess && response.data != null) { - val list = response.data as List - safeSnapshotDao.insertListSuspend(list) - list.forEach { item -> - if (tokenDao.simpleAsset(item.assetId) == null) { - jobManager.addJobInBackground(RefreshTokensJob(item.assetId)) - } + refreshSnapshots(startPosition) + } + + private suspend fun refreshSnapshots(offset: String): List? { + val response = tokenService.getSnapshots(offset = offset, limit = LIMIT) + if (response.isSuccess && response.data != null) { + val snapshots = response.data as List + safeSnapshotDao.insertListSuspend(snapshots) + snapshots.forEach { item -> + if (tokenDao.simpleAsset(item.assetId) == null) { + jobManager.addJobInBackground(RefreshTokensJob(item.assetId)) } - list.lastOrNull()?.let { - PropertyHelper.updateKeyValue(PREF_SNAPSHOT_OFFSET, it.createdAt) - if (list.size >= LIMIT) { - jobManager.addJobInBackground(RefreshSnapshotsJob()) - } + } + snapshots.lastOrNull()?.let { + PropertyHelper.updateKeyValue(PREF_SNAPSHOT_OFFSET, it.createdAt) + if (snapshots.size >= LIMIT) { + return refreshSnapshots(it.createdAt) } } + return snapshots } + return null + } }