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

View file

@ -6,6 +6,7 @@ import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
@ -45,6 +46,7 @@ public class CatalogueActivity extends BaseActivity<CataloguePresenter> {
private EasyAdapter<Manga> adapter;
private EndlessScrollListener scroll_listener;
private String search;
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);
if (savedInstanceState == null)
getPresenter().initializeRequest(source_id);
getPresenter().startRequesting(source_id);
}
@Override
@ -79,7 +81,8 @@ public class CatalogueActivity extends BaseActivity<CataloguePresenter> {
}
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() {
@Override
public boolean onQueryTextSubmit(String query) {
@ -93,6 +96,11 @@ public class CatalogueActivity extends BaseActivity<CataloguePresenter> {
return true;
}
});
if (search != null && !search.equals("")) {
searchItem.expandActionView();
sv.setQuery(search, true);
sv.clearFocus();
}
}
public EasyAdapter<Manga> getAdapter() {
@ -172,4 +180,7 @@ public class CatalogueActivity extends BaseActivity<CataloguePresenter> {
}
}
public void restoreSearch(String mSearchName) {
search = mSearchName;
}
}