Просмотр исходного кода

refactor: 清零 ReadBookViewModel 非阅读态 DAO 直连(httpTTS/书源/书签)

5151561 3 месяцев назад
Родитель
Сommit
4fc0b860ff

+ 12 - 0
app/src/main/java/io/legado/app/data/repository/HttpTtsRepository.kt

@@ -13,7 +13,19 @@ class HttpTtsRepository(
         dao.get(id)
         dao.get(id)
     }
     }
 
 
+    suspend fun getAll(): List<HttpTTS> = withContext(Dispatchers.IO) {
+        dao.all
+    }
+
+    fun getAllSync(): List<HttpTTS> = dao.all
+
+    fun getNameSync(id: Long): String? = dao.getName(id)
+
     suspend fun insert(vararg sources: HttpTTS) = withContext(Dispatchers.IO) {
     suspend fun insert(vararg sources: HttpTTS) = withContext(Dispatchers.IO) {
         dao.insert(*sources)
         dao.insert(*sources)
     }
     }
+
+    suspend fun delete(vararg sources: HttpTTS) = withContext(Dispatchers.IO) {
+        dao.delete(*sources)
+    }
 }
 }

+ 3 - 0
app/src/main/java/io/legado/app/di/appModule.kt

@@ -678,6 +678,9 @@ val appModule = module {
             downloadCacheSettingsGateway = get(),
             downloadCacheSettingsGateway = get(),
             backupSettingsGateway = get(),
             backupSettingsGateway = get(),
             themeSettingsGateway = get(),
             themeSettingsGateway = get(),
+            httpTtsRepository = get(),
+            bookSourceRepository = get(),
+            bookmarkRepository = get(),
         )
         )
     }
     }
     viewModelOf(::ChangeCoverViewModel)
     viewModelOf(::ChangeCoverViewModel)

+ 22 - 16
app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt

@@ -30,7 +30,10 @@ import io.legado.app.data.entities.Bookmark
 import io.legado.app.data.entities.HighlightRule
 import io.legado.app.data.entities.HighlightRule
 import io.legado.app.data.entities.HttpTTS
 import io.legado.app.data.entities.HttpTTS
 import io.legado.app.data.local.preferences.LocalPreferencesKeys
 import io.legado.app.data.local.preferences.LocalPreferencesKeys
+import io.legado.app.data.repository.BookSourceRepository
+import io.legado.app.data.repository.BookmarkRepository
 import io.legado.app.data.repository.HighlightRuleRepository
 import io.legado.app.data.repository.HighlightRuleRepository
+import io.legado.app.data.repository.HttpTtsRepository
 import io.legado.app.data.repository.ReadAloudSettingsRepository
 import io.legado.app.data.repository.ReadAloudSettingsRepository
 import io.legado.app.data.repository.ReadPreferences
 import io.legado.app.data.repository.ReadPreferences
 import io.legado.app.data.repository.ReadSettingsRepository
 import io.legado.app.data.repository.ReadSettingsRepository
