HapeLee 3 месяцев назад
Родитель
Сommit
66adbc2737

+ 38 - 2
app/src/main/java/io/legado/app/data/repository/SettingsRepository.kt

@@ -2,9 +2,18 @@ package io.legado.app.data.repository
 
 import android.content.Context
 import androidx.core.content.edit
+import androidx.datastore.core.DataMigration
 import androidx.datastore.core.DataStore
 import androidx.datastore.preferences.SharedPreferencesMigration
-import androidx.datastore.preferences.core.*
+import androidx.datastore.preferences.core.Preferences
+import androidx.datastore.preferences.core.booleanPreferencesKey
+import androidx.datastore.preferences.core.edit
+import androidx.datastore.preferences.core.emptyPreferences
+import androidx.datastore.preferences.core.floatPreferencesKey
+import androidx.datastore.preferences.core.intPreferencesKey
+import androidx.datastore.preferences.core.longPreferencesKey
+import androidx.datastore.preferences.core.stringPreferencesKey
+import androidx.datastore.preferences.core.stringSetPreferencesKey
 import androidx.datastore.preferences.preferencesDataStore
 import io.legado.app.constant.PreferKey
 import io.legado.app.utils.defaultSharedPreferences
@@ -22,11 +31,38 @@ val Context.dataStore: DataStore<Preferences> by preferencesDataStore(
             SharedPreferencesMigration(
                 context,
                 "${context.packageName}_preferences"
-            )
+            ),
+            ShowBrightnessViewMigration,
         )
     }
 )
 
+internal object ShowBrightnessViewMigration : DataMigration<Preferences> {
+
+    private val booleanKey = booleanPreferencesKey(PreferKey.showBrightnessView)
+    private val stringKey = stringPreferencesKey(PreferKey.showBrightnessView)
+
+    override suspend fun shouldMigrate(currentData: Preferences): Boolean =
+        currentData.asMap().entries.any { (key, value) ->
+            key.name == PreferKey.showBrightnessView && value is Boolean
+        }
+
+    override suspend fun migrate(currentData: Preferences): Preferences {
+        val oldValue = currentData.asMap().entries
+            .firstOrNull { (key, value) ->
+                key.name == PreferKey.showBrightnessView && value is Boolean
+            }
+            ?.value as? Boolean
+            ?: return currentData
+        return currentData.toMutablePreferences().apply {
+            remove(booleanKey)
+            this[stringKey] = if (oldValue) "1" else "0"
+        }
+    }
+
+    override suspend fun cleanUp() = Unit
+}
+
 /**
  * 设置仓储
  * 以 DataStore 为唯一写入源,读取以 DataStore 为准。

+ 43 - 0
app/src/test/java/io/legado/app/data/repository/ShowBrightnessViewMigrationTest.kt

@@ -0,0 +1,43 @@
+package io.legado.app.data.repository
+
+import androidx.datastore.preferences.core.booleanPreferencesKey
+import androidx.datastore.preferences.core.mutablePreferencesOf
+import androidx.datastore.preferences.core.stringPreferencesKey
+import io.legado.app.constant.PreferKey
+import kotlinx.coroutines.runBlocking
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertFalse
+import org.junit.Assert.assertTrue
+import org.junit.Test
+
+class ShowBrightnessViewMigrationTest {
+
+    private val booleanKey = booleanPreferencesKey(PreferKey.showBrightnessView)
+    private val stringKey = stringPreferencesKey(PreferKey.showBrightnessView)
+
+    @Test
+    fun `migrates legacy enabled value to horizontal mode`() = runBlocking {
+        val preferences = mutablePreferencesOf(booleanKey to true)
+
+        assertTrue(ShowBrightnessViewMigration.shouldMigrate(preferences))
+
+        val migrated = ShowBrightnessViewMigration.migrate(preferences)
+        assertEquals("1", migrated[stringKey])
+    }
+
+    @Test
+    fun `migrates legacy disabled value to off mode`() = runBlocking {
+        val preferences = mutablePreferencesOf(booleanKey to false)
+
+        val migrated = ShowBrightnessViewMigration.migrate(preferences)
+        assertEquals("0", migrated[stringKey])
+    }
+
+    @Test
+    fun `does not migrate current string value`() = runBlocking {
+        val preferences = mutablePreferencesOf(stringKey to "2")
+
+        assertFalse(ShowBrightnessViewMigration.shouldMigrate(preferences))
+        assertEquals(preferences, ShowBrightnessViewMigration.migrate(preferences))
+    }
+}