修改弃用方法

pull/921/head
gedoor 4 years ago
parent e6b2272f00
commit 65f2880920
  1. 13
      app/src/main/java/io/legado/app/ui/book/toc/BookmarkFragment.kt
  2. 7
      app/src/main/java/io/legado/app/ui/book/toc/ChapterListActivity.kt
  3. 18
      app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt
  4. 11
      app/src/main/java/io/legado/app/ui/book/toc/ChapterListViewModel.kt
  5. 2
      app/src/main/java/io/legado/app/ui/rss/article/RssSortActivity.kt

@ -13,6 +13,7 @@ import androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.VMBaseFragment import io.legado.app.base.VMBaseFragment
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.Bookmark import io.legado.app.data.entities.Bookmark
import io.legado.app.databinding.DialogBookmarkBinding import io.legado.app.databinding.DialogBookmarkBinding
import io.legado.app.databinding.FragmentBookmarkBinding import io.legado.app.databinding.FragmentBookmarkBinding
@ -35,7 +36,9 @@ class BookmarkFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragment_
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
viewModel.bookMarkCallBack = this viewModel.bookMarkCallBack = this
initRecyclerView() initRecyclerView()
initData() viewModel.bookData.observe(this) {
initData(it)
}
} }
private fun initRecyclerView() { private fun initRecyclerView() {
@ -46,8 +49,7 @@ class BookmarkFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragment_
binding.recyclerView.adapter = adapter binding.recyclerView.adapter = adapter
} }
private fun initData() { private fun initData(book: Book) {
viewModel.book?.let { book ->
bookmarkLiveData?.removeObservers(viewLifecycleOwner) bookmarkLiveData?.removeObservers(viewLifecycleOwner)
bookmarkLiveData = bookmarkLiveData =
LivePagedListBuilder( LivePagedListBuilder(
@ -55,11 +57,12 @@ class BookmarkFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragment_
).build() ).build()
bookmarkLiveData?.observe(viewLifecycleOwner, { adapter.submitList(it) }) bookmarkLiveData?.observe(viewLifecycleOwner, { adapter.submitList(it) })
} }
}
override fun startBookmarkSearch(newText: String?) { override fun startBookmarkSearch(newText: String?) {
if (newText.isNullOrBlank()) { if (newText.isNullOrBlank()) {
initData() viewModel.bookData.value?.let {
initData(it)
}
} else { } else {
bookmarkLiveData?.removeObservers(viewLifecycleOwner) bookmarkLiveData?.removeObservers(viewLifecycleOwner)
bookmarkLiveData = LivePagedListBuilder( bookmarkLiveData = LivePagedListBuilder(

@ -34,16 +34,15 @@ class ChapterListActivity : VMBaseActivity<ActivityChapterListBinding, ChapterLi
tabLayout = binding.titleBar.findViewById(R.id.tab_layout) tabLayout = binding.titleBar.findViewById(R.id.tab_layout)
tabLayout.isTabIndicatorFullWidth = false tabLayout.isTabIndicatorFullWidth = false
tabLayout.setSelectedTabIndicatorColor(accentColor) tabLayout.setSelectedTabIndicatorColor(accentColor)
intent.getStringExtra("bookUrl")?.let {
viewModel.initBook(it) {
binding.viewPager.adapter = TabFragmentPageAdapter() binding.viewPager.adapter = TabFragmentPageAdapter()
TabLayoutMediator(tabLayout, binding.viewPager) { tab, position -> TabLayoutMediator(tabLayout, binding.viewPager) { tab, position ->
when (position) { when (position) {
0 -> tab.setText(R.string.chapter_list) 0 -> tab.setText(R.string.chapter_list)
else -> tab.setText(R.string.bookmark) else -> tab.setText(R.string.bookmark)
} }
} }.attach()
} intent.getStringExtra("bookUrl")?.let {
viewModel.initBook(it)
} }
} }

@ -49,7 +49,9 @@ class ChapterListFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragme
ivChapterBottom.setColorFilter(btc) ivChapterBottom.setColorFilter(btc)
initRecyclerView() initRecyclerView()
initView() initView()
initBook() viewModel.bookData.observe(this@ChapterListFragment) {
initBook(it)
}
} }
private fun initRecyclerView() { private fun initRecyclerView() {
@ -73,15 +75,13 @@ class ChapterListFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragme
} }
@SuppressLint("SetTextI18n") @SuppressLint("SetTextI18n")
private fun initBook() { private fun initBook(book: Book) {
launch { launch {
initDoc() initDoc()
viewModel.book?.let { durChapterIndex = book.durChapterIndex
durChapterIndex = it.durChapterIndex
binding.tvCurrentChapterInfo.text = binding.tvCurrentChapterInfo.text =
"${it.durChapterTitle}(${it.durChapterIndex + 1}/${it.totalChapterNum})" "${book.durChapterTitle}(${book.durChapterIndex + 1}/${book.totalChapterNum})"
initCacheFileNames(it) initCacheFileNames(book)
}
} }
} }
@ -108,7 +108,7 @@ class ChapterListFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragme
override fun observeLiveBus() { override fun observeLiveBus() {
observeEvent<BookChapter>(EventBus.SAVE_CONTENT) { chapter -> observeEvent<BookChapter>(EventBus.SAVE_CONTENT) { chapter ->
viewModel.book?.bookUrl?.let { bookUrl -> viewModel.bookData.value?.bookUrl?.let { bookUrl ->
if (chapter.bookUrl == bookUrl) { if (chapter.bookUrl == bookUrl) {
adapter.cacheFileNames.add(chapter.getFileName()) adapter.cacheFileNames.add(chapter.getFileName())
adapter.notifyItemChanged(chapter.index, true) adapter.notifyItemChanged(chapter.index, true)
@ -130,7 +130,7 @@ class ChapterListFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragme
} }
override val isLocalBook: Boolean override val isLocalBook: Boolean
get() = viewModel.book?.isLocalBook() == true get() = viewModel.bookData.value?.isLocalBook() == true
override fun durChapterIndex(): Int { override fun durChapterIndex(): Int {
return min(durChapterIndex, adapter.itemCount) return min(durChapterIndex, adapter.itemCount)

@ -2,22 +2,23 @@ package io.legado.app.ui.book.toc
import android.app.Application import android.app.Application
import androidx.lifecycle.MutableLiveData
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
class ChapterListViewModel(application: Application) : BaseViewModel(application) { class ChapterListViewModel(application: Application) : BaseViewModel(application) {
var bookUrl: String = "" var bookUrl: String = ""
var book: Book? = null var bookData = MutableLiveData<Book>()
var chapterCallBack: ChapterListCallBack? = null var chapterCallBack: ChapterListCallBack? = null
var bookMarkCallBack: BookmarkCallBack? = null var bookMarkCallBack: BookmarkCallBack? = null
fun initBook(bookUrl: String, success: () -> Unit) { fun initBook(bookUrl: String) {
this.bookUrl = bookUrl this.bookUrl = bookUrl
execute { execute {
book = appDb.bookDao.getBook(bookUrl) appDb.bookDao.getBook(bookUrl)?.let {
}.onSuccess { bookData.postValue(it)
success.invoke() }
} }
} }

@ -41,7 +41,7 @@ class RssSortActivity : VMBaseActivity<ActivityRssArtivlesBinding, RssSortViewMo
binding.viewPager.adapter = adapter binding.viewPager.adapter = adapter
TabLayoutMediator(binding.tabLayout, binding.viewPager) { tab, position -> TabLayoutMediator(binding.tabLayout, binding.viewPager) { tab, position ->
tab.text = fragments.keys.elementAt(position) tab.text = fragments.keys.elementAt(position)
} }.attach()
viewModel.titleLiveData.observe(this, { viewModel.titleLiveData.observe(this, {
binding.titleBar.title = it binding.titleBar.title = it
}) })

Loading…
Cancel
Save