Skip to content

Commit 12b60dc

Browse files
authored
fix: schedule cache cleanup when tiles are evicted (#3453)
1 parent 788cee8 commit 12b60dc

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.kylecorry.trail_sense.shared.map_layers.tiles.infrastructure.persistance
2+
3+
import android.content.Context
4+
import androidx.work.CoroutineWorker
5+
import androidx.work.WorkerParameters
6+
import com.kylecorry.andromeda.background.IOneTimeTaskScheduler
7+
import com.kylecorry.andromeda.background.OneTimeTaskSchedulerFactory
8+
9+
class CachedTileCleanupWorker(
10+
private val context: Context,
11+
params: WorkerParameters
12+
) : CoroutineWorker(context, params) {
13+
14+
override suspend fun doWork(): Result {
15+
return try {
16+
CachedTileRepo.getInstance(context).clean()
17+
Result.success()
18+
} catch (e: Exception) {
19+
Result.failure()
20+
}
21+
}
22+
23+
companion object {
24+
private const val UNIQUE_ID = 91420833
25+
26+
private fun getScheduler(context: Context): IOneTimeTaskScheduler {
27+
return OneTimeTaskSchedulerFactory(context.applicationContext).deferrable(
28+
CachedTileCleanupWorker::class.java,
29+
UNIQUE_ID
30+
)
31+
}
32+
33+
fun start(context: Context) {
34+
getScheduler(context).start()
35+
}
36+
}
37+
}

app/src/main/java/com/kylecorry/trail_sense/shared/map_layers/tiles/infrastructure/persistance/PersistentTileCache.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ import java.util.UUID
1313

1414
class PersistentTileCache(context: Context) {
1515

16-
private val repo = CachedTileRepo.getInstance(context)
17-
private val files = CacheFileSystem(context)
16+
private val appContext = context.applicationContext
17+
private val repo = CachedTileRepo.getInstance(appContext)
18+
private val files = CacheFileSystem(appContext)
1819
private val imageSaver = ImageSaver()
1920

2021
suspend fun getOrPut(key: String, tile: Tile, producer: suspend () -> Bitmap): Bitmap = onIO {
@@ -39,6 +40,7 @@ class PersistentTileCache(context: Context) {
3940
val totalSize = repo.getTotalSizeBytes()
4041
if (totalSize > MAX_CACHE_SIZE_BYTES) {
4142
repo.deleteLeastRecentlyUsed(EVICTION_COUNT)
43+
CachedTileCleanupWorker.start(appContext)
4244
}
4345

4446
bitmap

0 commit comments

Comments
 (0)