@@ -204,6 +207,9 @@ class ReadBookViewModel(
     private val downloadCacheSettingsGateway: DownloadCacheSettingsGateway,
     private val downloadCacheSettingsGateway: DownloadCacheSettingsGateway,
     private val backupSettingsGateway: BackupSettingsGateway,
     private val backupSettingsGateway: BackupSettingsGateway,
     private val themeSettingsGateway: ThemeSettingsGateway,
     private val themeSettingsGateway: ThemeSettingsGateway,
+    private val httpTtsRepository: HttpTtsRepository,
+    private val bookSourceRepository: BookSourceRepository,
+    private val bookmarkRepository: BookmarkRepository,
 ) : BaseViewModel(application), ReadBook.CallBack {
 ) : BaseViewModel(application), ReadBook.CallBack {
 
 
     // --- MVI State ---
     // --- MVI State ---
@@ -1131,7 +1137,7 @@ class ReadBookViewModel(
                     }
                     }
                 } else {
                 } else {
                     execute {
                     execute {
-                        appDb.httpTTSDao.get(intent.engineId)
+                        httpTtsRepository.findById(intent.engineId)
                     }.onSuccess { tts ->
                     }.onSuccess { tts ->
                         _uiState.update {
                         _uiState.update {
                             it.copy(
                             it.copy(
@@ -1145,8 +1151,8 @@ class ReadBookViewModel(
 
 
             is ReadBookIntent.DeleteHttpTts -> {
             is ReadBookIntent.DeleteHttpTts -> {
                 execute {
                 execute {
-                    appDb.httpTTSDao.get(intent.engineId)?.let { tts ->
-                        appDb.httpTTSDao.delete(tts)
+                    httpTtsRepository.findById(intent.engineId)?.let { tts ->
+                        httpTtsRepository.delete(tts)
                     }
                     }
                 }.onSuccess {
                 }.onSuccess {
                     loadTtsEngineItems()
                     loadTtsEngineItems()
@@ -1161,7 +1167,7 @@ class ReadBookViewModel(
 
 
             is ReadBookIntent.SaveHttpTts -> {
             is ReadBookIntent.SaveHttpTts -> {
                 execute {
                 execute {
-                    appDb.httpTTSDao.insert(intent.httpTTS)
+                    httpTtsRepository.insert(intent.httpTTS)
                 }.onSuccess {
                 }.onSuccess {
                     loadTtsEngineItems()
                     loadTtsEngineItems()
                     _uiState.update {
                     _uiState.update {
@@ -1743,7 +1749,7 @@ class ReadBookViewModel(
     private fun loadTtsEngineItems(onSuccess: (() -> Unit)? = null) {
     private fun loadTtsEngineItems(onSuccess: (() -> Unit)? = null) {
         execute {
         execute {
             val systemTtsLabel = context.getString(R.string.system_tts)
             val systemTtsLabel = context.getString(R.string.system_tts)
-            val httpTtsList = appDb.httpTTSDao.all
+            val httpTtsList = httpTtsRepository.getAll()
             syncConfiguredTtsVoices(systemTtsLabel, httpTtsList)
             syncConfiguredTtsVoices(systemTtsLabel, httpTtsList)
             buildList {
             buildList {
                 add(ReadBookTtsEngineItem(systemTtsLabel, null))
                 add(ReadBookTtsEngineItem(systemTtsLabel, null))
@@ -1778,7 +1784,7 @@ class ReadBookViewModel(
 
 
     private suspend fun syncConfiguredTtsVoices(
     private suspend fun syncConfiguredTtsVoices(
         systemTtsLabel: String = context.getString(R.string.system_tts),
         systemTtsLabel: String = context.getString(R.string.system_tts),
-        httpTtsList: List<HttpTTS> = appDb.httpTTSDao.all,
+        httpTtsList: List<HttpTTS> = httpTtsRepository.getAllSync(),
     ) {
     ) {
         syncReadAloudVoicesUseCase(
         syncReadAloudVoicesUseCase(
             entries = buildList {
             entries = buildList {
@@ -1821,7 +1827,7 @@ class ReadBookViewModel(
         execute {
         execute {
             val list = importHttpTtsSourceAwait(text.trim())
             val list = importHttpTtsSourceAwait(text.trim())
             val items = list.map { httpTTS ->
             val items = list.map { httpTTS ->
-                val old = appDb.httpTTSDao.get(httpTTS.id)
+                val old = httpTtsRepository.findById(httpTTS.id)
                 val status = when {
                 val status = when {
                     old == null -> ImportStatus.New
                     old == null -> ImportStatus.New
                     httpTTS.lastUpdateTime > old.lastUpdateTime -> ImportStatus.Update
                     httpTTS.lastUpdateTime > old.lastUpdateTime -> ImportStatus.Update
@@ -1946,7 +1952,7 @@ class ReadBookViewModel(
             .map { it.data }
             .map { it.data }
         if (selected.isEmpty()) return
         if (selected.isEmpty()) return
         execute {
         execute {
-            appDb.httpTTSDao.insert(*selected.toTypedArray())
+            httpTtsRepository.insert(*selected.toTypedArray())
         }.onSuccess {
         }.onSuccess {
             loadTtsEngineItems()
             loadTtsEngineItems()
             _uiState.update {
             _uiState.update {
@@ -1969,14 +1975,14 @@ class ReadBookViewModel(
     }
     }
 
 
     private fun exportHttpTtsJson(): String {
     private fun exportHttpTtsJson(): String {
-        return GSON.toJson(appDb.httpTTSDao.all)
+        return GSON.toJson(httpTtsRepository.getAllSync())
     }
     }
 
 
     private fun computeSpeakEngineName(): String {
     private fun computeSpeakEngineName(): String {
         val ttsEngine = ReadAloud.ttsEngine
         val ttsEngine = ReadAloud.ttsEngine
             ?: return context.getString(R.string.system_tts)
             ?: return context.getString(R.string.system_tts)
         if (StringUtils.isNumeric(ttsEngine)) {
         if (StringUtils.isNumeric(ttsEngine)) {
-            return appDb.httpTTSDao.getName(ttsEngine.toLong())
+            return httpTtsRepository.getNameSync(ttsEngine.toLong())
                 ?: context.getString(R.string.system_tts)
                 ?: context.getString(R.string.system_tts)
         }
         }
         return GSON.fromJsonObject<SelectItem<String>>(ttsEngine)
         return GSON.fromJsonObject<SelectItem<String>>(ttsEngine)
@@ -3022,7 +3028,7 @@ class ReadBookViewModel(
         changeSourceCoroutine?.cancel()
         changeSourceCoroutine?.cancel()
         changeSourceCoroutine = execute {
         changeSourceCoroutine = execute {
             ReadBook.upMsg(context.getString(R.string.loading))
             ReadBook.upMsg(context.getString(R.string.loading))
-            val source = appDb.bookSourceDao.getBookSource(book.origin)
+            val source = bookSourceRepository.getBookSource(book.origin)
                 ?: throw NoStackTraceException("书源不存在")
                 ?: throw NoStackTraceException("书源不存在")
             if (book.tocUrl.isEmpty()) {
             if (book.tocUrl.isEmpty()) {
                 WebBook.getBookInfoAwait(source, book)
                 WebBook.getBookInfoAwait(source, book)
@@ -3056,7 +3062,7 @@ class ReadBookViewModel(
     private fun autoChangeSource(name: String, author: String) {
     private fun autoChangeSource(name: String, author: String) {
         if (!readSettingsRepository.currentSettings.autoChangeSource) return
         if (!readSettingsRepository.currentSettings.autoChangeSource) return
         execute {
         execute {
-            val sources = appDb.bookSourceDao.allTextEnabledPart
+            val sources = bookSourceRepository.getAllTextEnabledPart()
             flow {
             flow {
                 for (source in sources) {
                 for (source in sources) {
                     source.getBookSource()?.let {
                     source.getBookSource()?.let {
@@ -3169,7 +3175,7 @@ class ReadBookViewModel(
     fun upBookSource(success: (() -> Unit)? = null) {
     fun upBookSource(success: (() -> Unit)? = null) {
         execute {
         execute {
             ReadBook.book?.let { book ->
             ReadBook.book?.let { book ->
-                ReadBook.bookSource = appDb.bookSourceDao.getBookSource(book.origin)
+                ReadBook.bookSource = bookSourceRepository.getBookSource(book.origin)
             }
             }
         }.onSuccess {
         }.onSuccess {
             success?.invoke()
             success?.invoke()
@@ -4901,7 +4907,7 @@ class ReadBookViewModel(
 
 
     private fun saveBookmark(bookmark: Bookmark) {
     private fun saveBookmark(bookmark: Bookmark) {
         viewModelScope.launch(IO) {
         viewModelScope.launch(IO) {
-            appDb.bookmarkDao.insert(bookmark)
+            bookmarkRepository.save(bookmark)
             _uiState.update {
             _uiState.update {
                 it.copy(
                 it.copy(
                     activeSheet = null,
                     activeSheet = null,
@@ -4913,7 +4919,7 @@ class ReadBookViewModel(
 
 
     private fun deleteBookmark(bookmark: Bookmark) {
     private fun deleteBookmark(bookmark: Bookmark) {
         viewModelScope.launch(IO) {
         viewModelScope.launch(IO) {
-            appDb.bookmarkDao.delete(bookmark)
+            bookmarkRepository.delete(bookmark)
             _uiState.update {
             _uiState.update {
                 it.copy(
                 it.copy(
                     activeSheet = null,
                     activeSheet = null,
@@ -6360,7 +6366,7 @@ class ReadBookViewModel(
         execute {
         execute {
             ReadBook.bookSource?.let {
             ReadBook.bookSource?.let {
                 it.enabled = false
                 it.enabled = false
-                appDb.bookSourceDao.update(it)
+                bookSourceRepository.updateSources(it)
             }
             }
         }
         }
     }
     }

+ 1 - 1
build.gradle.kts

@@ -219,7 +219,7 @@ val verifyConfigArchitecture = tasks.register<VerifyConfigArchitectureTask>(
     )
     )
     legacyDaoInjectionBaseline.set(
     legacyDaoInjectionBaseline.set(
         mapOf(
         mapOf(
-            "io/legado/app/ui/book/read/ReadBookViewModel.kt" to 52,
+            "io/legado/app/ui/book/read/ReadBookViewModel.kt" to 36,
         )
         )
     )
     )
     // 非 ViewModel 的 UI 层文件直连 DAO 的历史债,只冻结不修复;
     // 非 ViewModel 的 UI 层文件直连 DAO 的历史债,只冻结不修复;