From c7540eec6550e66c60b4a1b28c17df8f1ee07097 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 28 Oct 2019 18:45:58 +0800 Subject: [PATCH] up --- .../main/java/io/legado/app/constant/PreferKey.kt | 1 + .../java/io/legado/app/data/dao/SearchBookDao.kt | 10 ---------- .../io/legado/app/ui/book/search/DiffCallBack.kt | 9 ++++----- .../io/legado/app/ui/book/search/SearchActivity.kt | 12 ++++++++---- .../io/legado/app/ui/book/search/SearchAdapter.kt | 9 ++++----- .../book/search/SearchEntry.kt} | 4 ++-- .../legado/app/ui/book/search/SearchViewModel.kt | 14 ++++++++++---- 7 files changed, 29 insertions(+), 30 deletions(-) rename app/src/main/java/io/legado/app/{data/entities/SearchShow.kt => ui/book/search/SearchEntry.kt} (91%) diff --git a/app/src/main/java/io/legado/app/constant/PreferKey.kt b/app/src/main/java/io/legado/app/constant/PreferKey.kt index c52c79c38..1bf8e9a30 100644 --- a/app/src/main/java/io/legado/app/constant/PreferKey.kt +++ b/app/src/main/java/io/legado/app/constant/PreferKey.kt @@ -5,4 +5,5 @@ object PreferKey { const val downloadPath = "downloadPath" const val hideStatusBar = "hideStatusBar" const val hideNavigationBar = "hideNavigationBar" + const val precisionSearch = "precisionSearch" } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/data/dao/SearchBookDao.kt b/app/src/main/java/io/legado/app/data/dao/SearchBookDao.kt index a8b61a068..7e623bfcc 100644 --- a/app/src/main/java/io/legado/app/data/dao/SearchBookDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/SearchBookDao.kt @@ -6,20 +6,10 @@ import androidx.room.Insert import androidx.room.OnConflictStrategy import androidx.room.Query import io.legado.app.data.entities.SearchBook -import io.legado.app.data.entities.SearchShow @Dao interface SearchBookDao { - @Query( - """SELECT name, author, min(time) time, max(kind) kind, max(coverUrl) coverUrl,max(intro) intro, max(wordCount) wordCount, - max(latestChapterTitle) latestChapterTitle, count(origin) originCount - FROM searchBooks where time >= :time - group by name, author - order by case when name = :key then 1 when author = :key then 2 when name like '%'||:key||'%' then 3 when author like '%'||:key||'%' then 4 else 5 end, time""" - ) - fun observeShow(key: String, time: Long): DataSource.Factory - @Query("SELECT * FROM searchBooks") fun observeAll(): DataSource.Factory diff --git a/app/src/main/java/io/legado/app/ui/book/search/DiffCallBack.kt b/app/src/main/java/io/legado/app/ui/book/search/DiffCallBack.kt index 0de21ee19..064c158a0 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/DiffCallBack.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/DiffCallBack.kt @@ -1,15 +1,14 @@ package io.legado.app.ui.book.search import androidx.recyclerview.widget.DiffUtil -import io.legado.app.data.entities.SearchShow -class DiffCallBack : DiffUtil.ItemCallback() { - override fun areItemsTheSame(oldItem: SearchShow, newItem: SearchShow): Boolean { +class DiffCallBack : DiffUtil.ItemCallback() { + override fun areItemsTheSame(oldItem: SearchEntry, newItem: SearchEntry): Boolean { return oldItem.name == newItem.name && oldItem.author == newItem.author } - override fun areContentsTheSame(oldItem: SearchShow, newItem: SearchShow): Boolean { + override fun areContentsTheSame(oldItem: SearchEntry, newItem: SearchEntry): Boolean { return oldItem.originCount == newItem.originCount && (oldItem.coverUrl == newItem.coverUrl || !oldItem.coverUrl.isNullOrEmpty()) && (oldItem.kind == newItem.kind || !oldItem.kind.isNullOrEmpty()) @@ -17,7 +16,7 @@ class DiffCallBack : DiffUtil.ItemCallback() { && oldItem.intro?.length ?: 0 > newItem.intro?.length ?: 0 } - override fun getChangePayload(oldItem: SearchShow, newItem: SearchShow): Any? { + override fun getChangePayload(oldItem: SearchEntry, newItem: SearchEntry): Any? { return when { oldItem.originCount != newItem.originCount -> 1 oldItem.coverUrl != newItem.coverUrl -> 2 diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt index b0c1efba9..4b79b95f7 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt @@ -11,6 +11,7 @@ import androidx.recyclerview.widget.RecyclerView import io.legado.app.App import io.legado.app.R import io.legado.app.base.VMBaseActivity +import io.legado.app.constant.PreferKey import io.legado.app.data.entities.Book import io.legado.app.data.entities.SearchKeyword import io.legado.app.help.IntentDataHelp @@ -37,7 +38,7 @@ class SearchActivity : VMBaseActivity(R.layout.activity_book_se override val viewModel: SearchViewModel get() = getViewModel(SearchViewModel::class.java) - private lateinit var adapter: SearchAdapter + override lateinit var adapter: SearchAdapter private lateinit var bookAdapter: BookAdapter private lateinit var historyKeyAdapter: HistoryKeyAdapter private lateinit var loadMoreView: LoadMoreView @@ -59,7 +60,7 @@ class SearchActivity : VMBaseActivity(R.layout.activity_book_se override fun onCompatCreateOptionsMenu(menu: Menu): Boolean { menuInflater.inflate(R.menu.book_search, menu) precisionSearchMenuItem = menu.findItem(R.id.menu_precision_search) - precisionSearchMenuItem?.isChecked = getPrefBoolean("precisionSearch") + precisionSearchMenuItem?.isChecked = getPrefBoolean(PreferKey.precisionSearch) this.menu = menu upGroupMenu() return super.onCompatCreateOptionsMenu(menu) @@ -68,8 +69,11 @@ class SearchActivity : VMBaseActivity(R.layout.activity_book_se override fun onCompatOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { R.id.menu_precision_search -> { - putPrefBoolean("precisionSearch", !getPrefBoolean("precisionSearch")) - precisionSearchMenuItem?.isChecked = getPrefBoolean("precisionSearch") + putPrefBoolean( + PreferKey.precisionSearch, + !getPrefBoolean(PreferKey.precisionSearch) + ) + precisionSearchMenuItem?.isChecked = getPrefBoolean(PreferKey.precisionSearch) } R.id.menu_source_manage -> startActivity() else -> if (item.groupId == R.id.source_group) { diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt index 33c9ae637..ce48fadb2 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt @@ -5,7 +5,6 @@ import android.view.View import io.legado.app.R import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.SimpleRecyclerAdapter -import io.legado.app.data.entities.SearchShow import io.legado.app.help.ImageLoader import io.legado.app.utils.gone import io.legado.app.utils.visible @@ -15,9 +14,9 @@ import kotlinx.android.synthetic.main.item_search.view.* import org.jetbrains.anko.sdk27.listeners.onClick class SearchAdapter(context: Context, val callBack: CallBack) : - SimpleRecyclerAdapter(context, R.layout.item_search) { + SimpleRecyclerAdapter(context, R.layout.item_search) { - override fun convert(holder: ItemViewHolder, item: SearchShow, payloads: MutableList) { + override fun convert(holder: ItemViewHolder, item: SearchEntry, payloads: MutableList) { if (payloads.isEmpty()) { bind(holder.itemView, item) } else { @@ -25,7 +24,7 @@ class SearchAdapter(context: Context, val callBack: CallBack) : } } - private fun bind(itemView: View, searchBook: SearchShow) { + private fun bind(itemView: View, searchBook: SearchEntry) { with(itemView) { tv_name.text = searchBook.name tv_author.text = context.getString(R.string.author_show, searchBook.author) @@ -80,7 +79,7 @@ class SearchAdapter(context: Context, val callBack: CallBack) : } } - private fun bindChange(itemView: View, searchBook: SearchShow, payloads: MutableList) { + private fun bindChange(itemView: View, searchBook: SearchEntry, payloads: MutableList) { with(itemView) { when (payloads[0]) { 1 -> bv_originCount.setBadgeCount(searchBook.originCount) diff --git a/app/src/main/java/io/legado/app/data/entities/SearchShow.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchEntry.kt similarity index 91% rename from app/src/main/java/io/legado/app/data/entities/SearchShow.kt rename to app/src/main/java/io/legado/app/ui/book/search/SearchEntry.kt index a738909ea..4350c7830 100644 --- a/app/src/main/java/io/legado/app/data/entities/SearchShow.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/SearchEntry.kt @@ -1,8 +1,8 @@ -package io.legado.app.data.entities +package io.legado.app.ui.book.search import io.legado.app.utils.splitNotBlank -data class SearchShow( +data class SearchEntry( var name: String = "", var author: String = "", var kind: String? = null, diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt index 9f5d42568..d6882f687 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt @@ -3,9 +3,9 @@ package io.legado.app.ui.book.search import android.app.Application import io.legado.app.App import io.legado.app.base.BaseViewModel +import io.legado.app.constant.PreferKey import io.legado.app.data.entities.SearchBook import io.legado.app.data.entities.SearchKeyword -import io.legado.app.data.entities.SearchShow import io.legado.app.help.coroutine.Coroutine import io.legado.app.model.WebBook import io.legado.app.utils.getPrefBoolean @@ -22,7 +22,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { var startTime: Long = 0 var searchPage = 0 var isLoading = false - private val booksShow = arrayListOf() + private val booksShow = arrayListOf() fun search( key: String, @@ -72,7 +72,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { private fun searchSuccess(searchBooks: List) { searchBooks.forEach { searchBook -> - if (context.getPrefBoolean("precisionSearch")) { + if (context.getPrefBoolean(PreferKey.precisionSearch)) { if (searchBook.name.contains(searchKey) || searchBook.author.contains(searchKey) ) App.db.searchBookDao().insert(searchBook) @@ -81,6 +81,12 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { } } + private fun addToAdapter(searchBooks: List) { + callBack?.adapter.let { adapter -> + + } + } + fun stop() { task?.cancel() } @@ -113,6 +119,6 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { } interface CallBack { - + var adapter: SearchAdapter } }