Parcourir la source

修复 HTTP TTS 删除后重启恢复

5151561 il y a 3 mois
Parent
commit
a63fb6b66c

+ 0 - 15
app/src/main/java/io/legado/app/data/dao/CloudTtsEngineDao.kt

@@ -22,21 +22,6 @@ interface CloudTtsEngineDao {
     @Insert(onConflict = OnConflictStrategy.REPLACE)
     suspend fun upsert(engine: CloudTtsEngineEntity)
 
-    @Query(
-        """
-        update cloud_tts_engines
-        set apiKey = :newApiKey, secretKey = :newSecretKey
-        where id = :id and apiKey = :oldApiKey and secretKey = :oldSecretKey
-        """
-    )
-    suspend fun updateCredentials(
-        id: String,
-        oldApiKey: String,
-        oldSecretKey: String,
-        newApiKey: String,
-        newSecretKey: String,
-    )
-
     @Delete
     suspend fun delete(engine: CloudTtsEngineEntity)
 }

+ 0 - 7
app/src/main/java/io/legado/app/data/repository/AppStartupRepository.kt

@@ -2,7 +2,6 @@ package io.legado.app.data.repository
 
 import io.legado.app.data.AppDatabase
 import io.legado.app.domain.gateway.AppStartupGateway
-import io.legado.app.help.DefaultData
 
 class AppStartupRepository(
     private val appDatabase: AppDatabase
@@ -11,10 +10,4 @@ class AppStartupRepository(
     override suspend fun deleteNotShelfBooks() {
         appDatabase.bookDao.deleteNotShelfBook()
     }
-
-    override suspend fun ensureDefaultHttpTts() {
-        if (appDatabase.httpTTSDao.count == 0) {
-            appDatabase.httpTTSDao.insert(*DefaultData.httpTTS.toTypedArray())
-        }
-    }
 }

+ 2 - 13
app/src/main/java/io/legado/app/data/repository/CloudTtsEngineRepository.kt

@@ -28,7 +28,7 @@ class CloudTtsEngineRepository(
     override suspend fun get(id: String): CloudTtsEngine? = withContext(Dispatchers.IO) {
         dao.get(id)?.let { engine ->
             val encrypted = engine.encryptCredentials(credentialCipher)
-            if (encrypted != engine) dao.updateCredentials(engine, encrypted)
+            if (encrypted != engine) dao.upsert(encrypted)
             toDomain(engine)
         }
     }
@@ -46,20 +46,9 @@ class CloudTtsEngineRepository(
     private suspend fun encryptLegacyCredentials(engines: List<CloudTtsEngineEntity>) {
         engines.forEach { engine ->
             val encrypted = engine.encryptCredentials(credentialCipher)
-            if (encrypted != engine) dao.updateCredentials(engine, encrypted)
+            if (encrypted != engine) dao.upsert(encrypted)
         }
     }
-
-    private suspend fun CloudTtsEngineDao.updateCredentials(
-        old: CloudTtsEngineEntity,
-        new: CloudTtsEngineEntity,
-    ) = updateCredentials(
-        id = old.id,
-        oldApiKey = old.apiKey,
-        oldSecretKey = old.secretKey,
-        newApiKey = new.apiKey,
-        newSecretKey = new.secretKey,
-    )
 }
 
 private fun CloudTtsEngineEntity.encryptCredentials(cipher: CloudTtsCredentialCipher) = copy(

+ 0 - 1
app/src/main/java/io/legado/app/domain/gateway/AppStartupGateway.kt

@@ -2,5 +2,4 @@ package io.legado.app.domain.gateway
 
 interface AppStartupGateway {
     suspend fun deleteNotShelfBooks()
-    suspend fun ensureDefaultHttpTts()
 }

+ 0 - 4
app/src/main/java/io/legado/app/domain/usecase/AppStartupMaintenanceUseCase.kt

@@ -9,8 +9,4 @@ class AppStartupMaintenanceUseCase(
     suspend fun deleteNotShelfBooks() {
         appStartupGateway.deleteNotShelfBooks()
     }
-
-    suspend fun ensureDefaultHttpTts() {
-        appStartupGateway.ensureDefaultHttpTts()
-    }
 }

+ 0 - 1
app/src/main/java/io/legado/app/ui/main/MainActivity.kt

@@ -235,7 +235,6 @@ open class MainActivity : BaseComposeActivity(), VariableDialog.Callback {
             if (otherSettingsGateway.currentSettings.autoRefresh && !isAutoRefreshedBook) {
                 viewModel.upAllBookToc()
             }
-            viewModel.postLoad()
             if (shouldAutoCheckUpdate) {
                 checkUpdateOnStart()
             }

+ 0 - 4
app/src/main/java/io/legado/app/ui/main/MainViewModel.kt

@@ -59,10 +59,6 @@ class MainViewModel(
         FlowEventBus.post(EventBus.UP_ALL_BOOK_TOC, Unit)
     }
 
-    fun postLoad() {
-        viewModelScope.launch { appStartupMaintenanceUseCase.ensureDefaultHttpTts() }
-    }
-
     fun restoreWebDav(name: String) {
         viewModelScope.launch { webDavBackupUseCase.restore(name) }
     }

+ 0 - 60
app/src/test/java/io/legado/app/data/dao/CloudTtsEngineDaoTest.kt

@@ -1,60 +0,0 @@
-package io.legado.app.data.dao
-
-import android.app.Application
-import androidx.room.Room
-import io.legado.app.data.AppDatabase
-import io.legado.app.data.entities.CloudTtsEngineEntity
-import kotlinx.coroutines.runBlocking
-import org.junit.After
-import org.junit.Assert.assertNull
-import org.junit.Before
-import org.junit.Test
-import org.junit.runner.RunWith
-import org.robolectric.RobolectricTestRunner
-import org.robolectric.RuntimeEnvironment
-import org.robolectric.annotation.Config
-
-@RunWith(RobolectricTestRunner::class)
-@Config(application = Application::class, sdk = [35])
-class CloudTtsEngineDaoTest {
-
-    private lateinit var database: AppDatabase
-    private lateinit var dao: CloudTtsEngineDao
-
-    @Before
-    fun setUp() {
-        database = Room.inMemoryDatabaseBuilder(
-            RuntimeEnvironment.getApplication(),
-            AppDatabase::class.java,
-        ).allowMainThreadQueries().build()
-        dao = database.cloudTtsEngineDao
-    }
-
-    @After
-    fun tearDown() {
-        database.close()
-    }
-
-    @Test
-    fun `旧凭据迁移不会恢复已删除的引擎`() = runBlocking {
-        val engine = CloudTtsEngineEntity(
-            id = "deleted-engine",
-            name = "Deleted engine",
-            provider = "openai_speech",
-            apiKey = "plain-api-key",
-            secretKey = "plain-secret-key",
-        )
-        dao.upsert(engine)
-        dao.delete(engine)
-
-        dao.updateCredentials(
-            id = engine.id,
-            oldApiKey = engine.apiKey,
-            oldSecretKey = engine.secretKey,
-            newApiKey = "enc:v1:api-key",
-            newSecretKey = "enc:v1:secret-key",
-        )
-
-        assertNull(dao.get(engine.id))
-    }
-}