Skip to content

Commit

Permalink
Correctly determine whether to search initially.
Browse files Browse the repository at this point in the history
We can't simply compare against the initial search argument for that as
we want different behaviors: searches triggered via URL should start
immediately, searches triggered via repo activity should not.
  • Loading branch information
maniac103 committed Mar 12, 2019
1 parent 34622e6 commit 471e599
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 12 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/com/gh4a/activities/RepositoryActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ public boolean onOptionsItemSelected(MenuItem item) {
return true;
case R.id.search:
String initialSearch = "repo:" + mRepoOwner + "/" + mRepoName + " ";
startActivity(SearchActivity.makeIntent(this,
initialSearch, SearchActivity.SEARCH_TYPE_CODE));
startActivity(SearchActivity.makeIntent(this, initialSearch,
SearchActivity.SEARCH_TYPE_CODE, false));
return true;
case R.id.bookmark:
String bookmarkUrl = getBookmarkUrl();
Expand Down
9 changes: 6 additions & 3 deletions app/src/main/java/com/gh4a/activities/SearchActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ public class SearchActivity extends FragmentContainerActivity {
public static final int SEARCH_TYPE_USER = SearchFragment.SEARCH_TYPE_USER;
public static final int SEARCH_TYPE_CODE = SearchFragment.SEARCH_TYPE_CODE;

public static Intent makeIntent(Context context, String initialSearch, int searchType) {
public static Intent makeIntent(Context context, String initialSearch,
int searchType, boolean startSearchImmediately) {
return makeIntent(context)
.putExtra("initial_search", initialSearch)
.putExtra("search_type", searchType);
.putExtra("search_type", searchType)
.putExtra("search_immediately", startSearchImmediately);
}

public static Intent makeIntent(Context context) {
Expand All @@ -49,7 +51,8 @@ protected Fragment onCreateFragment() {
Intent intent = getIntent();
int searchType = intent.getIntExtra("search_type", SEARCH_TYPE_REPO);
String initialQuery = intent.getStringExtra("initial_search");
boolean startSearchImmediately = intent.getBooleanExtra("search_immediately", true);

return SearchFragment.newInstance(searchType, initialQuery);
return SearchFragment.newInstance(searchType, initialQuery, startSearchImmediately);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ protected int[] getTabTitleResIds() {

@Override
protected Fragment makeFragment(int position) {
return SearchFragment.newInstance(SearchFragment.SEARCH_TYPE_REPO, null);
return SearchFragment.newInstance(SearchFragment.SEARCH_TYPE_REPO, null, false);
}
}
11 changes: 8 additions & 3 deletions app/src/main/java/com/gh4a/fragment/PagedDataBaseFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ public abstract class PagedDataBaseFragment<T> extends LoadingListFragmentBase i
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

setContentShown(false);

mRxLoader = new RxLoader(getActivity(), getLoaderManager());
resetSubject();
load(false);
if (shouldDoInitialLoad()) {
setContentShown(false);
load(false);
}
}

@Override
Expand Down Expand Up @@ -103,6 +104,10 @@ protected void resetSubject() {
mPageSubject.onNext(1);
}

protected boolean shouldDoInitialLoad() {
return true;
}

private void load(boolean force) {
mSubscription = mPageSubject
.flatMap(page -> loadPage(page, force)
Expand Down
11 changes: 9 additions & 2 deletions app/src/main/java/com/gh4a/fragment/SearchFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ public class SearchFragment extends PagedDataBaseFragment<Object> implements
SearchView.OnQueryTextListener, SearchView.OnCloseListener,
SearchView.OnSuggestionListener, FilterQueryProvider,
AdapterView.OnItemSelectedListener, SearchAdapter.Callback {
public static SearchFragment newInstance(int initialType, String initialQuery) {
public static SearchFragment newInstance(int initialType, String initialQuery,
boolean startSearchImmediately) {
SearchFragment f = new SearchFragment();
Bundle args = new Bundle();
args.putInt("search_type", initialType);
args.putString("initial_search", initialQuery);
args.putBoolean("do_initial_load", startSearchImmediately);
f.setArguments(args);
return f;
}
Expand Down Expand Up @@ -149,9 +151,14 @@ public void onSaveInstanceState(Bundle outState) {
return mAdapter;
}

@Override
protected boolean shouldDoInitialLoad() {
return getArguments().getBoolean("do_initial_load", true);
}

@Override
protected Single<Response<Page<Object>>> loadPage(int page, boolean bypassCache) {
if (TextUtils.isEmpty(mQuery) || mQuery.equals(getArguments().getString("initial_search"))) {
if (TextUtils.isEmpty(mQuery)) {
return Single.just(Response.success(new ApiHelpers.DummyPage<>()));
}
switch (mSelectedSearchType) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/gh4a/resolver/LinkParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ private static ParseResult parseSearchLink(FragmentActivity activity, @NonNull U
}
}
String query = uri.getQueryParameter("q");
return new ParseResult(SearchActivity.makeIntent(activity, query, typeInt));
return new ParseResult(SearchActivity.makeIntent(activity, query, typeInt, true));
}

@NonNull
Expand Down

0 comments on commit 471e599

Please sign in to comment.