Просмотр исходного кода

Show page number, improve the load of the first image of a chapter

inorichi 9 лет назад
Родитель
Сommit
4c786077a8

+ 15 - 11
app/src/main/java/eu/kanade/mangafeed/presenter/ReaderPresenter.java

@@ -11,6 +11,7 @@ import eu.kanade.mangafeed.sources.Source;
 import eu.kanade.mangafeed.ui.activity.ReaderActivity;
 import eu.kanade.mangafeed.util.EventBusHook;
 import eu.kanade.mangafeed.util.events.SourceChapterEvent;
+import icepick.State;
 import rx.Observable;
 import rx.android.schedulers.AndroidSchedulers;
 import rx.schedulers.Schedulers;
@@ -20,7 +21,7 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
     private Source source;
     private Chapter chapter;
     private List<Page> pageList;
-    private boolean pageListStarted;
+    @State int savedSelectedPage = -1;
 
     private static final int GET_PAGE_LIST = 1;
     private static final int GET_PAGE_IMAGES = 2;
@@ -30,21 +31,20 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
         super.onCreate(savedState);
 
         restartableLatestCache(GET_PAGE_LIST,
-                this::getPageListObservable,
+                () -> getPageListObservable()
+                        .doOnNext(pages -> pageList = pages)
+                        .doOnCompleted(() -> start(GET_PAGE_IMAGES)),
                 (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);
+                    if (page.getPageNumber() == savedSelectedPage) {
+                        view.setCurrentPage(savedSelectedPage);
+                    }
                 });
     }
 
@@ -85,9 +85,9 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
 
     private Observable<Page> getPageImagesObservable() {
         return Observable.merge(
-                    Observable.from(pageList).filter(page -> page.getImageUrl() != null),
-                    source.getRemainingImageUrlsFromPageList(pageList)
-                            .doOnNext(this::replacePageUrl))
+                Observable.from(pageList).filter(page -> page.getImageUrl() != null),
+                source.getRemainingImageUrlsFromPageList(pageList)
+                        .doOnNext(this::replacePageUrl))
                 .subscribeOn(Schedulers.io())
                 .observeOn(AndroidSchedulers.mainThread());
     }
@@ -100,4 +100,8 @@ public class ReaderPresenter extends BasePresenter<ReaderActivity> {
             }
         }
     }
+
+    public void setCurrentPage(int savedPage) {
+        this.savedSelectedPage = savedPage;
+    }
 }

+ 12 - 4
app/src/main/java/eu/kanade/mangafeed/sources/Source.java

