From 089b5d33260e9f9acd75a123d54a9a135bfd747d Mon Sep 17 00:00:00 2001 From: inorichi Date: Wed, 11 Nov 2015 04:56:16 +0100 Subject: [PATCH] More improvements to webtoon viewer --- .../data/helpers/DatabaseHelper.java | 5 ++ .../mangafeed/ui/activity/ReaderActivity.java | 1 + .../mangafeed/ui/adapter/WebtoonAdapter.java | 11 +++- .../mangafeed/ui/viewer/WebtoonViewer.java | 63 ++++++++++++++++++- .../mangafeed/ui/viewer/base/BaseViewer.java | 2 + app/src/main/res/layout/chapter_image.xml | 13 +--- .../main/res/layout/item_webtoon_viewer.xml | 16 +++++ 7 files changed, 97 insertions(+), 14 deletions(-) create mode 100644 app/src/main/res/layout/item_webtoon_viewer.xml 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 fdcb1998a3..d4e8400000 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 @@ -26,6 +26,7 @@ import eu.kanade.mangafeed.data.models.MangaStorIOSQLitePutResolver; import eu.kanade.mangafeed.data.resolvers.MangaWithUnreadGetResolver; import eu.kanade.mangafeed.data.tables.ChaptersTable; import eu.kanade.mangafeed.data.tables.MangasTable; +import eu.kanade.mangafeed.util.ChapterRecognition; import eu.kanade.mangafeed.util.PostResult; import rx.Observable; @@ -192,6 +193,10 @@ public class DatabaseHelper { Observable newChaptersObs = chapterList .flatMap(dbChapters -> Observable.from(chapters) .filter(c -> !dbChapters.contains(c)) + .map(c -> { + ChapterRecognition.parseChapterNumber(c, manga); + return c; + }) .toList() .flatMap(newChapters -> insertChapters(newChapters).createObservable()) .map(PutResults::numberOfInserts)); 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 1fe1f950c7..0b624a2621 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 @@ -78,6 +78,7 @@ public class ReaderActivity extends BaseRxActivity { @Override protected void onPause() { getPresenter().setCurrentPage(viewer.getCurrentPosition()); + viewer.destroySubscriptions(); super.onPause(); } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/adapter/WebtoonAdapter.java b/app/src/main/java/eu/kanade/mangafeed/ui/adapter/WebtoonAdapter.java index 8fbb481ea8..7e0b403248 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/adapter/WebtoonAdapter.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/adapter/WebtoonAdapter.java @@ -2,6 +2,7 @@ package eu.kanade.mangafeed.ui.adapter; import android.content.Context; import android.view.View; +import android.widget.ProgressBar; import com.davemorrissey.labs.subscaleview.ImageSource; import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView; @@ -41,10 +42,16 @@ public class WebtoonAdapter extends BaseEasyRecyclerAdapter { notifyDataSetChanged(); } - @LayoutId(R.layout.chapter_image) + public void addPage(Page page) { + pages.add(page); + notifyItemChanged(page.getPageNumber()); + } + + @LayoutId(R.layout.item_webtoon_viewer) static class ImageViewHolder extends ItemViewHolder { @ViewId(R.id.page_image_view) SubsamplingScaleImageView imageView; + @ViewId(R.id.progress) ProgressBar progressBar; public ImageViewHolder(View view) { super(view); @@ -60,8 +67,10 @@ public class WebtoonAdapter extends BaseEasyRecyclerAdapter { if (page.getImagePath() != null) { imageView.setVisibility(View.VISIBLE); imageView.setImage(ImageSource.uri(page.getImagePath()).tilingDisabled()); + progressBar.setVisibility(View.GONE); } else { imageView.setVisibility(View.GONE); + progressBar.setVisibility(View.VISIBLE); } } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/viewer/WebtoonViewer.java b/app/src/main/java/eu/kanade/mangafeed/ui/viewer/WebtoonViewer.java index 293a695285..f146868bc0 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/viewer/WebtoonViewer.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/viewer/WebtoonViewer.java @@ -11,27 +11,43 @@ import eu.kanade.mangafeed.data.models.Page; import eu.kanade.mangafeed.ui.activity.ReaderActivity; import eu.kanade.mangafeed.ui.adapter.WebtoonAdapter; import eu.kanade.mangafeed.ui.viewer.base.BaseViewer; +import rx.Subscription; +import rx.android.schedulers.AndroidSchedulers; +import rx.subjects.PublishSubject; public class WebtoonViewer extends BaseViewer { private RecyclerView recycler; + private LinearLayoutManager layoutManager; private WebtoonAdapter adapter; + private List pages; + private Subscription subscription; public WebtoonViewer(ReaderActivity activity, FrameLayout container) { super(activity, container); recycler = new RecyclerView(activity); - LinearLayoutManager layoutManager = new LinearLayoutManager(activity); + layoutManager = new LinearLayoutManager(activity); recycler.setLayoutManager(layoutManager); adapter = new WebtoonAdapter(activity); recycler.setAdapter(adapter); + recycler.addOnScrollListener(new RecyclerView.OnScrollListener() { + @Override + public void onScrolled(RecyclerView recyclerView, int dx, int dy) { + super.onScrolled(recyclerView, dx, dy); + + currentPosition = layoutManager.findFirstVisibleItemPosition(); + updatePageNumber(); + } + }); + container.addView(recycler); } @Override public int getTotalPages() { - return adapter.getItemCount(); + return pages.size(); } @Override @@ -42,11 +58,52 @@ public class WebtoonViewer extends BaseViewer { @Override public void onPageListReady(List pages) { - adapter.setPages(pages); + this.pages = pages; + observeStatus(0); } @Override public boolean onImageTouch(MotionEvent motionEvent) { return true; } + + private void observeStatus(int position) { + if (position == pages.size()) + return; + + final Page page = pages.get(position); + adapter.addPage(page); + + PublishSubject statusSubject = PublishSubject.create(); + page.setStatusSubject(statusSubject); + + if (subscription != null && !subscription.isUnsubscribed()) + subscription.unsubscribe(); + + subscription = statusSubject + .startWith(page.getStatus()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(status -> processStatus(position, status)); + } + + private void processStatus(int position, int status) { + switch (status) { + case Page.LOAD_PAGE: + break; + case Page.DOWNLOAD_IMAGE: + break; + case Page.READY: + adapter.notifyItemChanged(position); + observeStatus(position + 1); + break; + case Page.ERROR: + break; + } + } + + @Override + public void destroySubscriptions() { + if (subscription != null && !subscription.isUnsubscribed()) + subscription.unsubscribe(); + } } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/viewer/base/BaseViewer.java b/app/src/main/java/eu/kanade/mangafeed/ui/viewer/base/BaseViewer.java index f1424b9f13..5a80dbab0f 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/viewer/base/BaseViewer.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/viewer/base/BaseViewer.java @@ -33,6 +33,8 @@ public abstract class BaseViewer { return getCurrentPageIndex(currentPosition); } + public void destroySubscriptions() {} + public abstract int getTotalPages(); public abstract void setSelectedPage(int pageNumber); public abstract void onPageListReady(List pages); diff --git a/app/src/main/res/layout/chapter_image.xml b/app/src/main/res/layout/chapter_image.xml index 4d52170198..7ae798b1a4 100644 --- a/app/src/main/res/layout/chapter_image.xml +++ b/app/src/main/res/layout/chapter_image.xml @@ -1,13 +1,6 @@ - - - - - \ No newline at end of file + android:layout_height="match_parent" + android:id="@+id/page_image_view" /> diff --git a/app/src/main/res/layout/item_webtoon_viewer.xml b/app/src/main/res/layout/item_webtoon_viewer.xml new file mode 100644 index 0000000000..91677a1f34 --- /dev/null +++ b/app/src/main/res/layout/item_webtoon_viewer.xml @@ -0,0 +1,16 @@ + + + + + + + + \ No newline at end of file