Search survive screen rotation

This commit is contained in:
inorichi 2015-10-19 04:15:30 +02:00
parent 264d627dea
commit 85d34ab9e5
2 changed files with 39 additions and 25 deletions

View File

@ -2,6 +2,8 @@ package eu.kanade.mangafeed.presenter;
import android.os.Bundle; import android.os.Bundle;
import com.pushtorefresh.storio.sqlite.operations.put.PutResult;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -16,7 +18,6 @@ import eu.kanade.mangafeed.ui.activity.CatalogueActivity;
import eu.kanade.mangafeed.util.PageBundle; import eu.kanade.mangafeed.util.PageBundle;
import eu.kanade.mangafeed.util.RxPager; import eu.kanade.mangafeed.util.RxPager;
import icepick.State; import icepick.State;
import nucleus.presenter.RxPresenter;
import rx.Observable; import rx.Observable;
import rx.Subscription; import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers; import rx.android.schedulers.AndroidSchedulers;
@ -24,15 +25,15 @@ import rx.schedulers.Schedulers;
import rx.subjects.PublishSubject; import rx.subjects.PublishSubject;
import timber.log.Timber; import timber.log.Timber;
public class CataloguePresenter extends RxPresenter<CatalogueActivity> { public class CataloguePresenter extends BasePresenter<CatalogueActivity> {
@Inject SourceManager sourceManager; @Inject SourceManager sourceManager;
@Inject DatabaseHelper db; @Inject DatabaseHelper db;
private Source selectedSource; private Source selectedSource;
private String mSearchName; @State protected String mSearchName;
private boolean mSearchMode; @State protected boolean mSearchMode;
private final int SEARCH_TIMEOUT = 1000; private final int SEARCH_TIMEOUT = 1000;
@State protected int mCurrentPage; @State protected int mCurrentPage;
@ -72,15 +73,17 @@ public class CataloguePresenter extends RxPresenter<CatalogueActivity> {
view.setToolbarTitle(selectedSource.getName()); view.setToolbarTitle(selectedSource.getName());
if (view.getAdapter().getCount() == 0) if (mSearchMode)
view.showProgressBar(); view.restoreSearch(mSearchName);
} }
public void requestNext() { public void requestNext() {
pager.requestNext(++mCurrentPage); pager.requestNext(++mCurrentPage);
if (getView() != null)
getView().showGridProgressBar();
} }
public void initializeRequest(int source_id) { public void startRequesting(int source_id) {
this.selectedSource = sourceManager.get(source_id); this.selectedSource = sourceManager.get(source_id);
restartRequest(); restartRequest();
} }
@ -90,6 +93,8 @@ public class CataloguePresenter extends RxPresenter<CatalogueActivity> {
mCurrentPage = 1; mCurrentPage = 1;
pager = new RxPager(); pager = new RxPager();
start(GET_MANGA_LIST); start(GET_MANGA_LIST);
if (getView() != null)
getView().showProgressBar();
} }
private Observable<List<Manga>> getMangaObs(int page) { private Observable<List<Manga>> getMangaObs(int page) {
@ -102,12 +107,12 @@ public class CataloguePresenter extends RxPresenter<CatalogueActivity> {
return obs.subscribeOn(Schedulers.io()) return obs.subscribeOn(Schedulers.io())
.flatMap(Observable::from) .flatMap(Observable::from)
.map(this::networkToLocalManga) .map(this::networkToLocalManga)
.toList() .toList();
.observeOn(AndroidSchedulers.mainThread());
} }
private void initializeSearch() { private void initializeSearch() {
remove(mSearchViewSubscription); if (mSearchViewSubscription != null)
return;
mSearchName = ""; mSearchName = "";
mSearchMode = false; mSearchMode = false;
@ -125,7 +130,8 @@ public class CataloguePresenter extends RxPresenter<CatalogueActivity> {
} }
private void initializeMangaDetailsLoader() { private void initializeMangaDetailsLoader() {
remove(mMangaDetailFetchSubscription); if (mMangaDetailFetchSubscription != null)
return;
mMangaDetailPublishSubject = PublishSubject.create(); mMangaDetailPublishSubject = PublishSubject.create();
@ -151,8 +157,10 @@ public class CataloguePresenter extends RxPresenter<CatalogueActivity> {
.filter(manga -> manga.initialized) .filter(manga -> manga.initialized)
.onBackpressureBuffer() .onBackpressureBuffer()
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.compose(deliverReplay()) .subscribe(manga -> {
.subscribe(this.split(CatalogueActivity::updateImage)); if (getView() != null)
getView().updateImage(manga);
});
add(mMangaDetailFetchSubscription); add(mMangaDetailFetchSubscription);
} }
@ -160,8 +168,9 @@ public class CataloguePresenter extends RxPresenter<CatalogueActivity> {
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) {
db.insertMangaBlock(networkManga); PutResult result = db.insertMangaBlock(networkManga);
localManga = db.getMangaBlock(networkManga.url); networkManga.id = result.insertedId();
localManga = networkManga;
} }
return localManga; return localManga;
} }
@ -172,8 +181,8 @@ public class CataloguePresenter extends RxPresenter<CatalogueActivity> {
} }
private void queryFromSearch(String query) { private void queryFromSearch(String query) {
// If search button clicked // If text didn't change
if (mSearchName.equals("") && query.equals("")) { if (mSearchName.equals(query)) {
return; return;
} }
// If going to search mode // If going to search mode
@ -186,12 +195,6 @@ public class CataloguePresenter extends RxPresenter<CatalogueActivity> {
} }
mSearchName = query; mSearchName = query;
if (getView() != null) {
if (mCurrentPage == 1)
getView().showProgressBar();
else
getView().showGridProgressBar();
}
restartRequest(); restartRequest();
} }