@@ -98,8 +98,10 @@ public abstract class Source {
                 .onErrorResumeNext(throwable -> {
                     return mNetworkService
                             .getStringResponse(chapterUrl, mNetworkService.NULL_CACHE_CONTROL, mRequestHeaders)
-                            .flatMap(unparsedHtml -> Observable.just(parseHtmlToPageUrls(unparsedHtml)))
-                            .flatMap(this::convertToPages)
+                            .flatMap(unparsedHtml -> {
+                                List<String> pageUrls = parseHtmlToPageUrls(unparsedHtml);
+                                return Observable.just(getFirstImageFromPageUrls(pageUrls, unparsedHtml));
+                            })
                             .doOnNext(pages -> savePageList(chapterUrl, pages));
                 })
                 .onBackpressureBuffer();
@@ -134,13 +136,19 @@ public abstract class Source {
         mCacheManager.putPageUrlsToDiskCache(chapterUrl, pages);
     }
 
-    private Observable<List<Page>> convertToPages(List<String> pageUrls) {
+    private List<Page> convertToPages(List<String> pageUrls) {
         List<Page> pages = new ArrayList<>();
         for (int i = 0; i < pageUrls.size(); i++) {
             pages.add(new Page(i, pageUrls.get(i)));
         }
-        return Observable.just(pages);
+        return pages;
     }
 
+    private List<Page> getFirstImageFromPageUrls(List<String> pageUrls, String unparsedHtml) {
+        List<Page> pages = convertToPages(pageUrls);
+        String firstImage = parseHtmlToImageUrl(unparsedHtml);
+        pages.get(0).setImageUrl(firstImage);
+        return pages;
+    }
 
 }

+ 41 - 1
app/src/main/java/eu/kanade/mangafeed/ui/activity/ReaderActivity.java

@@ -4,6 +4,7 @@ import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
 import android.support.v4.view.ViewPager;
+import android.widget.TextView;
 
 import java.util.List;
 
@@ -19,8 +20,10 @@ import nucleus.factory.RequiresPresenter;
 public class ReaderActivity extends BaseActivity<ReaderPresenter> {
 
     @Bind(R.id.view_pager) ViewPager viewPager;
+    @Bind(R.id.page_number) TextView pageNumber;
 
     private ReaderPageAdapter adapter;
+    private int currentPage;
 
     public static Intent newInstance(Context context) {
         return new Intent(context, ReaderActivity.class);
@@ -33,7 +36,7 @@ public class ReaderActivity extends BaseActivity<ReaderPresenter> {
         ButterKnife.bind(this);
 
         createAdapter();
-        viewPager.setOffscreenPageLimit(3);
+        setupViewPager();
     }
 
     private void createAdapter() {
@@ -41,11 +44,48 @@ public class ReaderActivity extends BaseActivity<ReaderPresenter> {
         viewPager.setAdapter(adapter);
     }
 
+    private void setupViewPager() {
+        viewPager.setOffscreenPageLimit(3);
+        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
+            @Override
+            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
+
+            }
+
+            @Override
+            public void onPageSelected(int position) {
+                currentPage = position;
+                updatePageNumber();
+            }
+
+            @Override
+            public void onPageScrollStateChanged(int state) {
+
+            }
+        });
+    }
+
+    @Override
+    public void onDestroy() {
+        getPresenter().setCurrentPage(currentPage);
+        super.onDestroy();
+    }
+
     public void onPageList(List<Page> pages) {
         adapter.setPages(pages);
+        updatePageNumber();
     }
 
     public void onPageDownloaded(Page page) {
         adapter.replacePage(page.getPageNumber(), page);
     }
+
+    public void setCurrentPage(int position) {
+        viewPager.setCurrentItem(position);
+    }
+
+    private void updatePageNumber() {
+        String page = (currentPage+1) + "/" + adapter.getCount();
+        pageNumber.setText(page);
+    }
 }

+ 4 - 1
app/src/main/java/eu/kanade/mangafeed/ui/adapter/ReaderPageAdapter.java

@@ -29,6 +29,10 @@ public class ReaderPageAdapter extends SmartFragmentStatePagerAdapter {
         return ReaderPageFragment.newInstance(pages.get(position));
     }
 
+    public List<Page> getPages() {
+        return pages;
+    }
+
     public void setPages(List<Page> pages) {
         this.pages = pages;
         notifyDataSetChanged();
@@ -36,7 +40,6 @@ public class ReaderPageAdapter extends SmartFragmentStatePagerAdapter {
 
     public void replacePage(int position, Page page) {
         pages.set(position, page);
-        notifyDataSetChanged();
 
         ReaderPageFragment fragment = (ReaderPageFragment)getRegisteredFragment(position);
         if (fragment != null) {

+ 6 - 2
app/src/main/java/eu/kanade/mangafeed/ui/fragment/ReaderPageFragment.java

@@ -33,6 +33,8 @@ public class ReaderPageFragment extends Fragment {
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
 
+        setRetainInstance(true);
+
         Bundle arguments = getArguments();
         if (arguments != null) {
             if (arguments.containsKey(URL_ARGUMENT_KEY)) {
@@ -42,8 +44,10 @@ public class ReaderPageFragment extends Fragment {
     }
 
     public void setPage(Page page) {
-        mUrl = page.getImageUrl();
-        loadImage();
+        if (!page.getImageUrl().equals(mUrl)) {
+            mUrl = page.getImageUrl();
+            loadImage();
+        }
     }
 
     private void loadImage() {

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

@@ -9,5 +9,14 @@
         android:layout_height="wrap_content">
     </android.support.v4.view.ViewPager>
 
+    <TextView
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:padding="8dp"
+        android:layout_gravity="bottom|left"
+        android:background="@color/bg_light_grey"
+        android:textColor="@color/black_87pc"
+        android:textSize="12sp"
+        android:id="@+id/page_number"/>
 
 </FrameLayout>