瀏覽代碼

Reader working. Yay!

inorichi 9 年之前
父節點
當前提交
74da971b2e

+ 27 - 17
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<ReaderActivity> {
 
-    private static final int GET_PAGE_LIST = 1;
     private Source source;
     private Chapter chapter;
     private List<Page> 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<ReaderActivity> {
         }
     }
 
-    private Observable<Page> getPageListObservable() {
+    private Observable<List<Page>> getPageListObservable() {
         return source.pullPageListFromNetwork(chapter.url)
                 .subscribeOn(Schedulers.io())
-                .observeOn(AndroidSchedulers.mainThread())
-                .flatMap(pageList -> {
-                    this.pageList = pageList;
-
-                    return Observable.merge(
-                        Observable.from(pageList)
-                                .filter(page -> page.getImageUrl() != null),
+                .observeOn(AndroidSchedulers.mainThread());
+    }
 
-                        source.getRemainingImageUrlsFromPageList(pageList)
-                                .doOnNext(this::replacePageUrl));
-                });
+    private Observable<Page> 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) {

+ 12 - 1
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<ReaderPresenter> {
     @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<ReaderPresenter> {
         viewPager.setAdapter(adapter);
     }
 
+    public void onPageList(List<Page> pages) {
+        adapter.setPages(pages);
+    }
+
+    public void onPageDownloaded(Page page) {
+        adapter.replacePage(page.getPageNumber(), page);
+    }
 }

+ 16 - 8
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<String> imageUrls;
+    private List<Page> 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<String> getImageUrls() {
-        return imageUrls;
+    public void setPages(List<Page> pages) {
+        this.pages = pages;
+        notifyDataSetChanged();
     }
 
-    public void setImageUrls(List<String> 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);
+        }
     }
 
 }

+ 15 - 7
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();
     }
 }

+ 0 - 0
app/src/main/res/layout/activity_viewer.xml → app/src/main/res/layout/activity_reader.xml