From 66ab8caf76fe2795fbaecdb655294ce0bc71de64 Mon Sep 17 00:00:00 2001 From: inorichi Date: Mon, 12 Oct 2015 23:00:55 +0200 Subject: [PATCH] Get mangas from search first steps --- .../presenter/CatalogueListPresenter.java | 61 ++++++++++++++++++- .../presenter/CataloguePresenter.java | 2 +- .../ui/activity/CatalogueListActivity.java | 35 +++++++++-- .../eu/kanade/mangafeed/view/BaseView.java | 2 - .../mangafeed/view/CatalogueListView.java | 2 +- app/src/main/res/menu/catalogue_list.xml | 11 ++++ 6 files changed, 102 insertions(+), 11 deletions(-) create mode 100644 app/src/main/res/menu/catalogue_list.xml diff --git a/app/src/main/java/eu/kanade/mangafeed/presenter/CatalogueListPresenter.java b/app/src/main/java/eu/kanade/mangafeed/presenter/CatalogueListPresenter.java index e847ea821..ea3e24090 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/CatalogueListPresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/CatalogueListPresenter.java @@ -2,6 +2,8 @@ package eu.kanade.mangafeed.presenter; import android.content.Intent; +import java.util.concurrent.TimeUnit; + import javax.inject.Inject; import eu.kanade.mangafeed.App; @@ -15,6 +17,8 @@ import rx.Observable; import rx.Subscription; import rx.android.schedulers.AndroidSchedulers; import rx.schedulers.Schedulers; +import rx.subjects.PublishSubject; +import timber.log.Timber; import uk.co.ribot.easyadapter.EasyAdapter; public class CatalogueListPresenter extends BasePresenter { @@ -26,7 +30,13 @@ public class CatalogueListPresenter extends BasePresenter { @Inject SourceManager sourceManager; @Inject DatabaseHelper db; + private String mSearchName; + private final int SEARCH_TIMEOUT = 1000; + private Subscription mMangaFetchSubscription; + private Subscription mMangaSearchSubscription; + private Subscription mSearchViewSubscription; + private PublishSubject> mSearchViewPublishSubject; public CatalogueListPresenter(CatalogueListView view) { @@ -37,12 +47,14 @@ public class CatalogueListPresenter extends BasePresenter { public void initialize() { int sourceId = view.getIntent().getIntExtra(Intent.EXTRA_UID, -1); selectedSource = sourceManager.get(sourceId); - view.setSource(selectedSource); + view.setSourceTitle(selectedSource.getName()); adapter = new EasyAdapter<>(view.getActivity(), CatalogueListHolder.class); view.setAdapter(adapter); view.setScrollListener(); + initializeSearch(); + getMangasFromSource(1); } @@ -60,6 +72,14 @@ public class CatalogueListPresenter extends BasePresenter { subscriptions.add(mMangaFetchSubscription); } + public void getMangasFromSearch(int page) { + subscriptions.remove(mMangaSearchSubscription); + + // TODO fetch mangas from source + + subscriptions.add(mMangaSearchSubscription); + } + private Manga networkToLocalManga(Manga networkManga) { Manga localManga = db.getMangaBlock(networkManga.url); if (localManga == null) { @@ -69,4 +89,43 @@ public class CatalogueListPresenter extends BasePresenter { return localManga; } + public void onQueryTextChange(String query) { + if (mSearchViewPublishSubject != null) + mSearchViewPublishSubject.onNext(Observable.just(query)); + } + + private void initializeSearch() { + mSearchViewPublishSubject = PublishSubject.create(); + mSearchViewSubscription = Observable.switchOnNext(mSearchViewPublishSubject) + .debounce(SEARCH_TIMEOUT, TimeUnit.MILLISECONDS) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe( + this::queryFromSearch, + error -> Timber.e(error.getCause(), error.getMessage())); + + subscriptions.add(mSearchViewSubscription); + } + + private void queryFromSearch(String query) { + mSearchName = query; + if (!isSearchMode()) { + getMangasFromSource(1); + } else { + getMangasFromSearch(1); + } + view.setScrollListener(); + } + + public void loadMoreMangas(int page) { + if (!isSearchMode()) { + getMangasFromSource(page); + } else { + getMangasFromSearch(page); + } + } + + private boolean isSearchMode() { + return !mSearchName.equals(""); + } } 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 2fa2f4703..5f28b625a 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/CataloguePresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/CataloguePresenter.java @@ -39,6 +39,6 @@ public class CataloguePresenter { public void onSourceClick(int position) { Intent intent = new Intent(view.getActivity(), CatalogueListActivity.class); intent.putExtra(Intent.EXTRA_UID, adapter.getItem(position).getSource()); - view.startActivity(intent); + view.getActivity().startActivity(intent); } } diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/activity/CatalogueListActivity.java b/app/src/main/java/eu/kanade/mangafeed/ui/activity/CatalogueListActivity.java index 97dd0461b..81ea71914 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/activity/CatalogueListActivity.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/activity/CatalogueListActivity.java @@ -1,14 +1,15 @@ package eu.kanade.mangafeed.ui.activity; import android.os.Bundle; +import android.support.v7.widget.SearchView; import android.support.v7.widget.Toolbar; +import android.view.Menu; import android.widget.ListView; import butterknife.Bind; import butterknife.ButterKnife; import eu.kanade.mangafeed.R; import eu.kanade.mangafeed.presenter.CatalogueListPresenter; -import eu.kanade.mangafeed.sources.Source; import eu.kanade.mangafeed.view.CatalogueListView; import eu.kanade.mangafeed.widget.EndlessScrollListener; import uk.co.ribot.easyadapter.EasyAdapter; @@ -22,7 +23,6 @@ public class CatalogueListActivity extends BaseActivity implements CatalogueList ListView manga_list; private CatalogueListPresenter presenter; - private Source source; @Override public void onCreate(Bundle savedInstanceState) { @@ -42,9 +42,32 @@ public class CatalogueListActivity extends BaseActivity implements CatalogueList presenter.destroySubscriptions(); } - public void setSource(Source source) { - this.source = source; - setToolbarTitle(source.getName()); + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.catalogue_list, menu); + initializeSearch(menu); + return super.onCreateOptionsMenu(menu); + } + + private void initializeSearch(Menu menu) { + final SearchView sv = (SearchView) menu.findItem(R.id.action_search).getActionView(); + sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() { + @Override + public boolean onQueryTextSubmit(String query) { + presenter.onQueryTextChange(query); + return true; + } + + @Override + public boolean onQueryTextChange(String newText) { + presenter.onQueryTextChange(newText); + return true; + } + }); + } + + public void setSourceTitle(String title) { + setToolbarTitle(title); } public void setAdapter(EasyAdapter adapter) { @@ -55,7 +78,7 @@ public class CatalogueListActivity extends BaseActivity implements CatalogueList manga_list.setOnScrollListener(new EndlessScrollListener() { @Override public boolean onLoadMore(int page, int totalItemsCount) { - presenter.getMangasFromSource(page); + presenter.loadMoreMangas(page); return true; } }); diff --git a/app/src/main/java/eu/kanade/mangafeed/view/BaseView.java b/app/src/main/java/eu/kanade/mangafeed/view/BaseView.java index ef322c429..019d18786 100644 --- a/app/src/main/java/eu/kanade/mangafeed/view/BaseView.java +++ b/app/src/main/java/eu/kanade/mangafeed/view/BaseView.java @@ -1,9 +1,7 @@ package eu.kanade.mangafeed.view; import android.content.Context; -import android.content.Intent; public interface BaseView { Context getActivity(); - void startActivity(Intent intent); } diff --git a/app/src/main/java/eu/kanade/mangafeed/view/CatalogueListView.java b/app/src/main/java/eu/kanade/mangafeed/view/CatalogueListView.java index 914598535..c304d904f 100644 --- a/app/src/main/java/eu/kanade/mangafeed/view/CatalogueListView.java +++ b/app/src/main/java/eu/kanade/mangafeed/view/CatalogueListView.java @@ -7,7 +7,7 @@ import uk.co.ribot.easyadapter.EasyAdapter; public interface CatalogueListView extends BaseView { Intent getIntent(); - void setSource(Source source); + void setSourceTitle(String title); void setAdapter(EasyAdapter adapter); void setScrollListener(); } diff --git a/app/src/main/res/menu/catalogue_list.xml b/app/src/main/res/menu/catalogue_list.xml new file mode 100644 index 000000000..c0b1a7c5e --- /dev/null +++ b/app/src/main/res/menu/catalogue_list.xml @@ -0,0 +1,11 @@ + + +