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 b2af627051..aba72bcec3 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/ReaderPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/ReaderPresenter.java @@ -4,10 +4,7 @@ import android.os.Bundle; import java.util.List; -import javax.inject.Inject; - import de.greenrobot.event.EventBus; -import eu.kanade.mangafeed.data.caches.CacheManager; import eu.kanade.mangafeed.data.models.Chapter; import eu.kanade.mangafeed.data.models.Page; import eu.kanade.mangafeed.sources.Source; @@ -20,20 +17,34 @@ import rx.schedulers.Schedulers; public class ReaderPresenter extends BasePresenter { - private static final int GET_PAGE_LIST = 1; private Source source; private Chapter chapter; private List pageList; + private boolean pageListStarted; - @Inject CacheManager cacheManager; + private static final int GET_PAGE_LIST = 1; + private static final int GET_PAGE_IMAGES = 2; @Override protected void onCreate(Bundle savedState) { super.onCreate(savedState); - restartableReplay(GET_PAGE_LIST, + restartableLatestCache(GET_PAGE_LIST, this::getPageListObservable, + (view, pages) -> { + pageList = pages; + view.onPageList(pages); + if (!pageListStarted) { + pageListStarted = true; + start(GET_PAGE_IMAGES); + } + + }); + + restartableReplay(GET_PAGE_IMAGES, + this::getPageImagesObservable, (view, page) -> { + view.onPageDownloaded(page); }); } @@ -66,20 +77,19 @@ public class ReaderPresenter extends BasePresenter { } } - private Observable getPageListObservable() { + private Observable> getPageListObservable() { return source.pullPageListFromNetwork(chapter.url) .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .flatMap(pageList -> { - this.pageList = pageList; + .observeOn(AndroidSchedulers.mainThread()); + } - return Observable.merge( - Observable.from(pageList) - .filter(page -> page.getImageUrl() != null), - - source.getRemainingImageUrlsFromPageList(pageList) - .doOnNext(this::replacePageUrl)); - }); + private Observable getPageImagesObservable() { + return Observable.merge( + Observable.from(pageList).filter(page -> page.getImageUrl() != null), + source.getRemainingImageUrlsFromPageList(pageList) + .doOnNext(this::replacePageUrl)) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()); } private void replacePageUrl(Page page) { 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 67556b9ef4..964b5d442d 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 @@ -5,9 +5,12 @@ import android.content.Intent; import android.os.Bundle; import android.support.v4.view.ViewPager; +import java.util.List; + import butterknife.Bind; import butterknife.ButterKnife; import eu.kanade.mangafeed.R; +import eu.kanade.mangafeed.data.models.Page; import eu.kanade.mangafeed.presenter.ReaderPresenter; import eu.kanade.mangafeed.ui.adapter.ReaderPageAdapter; import nucleus.factory.RequiresPresenter; @@ -26,10 +29,11 @@ public class ReaderActivity extends BaseActivity { @Override public void onCreate(Bundle savedState) { super.onCreate(savedState); - setContentView(R.layout.activity_viewer); + setContentView(R.layout.activity_reader); ButterKnife.bind(this); createAdapter(); + viewPager.setOffscreenPageLimit(3); } private void createAdapter() { @@ -37,4 +41,11 @@ public class ReaderActivity extends BaseActivity { viewPager.setAdapter(adapter); } + public void onPageList(List pages) { + adapter.setPages(pages); + } + + public void onPageDownloaded(Page page) { + adapter.replacePage(page.getPageNumber(), page); + } } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/adapter/ReaderPageAdapter.java b/app/src/main/java/eu/kanade/mangafeed/ui/adapter/ReaderPageAdapter.java index a3c7ca5601..8df53fec16 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/adapter/ReaderPageAdapter.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/adapter/ReaderPageAdapter.java @@ -5,11 +5,12 @@ import android.support.v4.app.FragmentManager; import java.util.List; +import eu.kanade.mangafeed.data.models.Page; import eu.kanade.mangafeed.ui.fragment.ReaderPageFragment; public class ReaderPageAdapter extends SmartFragmentStatePagerAdapter { - private List imageUrls; + private List pages; public ReaderPageAdapter(FragmentManager fragmentManager) { super(fragmentManager); @@ -17,23 +18,30 @@ public class ReaderPageAdapter extends SmartFragmentStatePagerAdapter { @Override public int getCount() { - if (imageUrls != null) - return imageUrls.size(); + if (pages != null) + return pages.size(); return 0; } @Override public Fragment getItem(int position) { - return ReaderPageFragment.newInstance(imageUrls.get(position), position); + return ReaderPageFragment.newInstance(pages.get(position)); } - public List getImageUrls() { - return imageUrls; + public void setPages(List pages) { + this.pages = pages; + notifyDataSetChanged(); } - public void setImageUrls(List imageUrls) { - this.imageUrls = imageUrls; + public void replacePage(int position, Page page) { + pages.set(position, page); + notifyDataSetChanged(); + + ReaderPageFragment fragment = (ReaderPageFragment)getRegisteredFragment(position); + if (fragment != null) { + fragment.setPage(page); + } } } 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 467bc7feea..d487a7d124 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 @@ -11,6 +11,7 @@ import com.bumptech.glide.Glide; import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView; import eu.kanade.mangafeed.R; +import eu.kanade.mangafeed.data.models.Page; import eu.kanade.mangafeed.util.PageFileTarget; public class ReaderPageFragment extends Fragment { @@ -20,10 +21,10 @@ public class ReaderPageFragment extends Fragment { private String mUrl; - public static ReaderPageFragment newInstance(String url, int position) { + public static ReaderPageFragment newInstance(Page page) { ReaderPageFragment newInstance = new ReaderPageFragment(); Bundle arguments = new Bundle(); - arguments.putString(URL_ARGUMENT_KEY, url); + arguments.putString(URL_ARGUMENT_KEY, page.getImageUrl()); newInstance.setArguments(arguments); return newInstance; } @@ -40,10 +41,20 @@ public class ReaderPageFragment extends Fragment { } } + public void setPage(Page page) { + mUrl = page.getImageUrl(); + loadImage(); + } + + private void loadImage() { + Glide.with(getActivity()) + .load(mUrl) + .downloadOnly(new PageFileTarget(mPageImageView)); + } + @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { mPageImageView = (SubsamplingScaleImageView)inflater.inflate(R.layout.fragment_page, container, false); - mPageImageView.setVisibility(View.INVISIBLE); mPageImageView.setDoubleTapZoomStyle(SubsamplingScaleImageView.ZOOM_FOCUS_FIXED); mPageImageView.setPanLimit(SubsamplingScaleImageView.PAN_LIMIT_INSIDE); mPageImageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_INSIDE); @@ -76,9 +87,6 @@ public class ReaderPageFragment extends Fragment { @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); - - Glide.with(getActivity()) - .load(mUrl) - .downloadOnly(new PageFileTarget(mPageImageView)); + loadImage(); } } diff --git a/app/src/main/res/layout/activity_viewer.xml b/app/src/main/res/layout/activity_reader.xml similarity index 100% rename from app/src/main/res/layout/activity_viewer.xml rename to app/src/main/res/layout/activity_reader.xml