From afebfec7375be282e56139cb709cc70065509d93 Mon Sep 17 00:00:00 2001 From: inorichi Date: Thu, 29 Oct 2015 00:45:51 +0100 Subject: [PATCH] Initial support for read chapters --- .../data/helpers/DatabaseHelper.java | 5 +++ .../mangafeed/data/helpers/NetworkHelper.java | 5 ++- .../data/managers/ChapterManager.java | 2 ++ .../data/managers/ChapterManagerImpl.java | 8 +++++ .../kanade/mangafeed/data/models/Chapter.java | 15 +++----- .../mangafeed/data/tables/ChaptersTable.java | 4 +++ .../mangafeed/presenter/ReaderPresenter.java | 35 +++++++++++-------- .../mangafeed/ui/activity/ReaderActivity.java | 12 ++----- .../mangafeed/ui/adapter/LibraryAdapter.java | 1 + .../ui/fragment/CatalogueFragment.java | 2 +- .../ui/fragment/MangaChaptersFragment.java | 2 +- .../mangafeed/ui/fragment/SourceFragment.java | 2 +- .../{adapter => holder}/CatalogueHolder.java | 2 +- .../ChapterListHolder.java | 24 +++++++++---- .../ui/{adapter => holder}/LibraryHolder.java | 2 +- .../ui/{adapter => holder}/SourceHolder.java | 2 +- .../mangafeed/ui/viewer/VerticalViewer.java | 5 ++- app/src/main/res/values/colors.xml | 1 + app/src/main/res/values/strings.xml | 1 + 19 files changed, 77 insertions(+), 53 deletions(-) rename app/src/main/java/eu/kanade/mangafeed/ui/{adapter => holder}/CatalogueHolder.java (96%) rename app/src/main/java/eu/kanade/mangafeed/ui/{adapter => holder}/ChapterListHolder.java (62%) rename app/src/main/java/eu/kanade/mangafeed/ui/{adapter => holder}/LibraryHolder.java (97%) rename app/src/main/java/eu/kanade/mangafeed/ui/{adapter => holder}/SourceHolder.java (94%) diff --git a/app/src/main/java/eu/kanade/mangafeed/data/helpers/DatabaseHelper.java b/app/src/main/java/eu/kanade/mangafeed/data/helpers/DatabaseHelper.java index c0975d92e0..beb7ad8f50 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/helpers/DatabaseHelper.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/helpers/DatabaseHelper.java @@ -73,6 +73,11 @@ public class DatabaseHelper implements MangaManager, ChapterManager { return mChapterManager.insertChapters(chapters); } + @Override + public PutResult insertChapterBlock(Chapter chapter) { + return mChapterManager.insertChapterBlock(chapter); + } + @Override public Observable insertOrRemoveChapters(Manga manga, List chapters) { return mChapterManager.insertOrRemoveChapters(manga, chapters); diff --git a/app/src/main/java/eu/kanade/mangafeed/data/helpers/NetworkHelper.java b/app/src/main/java/eu/kanade/mangafeed/data/helpers/NetworkHelper.java index f3a5ef4d75..30242c9c14 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/helpers/NetworkHelper.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/helpers/NetworkHelper.java @@ -15,7 +15,6 @@ import java.net.CookieManager; import java.net.CookiePolicy; import java.net.CookieStore; -import eu.kanade.mangafeed.data.models.Page; import okio.Buffer; import okio.BufferedSource; import okio.ForwardingSource; @@ -91,7 +90,7 @@ public final class NetworkHelper { }); } - public Observable getProgressResponse(final String url, final Headers headers, final Page page) { + public Observable getProgressResponse(final String url, final Headers headers, final ProgressListener listener) { return Observable.create(subscriber -> { try { if (!subscriber.isUnsubscribed()) { @@ -106,7 +105,7 @@ public final class NetworkHelper { progressClient.networkInterceptors().add(chain -> { Response originalResponse = chain.proceed(chain.request()); return originalResponse.newBuilder() - .body(new ProgressResponseBody(originalResponse.body(), page)) + .body(new ProgressResponseBody(originalResponse.body(), listener)) .build(); }); subscriber.onNext(progressClient.newCall(request).execute()); diff --git a/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManager.java b/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManager.java index c692a9d92a..28a3881954 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManager.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManager.java @@ -22,6 +22,8 @@ public interface ChapterManager { Observable> insertChapters(List chapters); + PutResult insertChapterBlock(Chapter chapter); + Observable insertOrRemoveChapters(Manga manga, List chapters); Observable deleteChapter(Chapter chapter); diff --git a/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManagerImpl.java b/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManagerImpl.java index ffe87e175e..3d6a7b4878 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManagerImpl.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManagerImpl.java @@ -67,6 +67,14 @@ public class ChapterManagerImpl extends BaseManager implements ChapterManager { .createObservable(); } + @Override + public PutResult insertChapterBlock(Chapter chapter) { + return db.put() + .object(chapter) + .prepare() + .executeAsBlocking(); + } + // Add new chapters or delete if the source deletes them @Override public Observable insertOrRemoveChapters(Manga manga, List chapters) { diff --git a/app/src/main/java/eu/kanade/mangafeed/data/models/Chapter.java b/app/src/main/java/eu/kanade/mangafeed/data/models/Chapter.java index 5b3ad36554..86dd0d101c 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/models/Chapter.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/models/Chapter.java @@ -1,8 +1,5 @@ package eu.kanade.mangafeed.data.models; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; - import com.pushtorefresh.storio.sqlite.annotations.StorIOSQLiteColumn; import com.pushtorefresh.storio.sqlite.annotations.StorIOSQLiteType; @@ -11,31 +8,27 @@ import eu.kanade.mangafeed.data.tables.ChaptersTable; @StorIOSQLiteType(table = ChaptersTable.TABLE) public class Chapter { - @Nullable @StorIOSQLiteColumn(name = ChaptersTable.COLUMN_ID, key = true) public Long id; - @NonNull @StorIOSQLiteColumn(name = ChaptersTable.COLUMN_MANGA_ID) public Long manga_id; - @NonNull @StorIOSQLiteColumn(name = ChaptersTable.COLUMN_URL) public String url; - @NonNull @StorIOSQLiteColumn(name = ChaptersTable.COLUMN_NAME) public String name; - @NonNull @StorIOSQLiteColumn(name = ChaptersTable.COLUMN_READ) - public int read; + public boolean read; + + @StorIOSQLiteColumn(name = ChaptersTable.COLUMN_LAST_PAGE_READ) + public int last_page_read; - @NonNull @StorIOSQLiteColumn(name = ChaptersTable.COLUMN_DATE_FETCH) public long date_fetch; - @NonNull @StorIOSQLiteColumn(name = ChaptersTable.COLUMN_DATE_UPLOAD) public long date_upload; diff --git a/app/src/main/java/eu/kanade/mangafeed/data/tables/ChaptersTable.java b/app/src/main/java/eu/kanade/mangafeed/data/tables/ChaptersTable.java index f8fe05993e..394c27360a 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/tables/ChaptersTable.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/tables/ChaptersTable.java @@ -28,6 +28,9 @@ public class ChaptersTable { @NonNull public static final String COLUMN_DATE_UPLOAD = "date_upload"; + @NonNull + public static final String COLUMN_LAST_PAGE_READ = "last_page_read"; + @NonNull public static String getCreateTableQuery() { return "CREATE TABLE " + TABLE + "(" @@ -36,6 +39,7 @@ public class ChaptersTable { + COLUMN_URL + " TEXT NOT NULL, " + COLUMN_NAME + " TEXT NOT NULL, " + COLUMN_READ + " BOOLEAN NOT NULL, " + + COLUMN_LAST_PAGE_READ + " INT NOT NULL, " + COLUMN_DATE_FETCH + " LONG NOT NULL, " + COLUMN_DATE_UPLOAD + " LONG NOT NULL, " + "FOREIGN KEY(" + COLUMN_MANGA_ID + ") REFERENCES " + MangasTable.TABLE + "(" + MangasTable.COLUMN_ID + ") " diff --git a/app/src/main/java/eu/kanade/mangafeed/presenter/ReaderPresenter.java b/app/src/main/java/eu/kanade/mangafeed/presenter/ReaderPresenter.java index 2e688eb2d8..9efee111b5 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/ReaderPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/ReaderPresenter.java @@ -7,6 +7,7 @@ import java.util.List; import javax.inject.Inject; import de.greenrobot.event.EventBus; +import eu.kanade.mangafeed.data.helpers.DatabaseHelper; import eu.kanade.mangafeed.data.helpers.PreferencesHelper; import eu.kanade.mangafeed.data.models.Chapter; import eu.kanade.mangafeed.data.models.Page; @@ -23,11 +24,13 @@ import timber.log.Timber; public class ReaderPresenter extends BasePresenter { @Inject PreferencesHelper prefs; + @Inject DatabaseHelper db; private Source source; private Chapter chapter; private List pageList; - @State int savedSelectedPage = 0; + private boolean initialStart = true; + @State int currentPage = 0; private static final int GET_PAGE_LIST = 1; private static final int GET_PAGE_IMAGES = 2; @@ -42,9 +45,10 @@ public class ReaderPresenter extends BasePresenter { .doOnCompleted(() -> start(GET_PAGE_IMAGES)), (view, pages) -> { view.onPageListReady(pages); - if (savedSelectedPage != 0) { - view.setSelectedPage(savedSelectedPage); - } + if (initialStart && !chapter.read) + view.setSelectedPage(chapter.last_page_read - 1); + else if (currentPage != 0) + view.setSelectedPage(currentPage); }, (view, error) -> Timber.e("An error occurred while downloading page list") ); @@ -69,14 +73,16 @@ public class ReaderPresenter extends BasePresenter { @Override protected void onDropView() { unregisterForEvents(); + initialStart = false; super.onDropView(); } @Override protected void onDestroy() { - super.onDestroy(); EventBus.getDefault().removeStickyEvent(SourceChapterEvent.class); source.savePageList(chapter.url, pageList); + saveChapter(); + super.onDestroy(); } @EventBusHook @@ -99,23 +105,22 @@ public class ReaderPresenter extends BasePresenter { return Observable.merge( Observable.from(pageList).filter(page -> page.getImageUrl() != null), source.getRemainingImageUrlsFromPageList(pageList) - .doOnNext(this::replacePageUrl) ) .flatMap(source::getCachedImage) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()); } - private void replacePageUrl(Page page) { - for (int i = 0; i < pageList.size(); i++) { - if (pageList.get(i).getPageNumber() == page.getPageNumber()) { - pageList.set(i, page); - return; - } - } + public void setCurrentPage(int currentPage) { + this.currentPage = currentPage; } - public void setCurrentPage(int savedPage) { - this.savedSelectedPage = savedPage; + private void saveChapter() { + chapter.last_page_read = currentPage + 1; + if (currentPage == pageList.size() - 1) { + chapter.read = true; + + } + db.insertChapterBlock(chapter); } } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/activity/ReaderActivity.java b/app/src/main/java/eu/kanade/mangafeed/ui/activity/ReaderActivity.java index 5c8ada4582..856b9a10dc 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/activity/ReaderActivity.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/activity/ReaderActivity.java @@ -36,7 +36,6 @@ public class ReaderActivity extends BaseRxActivity { @Inject PreferencesHelper prefs; - private int currentPage; private BaseViewer viewer; private static final int LEFT_TO_RIGHT = 1; @@ -60,19 +59,14 @@ public class ReaderActivity extends BaseRxActivity { enableHardwareAcceleration(); } - - @Override - public void onDestroy() { - getPresenter().setCurrentPage(currentPage); - super.onDestroy(); - } - + public void onPageListReady(List pages) { viewer.onPageListReady(pages); } public void onPageChanged(int currentPageIndex, int totalPages) { - currentPage = currentPageIndex; + if (currentPageIndex != 0) + getPresenter().setCurrentPage(currentPageIndex); String page = (currentPageIndex + 1) + "/" + totalPages; pageNumber.setText(page); } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/adapter/LibraryAdapter.java b/app/src/main/java/eu/kanade/mangafeed/ui/adapter/LibraryAdapter.java index dc42d05581..77bd90a9f8 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/adapter/LibraryAdapter.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/adapter/LibraryAdapter.java @@ -7,6 +7,7 @@ import android.widget.Filterable; import java.util.List; import eu.kanade.mangafeed.data.models.Manga; +import eu.kanade.mangafeed.ui.holder.LibraryHolder; import rx.Observable; import uk.co.ribot.easyadapter.EasyAdapter; diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/CatalogueFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/CatalogueFragment.java index fbfbc37308..93c0c27b6a 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/CatalogueFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/CatalogueFragment.java @@ -24,7 +24,7 @@ import eu.kanade.mangafeed.R; import eu.kanade.mangafeed.data.models.Manga; import eu.kanade.mangafeed.presenter.CataloguePresenter; import eu.kanade.mangafeed.ui.activity.MangaDetailActivity; -import eu.kanade.mangafeed.ui.adapter.CatalogueHolder; +import eu.kanade.mangafeed.ui.holder.CatalogueHolder; import eu.kanade.mangafeed.ui.fragment.base.BaseRxFragment; import eu.kanade.mangafeed.util.PageBundle; import eu.kanade.mangafeed.widget.EndlessScrollListener; diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/MangaChaptersFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/MangaChaptersFragment.java index c18f7fcb57..b58153cde8 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/MangaChaptersFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/MangaChaptersFragment.java @@ -22,7 +22,7 @@ import eu.kanade.mangafeed.data.models.Chapter; import eu.kanade.mangafeed.presenter.MangaChaptersPresenter; import eu.kanade.mangafeed.ui.activity.MangaDetailActivity; import eu.kanade.mangafeed.ui.activity.ReaderActivity; -import eu.kanade.mangafeed.ui.adapter.ChapterListHolder; +import eu.kanade.mangafeed.ui.holder.ChapterListHolder; import eu.kanade.mangafeed.ui.fragment.base.BaseRxFragment; import nucleus.factory.RequiresPresenter; import uk.co.ribot.easyadapter.EasyRecyclerAdapter; diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/SourceFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/SourceFragment.java index 3d661fe100..d35e5e6cd5 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/SourceFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/SourceFragment.java @@ -15,7 +15,7 @@ import eu.kanade.mangafeed.R; import eu.kanade.mangafeed.presenter.SourcePresenter; import eu.kanade.mangafeed.sources.base.Source; import eu.kanade.mangafeed.ui.activity.MainActivity; -import eu.kanade.mangafeed.ui.adapter.SourceHolder; +import eu.kanade.mangafeed.ui.holder.SourceHolder; import eu.kanade.mangafeed.ui.fragment.base.BaseRxFragment; import nucleus.factory.RequiresPresenter; import uk.co.ribot.easyadapter.EasyAdapter; diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/adapter/CatalogueHolder.java b/app/src/main/java/eu/kanade/mangafeed/ui/holder/CatalogueHolder.java similarity index 96% rename from app/src/main/java/eu/kanade/mangafeed/ui/adapter/CatalogueHolder.java rename to app/src/main/java/eu/kanade/mangafeed/ui/holder/CatalogueHolder.java index a95a201167..c1850a4912 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/adapter/CatalogueHolder.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/holder/CatalogueHolder.java @@ -1,4 +1,4 @@ -package eu.kanade.mangafeed.ui.adapter; +package eu.kanade.mangafeed.ui.holder; import android.view.View; import android.widget.ImageView; diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/adapter/ChapterListHolder.java b/app/src/main/java/eu/kanade/mangafeed/ui/holder/ChapterListHolder.java similarity index 62% rename from app/src/main/java/eu/kanade/mangafeed/ui/adapter/ChapterListHolder.java rename to app/src/main/java/eu/kanade/mangafeed/ui/holder/ChapterListHolder.java index ee18a9c39d..ffa40386c7 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/adapter/ChapterListHolder.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/holder/ChapterListHolder.java @@ -1,5 +1,7 @@ -package eu.kanade.mangafeed.ui.adapter; +package eu.kanade.mangafeed.ui.holder; +import android.graphics.Color; +import android.support.v4.content.ContextCompat; import android.view.View; import android.widget.ImageView; import android.widget.TextView; @@ -14,11 +16,9 @@ import uk.co.ribot.easyadapter.annotations.ViewId; @LayoutId(R.layout.item_chapter) public class ChapterListHolder extends ItemViewHolder { - @ViewId(R.id.chapter_title) - TextView title; - - @ViewId(R.id.chapter_download_image) - ImageView download_icon; + @ViewId(R.id.chapter_title) TextView title; + @ViewId(R.id.chapter_download_image) ImageView download_icon; + @ViewId(R.id.chapter_pages) TextView pages; View view; @@ -30,6 +30,18 @@ public class ChapterListHolder extends ItemViewHolder { public void onSetValues(Chapter chapter, PositionInfo positionInfo) { title.setText(chapter.name); download_icon.setImageResource(R.drawable.ic_file_download_black_48dp); + + if (chapter.read) { + title.setTextColor(ContextCompat.getColor(getContext(), R.color.chapter_read_text)); + } else { + title.setTextColor(Color.BLACK); + } + + if (chapter.last_page_read > 0 && !chapter.read) { + pages.setText(getContext().getString(R.string.chapter_progress, chapter.last_page_read)); + } else { + pages.setText(""); + } } @Override diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/adapter/LibraryHolder.java b/app/src/main/java/eu/kanade/mangafeed/ui/holder/LibraryHolder.java similarity index 97% rename from app/src/main/java/eu/kanade/mangafeed/ui/adapter/LibraryHolder.java rename to app/src/main/java/eu/kanade/mangafeed/ui/holder/LibraryHolder.java index 42c540db53..6cf11ad9e9 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/adapter/LibraryHolder.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/holder/LibraryHolder.java @@ -1,4 +1,4 @@ -package eu.kanade.mangafeed.ui.adapter; +package eu.kanade.mangafeed.ui.holder; import android.view.View; import android.widget.ImageView; diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/adapter/SourceHolder.java b/app/src/main/java/eu/kanade/mangafeed/ui/holder/SourceHolder.java similarity index 94% rename from app/src/main/java/eu/kanade/mangafeed/ui/adapter/SourceHolder.java rename to app/src/main/java/eu/kanade/mangafeed/ui/holder/SourceHolder.java index 4bdf927fe8..de0a65641a 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/adapter/SourceHolder.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/holder/SourceHolder.java @@ -1,4 +1,4 @@ -package eu.kanade.mangafeed.ui.adapter; +package eu.kanade.mangafeed.ui.holder; import android.view.View; import android.widget.TextView; diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/viewer/VerticalViewer.java b/app/src/main/java/eu/kanade/mangafeed/ui/viewer/VerticalViewer.java index 02e65ddd99..28842cc65a 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/viewer/VerticalViewer.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/viewer/VerticalViewer.java @@ -54,8 +54,7 @@ public class VerticalViewer extends BaseViewer { @Override public void setSelectedPage(int pageNumber) { - // TODO - return; + viewPager.setCurrentItem(getCurrentPageIndex(pageNumber)); } @Override @@ -66,6 +65,6 @@ public class VerticalViewer extends BaseViewer { @Override public boolean onImageTouch(MotionEvent motionEvent) { - return false; + return true; } } diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 503bec0f0b..7aa1362cc8 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -15,4 +15,5 @@ #E9E9E9 #DD000000 #E8E8E8 + #909090 \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index dd7f19e38e..9123594de4 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -75,5 +75,6 @@ Favorite Downloading… Downloaded %1$d%% + Page: %1$d