File tree Expand file tree Collapse file tree
app/src/main/java/com/kylecorry/trail_sense/shared/map_layers/tiles/infrastructure/persistance Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change @@ -13,8 +13,9 @@ import java.util.UUID
1313
1414class 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
You can’t perform that action at this time.
0 commit comments