开启书籍标签搜索

pull/75/head
Administrator 5 years ago
parent 5ed5352634
commit 445c108450
  1. 3
      app/src/main/java/io/legado/app/data/dao/BookmarkDao.kt
  2. 14
      app/src/main/java/io/legado/app/ui/chapterlist/BookmarkFragment.kt
  3. 2
      app/src/main/java/io/legado/app/ui/chapterlist/ChapterListActivity.kt
  4. 16
      app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt
  5. 17
      app/src/main/java/io/legado/app/ui/chapterlist/ChapterListViewModel.kt

@ -17,6 +17,9 @@ interface BookmarkDao {
@Query("select * from bookmarks where bookUrl = :bookUrl")
fun observeByBook(bookUrl: String): DataSource.Factory<Int, Bookmark>
@Query("SELECT * FROM bookmarks where bookUrl = :bookUrl and chapterName like '%'||:key||'%' or content like '%'||:key||'%'")
fun liveDataSearch(bookUrl: String, key: String): DataSource.Factory<Int, Bookmark>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(vararg bookmark: Bookmark)

@ -21,7 +21,8 @@ import kotlinx.android.synthetic.main.fragment_bookmark.*
class BookmarkFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragment_bookmark),
BookmarkAdapter.Callback {
BookmarkAdapter.Callback,
ChapterListViewModel.BookmarkCallBack {
override val viewModel: ChapterListViewModel
get() = getViewModelOfActivity(ChapterListViewModel::class.java)
@ -30,6 +31,7 @@ class BookmarkFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragment_
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel.bookMarkCallBack = this
initRecyclerView()
initData()
}
@ -53,6 +55,16 @@ class BookmarkFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragment_
bookmarkLiveData?.observe(viewLifecycleOwner, Observer { adapter.submitList(it) })
}
override fun startBookmarkSearch(newText: String?) {
if (newText.isNullOrBlank()) {
initData()
} else {
bookmarkLiveData?.removeObservers(viewLifecycleOwner)
bookmarkLiveData = LivePagedListBuilder(App.db.bookmarkDao().liveDataSearch(viewModel.bookUrl ?: "", newText), 20).build()
bookmarkLiveData?.observe(viewLifecycleOwner, Observer { adapter.submitList(it) })
}
}
override fun open(bookmark: Bookmark) {
val bookmarkData = Intent()
bookmarkData.putExtra("index", bookmark.chapterIndex)

@ -54,7 +54,7 @@ class ChapterListActivity : VMBaseActivity<ChapterListViewModel>(R.layout.activi
override fun onQueryTextChange(newText: String): Boolean {
if (tab_layout.selectedTabPosition == 1) {
viewModel.startBookmarkSearch(newText)
} else {
viewModel.startChapterListSearch(newText)
}

@ -20,7 +20,7 @@ import org.jetbrains.anko.sdk27.listeners.onClick
class ChapterListFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragment_chapter_list),
ChapterListAdapter.Callback,
ChapterListViewModel.CallBack {
ChapterListViewModel.ChapterListCallBack{
override val viewModel: ChapterListViewModel
get() = getViewModelOfActivity(ChapterListViewModel::class.java)
@ -30,6 +30,7 @@ class ChapterListFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragme
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel.chapterCallBack = this
initRecyclerView()
initView()
initData()
@ -74,19 +75,16 @@ class ChapterListFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragme
mLayoutManager.scrollToPositionWithOffset(it.durChapterIndex, 0)
}
}
viewModel.callBack = this
}
override fun startSearch(newText: String?) {
override fun startChapterListSearch(newText: String?) {
if (newText.isNullOrBlank()) {
initData()
} else {
viewModel.bookUrl?.let { bookUrl ->
App.db.bookChapterDao().liveDataSearch(bookUrl, newText).observe(viewLifecycleOwner, Observer {
adapter.setItems(it)
mLayoutManager.scrollToPositionWithOffset(0, 0)
})
}
App.db.bookChapterDao().liveDataSearch(viewModel.bookUrl ?: "", newText).observe(viewLifecycleOwner, Observer {
adapter.setItems(it)
mLayoutManager.scrollToPositionWithOffset(0, 0)
})
}
}

@ -9,7 +9,8 @@ import io.legado.app.data.entities.Book
class ChapterListViewModel(application: Application) : BaseViewModel(application) {
var bookUrl: String? = null
var book: Book? = null
var callBack: CallBack? = null
var chapterCallBack: ChapterListCallBack? = null
var bookMarkCallBack: BookmarkCallBack? = null
fun loadBook(success: () -> Unit) {
execute {
@ -22,10 +23,18 @@ class ChapterListViewModel(application: Application) : BaseViewModel(application
}
fun startChapterListSearch(newText: String?) {
callBack?.startSearch(newText)
chapterCallBack?.startChapterListSearch(newText)
}
interface CallBack {
fun startSearch(newText: String?)
fun startBookmarkSearch(newText: String?) {
bookMarkCallBack?.startBookmarkSearch(newText)
}
interface ChapterListCallBack {
fun startChapterListSearch(newText: String?)
}
interface BookmarkCallBack {
fun startBookmarkSearch(newText: String?)
}
}
Loading…
Cancel
Save