Reader working. Yay!

This commit is contained in:
inorichi 2015-10-21 02:18:29 +02:00
parent 915847c0ed
commit 74da971b2e
5 changed files with 70 additions and 33 deletions

View File

@ -4,10 +4,7 @@ import android.os.Bundle;
import java.util.List; import java.util.List;
import javax.inject.Inject;
import de.greenrobot.event.EventBus; 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.Chapter;
import eu.kanade.mangafeed.data.models.Page; import eu.kanade.mangafeed.data.models.Page;
import eu.kanade.mangafeed.sources.Source; import eu.kanade.mangafeed.sources.Source;
@ -20,20 +17,34 @@ import rx.schedulers.Schedulers;
public class ReaderPresenter extends BasePresenter<ReaderActivity> { public class ReaderPresenter extends BasePresenter<ReaderActivity> {
private static final int GET_PAGE_LIST = 1;
private Source source; private Source source;
private Chapter chapter; private Chapter chapter;
private List<Page> pageList; 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 @Override
protected void onCreate(Bundle savedState) { protected void onCreate(Bundle savedState) {
super.onCreate(savedState); super.onCreate(savedState);
restartableReplay(GET_PAGE_LIST, restartableLatestCache(GET_PAGE_LIST,
this::getPageListObservable, 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, 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) return source.pullPageListFromNetwork(chapter.url)
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread());
.flatMap(pageList -> { }
this.pageList = pageList;
return Observable.merge( private Observable<Page> getPageImagesObservable() {
Observable.from(pageList) return Observable.merge(
.filter(page -> page.getImageUrl() != null), Observable.from(pageList).filter(page -> page.getImageUrl() != null),
source.getRemainingImageUrlsFromPageList(pageList)
source.getRemainingImageUrlsFromPageList(pageList) .doOnNext(this::replacePageUrl))
.doOnNext(this::replacePageUrl)); .subscribeOn(Schedulers.io())
}); .observeOn(AndroidSchedulers.mainThread());
} }
private void replacePageUrl(Page page) { private void replacePageUrl(Page page) {

View File

@ -5,9 +5,12 @@ import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager;
import java.util.List;
import butterknife.Bind; import butterknife.Bind;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import eu.kanade.mangafeed.R; import eu.kanade.mangafeed.R;
import eu.kanade.mangafeed.data.models.Page;
import eu.kanade.mangafeed.presenter.ReaderPresenter; import eu.kanade.mangafeed.presenter.ReaderPresenter;
import eu.kanade.mangafeed.ui.adapter.ReaderPageAdapter; import eu.kanade.mangafeed.ui.adapter.ReaderPageAdapter;
import nucleus.factory.RequiresPresenter; import nucleus.factory.RequiresPresenter;
@ -26,10 +29,11 @@ public class ReaderActivity extends BaseActivity<ReaderPresenter> {
@Override @Override
public void onCreate(Bundle savedState) { public void onCreate(Bundle savedState) {
super.onCreate(savedState); super.onCreate(savedState);
setContentView(R.layout.activity_viewer); setContentView(R.layout.activity_reader);
ButterKnife.bind(this); ButterKnife.bind(this);
createAdapter(); createAdapter();
viewPager.setOffscreenPageLimit(3);
} }
private void createAdapter() { private void createAdapter() {
@ -37,4 +41,11 @@ public class ReaderActivity extends BaseActivity<ReaderPresenter> {
viewPager.setAdapter(adapter); viewPager.setAdapter(adapter);
} }
public void onPageList(List<Page> pages) {
adapter.setPages(pages);
}
public void onPageDownloaded(Page page) {
adapter.replacePage(page.getPageNumber(), page);
}
} }

View File

@ -5,11 +5,12 @@ import android.support.v4.app.FragmentManager;
import java.util.List; import java.util.List;
import eu.kanade.mangafeed.data.models.Page;
import eu.kanade.mangafeed.ui.fragment.ReaderPageFragment; import eu.kanade.mangafeed.ui.fragment.ReaderPageFragment;
public class ReaderPageAdapter extends SmartFragmentStatePagerAdapter { public class ReaderPageAdapter extends SmartFragmentStatePagerAdapter {
private List<String> imageUrls; private List<Page> pages;
public ReaderPageAdapter(FragmentManager fragmentManager) { public ReaderPageAdapter(FragmentManager fragmentManager) {
super(fragmentManager); super(fragmentManager);
@ -17,23 +18,30 @@ public class ReaderPageAdapter extends SmartFragmentStatePagerAdapter {
@Override @Override
public int getCount() { public int getCount() {
if (imageUrls != null) if (pages != null)
return imageUrls.size(); return pages.size();
return 0; return 0;
} }
@Override @Override
public Fragment getItem(int position) { public Fragment getItem(int position) {
return ReaderPageFragment.newInstance(imageUrls.get(position), position); return ReaderPageFragment.newInstance(pages.get(position));
} }
public List<String> getImageUrls() { public void setPages(List<Page> pages) {
return imageUrls; this.pages = pages;
notifyDataSetChanged();
} }
public void setImageUrls(List<String> imageUrls) { public void replacePage(int position, Page page) {
this.imageUrls = imageUrls; pages.set(position, page);
notifyDataSetChanged();
ReaderPageFragment fragment = (ReaderPageFragment)getRegisteredFragment(position);
if (fragment != null) {
fragment.setPage(page);
}
} }
} }

View File

@ -11,6 +11,7 @@ import com.bumptech.glide.Glide;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView; import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
import eu.kanade.mangafeed.R; import eu.kanade.mangafeed.R;
import eu.kanade.mangafeed.data.models.Page;
import eu.kanade.mangafeed.util.PageFileTarget; import eu.kanade.mangafeed.util.PageFileTarget;
public class ReaderPageFragment extends Fragment { public class ReaderPageFragment extends Fragment {
@ -20,10 +21,10 @@ public class ReaderPageFragment extends Fragment {
private String mUrl; private String mUrl;
public static ReaderPageFragment newInstance(String url, int position) { public static ReaderPageFragment newInstance(Page page) {
ReaderPageFragment newInstance = new ReaderPageFragment(); ReaderPageFragment newInstance = new ReaderPageFragment();
Bundle arguments = new Bundle(); Bundle arguments = new Bundle();
arguments.putString(URL_ARGUMENT_KEY, url); arguments.putString(URL_ARGUMENT_KEY, page.getImageUrl());
newInstance.setArguments(arguments); newInstance.setArguments(arguments);
return newInstance; 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 @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mPageImageView = (SubsamplingScaleImageView)inflater.inflate(R.layout.fragment_page, container, false); mPageImageView = (SubsamplingScaleImageView)inflater.inflate(R.layout.fragment_page, container, false);
mPageImageView.setVisibility(View.INVISIBLE);
mPageImageView.setDoubleTapZoomStyle(SubsamplingScaleImageView.ZOOM_FOCUS_FIXED); mPageImageView.setDoubleTapZoomStyle(SubsamplingScaleImageView.ZOOM_FOCUS_FIXED);
mPageImageView.setPanLimit(SubsamplingScaleImageView.PAN_LIMIT_INSIDE); mPageImageView.setPanLimit(SubsamplingScaleImageView.PAN_LIMIT_INSIDE);
mPageImageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_INSIDE); mPageImageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_INSIDE);
@ -76,9 +87,6 @@ public class ReaderPageFragment extends Fragment {
@Override @Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) { public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
loadImage();
Glide.with(getActivity())
.load(mUrl)
.downloadOnly(new PageFileTarget(mPageImageView));
} }
} }