Skip to content

Commit a4a2735

Browse files
Refactor: Improve preference migration and build script variables
The preference migration logic in `Migration.kt` has been enhanced to support wildcard matching for keys, allowing for more flexible cleanup of related preferences (e.g., `APP_ALIAS_*`). The migration now runs for versions between the saved and current version codes, inclusive. Additional logging has been added to provide detailed insight into which keys are removed during the process. In `app/build.gradle.kts`, the `versionCodeBase` variable has been renamed to `baseVersionCode` for clarity. The `app_version` string resource in the `debug` build type now correctly uses the `baseVersionCode` instead of the `baseVersionName`.
1 parent dc1a81f commit a4a2735

File tree

2 files changed

+50
-12
lines changed

2 files changed

+50
-12
lines changed

app/build.gradle.kts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ val build = 4
1616

1717
val baseVersionName = "$major.$minor.$patch Build $build"
1818

19-
val versionCodeBase =
19+
val baseVersionCode =
2020
(String.format("%02d", major) +
2121
String.format("%02d", minor) +
2222
String.format("%02d", patch) +
@@ -36,7 +36,7 @@ android {
3636
defaultConfig {
3737
minSdk = 28
3838
targetSdk = 36
39-
versionCode = versionCodeBase
39+
versionCode = baseVersionCode
4040
versionName = baseVersionName
4141
}
4242

@@ -96,7 +96,7 @@ android {
9696

9797
signingConfig = signingConfigs["release"]
9898

99-
resValue("string", "app_version", baseVersionName)
99+
resValue("string", "app_version", baseVersionCode.toString())
100100
resValue("string", "app_name", "Multi Launcher Debug")
101101
resValue("string", "empty", "")
102102
}

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

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.codeworkscreativehub.mlauncher.data
22

33
import android.content.Context
4+
import androidx.core.content.edit
45
import com.github.codeworkscreativehub.common.AppLogger
56
import com.github.codeworkscreativehub.mlauncher.BuildConfig
67
import java.io.File
@@ -10,7 +11,9 @@ class Migration(val context: Context) {
1011
val currentVersionCode = BuildConfig.VERSION_CODE
1112
val savedVersionCode = prefs.appVersion
1213

13-
// Define a map of version code -> preferences to clear
14+
AppLogger.d("PrefsMigration", "Starting migration: savedVersion=$savedVersionCode, currentVersion=$currentVersionCode")
15+
16+
// Map of version code -> preferences to clear (wildcards allowed)
1417
val versionCleanupMap = mapOf(
1518
171 to listOf(
1619
"APP_DARK_COLORS",
@@ -45,22 +48,57 @@ class Migration(val context: Context) {
4548
"SHORT_SWIPE_THRESHOLD",
4649
"LONG_SWIPE_THRESHOLD",
4750
),
48-
// Add more versions and preferences to remove here
51+
1110303 to listOf(
52+
"APP_ALIAS_*"
53+
)
4954
)
5055

51-
// Iterate over the versions and clear the relevant preferences
56+
var totalRemoved = 0
57+
5258
for ((version, keys) in versionCleanupMap) {
53-
// Only clear preferences for versions between savedVersionCode and currentVersionCode
54-
if (version in (savedVersionCode + 1)..currentVersionCode) {
55-
// Remove the preferences for this version
56-
keys.forEach { key ->
57-
prefs.remove(key)
59+
if (version in (savedVersionCode)..currentVersionCode) {
60+
val allKeys = prefs.prefsNormal.all.keys
61+
val removedThisVersion = mutableListOf<String>()
62+
63+
prefs.prefsNormal.edit {
64+
keys.forEach { keyPattern ->
65+
if (keyPattern.contains("*")) {
66+
val prefix = keyPattern.removeSuffix("*")
67+
val matchedKeys = allKeys.filter { it.startsWith(prefix) }
68+
matchedKeys.forEach { key ->
69+
remove(key)
70+
removedThisVersion.add(key)
71+
totalRemoved++
72+
}
73+
if (matchedKeys.isNotEmpty()) {
74+
AppLogger.d(
75+
"PrefsMigration",
76+
"Version $version wildcard pattern '$keyPattern' removed: ${matchedKeys.joinToString()}"
77+
)
78+
}
79+
} else {
80+
remove(keyPattern)
81+
removedThisVersion.add(keyPattern)
82+
totalRemoved++
83+
AppLogger.d(
84+
"PrefsMigration",
85+
"Version $version removed key: $keyPattern"
86+
)
87+
}
88+
}
89+
}
90+
91+
if (removedThisVersion.isEmpty()) {
92+
AppLogger.d("PrefsMigration", "Version $version had no keys to remove.")
5893
}
5994
}
6095
}
6196

62-
// Update the stored version code after cleanup
6397
prefs.appVersion = currentVersionCode
98+
AppLogger.d(
99+
"PrefsMigration",
100+
"Migration completed: updated app version to $currentVersionCode, total keys removed: $totalRemoved"
101+
)
64102
}
65103

66104
fun migrateMessages(prefs: Prefs) {

0 commit comments

Comments
 (0)