From dab348e601b6e546d5543984f04fc45c6ee82bea Mon Sep 17 00:00:00 2001 From: atbest Date: Sat, 25 May 2019 19:10:14 -0400 Subject: [PATCH] Extracted search and explore urls to separate table --- .../java/io/legado/app/base/BaseActivity.kt | 3 +- .../app/data/dao/ExploreSearchUrlDao.kt | 71 +++++++++++++++++++ .../io/legado/app/data/dao/ReplaceRuleDao.kt | 1 + .../legado/app/data/dao/SearchKeywordDao.kt | 32 +++++++++ .../app/data/entities/ExploreSearchUrl.kt | 29 ++++++++ .../legado/app/data/entities/SearchKeyword.kt | 17 +++++ 6 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/io/legado/app/data/dao/ExploreSearchUrlDao.kt create mode 100644 app/src/main/java/io/legado/app/data/dao/SearchKeywordDao.kt create mode 100644 app/src/main/java/io/legado/app/data/entities/ExploreSearchUrl.kt create mode 100644 app/src/main/java/io/legado/app/data/entities/SearchKeyword.kt diff --git a/app/src/main/java/io/legado/app/base/BaseActivity.kt b/app/src/main/java/io/legado/app/base/BaseActivity.kt index 8e23928f4..c85db5b5c 100644 --- a/app/src/main/java/io/legado/app/base/BaseActivity.kt +++ b/app/src/main/java/io/legado/app/base/BaseActivity.kt @@ -6,6 +6,7 @@ import androidx.appcompat.app.AppCompatActivity import androidx.databinding.DataBindingUtil import androidx.databinding.ViewDataBinding import androidx.lifecycle.ViewModel +import com.google.android.material.bottomnavigation.BottomNavigationView abstract class BaseActivity : AppCompatActivity() { @@ -33,7 +34,7 @@ abstract class BaseActivity : AppCompatAct return true } } - return if (item == null) false else onCompatOptionsItemSelected(item) + return item != null && onCompatOptionsItemSelected(item) } open fun onCompatOptionsItemSelected(item: MenuItem): Boolean { diff --git a/app/src/main/java/io/legado/app/data/dao/ExploreSearchUrlDao.kt b/app/src/main/java/io/legado/app/data/dao/ExploreSearchUrlDao.kt new file mode 100644 index 000000000..eef3d93ed --- /dev/null +++ b/app/src/main/java/io/legado/app/data/dao/ExploreSearchUrlDao.kt @@ -0,0 +1,71 @@ +package io.legado.app.data.dao + +import androidx.paging.DataSource +import androidx.room.* +import io.legado.app.data.entities.ExploreSearchUrl + + +@Dao +interface ExploreSearchUrlDao { + + companion object { + private const val ORDER_DEFAULT = "ORDER BY sourceId ASC, defOrder ASC" + private const val ORDER_USAGE = "ORDER BY usage DESC, lastUseTime DESC" + private const val ORDER_TIME = "ORDER BY lastUseTime DESC" + private const val QUERY_NAME = "name LIKE '%' || :name || '%'" + private const val QUERY_ENABLED_EXPLORE = "WHERE type = 0 AND isEnabled = 1" + } + + // 用于发现列表,默认排序 + @Query("SELECT * FROM explore_search_urls $QUERY_ENABLED_EXPLORE $ORDER_DEFAULT") + fun observeExploreUrls(): DataSource.Factory + + // 用于发现列表,按使用次数排序 + @Query("SELECT * FROM explore_search_urls $QUERY_ENABLED_EXPLORE $ORDER_USAGE") + fun observeExploreUrlsByUsage(): DataSource.Factory + + // 用于发现列表,按使用时间排序 + @Query("SELECT * FROM explore_search_urls $QUERY_ENABLED_EXPLORE $ORDER_TIME") + fun observeExploreUrlsByTime(): DataSource.Factory + + // 用于搜索时的发现列表,默认排序 + @Query("SELECT * FROM explore_search_urls $QUERY_ENABLED_EXPLORE AND $QUERY_NAME $ORDER_DEFAULT") + fun observeFilteredExploreUrls(name: String): DataSource.Factory + + // 用于搜索时的发现列表,按使用次数排序 + @Query("SELECT * FROM explore_search_urls $QUERY_ENABLED_EXPLORE AND $QUERY_NAME $ORDER_USAGE") + fun observeFilteredExploreUrlsByUsage(): DataSource.Factory + + // 用于搜索时的发现列表,按使用时间排序 + @Query("SELECT * FROM explore_search_urls $QUERY_ENABLED_EXPLORE AND $QUERY_NAME $ORDER_TIME") + fun observeFilteredExploreUrlsByTime(): DataSource.Factory + + // 获取特定书源的发现 + @Query("SELECT * FROM explore_search_urls $QUERY_ENABLED_EXPLORE AND sourceId = :sourceId") + fun findExploreUrlsBySourceId(sourceId: Int): List + + // 获取特定书源的搜索链接 + @Query("SELECT * FROM explore_search_urls WHERE type = 1 AND sourceId = :sourceId") + fun findSearchUrlsBySourceId(sourceId: Int): List + + // 所有的搜索链接 + @get:Query("SELECT * FROM explore_search_urls WHERE type = 1") + val allSearchUrls: List + + @Insert(onConflict = OnConflictStrategy.REPLACE) + fun insert(vararg keywords: ExploreSearchUrl) + + @Insert(onConflict = OnConflictStrategy.REPLACE) + fun insert(keyword: ExploreSearchUrl): Long + + @Update + fun update(vararg keywords: ExploreSearchUrl) + + @Delete + fun delete(vararg keywords: ExploreSearchUrl) + + // 批量删除特定书源的发现和搜索链接,一般用于更新书源时 + @Query("DELETE FROM explore_search_urls WHERE sourceId = :sourceId") + fun deleteBySourceId(sourceId: Int) + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt b/app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt index 919855143..d4a2073c0 100644 --- a/app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt @@ -44,6 +44,7 @@ interface ReplaceRuleDao { @Insert(onConflict = OnConflictStrategy.REPLACE) fun insert(replaceRule: ReplaceRule): Long + @Update fun update(vararg replaceRules: ReplaceRule) diff --git a/app/src/main/java/io/legado/app/data/dao/SearchKeywordDao.kt b/app/src/main/java/io/legado/app/data/dao/SearchKeywordDao.kt new file mode 100644 index 000000000..b2370ceb6 --- /dev/null +++ b/app/src/main/java/io/legado/app/data/dao/SearchKeywordDao.kt @@ -0,0 +1,32 @@ +package io.legado.app.data.dao + +import androidx.paging.DataSource +import androidx.room.* +import io.legado.app.data.entities.SearchKeyword + + +@Dao +interface SearchKeywordDao { + + @Query("SELECT * FROM search_keywords ORDER BY usage DESC") + fun observeByUsage(): DataSource.Factory + + @Query("SELECT * FROM search_keywords ORDER BY lastUseTime DESC") + fun observeByTime(): DataSource.Factory + + @Insert(onConflict = OnConflictStrategy.REPLACE) + fun insert(vararg keywords: SearchKeyword) + + @Insert(onConflict = OnConflictStrategy.REPLACE) + fun insert(keyword: SearchKeyword): Long + + @Update + fun update(vararg keywords: SearchKeyword) + + @Delete + fun delete(vararg keywords: SearchKeyword) + + @Query("DELETE FROM search_keywords") + fun deleteAll() + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/data/entities/ExploreSearchUrl.kt b/app/src/main/java/io/legado/app/data/entities/ExploreSearchUrl.kt new file mode 100644 index 000000000..2df63b4bb --- /dev/null +++ b/app/src/main/java/io/legado/app/data/entities/ExploreSearchUrl.kt @@ -0,0 +1,29 @@ +package io.legado.app.data.entities + +import android.os.Parcelable +import androidx.room.Entity +import androidx.room.ForeignKey +import androidx.room.Index +import androidx.room.PrimaryKey +import kotlinx.android.parcel.Parcelize + +@Parcelize +@Entity(tableName = "explore_search_urls", + indices = [(Index(value = ["sourceId", "url"], unique = true))], + foreignKeys = [(ForeignKey(entity = Source::class, + parentColumns = ["sourceId"], + childColumns = ["sourceId"], + onDelete = ForeignKey.CASCADE))]) // 删除书源时自动删除章节 +data class ExploreSearchUrl ( + @PrimaryKey(autoGenerate = true) + var esId: Int = 0, // 编号 + var sourceId: Int = 0, // 书源Id + var name: String = "", // 发现名称,搜索可以没有 + var url: String = "", // 地址 + var type: Int = 0, // 类型,0 为发现,1 为搜索 + var isEnabled: Boolean = true, // 是否启用 + var defOrder: Int = 0, // 默认排序,是在编辑书源的时候的顺序 + var usage: Int = 0, // 使用次数,用于按使用次数排序 + var lastUseTime: Long = 0L // 最后一次使用的时间 + ) : Parcelable + diff --git a/app/src/main/java/io/legado/app/data/entities/SearchKeyword.kt b/app/src/main/java/io/legado/app/data/entities/SearchKeyword.kt new file mode 100644 index 000000000..e8dce4fd9 --- /dev/null +++ b/app/src/main/java/io/legado/app/data/entities/SearchKeyword.kt @@ -0,0 +1,17 @@ +package io.legado.app.data.entities + +import android.os.Parcelable +import androidx.room.Entity +import androidx.room.Index +import androidx.room.PrimaryKey +import kotlinx.android.parcel.Parcelize + + +@Parcelize +@Entity(tableName = "search_keywords", indices = [(Index(value = ["word"], unique = true))]) +data class SearchKeyword ( + @PrimaryKey + var word: String = "", // 搜索关键词 + var usage: Int = 1, // 使用次数 + var lastUseTime: Long = 0 // 最后一次使用时间 +): Parcelable