Skip to content

Commit

Permalink
update refresh snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
crossle committed Jul 18, 2024
1 parent 75284ce commit e6985ea
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions app/src/main/java/one/mixin/android/job/RefreshSnapshotsJob.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<SafeSnapshot>
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<SafeSnapshot>? {
val response = tokenService.getSnapshots(offset = offset, limit = LIMIT)
if (response.isSuccess && response.data != null) {
val snapshots = response.data as List<SafeSnapshot>
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
}
}

0 comments on commit e6985ea

Please sign in to comment.