From 9ad6efbadaf95cb4d41855e19e226d9f516293c7 Mon Sep 17 00:00:00 2001 From: inorichi Date: Mon, 19 Oct 2015 15:10:25 +0200 Subject: [PATCH] Minor changes --- .../mangafeed/data/helpers/SourceManager.java | 10 +-- .../presenter/CataloguePresenter.java | 78 +++++++++---------- .../mangafeed/presenter/SourcePresenter.java | 3 - .../ui/activity/CatalogueActivity.java | 13 +--- .../mangafeed/ui/fragment/SourceFragment.java | 4 +- 5 files changed, 44 insertions(+), 64 deletions(-) diff --git a/app/src/main/java/eu/kanade/mangafeed/data/helpers/SourceManager.java b/app/src/main/java/eu/kanade/mangafeed/data/helpers/SourceManager.java index 80e77d9855..c8ea9ed527 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/helpers/SourceManager.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/helpers/SourceManager.java @@ -17,7 +17,6 @@ public class SourceManager { private HashMap mSourcesMap; private NetworkHelper mNetworkHelper; private CacheManager mCacheManager; - private Source selected; public SourceManager(NetworkHelper networkHelper, CacheManager cacheManager) { mSourcesMap = new HashMap<>(); @@ -51,14 +50,7 @@ public class SourceManager { } public List getSources() { - return new ArrayList(mSourcesMap.values()); + return new ArrayList<>(mSourcesMap.values()); } - public void setSelectedSource(int sourceId) { - selected = get(sourceId); - } - - public Source getSelectedSource() { - return selected; - } } diff --git a/app/src/main/java/eu/kanade/mangafeed/presenter/CataloguePresenter.java b/app/src/main/java/eu/kanade/mangafeed/presenter/CataloguePresenter.java index 34161a6aac..05a5fbec68 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/CataloguePresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/CataloguePresenter.java @@ -23,7 +23,6 @@ import rx.Subscription; import rx.android.schedulers.AndroidSchedulers; import rx.schedulers.Schedulers; import rx.subjects.PublishSubject; -import timber.log.Timber; public class CataloguePresenter extends BasePresenter { @@ -36,12 +35,12 @@ public class CataloguePresenter extends BasePresenter { @State protected boolean mSearchMode; private final int SEARCH_TIMEOUT = 1000; - @State protected int mCurrentPage; + private int mCurrentPage; private RxPager pager; - private Subscription mSearchViewSubscription; + private Subscription mQueryDebouncerSubscription; private Subscription mMangaDetailFetchSubscription; - private PublishSubject> mSearchViewPublishSubject; + private PublishSubject> mQueryDebouncerSubject; private PublishSubject>> mMangaDetailPublishSubject; private static final int GET_MANGA_LIST = 1; @@ -77,14 +76,8 @@ public class CataloguePresenter extends BasePresenter { view.restoreSearch(mSearchName); } - public void requestNext() { - pager.requestNext(++mCurrentPage); - if (getView() != null) - getView().showGridProgressBar(); - } - - public void startRequesting(int source_id) { - this.selectedSource = sourceManager.get(source_id); + public void startRequesting(int sourceId) { + selectedSource = sourceManager.get(sourceId); restartRequest(); } @@ -92,9 +85,17 @@ public class CataloguePresenter extends BasePresenter { stop(GET_MANGA_LIST); mCurrentPage = 1; pager = new RxPager(); - start(GET_MANGA_LIST); if (getView() != null) getView().showProgressBar(); + + start(GET_MANGA_LIST); + } + + public void requestNext() { + if (getView() != null) + getView().showGridProgressBar(); + + pager.requestNext(++mCurrentPage); } private Observable> getMangaObs(int page) { @@ -110,21 +111,29 @@ public class CataloguePresenter extends BasePresenter { .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() { - if (mSearchViewSubscription != null) + if (mQueryDebouncerSubscription != null) return; mSearchName = ""; mSearchMode = false; - mSearchViewPublishSubject = PublishSubject.create(); + mQueryDebouncerSubject = PublishSubject.create(); - mSearchViewSubscription = Observable.switchOnNext(mSearchViewPublishSubject) + add(mQueryDebouncerSubscription = Observable.switchOnNext(mQueryDebouncerSubject) .debounce(SEARCH_TIMEOUT, TimeUnit.MILLISECONDS) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) - .subscribe(this::queryFromSearch); - - add(mSearchViewSubscription); + .subscribe(this::queryFromSearch)); } private void initializeMangaDetailsLoader() { @@ -133,7 +142,7 @@ public class CataloguePresenter extends BasePresenter { mMangaDetailPublishSubject = PublishSubject.create(); - mMangaDetailFetchSubscription = Observable.switchOnNext(mMangaDetailPublishSubject) + add(mMangaDetailFetchSubscription = Observable.switchOnNext(mMangaDetailPublishSubject) .subscribeOn(Schedulers.io()) .flatMap(Observable::from) .filter(manga -> !manga.initialized) @@ -158,32 +167,19 @@ public class CataloguePresenter extends BasePresenter { .subscribe(manga -> { if (getView() != null) getView().updateImage(manga); - }); - - add(mMangaDetailFetchSubscription); + })); } - 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; - } - - public void onQueryTextChange(String query) { - if (query.equals("")) { + public void onSearchEvent(String query, boolean now) { + // If the query is empty or not debounced, resolve it instantly + if (now || query.equals("")) queryFromSearch(query); - return; - } - if (mSearchViewPublishSubject != null) - mSearchViewPublishSubject.onNext(Observable.just(query)); + else if (mQueryDebouncerSubject != null) + mQueryDebouncerSubject.onNext(Observable.just(query)); } - public void queryFromSearch(String query) { - // If text didn't change + private void queryFromSearch(String query) { + // If text didn't change, do nothing if (mSearchName.equals(query)) { return; } diff --git a/app/src/main/java/eu/kanade/mangafeed/presenter/SourcePresenter.java b/app/src/main/java/eu/kanade/mangafeed/presenter/SourcePresenter.java index 7a267392ae..5e29dde3fc 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/SourcePresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/SourcePresenter.java @@ -18,7 +18,4 @@ public class SourcePresenter extends BasePresenter { view.setItems(sourceManager.getSources()); } - public void setSelectedSource(Source source) { - sourceManager.setSelectedSource(source.getSourceId()); - } } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/activity/CatalogueActivity.java b/app/src/main/java/eu/kanade/mangafeed/ui/activity/CatalogueActivity.java index 85744def11..52a82422da 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/activity/CatalogueActivity.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/activity/CatalogueActivity.java @@ -14,7 +14,6 @@ import android.widget.ProgressBar; import com.bumptech.glide.Glide; -import java.util.ArrayList; import java.util.List; import butterknife.Bind; @@ -86,13 +85,13 @@ public class CatalogueActivity extends BaseActivity { sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { - getPresenter().queryFromSearch(query); + getPresenter().onSearchEvent(query, true); return true; } @Override public boolean onQueryTextChange(String newText) { - getPresenter().onQueryTextChange(newText); + getPresenter().onSearchEvent(newText, false); return true; } }); @@ -126,10 +125,6 @@ public class CatalogueActivity extends BaseActivity { manga_list.setOnScrollListener(scroll_listener); } - public void resetScrollListener() { - scroll_listener.resetScroll(); - } - public void showProgressBar() { progress.setVisibility(ProgressBar.VISIBLE); } @@ -145,8 +140,8 @@ public class CatalogueActivity extends BaseActivity { public void onAddPage(PageBundle> page) { if (page.page == 0) { - adapter.setItems(new ArrayList<>()); - resetScrollListener(); + adapter.getItems().clear(); + scroll_listener.resetScroll(); } adapter.addItems(page.data); } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/SourceFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/SourceFragment.java index 88fb686ec3..23a4b4e18d 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/fragment/SourceFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/fragment/SourceFragment.java @@ -44,9 +44,10 @@ public class SourceFragment extends BaseFragment { Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_catalogue, container, false); - activity.setToolbarTitle(R.string.catalogues_title); ButterKnife.bind(this, view); + activity.setToolbarTitle(R.string.catalogues_title); + createAdapter(); return view; @@ -55,7 +56,6 @@ public class SourceFragment extends BaseFragment { @OnItemClick(R.id.catalogue_list) public void onSourceClick(int position) { Source source = adapter.getItem(position); - getPresenter().setSelectedSource(source); Intent intent = CatalogueActivity.newIntent(activity, source.getSourceId()); startActivity(intent);