download new chapters changes (#3193)

* download new chapters changes

* move initialFetchChapters logic into onNextChapters

* refractor download new chapter logic to be more explicit
This commit is contained in:
MCAxiaz
2020-05-17 14:33:26 -07:00
committed by GitHub
parent 102a372df9
commit ed029c52ae
4 changed files with 38 additions and 31 deletions

View File

@@ -99,7 +99,7 @@ class ChaptersController :
adapter?.fastScroller = binding.fastScroller
binding.swipeRefresh.refreshes()
.onEach { fetchChaptersFromSource() }
.onEach { fetchChaptersFromSource(manualFetch = true) }
.launchIn(scope)
binding.fab.clicks()
@@ -263,10 +263,10 @@ class ChaptersController :
}
fun onNextChapters(chapters: List<ChapterItem>) {
// If the list is empty, fetch chapters from source if the conditions are met
// If the list is empty and it hasn't requested previously, fetch chapters from source
// We use presenter chapters instead because they are always unfiltered
if (presenter.chapters.isEmpty()) {
initialFetchChapters()
if (!presenter.hasRequested && presenter.chapters.isEmpty()) {
fetchChaptersFromSource()
}
val adapter = adapter ?: return
@@ -285,16 +285,9 @@ class ChaptersController :
}
}
private fun initialFetchChapters() {
// Only fetch if this view is from the catalog and it hasn't requested previously
if ((parentController as MangaController).fromSource && !presenter.hasRequested) {
fetchChaptersFromSource()
}
}
private fun fetchChaptersFromSource() {
private fun fetchChaptersFromSource(manualFetch: Boolean = false) {
binding.swipeRefresh.isRefreshing = true
presenter.fetchChaptersFromSource()
presenter.fetchChaptersFromSource(manualFetch)
}
fun onFetchChaptersDone() {

View File

@@ -14,6 +14,7 @@ import eu.kanade.tachiyomi.ui.base.presenter.BasePresenter
import eu.kanade.tachiyomi.util.chapter.syncChaptersWithSource
import eu.kanade.tachiyomi.util.isLocal
import eu.kanade.tachiyomi.util.lang.isNullOrUnsubscribed
import eu.kanade.tachiyomi.util.shouldDownloadNewChapters
import java.util.Date
import rx.Observable
import rx.Subscription
@@ -153,13 +154,18 @@ class ChaptersPresenter(
/**
* Requests an updated list of chapters from the source.
*/
fun fetchChaptersFromSource() {
fun fetchChaptersFromSource(manualFetch: Boolean = false) {
hasRequested = true
if (!fetchChaptersSubscription.isNullOrUnsubscribed()) return
fetchChaptersSubscription = Observable.defer { source.fetchChapterList(manga) }
.subscribeOn(Schedulers.io())
.map { syncChaptersWithSource(db, it, manga, source) }
.doOnNext {
if (manualFetch) {
downloadNewChapters(it.first)
}
}
.observeOn(AndroidSchedulers.mainThread())
.subscribeFirst(
{ view, _ ->
@@ -262,7 +268,7 @@ class ChaptersPresenter(
* Downloads the given list of chapters with the manager.
* @param chapters the list of chapters to download.
*/
fun downloadChapters(chapters: List<ChapterItem>) {
fun downloadChapters(chapters: List<Chapter>) {
downloadManager.downloadChapters(manga, chapters)
}
@@ -299,6 +305,12 @@ class ChaptersPresenter(
)
}
private fun downloadNewChapters(chapters: List<Chapter>) {
if (chapters.isEmpty() || !manga.shouldDownloadNewChapters(db, preferences)) return
downloadChapters(chapters)
}
/**
* Deletes a list of chapters from disk. This method is called in a background thread.
* @param chapters the chapters to delete.