From 85d34ab9e5ec58dbf02578ad8ee14b3f7753fbd0 Mon Sep 17 00:00:00 2001 From: inorichi Date: Mon, 19 Oct 2015 04:15:30 +0200 Subject: [PATCH] Search survive screen rotation --- .../presenter/CataloguePresenter.java | 49 ++++++++++--------- .../ui/activity/CatalogueActivity.java | 15 +++++- 2 files changed, 39 insertions(+), 25 deletions(-) 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 86b4afbb33..1adeb03671 100644 --- a/app/src/main/java/eu/kanade/mangafeed/presenter/CataloguePresenter.java +++ b/app/src/main/java/eu/kanade/mangafeed/presenter/CataloguePresenter.java @@ -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 { +public class CataloguePresenter extends BasePresenter { @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 { 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 { mCurrentPage = 1; pager = new RxPager(); start(GET_MANGA_LIST); + if (getView() != null) + getView().showProgressBar(); } private Observable> getMangaObs(int page) { @@ -102,12 +107,12 @@ public class CataloguePresenter extends RxPresenter { 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 { } private void initializeMangaDetailsLoader() { - remove(mMangaDetailFetchSubscription); + if (mMangaDetailFetchSubscription != null) + return; mMangaDetailPublishSubject = PublishSubject.create(); @@ -151,8 +157,10 @@ public class CataloguePresenter extends RxPresenter { .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 { 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 { } 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 { } mSearchName = query; - if (getView() != null) { - if (mCurrentPage == 1) - getView().showProgressBar(); - else - getView().showGridProgressBar(); - } restartRequest(); } 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 067de0e438..a46899e0b2 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 @@ -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 { private EasyAdapter 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 { 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 { } 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 { return true; } }); + if (search != null && !search.equals("")) { + searchItem.expandActionView(); + sv.setQuery(search, true); + sv.clearFocus(); + } } public EasyAdapter getAdapter() { @@ -172,4 +180,7 @@ public class CatalogueActivity extends BaseActivity { } } + public void restoreSearch(String mSearchName) { + search = mSearchName; + } }