|
|
|
@ -21,10 +21,11 @@ import io.legado.app.ui.widget.recycler.VerticalDivider |
|
|
|
|
import io.legado.app.utils.ColorUtils |
|
|
|
|
import io.legado.app.utils.observeEvent |
|
|
|
|
import io.legado.app.utils.viewbindingdelegate.viewBinding |
|
|
|
|
import kotlinx.coroutines.* |
|
|
|
|
import kotlinx.coroutines.CoroutineScope |
|
|
|
|
import kotlinx.coroutines.Dispatchers.IO |
|
|
|
|
import kotlinx.coroutines.Dispatchers.Main |
|
|
|
|
import kotlinx.coroutines.flow.conflate |
|
|
|
|
import kotlinx.coroutines.launch |
|
|
|
|
import kotlinx.coroutines.withContext |
|
|
|
|
|
|
|
|
|
class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapter_list), |
|
|
|
|
ChapterListAdapter.Callback, |
|
|
|
@ -34,10 +35,9 @@ class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapt |
|
|
|
|
private val mLayoutManager by lazy { UpLinearLayoutManager(requireContext()) } |
|
|
|
|
private val adapter by lazy { ChapterListAdapter(requireContext(), this) } |
|
|
|
|
private var durChapterIndex = 0 |
|
|
|
|
private var tocFlowJob: Job? = null |
|
|
|
|
|
|
|
|
|
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) = binding.run { |
|
|
|
|
viewModel.chapterCallBack = this@ChapterListFragment |
|
|
|
|
viewModel.chapterListCallBack = this@ChapterListFragment |
|
|
|
|
val bbg = bottomBackground |
|
|
|
|
val btc = requireContext().getPrimaryTextColor(ColorUtils.isColorLight(bbg)) |
|
|
|
|
llChapterBaseInfo.setBackgroundColor(bbg) |
|
|
|
@ -103,17 +103,17 @@ class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapt |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
override fun upChapterList(searchKey: String?) { |
|
|
|
|
tocFlowJob?.cancel() |
|
|
|
|
tocFlowJob = launch { |
|
|
|
|
when { |
|
|
|
|
searchKey.isNullOrBlank() -> appDb.bookChapterDao.flowByBook(viewModel.bookUrl) |
|
|
|
|
else -> appDb.bookChapterDao.flowSearch(viewModel.bookUrl, searchKey) |
|
|
|
|
}.conflate().collect { |
|
|
|
|
launch { |
|
|
|
|
withContext(IO) { |
|
|
|
|
when { |
|
|
|
|
searchKey.isNullOrBlank() -> appDb.bookChapterDao.getChapterList(viewModel.bookUrl) |
|
|
|
|
else -> appDb.bookChapterDao.search(viewModel.bookUrl, searchKey) |
|
|
|
|
} |
|
|
|
|
}.let { |
|
|
|
|
adapter.setItems(it) |
|
|
|
|
if (searchKey.isNullOrBlank() && mLayoutManager.findFirstVisibleItemPosition() < 0) { |
|
|
|
|
mLayoutManager.scrollToPositionWithOffset(durChapterIndex, 0) |
|
|
|
|
} |
|
|
|
|
delay(100) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|