From 9407d9b4f56037cc10d7b240978ea01bf06c5c62 Mon Sep 17 00:00:00 2001 From: inorichi Date: Sun, 1 Nov 2015 21:28:03 +0100 Subject: [PATCH] Minor changes --- .../eu/kanade/mangafeed/data/models/Page.java | 18 +++++------ .../mangafeed/presenter/ReaderPresenter.java | 14 ++++----- .../ui/fragment/ReaderPageFragment.java | 30 +++++++++++-------- 3 files changed, 30 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/eu/kanade/mangafeed/data/models/Page.java b/app/src/main/java/eu/kanade/mangafeed/data/models/Page.java index d2c9e6e08..8006b4094 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/models/Page.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/models/Page.java @@ -61,23 +61,13 @@ public class Page implements NetworkHelper.ProgressListener { public void setStatus(int status) { this.status = status; - if (statusSubject != null) - statusSubject.onNext(status); + notifyStatus(); } public int getProgress() { return progress; } - @Override - public String toString() { - return "Page{" + - "pageNumber=" + pageNumber + - ", url='" + url + '\'' + - ", imageUrl='" + imageUrl + '\'' + - '}'; - } - @Override public void update(long bytesRead, long contentLength, boolean done) { progress = (int) ((100 * bytesRead) / contentLength); @@ -85,6 +75,12 @@ public class Page implements NetworkHelper.ProgressListener { public void setStatusSubject(BehaviorSubject subject) { this.statusSubject = subject; + notifyStatus(); + } + + private void notifyStatus() { + if (statusSubject != null) + statusSubject.onNext(status); } } 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 f13467d9e..e711f377a 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/ReaderPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/ReaderPresenter.java @@ -71,7 +71,6 @@ public class ReaderPresenter extends BasePresenter { @Override protected void onDestroy() { - EventBus.getDefault().removeStickyEvent(SourceChapterEvent.class); source.savePageList(chapter.url, pageList); saveChapter(); super.onDestroy(); @@ -79,14 +78,13 @@ public class ReaderPresenter extends BasePresenter { @EventBusHook public void onEventMainThread(SourceChapterEvent event) { - if (source == null || chapter == null) { - source = event.getSource(); - chapter = event.getChapter(); - if (chapter.last_page_read != 0) - currentPage = chapter.last_page_read; + source = event.getSource(); + chapter = event.getChapter(); + if (chapter.last_page_read != 0 && !chapter.read) + currentPage = chapter.last_page_read; - start(1); - } + start(1); + EventBus.getDefault().removeStickyEvent(SourceChapterEvent.class); } private Observable> getPageListObservable() { diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/ReaderPageFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/ReaderPageFragment.java index baae29f88..9360a4ba1 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/ReaderPageFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/ReaderPageFragment.java @@ -14,6 +14,7 @@ import com.davemorrissey.labs.subscaleview.ImageSource; import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; import butterknife.Bind; import butterknife.ButterKnife; @@ -124,29 +125,32 @@ public class ReaderPageFragment extends Fragment { } private void observeStatus() { - if (page == null) + if (page == null || statusSubscription != null) return; - if (page.getStatus() == Page.READY) { - showImage(); - } else { - BehaviorSubject statusSubject = BehaviorSubject.create(); - page.setStatusSubject(statusSubject); + BehaviorSubject statusSubject = BehaviorSubject.create(); + page.setStatusSubject(statusSubject); - statusSubscription = statusSubject - .observeOn(AndroidSchedulers.mainThread()) - .subscribe(this::processStatus); - } + statusSubscription = statusSubject + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(this::processStatus); } private void observeProgress() { + if (progressSubscription != null) + return; + + final AtomicInteger currentValue = new AtomicInteger(-1); + progressSubscription = Observable.interval(75, TimeUnit.MILLISECONDS) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(tick -> { - if (page.getProgress() != 0) - progressText.setText( - getString(R.string.download_progress, page.getProgress())); + // Refresh UI only if progress change + if (page.getProgress() != currentValue.get()) { + currentValue.set(page.getProgress()); + progressText.setText(getString(R.string.download_progress, page.getProgress())); + } }); }