From fa71e906c98ea207c3db9c04c18fb5d3e2562324 Mon Sep 17 00:00:00 2001 From: inorichi Date: Thu, 21 Jan 2016 02:26:34 +0100 Subject: [PATCH] Change recent chapters query, now it shows last month updates. Download manager now uses a thread pool. --- .../data/database/DatabaseHelper.java | 4 ++-- .../resolvers/MangaChapterGetResolver.java | 12 +++++++---- .../data/download/DownloadManager.java | 20 +++++++++++++------ .../ui/recent/RecentChaptersPresenter.java | 6 +++++- 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/DatabaseHelper.java b/app/src/main/java/eu/kanade/tachiyomi/data/database/DatabaseHelper.java index bda2b1fb7d..2ae8791a97 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/DatabaseHelper.java +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/DatabaseHelper.java @@ -162,11 +162,11 @@ public class DatabaseHelper { .prepare(); } - public PreparedGetListOfObjects getRecentChapters() { + public PreparedGetListOfObjects getRecentChapters(Date date) { return db.get() .listOfObjects(MangaChapter.class) .withQuery(RawQuery.builder() - .query(MangaChapterGetResolver.RECENT_CHAPTERS_QUERY) + .query(MangaChapterGetResolver.getRecentChaptersQuery(date)) .observesTables(ChapterTable.TABLE) .build()) .withGetResolver(MangaChapterGetResolver.INSTANCE) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/database/resolvers/MangaChapterGetResolver.java b/app/src/main/java/eu/kanade/tachiyomi/data/database/resolvers/MangaChapterGetResolver.java index 6affb16a08..67eb65e83f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/database/resolvers/MangaChapterGetResolver.java +++ b/app/src/main/java/eu/kanade/tachiyomi/data/database/resolvers/MangaChapterGetResolver.java @@ -5,6 +5,8 @@ import android.support.annotation.NonNull; import com.pushtorefresh.storio.sqlite.operations.get.DefaultGetResolver; +import java.util.Date; + import eu.kanade.tachiyomi.data.database.models.Chapter; import eu.kanade.tachiyomi.data.database.models.ChapterStorIOSQLiteGetResolver; import eu.kanade.tachiyomi.data.database.models.Manga; @@ -24,10 +26,12 @@ public class MangaChapterGetResolver extends DefaultGetResolver { MangaTable.COLUMN_ID, ChapterTable.COLUMN_MANGA_ID); - public static final String RECENT_CHAPTERS_QUERY = String.format( - QUERY + " WHERE %1$s = 1 ORDER BY %2$s DESC LIMIT 100", - MangaTable.COLUMN_FAVORITE, - ChapterTable.COLUMN_DATE_UPLOAD); + public static String getRecentChaptersQuery(Date date) { + return QUERY + String.format(" WHERE %1$s = 1 AND %2$s > %3$d ORDER BY %2$s DESC", + MangaTable.COLUMN_FAVORITE, + ChapterTable.COLUMN_DATE_UPLOAD, + date.getTime()); + } @NonNull private final MangaStorIOSQLiteGetResolver mangaGetResolver; diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.java b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.java index 16fe3f9638..e4f71632bf 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.java +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadManager.java @@ -15,6 +15,8 @@ import java.io.IOException; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import eu.kanade.tachiyomi.data.database.models.Chapter; import eu.kanade.tachiyomi.data.database.models.Manga; @@ -48,6 +50,8 @@ public class DownloadManager { private DownloadQueue queue; private volatile boolean isRunning; + private ExecutorService threadPool; + public static final String PAGE_LIST_FILE = "index.json"; public DownloadManager(Context context, SourceManager sourceManager, PreferencesHelper preferences) { @@ -66,10 +70,11 @@ public class DownloadManager { if (downloadsSubscription != null && !downloadsSubscription.isUnsubscribed()) downloadsSubscription.unsubscribe(); + threadPool = Executors.newFixedThreadPool(preferences.downloadThreads()); + downloadsSubscription = downloadsQueueSubject - .concatMap(downloads -> Observable.from(downloads) - .flatMap(this::downloadChapter, preferences.downloadThreads())) - .onBackpressureBuffer() + .flatMap(Observable::from) + .flatMap(c -> downloadChapter(c).subscribeOn(Schedulers.from(threadPool))) .observeOn(AndroidSchedulers.mainThread()) .map(download -> areAllDownloadsFinished()) .subscribe(finished -> { @@ -94,6 +99,10 @@ public class DownloadManager { downloadsSubscription.unsubscribe(); downloadsSubscription = null; } + + if (threadPool != null && !threadPool.isShutdown()) { + threadPool.shutdown(); + } } // Create a download object for every chapter in the event and add them to the downloads queue @@ -181,8 +190,7 @@ public class DownloadManager { // Or if the page list already exists, start from the file Observable.just(download.pages); - return pageListObservable - .subscribeOn(Schedulers.io()) + return Observable.defer(() -> pageListObservable .doOnNext(pages -> { download.downloadedImages = 0; download.setStatus(Download.DOWNLOADING); @@ -199,7 +207,7 @@ public class DownloadManager { .onErrorResumeNext(error -> { download.setStatus(Download.ERROR); return Observable.just(download); - }); + })); } // Get the image from the filesystem if it exists or download from network diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/RecentChaptersPresenter.java b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/RecentChaptersPresenter.java index 0cae1d73ea..5603423512 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/RecentChaptersPresenter.java +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/RecentChaptersPresenter.java @@ -42,7 +42,11 @@ public class RecentChaptersPresenter extends BasePresenter> getRecentChaptersObservable() { - return db.getRecentChapters().asRxObservable() + Calendar cal = Calendar.getInstance(); + cal.setTime(new Date()); + cal.add(Calendar.MONTH, -1); + + return db.getRecentChapters(cal.getTime()).asRxObservable() // group chapters by the date they were fetched on a ordered map .flatMap(recents -> Observable.from(recents) .toMultimap(