View File

@ -6,6 +6,7 @@ import android.os.Bundle;
import android.support.v7.widget.SearchView; import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.GridView; import android.widget.GridView;
import android.widget.ImageView; import android.widget.ImageView;
@ -45,6 +46,7 @@ public class CatalogueActivity extends BaseActivity<CataloguePresenter> {
private EasyAdapter<Manga> adapter; private EasyAdapter<Manga> adapter;
private EndlessScrollListener scroll_listener; private EndlessScrollListener scroll_listener;
private String search;
public final static String SOURCE_ID = "source_id"; public final static String SOURCE_ID = "source_id";
@ -68,7 +70,7 @@ public class CatalogueActivity extends BaseActivity<CataloguePresenter> {
int source_id = getIntent().getIntExtra(SOURCE_ID, -1); int source_id = getIntent().getIntExtra(SOURCE_ID, -1);
if (savedInstanceState == null) if (savedInstanceState == null)
getPresenter().initializeRequest(source_id); getPresenter().startRequesting(source_id);
} }
@Override @Override
@ -79,7 +81,8 @@ public class CatalogueActivity extends BaseActivity<CataloguePresenter> {
} }
private void initializeSearch(Menu menu) { private void initializeSearch(Menu menu) {
final SearchView sv = (SearchView) menu.findItem(R.id.action_search).getActionView(); MenuItem searchItem = menu.findItem(R.id.action_search);
final SearchView sv = (SearchView) searchItem.getActionView();
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() { sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override @Override
public boolean onQueryTextSubmit(String query) { public boolean onQueryTextSubmit(String query) {
@ -93,6 +96,11 @@ public class CatalogueActivity extends BaseActivity<CataloguePresenter> {
return true; return true;
} }
}); });
if (search != null && !search.equals("")) {
searchItem.expandActionView();
sv.setQuery(search, true);
sv.clearFocus();
}
} }
public EasyAdapter<Manga> getAdapter() { public EasyAdapter<Manga> getAdapter() {
@ -172,4 +180,7 @@ public class CatalogueActivity extends BaseActivity<CataloguePresenter> {
} }
} }
public void restoreSearch(String mSearchName) {
search = mSearchName;
}
} }