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

refactor: 清零书源管理 DAO 直连

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

+ 122 - 0
app/src/main/java/io/legado/app/data/repository/BookSourceRepository.kt

@@ -3,6 +3,7 @@ package io.legado.app.data.repository
 import io.legado.app.data.dao.BookSourceDao
 import io.legado.app.data.entities.BookSource
 import io.legado.app.data.entities.BookSourcePart
+import io.legado.app.help.source.SourceHelp
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.flow.Flow
 import kotlinx.coroutines.withContext
@@ -17,6 +18,10 @@ class BookSourceRepository(private val bookSourceDao: BookSourceDao) {
         return bookSourceDao.flowEnabled()
     }
 
+    fun flowGroups(): Flow<List<String>> {
+        return bookSourceDao.flowGroups()
+    }
+
     fun flowHomepageModules(): Flow<List<BookSource>> {
         return bookSourceDao.flowHomepageModules()
     }
@@ -62,4 +67,121 @@ class BookSourceRepository(private val bookSourceDao: BookSourceDao) {
             bookSourceDao.allEnabledPart
         }
     }
+
+    suspend fun getAllPart(): List<BookSourcePart> {
+        return withContext(Dispatchers.IO) {
+            bookSourceDao.allPart
+        }
+    }
+
+    suspend fun moveToEdge(sources: List<BookSourcePart>, toTop: Boolean) =
+        withContext(Dispatchers.IO) {
+            val selected = sources.sortedBy { it.customOrder }
+            val start = if (toTop) {
+                bookSourceDao.minOrder - selected.size
+            } else {
+                bookSourceDao.maxOrder + 1
+            }
+            bookSourceDao.upOrder(
+                selected.mapIndexed { index, part -> part.copy(customOrder = start + index) }
+            )
+        }
+
+    suspend fun topSources(sources: List<BookSourcePart>) = withContext(Dispatchers.IO) {
+        val minOrder = bookSourceDao.minOrder - 1
+        val reorderedSources = sources.sortedBy { it.customOrder }.mapIndexed { index, source ->
+            source.copy(customOrder = minOrder - index)
+        }
+        bookSourceDao.upOrder(reorderedSources)
+    }
+
+    suspend fun bottomSources(sources: List<BookSourcePart>) = withContext(Dispatchers.IO) {
+        val maxOrder = bookSourceDao.maxOrder + 1
+        val reorderedSources = sources.sortedBy { it.customOrder }.mapIndexed { index, source ->
+            source.copy(customOrder = maxOrder + index)
+        }
+        bookSourceDao.upOrder(reorderedSources)
+    }
+
+    suspend fun deleteSourceParts(sources: List<BookSourcePart>) = withContext(Dispatchers.IO) {
+        SourceHelp.deleteBookSourceParts(sources)
+    }
+
+    suspend fun updateSources(vararg sources: BookSource) = withContext(Dispatchers.IO) {
+        bookSourceDao.update(*sources)
+    }
+
+    suspend fun updateOrder(sources: List<BookSourcePart>) = withContext(Dispatchers.IO) {
+        bookSourceDao.upOrder(sources)
+    }
+
+    suspend fun setEnabled(enabled: Boolean, sources: List<BookSourcePart>) =
+        withContext(Dispatchers.IO) {
+            bookSourceDao.enable(enabled, sources)
+        }
+
+    suspend fun setEnabled(sourceUrl: String, enabled: Boolean) = withContext(Dispatchers.IO) {
+        bookSourceDao.enable(sourceUrl, enabled)
+    }
+
+    suspend fun setExploreEnabled(enabled: Boolean, sources: List<BookSourcePart>) =
+        withContext(Dispatchers.IO) {
+            bookSourceDao.enableExplore(enabled, sources)
+        }
+
+    suspend fun updateGroups(sources: List<BookSourcePart>) = withContext(Dispatchers.IO) {
+        bookSourceDao.upGroup(sources)
+    }
+
+    suspend fun getAll(): List<BookSource> = withContext(Dispatchers.IO) {
+        bookSourceDao.all
+    }
+
+    suspend fun getAllEnabled(): List<BookSource> = withContext(Dispatchers.IO) {
+        bookSourceDao.allEnabled
+    }
+
+    suspend fun getAllDisabled(): List<BookSource> = withContext(Dispatchers.IO) {
+        bookSourceDao.allDisabled
+    }
+
+    suspend fun getAllLogin(): List<BookSource> = withContext(Dispatchers.IO) {
+        bookSourceDao.allLogin
+    }
+
+    suspend fun getAllNoGroup(): List<BookSource> = withContext(Dispatchers.IO) {
+        bookSourceDao.allNoGroup
+    }
+
+    suspend fun getAllExploreEnabled(): List<BookSource> = withContext(Dispatchers.IO) {
+        bookSourceDao.allEnabledExplore
+    }
+
+    suspend fun getAllExploreDisabled(): List<BookSource> = withContext(Dispatchers.IO) {
+        bookSourceDao.allDisabledExplore
+    }
+
+    suspend fun getByGroup(group: String): List<BookSource> = withContext(Dispatchers.IO) {
+        bookSourceDao.getByGroup(group)
+    }
+
+    suspend fun searchByGroup(group: String): List<BookSource> = withContext(Dispatchers.IO) {
+        bookSourceDao.groupSearch(group)
+    }
+
+    suspend fun search(searchKey: String): List<BookSource> = withContext(Dispatchers.IO) {
+        bookSourceDao.search(searchKey)
+    }
+
+    suspend fun getNoGroup(): List<BookSource> = withContext(Dispatchers.IO) {
+        bookSourceDao.noGroup
+    }
+
+    suspend fun delete(source: BookSource) = withContext(Dispatchers.IO) {
+        bookSourceDao.delete(source)
+    }
+
+    suspend fun insert(source: BookSource) = withContext(Dispatchers.IO) {
+        bookSourceDao.insert(source)
+    }
 }

