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