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 f6f3d8f07..a5f76d907 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 @@ -24,7 +24,10 @@ interface SearchBookDao { fun getSearchBook(bookUrl: String): SearchBook? @Query("select * from searchBooks where name = :name and author = :author order by originOrder limit 1") - fun getByNameAuthor(name: String, author: String?): SearchBook? + fun getFirstByNameAuthor(name: String, author: String): SearchBook? + + @Query("select * from searchBooks where name = :name and author = :author order by originOrder") + fun getByNameAuthor(name: String, author: String): List @Insert(onConflict = OnConflictStrategy.REPLACE) fun insert(vararg searchBook: SearchBook): List diff --git a/app/src/main/java/io/legado/app/data/entities/SearchShow.kt b/app/src/main/java/io/legado/app/data/entities/SearchShow.kt index 31268aefb..1611e04ac 100644 --- a/app/src/main/java/io/legado/app/data/entities/SearchShow.kt +++ b/app/src/main/java/io/legado/app/data/entities/SearchShow.kt @@ -3,8 +3,8 @@ package io.legado.app.data.entities import io.legado.app.utils.splitNotBlank data class SearchShow( - var name: String? = null, - var author: String? = null, + var name: String = "", + var author: String = "", var kind: String? = null, var coverUrl: String? = null, var intro: String? = null, diff --git a/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceDialog.kt b/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceDialog.kt index b34cef9cc..8ae5c0857 100644 --- a/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceDialog.kt +++ b/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceDialog.kt @@ -5,8 +5,11 @@ import android.util.DisplayMetrics import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.LinearLayout import androidx.appcompat.widget.SearchView import androidx.fragment.app.DialogFragment +import androidx.lifecycle.Observer +import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.R import io.legado.app.data.entities.Book @@ -70,7 +73,11 @@ class ChangeSourceDialog : DialogFragment() { private fun initRecyclerView() { changeSourceAdapter = ChangeSourceAdapter(requireContext()) recycler_view.layoutManager = LinearLayoutManager(context) + recycler_view.addItemDecoration(DividerItemDecoration(requireContext(), LinearLayout.VERTICAL)) recycler_view.adapter = changeSourceAdapter + viewModel.searchBookData.observe(viewLifecycleOwner, Observer { + changeSourceAdapter.addItems(it) + }) } private fun initSearchView() { diff --git a/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.kt index 218a4868f..c95189097 100644 --- a/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.kt @@ -1,15 +1,22 @@ package io.legado.app.ui.changesource import android.app.Application +import androidx.lifecycle.MutableLiveData +import io.legado.app.App import io.legado.app.base.BaseViewModel +import io.legado.app.data.entities.SearchBook class ChangeSourceViewModel(application: Application) : BaseViewModel(application) { var name: String = "" var author: String = "" - + val searchBookData = MutableLiveData>() fun startSearch() { - + execute { + App.db.searchBookDao().getByNameAuthor(name, author).let { + searchBookData.postValue(it) + } + } } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/search/SearchActivity.kt b/app/src/main/java/io/legado/app/ui/search/SearchActivity.kt index 2493916b3..61030e62c 100644 --- a/app/src/main/java/io/legado/app/ui/search/SearchActivity.kt +++ b/app/src/main/java/io/legado/app/ui/search/SearchActivity.kt @@ -78,7 +78,7 @@ class SearchActivity : VMBaseActivity(R.layout.activity_search) searchBookData?.observe(this, Observer { adapter.submitList(it) }) } - override fun showBookInfo(name: String, author: String?) { + override fun showBookInfo(name: String, author: String) { viewModel.getSearchBook(name, author) { searchBook -> searchBook?.let { startActivity(Pair("searchBookUrl", it.bookUrl)) diff --git a/app/src/main/java/io/legado/app/ui/search/SearchAdapter.kt b/app/src/main/java/io/legado/app/ui/search/SearchAdapter.kt index e6653287f..d3bcf6bed 100644 --- a/app/src/main/java/io/legado/app/ui/search/SearchAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/search/SearchAdapter.kt @@ -96,12 +96,12 @@ class SearchAdapter : PagedListAdapter(D .setAsDrawable(iv_cover) } onClick { - callBack?.showBookInfo(searchBook.name!!, searchBook.author) + callBack?.showBookInfo(searchBook.name, searchBook.author) } } } interface CallBack { - fun showBookInfo(name: String, author: String?) + fun showBookInfo(name: String, author: String) } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt b/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt index 870e64d05..7ab0db2d8 100644 --- a/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt @@ -48,9 +48,9 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { task?.cancel() } - fun getSearchBook(name: String, author: String?, success: ((searchBook: SearchBook?) -> Unit)?) { + fun getSearchBook(name: String, author: String, success: ((searchBook: SearchBook?) -> Unit)?) { execute { - val searchBook = App.db.searchBookDao().getByNameAuthor(name, author) + val searchBook = App.db.searchBookDao().getFirstByNameAuthor(name, author) success?.invoke(searchBook) } } diff --git a/app/src/main/res/layout/dialog_change_source.xml b/app/src/main/res/layout/dialog_change_source.xml index cd6738e3c..5d96cbdf8 100644 --- a/app/src/main/res/layout/dialog_change_source.xml +++ b/app/src/main/res/layout/dialog_change_source.xml @@ -18,7 +18,7 @@ diff --git a/app/src/main/res/layout/item_change_source.xml b/app/src/main/res/layout/item_change_source.xml index 128f2710f..2cf9b5649 100644 --- a/app/src/main/res/layout/item_change_source.xml +++ b/app/src/main/res/layout/item_change_source.xml @@ -1,7 +1,7 @@