+ 8 - 5
app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditViewModel.kt

@@ -6,8 +6,8 @@ import androidx.lifecycle.viewModelScope
 import com.google.gson.JsonObject
 import com.google.gson.JsonParser
 import io.legado.app.R
-import io.legado.app.data.appDb
 import io.legado.app.data.entities.BookSource
+import io.legado.app.data.repository.BookSourceRepository
 import io.legado.app.exception.NoStackTraceException
 import io.legado.app.help.ConcurrentRateLimiter.Companion.concurrentRecordMap
 import io.legado.app.help.RuleComplete
@@ -37,7 +37,10 @@ import kotlinx.coroutines.flow.update
 import kotlinx.coroutines.launch
 import kotlinx.coroutines.withContext
 
-class BookSourceEditViewModel(application: Application) : AndroidViewModel(application) {
+class BookSourceEditViewModel(
+    application: Application,
+    private val repository: BookSourceRepository,
+) : AndroidViewModel(application) {
     private val _uiState = MutableStateFlow(BookSourceEditUiState())
     val uiState = _uiState.asStateFlow()
     private val _effects = MutableSharedFlow<BookSourceEditEffect>(extraBufferCapacity = 16)
@@ -102,7 +105,7 @@ class BookSourceEditViewModel(application: Application) : AndroidViewModel(appli
     }
 
     private fun load(sourceUrl: String?) = viewModelScope.launch(Dispatchers.IO) {
-        val source = sourceUrl?.let { appDb.bookSourceDao.getBookSource(it) } ?: BookSource()
+        val source = sourceUrl?.let { repository.getBookSource(it) } ?: BookSource()
         withContext(Dispatchers.Main) {
             applySource(source, asOriginal = true)
             if (sourceUrl == null) originalSource = null
@@ -212,10 +215,10 @@ class BookSourceEditViewModel(application: Application) : AndroidViewModel(appli
                 originalSource?.let {
                     if (it.bookSourceUrl != source.bookSourceUrl) SourceHelp.deleteBookSource(it.bookSourceUrl)
                     else {
-                        appDb.bookSourceDao.delete(it); SourceConfig.removeSource(it.bookSourceUrl)
+                        repository.delete(it); SourceConfig.removeSource(it.bookSourceUrl)
                     }
             }
-            appDb.bookSourceDao.insert(source)
+            repository.insert(source)
             concurrentRecordMap.remove(source.bookSourceUrl)
                 originalSource = source
                 baselineJson = GSON.toJson(source)

+ 21 - 21
app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceViewModel.kt

@@ -4,9 +4,8 @@ import android.app.Application
 import android.text.TextUtils
 import androidx.lifecycle.ViewModel
 import androidx.lifecycle.viewModelScope
-import io.legado.app.data.appDb
 import io.legado.app.data.entities.BookSourcePart
-import io.legado.app.help.source.SourceHelp
+import io.legado.app.data.repository.BookSourceRepository
 import io.legado.app.utils.NetworkUtils
 import io.legado.app.utils.splitNotBlank
 import kotlinx.collections.immutable.toImmutableList
@@ -21,7 +20,10 @@ import kotlinx.coroutines.flow.stateIn
 import kotlinx.coroutines.flow.update
 import kotlinx.coroutines.launch
 
-class BookSourceViewModel(application: Application) : ViewModel() {
+class BookSourceViewModel(
+    application: Application,
+    private val repository: BookSourceRepository,
+) : ViewModel() {
     companion object {
         const val FILTER_ENABLED = "@enabled"
         const val FILTER_DISABLED = "@disabled"
@@ -32,7 +34,6 @@ class BookSourceViewModel(application: Application) : ViewModel() {
         const val PREFIX_GROUP = "group:"
     }
 
-    private val dao = appDb.bookSourceDao
     private val searchKey = MutableStateFlow("")
     private val isSearchMode = MutableStateFlow(false)
     private val filter = MutableStateFlow<String?>(null)
@@ -44,8 +45,8 @@ class BookSourceViewModel(application: Application) : ViewModel() {
     private val enabledOverrides = MutableStateFlow<Map<String, Boolean>>(emptyMap())
 
     val uiState = combine(
-        dao.flowAll(),
-        dao.flowGroups(),
+        repository.flowAll(),
+        repository.flowGroups(),
         searchKey,
         isSearchMode,
         filter,
@@ -130,14 +131,14 @@ class BookSourceViewModel(application: Application) : ViewModel() {
 
             is BookSourceIntent.SetEnabled -> setEnabled(intent.id, intent.enabled)
             is BookSourceIntent.SetEnabledForSelection -> launch {
-                dao.enable(
+                repository.setEnabled(
                     intent.enabled,
                     parts(intent.ids)
                 )
             }
 
             is BookSourceIntent.SetExploreEnabled -> updateExplore(intent.ids, intent.enabled)
-            is BookSourceIntent.Delete -> launch { SourceHelp.deleteBookSourceParts(parts(intent.ids)); selectedIds.update { it - intent.ids } }
+            is BookSourceIntent.Delete -> launch { repository.deleteSourceParts(parts(intent.ids)); selectedIds.update { it - intent.ids } }
             is BookSourceIntent.MoveToEdge -> moveToEdge(intent.ids, intent.toTop)
             is BookSourceIntent.MoveItem -> moveItem(intent.from, intent.to)
             BookSourceIntent.SaveSortOrder -> saveSortOrder()
@@ -157,8 +158,8 @@ class BookSourceViewModel(application: Application) : ViewModel() {
         enabledOverrides.update { it + (id to enabled) }
         viewModelScope.launch(Dispatchers.IO) {
             runCatching {
-                dao.enable(id, enabled)
-                dao.flowAll().first { sources ->
+                repository.setEnabled(id, enabled)
+                repository.flowAll().first { sources ->
                     sources.firstOrNull { it.bookSourceUrl == id }?.enabled == enabled
                 }
             }
@@ -166,31 +167,30 @@ class BookSourceViewModel(application: Application) : ViewModel() {
         }
     }
 
-    private fun parts(ids: Set<String>) = dao.allPart.filter { it.bookSourceUrl in ids }
+    private suspend fun parts(ids: Set<String>) =
+        repository.getAllPart().filter { it.bookSourceUrl in ids }
     private fun updateExplore(ids: Set<String>, enabled: Boolean) =
-        launch { dao.enableExplore(enabled, parts(ids)) }
+        launch { repository.setExploreEnabled(enabled, parts(ids)) }
 
     private fun updateGroups(ids: Set<String>, group: String, add: Boolean) = launch {
         val changed = parts(ids).map { part ->
             part.copy().apply { if (add) addGroup(group) else removeGroup(group) }
         }
-        dao.upGroup(changed)
+        repository.updateGroups(changed)
     }
 
     private fun updateGroup(old: String, new: String) = launch {
-        val sources = dao.getByGroup(old)
+        val sources = repository.getByGroup(old)
         sources.forEach { source ->
             source.bookSourceGroup?.splitNotBlank(",")?.toHashSet()
                 ?.apply { remove(old); if (new.isNotBlank()) add(new) }
                 ?.let { source.bookSourceGroup = TextUtils.join(",", it) }
         }
-        dao.update(*sources.toTypedArray())
+        repository.updateSources(*sources.toTypedArray())
     }
 
     private fun moveToEdge(ids: Set<String>, toTop: Boolean) = launch {
-        val selected = parts(ids).sortedBy { it.customOrder }
-        val start = if (toTop) dao.minOrder - selected.size else dao.maxOrder + 1
-        dao.upOrder(selected.mapIndexed { index, part -> part.copy(customOrder = start + index) })
+        repository.moveToEdge(parts(ids), toTop)
     }
 
     private fun moveItem(from: Int, to: Int) {
@@ -205,13 +205,13 @@ class BookSourceViewModel(application: Application) : ViewModel() {
     private fun saveSortOrder() {
         val items = localItems.value ?: return
         launch {
-            dao.upOrder(items.mapIndexed { index, item -> item.copy(customOrder = index + 1) }); localItems.value =
+            repository.updateOrder(items.mapIndexed { index, item -> item.copy(customOrder = index + 1) }); localItems.value =
             null
         }
     }
 
     private fun commitSortOrder(ids: List<String>, ascending: Boolean) = launch {
-        val sourcesById = dao.allPart.associateBy { it.bookSourceUrl }
+        val sourcesById = repository.getAllPart().associateBy { it.bookSourceUrl }
         val orderSlots = ids.mapNotNull { sourcesById[it]?.customOrder }
             .let { if (ascending) it.sorted() else it.sortedDescending() }
         val ordered = ids.mapIndexedNotNull { index, id ->
@@ -219,7 +219,7 @@ class BookSourceViewModel(application: Application) : ViewModel() {
                 customOrder = orderSlots.getOrNull(index) ?: return@mapIndexedNotNull null
             )
         }
-        dao.upOrder(ordered)
+        repository.updateOrder(ordered)
     }
 
     private fun checkInterval(ids: Set<String>) {

+ 0 - 2
build.gradle.kts

@@ -207,8 +207,6 @@ val verifyConfigArchitecture = tasks.register<VerifyConfigArchitectureTask>(
             "io/legado/app/ui/book/info/edit/BookInfoEditViewModel.kt" to 2,
             "io/legado/app/ui/book/manga/ReadMangaViewModel.kt" to 17,
             "io/legado/app/ui/book/read/ReadBookViewModel.kt" to 52,
-            "io/legado/app/ui/book/source/edit/BookSourceEditViewModel.kt" to 3,
-            "io/legado/app/ui/book/source/manage/BookSourceViewModel.kt" to 29,
             "io/legado/app/ui/book/toc/TocViewModel.kt" to 13,
             "io/legado/app/ui/book/toc/rule/preview/TxtTocRulePreviewViewModel.kt" to 2,
             "io/legado/app/ui/browser/WebViewModel.kt" to 1,