pull/43/head
kunfei 5 years ago
parent 4327792ba7
commit c7540eec65
  1. 1
      app/src/main/java/io/legado/app/constant/PreferKey.kt
  2. 10
      app/src/main/java/io/legado/app/data/dao/SearchBookDao.kt
  3. 9
      app/src/main/java/io/legado/app/ui/book/search/DiffCallBack.kt
  4. 12
      app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt
  5. 9
      app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt
  6. 4
      app/src/main/java/io/legado/app/ui/book/search/SearchEntry.kt
  7. 14
      app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt

@ -5,4 +5,5 @@ object PreferKey {
const val downloadPath = "downloadPath"
const val hideStatusBar = "hideStatusBar"
const val hideNavigationBar = "hideNavigationBar"
const val precisionSearch = "precisionSearch"
}

@ -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<Int, SearchShow>
@Query("SELECT * FROM searchBooks")
fun observeAll(): DataSource.Factory<Int, SearchBook>

@ -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<SearchShow>() {
override fun areItemsTheSame(oldItem: SearchShow, newItem: SearchShow): Boolean {
class DiffCallBack : DiffUtil.ItemCallback<SearchEntry>() {
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<SearchShow>() {
&& 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

@ -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<SearchViewModel>(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<SearchViewModel>(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<SearchViewModel>(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<BookSourceActivity>()
else -> if (item.groupId == R.id.source_group) {

@ -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<SearchShow>(context, R.layout.item_search) {
SimpleRecyclerAdapter<SearchEntry>(context, R.layout.item_search) {
override fun convert(holder: ItemViewHolder, item: SearchShow, payloads: MutableList<Any>) {
override fun convert(holder: ItemViewHolder, item: SearchEntry, payloads: MutableList<Any>) {
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<Any>) {
private fun bindChange(itemView: View, searchBook: SearchEntry, payloads: MutableList<Any>) {
with(itemView) {
when (payloads[0]) {
1 -> bv_originCount.setBadgeCount(searchBook.originCount)

@ -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,

@ -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<SearchShow>()
private val booksShow = arrayListOf<SearchEntry>()
fun search(
key: String,
@ -72,7 +72,7 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
private fun searchSuccess(searchBooks: List<SearchBook>) {
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<SearchBook>) {
callBack?.adapter.let { adapter ->
}
}
fun stop() {
task?.cancel()
}
@ -113,6 +119,6 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
}
interface CallBack {
var adapter: SearchAdapter
}
}

Loading…
Cancel
Save