|
|
|
@ -6,7 +6,6 @@ import android.content.Intent |
|
|
|
|
import android.os.Bundle |
|
|
|
|
import android.view.View |
|
|
|
|
import androidx.fragment.app.activityViewModels |
|
|
|
|
import androidx.lifecycle.LiveData |
|
|
|
|
import io.legado.app.R |
|
|
|
|
import io.legado.app.base.VMBaseFragment |
|
|
|
|
import io.legado.app.constant.EventBus |
|
|
|
@ -24,6 +23,8 @@ import io.legado.app.utils.observeEvent |
|
|
|
|
import io.legado.app.utils.viewbindingdelegate.viewBinding |
|
|
|
|
import kotlinx.coroutines.Dispatchers.IO |
|
|
|
|
import kotlinx.coroutines.Dispatchers.Main |
|
|
|
|
import kotlinx.coroutines.Job |
|
|
|
|
import kotlinx.coroutines.flow.collect |
|
|
|
|
import kotlinx.coroutines.launch |
|
|
|
|
import kotlinx.coroutines.withContext |
|
|
|
|
import kotlin.math.min |
|
|
|
@ -36,7 +37,7 @@ class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapt |
|
|
|
|
lateinit var adapter: ChapterListAdapter |
|
|
|
|
private var durChapterIndex = 0 |
|
|
|
|
private lateinit var mLayoutManager: UpLinearLayoutManager |
|
|
|
|
private var tocLiveData: LiveData<List<BookChapter>>? = null |
|
|
|
|
private var tocFlowJob: Job? = null |
|
|
|
|
private var scrollToDurChapter = false |
|
|
|
|
|
|
|
|
|
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) = binding.run { |
|
|
|
@ -77,7 +78,7 @@ class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapt |
|
|
|
|
@SuppressLint("SetTextI18n") |
|
|
|
|
private fun initBook(book: Book) { |
|
|
|
|
launch { |
|
|
|
|
initToc() |
|
|
|
|
upChapterList(null) |
|
|
|
|
durChapterIndex = book.durChapterIndex |
|
|
|
|
binding.tvCurrentChapterInfo.text = |
|
|
|
|
"${book.durChapterTitle}(${book.durChapterIndex + 1}/${book.totalChapterNum})" |
|
|
|
@ -85,18 +86,6 @@ class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapt |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun initToc() { |
|
|
|
|
tocLiveData?.removeObservers(this@ChapterListFragment) |
|
|
|
|
tocLiveData = appDb.bookChapterDao.observeByBook(viewModel.bookUrl) |
|
|
|
|
tocLiveData?.observe(viewLifecycleOwner, { |
|
|
|
|
adapter.setItems(it) |
|
|
|
|
if (!scrollToDurChapter) { |
|
|
|
|
mLayoutManager.scrollToPositionWithOffset(durChapterIndex, 0) |
|
|
|
|
scrollToDurChapter = true |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun initCacheFileNames(book: Book) { |
|
|
|
|
launch(IO) { |
|
|
|
|
adapter.cacheFileNames.addAll(BookHelp.getChapterFiles(book)) |
|
|
|
@ -117,15 +106,19 @@ class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapt |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun startChapterListSearch(newText: String?) { |
|
|
|
|
if (newText.isNullOrBlank()) { |
|
|
|
|
initToc() |
|
|
|
|
} else { |
|
|
|
|
tocLiveData?.removeObservers(this) |
|
|
|
|
tocLiveData = appDb.bookChapterDao.liveDataSearch(viewModel.bookUrl, newText) |
|
|
|
|
tocLiveData?.observe(viewLifecycleOwner, { |
|
|
|
|
override fun upChapterList(searchKey: String?) { |
|
|
|
|
tocFlowJob?.cancel() |
|
|
|
|
tocFlowJob = launch { |
|
|
|
|
when { |
|
|
|
|
searchKey.isNullOrBlank() -> appDb.bookChapterDao.observeByBook(viewModel.bookUrl) |
|
|
|
|
else -> appDb.bookChapterDao.liveDataSearch(viewModel.bookUrl, searchKey) |
|
|
|
|
}.collect { |
|
|
|
|
adapter.setItems(it) |
|
|
|
|
}) |
|
|
|
|
if (searchKey.isNullOrBlank() && !scrollToDurChapter) { |
|
|
|
|
mLayoutManager.scrollToPositionWithOffset(durChapterIndex, 0) |
|
|
|
|
scrollToDurChapter = true |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|