Skip to content

Commit 2e38382

Browse files
feat(cache): Move cache files to cache directory
This commit moves the `apps_cache.json` and `contacts_cache.json` files from the `filesDir` to the `cacheDir`. A migration step has been added to delete the old cache files from the previous location upon application startup.
1 parent f4f8eec commit 2e38382

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

app/src/main/java/com/github/droidworksstudio/mlauncher/MainActivity.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@ class MainActivity : AppCompatActivity() {
520520
private fun migration() {
521521
migration.migratePreferencesOnVersionUpdate(prefs)
522522
migration.migrateMessages(prefs)
523+
migration.deleteOldCacheFiles(applicationContext)
523524
}
524525

525526
}

app/src/main/java/com/github/droidworksstudio/mlauncher/MainViewModel.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
6262
private val prefs = Prefs(appContext)
6363

6464
// Cache files
65-
private val appsCacheFile = File(appContext.filesDir, "apps_cache.json")
66-
private val contactsCacheFile = File(appContext.filesDir, "contacts_cache.json")
65+
private val appsCacheFile = File(appContext.cacheDir, "apps_cache.json")
66+
private val contactsCacheFile = File(appContext.cacheDir, "contacts_cache.json")
6767

6868
// in-memory caches for instant load
6969
private var appsMemoryCache: MutableList<AppListItem>? = null

app/src/main/java/com/github/droidworksstudio/mlauncher/data/Migration.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.github.droidworksstudio.mlauncher.data
33
import android.content.Context
44
import com.github.droidworksstudio.common.AppLogger
55
import com.github.droidworksstudio.mlauncher.BuildConfig
6+
import java.io.File
67

78
class Migration(val context: Context) {
89
fun migratePreferencesOnVersionUpdate(prefs: Prefs) {
@@ -90,4 +91,21 @@ class Migration(val context: Context) {
9091
}
9192
}
9293
}
94+
95+
fun deleteOldCacheFiles(appContext: Context) {
96+
// References to the old files in filesDir
97+
val oldAppsCacheFile = File(appContext.filesDir, "apps_cache.json")
98+
val oldContactsCacheFile = File(appContext.filesDir, "contacts_cache.json")
99+
100+
// Delete them if they exist
101+
if (oldAppsCacheFile.exists()) {
102+
oldAppsCacheFile.delete()
103+
AppLogger.d("CacheCleanup", "apps_cache.json deleted")
104+
}
105+
106+
if (oldContactsCacheFile.exists()) {
107+
oldContactsCacheFile.delete()
108+
AppLogger.d("CacheCleanup", "contacts_cache.json deleted")
109+
}
110+
}
93111
}

0 commit comments

Comments
 (0)