Minor changes

This commit is contained in:
inorichi 2015-10-19 15:10:25 +02:00
parent a3236e98d0
commit 9ad6efbada
5 changed files with 44 additions and 64 deletions

View file

@ -17,7 +17,6 @@ public class SourceManager {
private HashMap<Integer, Source> mSourcesMap; private HashMap<Integer, Source> mSourcesMap;
private NetworkHelper mNetworkHelper; private NetworkHelper mNetworkHelper;
private CacheManager mCacheManager; private CacheManager mCacheManager;
private Source selected;
public SourceManager(NetworkHelper networkHelper, CacheManager cacheManager) { public SourceManager(NetworkHelper networkHelper, CacheManager cacheManager) {
mSourcesMap = new HashMap<>(); mSourcesMap = new HashMap<>();
@ -51,14 +50,7 @@ public class SourceManager {
} }
public List<Source> getSources() { public List<Source> getSources() {
return new ArrayList<Source>(mSourcesMap.values()); return new ArrayList<>(mSourcesMap.values());
} }
public void setSelectedSource(int sourceId) {
selected = get(sourceId);
}
public Source getSelectedSource() {
return selected;
}
} }

View file

@ -23,7 +23,6 @@ import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers; import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers; import rx.schedulers.Schedulers;
import rx.subjects.PublishSubject; import rx.subjects.PublishSubject;
import timber.log.Timber;
public class CataloguePresenter extends BasePresenter<CatalogueActivity> { public class CataloguePresenter extends BasePresenter<CatalogueActivity> {
@ -36,12 +35,12 @@ public class CataloguePresenter extends BasePresenter<CatalogueActivity> {
@State protected boolean mSearchMode; @State protected boolean mSearchMode;
private final int SEARCH_TIMEOUT = 1000; private final int SEARCH_TIMEOUT = 1000;
@State protected int mCurrentPage; private int mCurrentPage;
private RxPager pager; private RxPager pager;
private Subscription mSearchViewSubscription; private Subscription mQueryDebouncerSubscription;
private Subscription mMangaDetailFetchSubscription; private Subscription mMangaDetailFetchSubscription;
private PublishSubject<Observable<String>> mSearchViewPublishSubject; private PublishSubject<Observable<String>> mQueryDebouncerSubject;
private PublishSubject<Observable<List<Manga>>> mMangaDetailPublishSubject; private PublishSubject<Observable<List<Manga>>> mMangaDetailPublishSubject;
private static final int GET_MANGA_LIST = 1; private static final int GET_MANGA_LIST = 1;
@ -77,14 +76,8 @@ public class CataloguePresenter extends BasePresenter<CatalogueActivity> {
view.restoreSearch(mSearchName); view.restoreSearch(mSearchName);
} }
public void requestNext() { public void startRequesting(int sourceId) {
pager.requestNext(++mCurrentPage); selectedSource = sourceManager.get(sourceId);
if (getView() != null)
getView().showGridProgressBar();
}
public void startRequesting(int source_id) {
this.selectedSource = sourceManager.get(source_id);
restartRequest(); restartRequest();
} }
@ -92,9 +85,17 @@ public class CataloguePresenter extends BasePresenter<CatalogueActivity> {
stop(GET_MANGA_LIST); stop(GET_MANGA_LIST);
mCurrentPage = 1; mCurrentPage = 1;
pager = new RxPager(); pager = new RxPager();
start(GET_MANGA_LIST);
if (getView() != null) if (getView() != null)
getView().showProgressBar(); getView().showProgressBar();
start(GET_MANGA_LIST);
}
public void requestNext() {
if (getView() != null)
getView().showGridProgressBar();
pager.requestNext(++mCurrentPage);
} }
private Observable<List<Manga>> getMangaObs(int page) { private Observable<List<Manga>> getMangaObs(int page) {
@ -110,21 +111,29 @@ public class CataloguePresenter extends BasePresenter<CatalogueActivity> {
.toList(); .toList();
} }
private Manga networkToLocalManga(Manga networkManga) {
Manga localManga = db.getMangaBlock(networkManga.url);
if (localManga == null) {
PutResult result = db.insertMangaBlock(networkManga);
networkManga.id = result.insertedId();
localManga = networkManga;
}
return localManga;
}
private void initializeSearch() { private void initializeSearch() {
if (mSearchViewSubscription != null) if (mQueryDebouncerSubscription != null)
return; return;
mSearchName = ""; mSearchName = "";
mSearchMode = false; mSearchMode = false;
mSearchViewPublishSubject = PublishSubject.create(); mQueryDebouncerSubject = PublishSubject.create();
mSearchViewSubscription = Observable.switchOnNext(mSearchViewPublishSubject) add(mQueryDebouncerSubscription = Observable.switchOnNext(mQueryDebouncerSubject)
.debounce(SEARCH_TIMEOUT, TimeUnit.MILLISECONDS) .debounce(SEARCH_TIMEOUT, TimeUnit.MILLISECONDS)
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(this::queryFromSearch); .subscribe(this::queryFromSearch));
add(mSearchViewSubscription);
} }
private void initializeMangaDetailsLoader() { private void initializeMangaDetailsLoader() {
@ -133,7 +142,7 @@ public class CataloguePresenter extends BasePresenter<CatalogueActivity> {
mMangaDetailPublishSubject = PublishSubject.create(); mMangaDetailPublishSubject = PublishSubject.create();
mMangaDetailFetchSubscription = Observable.switchOnNext(mMangaDetailPublishSubject) add(mMangaDetailFetchSubscription = Observable.switchOnNext(mMangaDetailPublishSubject)
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.flatMap(Observable::from) .flatMap(Observable::from)
.filter(manga -> !manga.initialized) .filter(manga -> !manga.initialized)
@ -158,32 +167,19 @@ public class CataloguePresenter extends BasePresenter<CatalogueActivity> {
.subscribe(manga -> { .subscribe(manga -> {
if (getView() != null) if (getView() != null)
getView().updateImage(manga); getView().updateImage(manga);
}); }));
add(mMangaDetailFetchSubscription);
} }
private Manga networkToLocalManga(Manga networkManga) { public void onSearchEvent(String query, boolean now) {
Manga localManga = db.getMangaBlock(networkManga.url); // If the query is empty or not debounced, resolve it instantly
if (localManga == null) { if (now || query.equals(""))
PutResult result = db.insertMangaBlock(networkManga);
networkManga.id = result.insertedId();
localManga = networkManga;
}
return localManga;
}
public void onQueryTextChange(String query) {
if (query.equals("")) {
queryFromSearch(query); queryFromSearch(query);
return; else if (mQueryDebouncerSubject != null)
} mQueryDebouncerSubject.onNext(Observable.just(query));
if (mSearchViewPublishSubject != null)
mSearchViewPublishSubject.onNext(Observable.just(query));
} }
public void queryFromSearch(String query) { private void queryFromSearch(String query) {
// If text didn't change // If text didn't change, do nothing
if (mSearchName.equals(query)) { if (mSearchName.equals(query)) {
return; return;
} }

View file

@ -18,7 +18,4 @@ public class SourcePresenter extends BasePresenter<SourceFragment> {
view.setItems(sourceManager.getSources()); view.setItems(sourceManager.getSources());
} }
public void setSelectedSource(Source source) {
sourceManager.setSelectedSource(source.getSourceId());
}
} }

View file

@ -14,7 +14,6 @@ import android.widget.ProgressBar;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import butterknife.Bind; import butterknife.Bind;
@ -86,13 +85,13 @@ public class CatalogueActivity extends BaseActivity<CataloguePresenter> {
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() { sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override @Override
public boolean onQueryTextSubmit(String query) { public boolean onQueryTextSubmit(String query) {
getPresenter().queryFromSearch(query); getPresenter().onSearchEvent(query, true);
return true; return true;
} }
@Override @Override
public boolean onQueryTextChange(String newText) { public boolean onQueryTextChange(String newText) {
getPresenter().onQueryTextChange(newText); getPresenter().onSearchEvent(newText, false);
return true; return true;
} }
}); });
@ -126,10 +125,6 @@ public class CatalogueActivity extends BaseActivity<CataloguePresenter> {
manga_list.setOnScrollListener(scroll_listener); manga_list.setOnScrollListener(scroll_listener);
} }
public void resetScrollListener() {
scroll_listener.resetScroll();
}
public void showProgressBar() { public void showProgressBar() {
progress.setVisibility(ProgressBar.VISIBLE); progress.setVisibility(ProgressBar.VISIBLE);
} }
@ -145,8 +140,8 @@ public class CatalogueActivity extends BaseActivity<CataloguePresenter> {
public void onAddPage(PageBundle<List<Manga>> page) { public void onAddPage(PageBundle<List<Manga>> page) {
if (page.page == 0) { if (page.page == 0) {
adapter.setItems(new ArrayList<>()); adapter.getItems().clear();
resetScrollListener(); scroll_listener.resetScroll();
} }
adapter.addItems(page.data); adapter.addItems(page.data);
} }

View file

@ -44,9 +44,10 @@ public class SourceFragment extends BaseFragment<SourcePresenter> {
Bundle savedInstanceState) { Bundle savedInstanceState) {
// Inflate the layout for this fragment // Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_catalogue, container, false); View view = inflater.inflate(R.layout.fragment_catalogue, container, false);
activity.setToolbarTitle(R.string.catalogues_title);
ButterKnife.bind(this, view); ButterKnife.bind(this, view);
activity.setToolbarTitle(R.string.catalogues_title);
createAdapter(); createAdapter();
return view; return view;
@ -55,7 +56,6 @@ public class SourceFragment extends BaseFragment<SourcePresenter> {
@OnItemClick(R.id.catalogue_list) @OnItemClick(R.id.catalogue_list)
public void onSourceClick(int position) { public void onSourceClick(int position) {
Source source = adapter.getItem(position); Source source = adapter.getItem(position);
getPresenter().setSelectedSource(source);
Intent intent = CatalogueActivity.newIntent(activity, source.getSourceId()); Intent intent = CatalogueActivity.newIntent(activity, source.getSourceId());
startActivity(intent); startActivity(intent);