Get mangas from search first steps

This commit is contained in:
inorichi 2015-10-12 23:00:55 +02:00
parent 38da2ba35b
commit 66ab8caf76
6 changed files with 102 additions and 11 deletions

View File

@ -2,6 +2,8 @@ package eu.kanade.mangafeed.presenter;
import android.content.Intent; import android.content.Intent;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject; import javax.inject.Inject;
import eu.kanade.mangafeed.App; import eu.kanade.mangafeed.App;
@ -15,6 +17,8 @@ import rx.Observable;
import rx.Subscription; 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 timber.log.Timber;
import uk.co.ribot.easyadapter.EasyAdapter; import uk.co.ribot.easyadapter.EasyAdapter;
public class CatalogueListPresenter extends BasePresenter { public class CatalogueListPresenter extends BasePresenter {
@ -26,7 +30,13 @@ public class CatalogueListPresenter extends BasePresenter {
@Inject SourceManager sourceManager; @Inject SourceManager sourceManager;
@Inject DatabaseHelper db; @Inject DatabaseHelper db;
private String mSearchName;
private final int SEARCH_TIMEOUT = 1000;
private Subscription mMangaFetchSubscription; private Subscription mMangaFetchSubscription;
private Subscription mMangaSearchSubscription;
private Subscription mSearchViewSubscription;
private PublishSubject<Observable<String>> mSearchViewPublishSubject;
public CatalogueListPresenter(CatalogueListView view) { public CatalogueListPresenter(CatalogueListView view) {
@ -37,12 +47,14 @@ public class CatalogueListPresenter extends BasePresenter {
public void initialize() { public void initialize() {
int sourceId = view.getIntent().getIntExtra(Intent.EXTRA_UID, -1); int sourceId = view.getIntent().getIntExtra(Intent.EXTRA_UID, -1);
selectedSource = sourceManager.get(sourceId); selectedSource = sourceManager.get(sourceId);
view.setSource(selectedSource); view.setSourceTitle(selectedSource.getName());
adapter = new EasyAdapter<>(view.getActivity(), CatalogueListHolder.class); adapter = new EasyAdapter<>(view.getActivity(), CatalogueListHolder.class);
view.setAdapter(adapter); view.setAdapter(adapter);
view.setScrollListener(); view.setScrollListener();
initializeSearch();
getMangasFromSource(1); getMangasFromSource(1);
} }
@ -60,6 +72,14 @@ public class CatalogueListPresenter extends BasePresenter {
subscriptions.add(mMangaFetchSubscription); subscriptions.add(mMangaFetchSubscription);
} }
public void getMangasFromSearch(int page) {
subscriptions.remove(mMangaSearchSubscription);
// TODO fetch mangas from source
subscriptions.add(mMangaSearchSubscription);
}
private Manga networkToLocalManga(Manga networkManga) { private Manga networkToLocalManga(Manga networkManga) {
Manga localManga = db.getMangaBlock(networkManga.url); Manga localManga = db.getMangaBlock(networkManga.url);
if (localManga == null) { if (localManga == null) {
@ -69,4 +89,43 @@ public class CatalogueListPresenter extends BasePresenter {
return localManga; 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("");
}
} }

View File

@ -39,6 +39,6 @@ public class CataloguePresenter {
public void onSourceClick(int position) { public void onSourceClick(int position) {
Intent intent = new Intent(view.getActivity(), CatalogueListActivity.class); Intent intent = new Intent(view.getActivity(), CatalogueListActivity.class);
intent.putExtra(Intent.EXTRA_UID, adapter.getItem(position).getSource()); intent.putExtra(Intent.EXTRA_UID, adapter.getItem(position).getSource());
view.startActivity(intent); view.getActivity().startActivity(intent);
} }
} }

View File

@ -1,14 +1,15 @@
package eu.kanade.mangafeed.ui.activity; package eu.kanade.mangafeed.ui.activity;
import android.os.Bundle; import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.widget.ListView; import android.widget.ListView;
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.presenter.CatalogueListPresenter; import eu.kanade.mangafeed.presenter.CatalogueListPresenter;
import eu.kanade.mangafeed.sources.Source;
import eu.kanade.mangafeed.view.CatalogueListView; import eu.kanade.mangafeed.view.CatalogueListView;
import eu.kanade.mangafeed.widget.EndlessScrollListener; import eu.kanade.mangafeed.widget.EndlessScrollListener;
import uk.co.ribot.easyadapter.EasyAdapter; import uk.co.ribot.easyadapter.EasyAdapter;
@ -22,7 +23,6 @@ public class CatalogueListActivity extends BaseActivity implements CatalogueList
ListView manga_list; ListView manga_list;
private CatalogueListPresenter presenter; private CatalogueListPresenter presenter;
private Source source;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
@ -42,9 +42,32 @@ public class CatalogueListActivity extends BaseActivity implements CatalogueList
presenter.destroySubscriptions(); presenter.destroySubscriptions();
} }
public void setSource(Source source) { @Override
this.source = source; public boolean onCreateOptionsMenu(Menu menu) {
setToolbarTitle(source.getName()); 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) { public void setAdapter(EasyAdapter adapter) {
@ -55,7 +78,7 @@ public class CatalogueListActivity extends BaseActivity implements CatalogueList
manga_list.setOnScrollListener(new EndlessScrollListener() { manga_list.setOnScrollListener(new EndlessScrollListener() {
@Override @Override
public boolean onLoadMore(int page, int totalItemsCount) { public boolean onLoadMore(int page, int totalItemsCount) {
presenter.getMangasFromSource(page); presenter.loadMoreMangas(page);
return true; return true;
} }
}); });

View File

@ -1,9 +1,7 @@
package eu.kanade.mangafeed.view; package eu.kanade.mangafeed.view;
import android.content.Context; import android.content.Context;
import android.content.Intent;
public interface BaseView { public interface BaseView {
Context getActivity(); Context getActivity();
void startActivity(Intent intent);
} }

View File

@ -7,7 +7,7 @@ import uk.co.ribot.easyadapter.EasyAdapter;
public interface CatalogueListView extends BaseView { public interface CatalogueListView extends BaseView {
Intent getIntent(); Intent getIntent();
void setSource(Source source); void setSourceTitle(String title);
void setAdapter(EasyAdapter adapter); void setAdapter(EasyAdapter adapter);
void setScrollListener(); void setScrollListener();
} }

View File

@ -0,0 +1,11 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".CatalogueListActivity">
<item
android:id="@+id/action_search"
android:title="@string/action_search"
android:icon="@drawable/ic_action_search"
android:orderInCategory="100"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>