Skip to content

Commit

Permalink
Allow custom behavior when search clicked (#66)
Browse files Browse the repository at this point in the history
* Allow custome behavior when search clicked

* Checkstyle
  • Loading branch information
sarahsnow1 authored and ecgreb committed Jan 20, 2017
1 parent 15b0b9b commit 230fb58
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 39 deletions.
10 changes: 10 additions & 0 deletions app/src/main/java/com/mapzen/sample/pelias/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.mapzen.pelias.widget.AutoCompleteAdapter;
import com.mapzen.pelias.widget.AutoCompleteListView;
import com.mapzen.pelias.widget.PeliasSearchView;
import com.mapzen.pelias.widget.SearchSubmitListener;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
Expand Down Expand Up @@ -96,5 +97,14 @@ private void setupPeliasSearchView() {
return "wof,osm,oa,gn";
}
});
searchView.setSearchSubmitListener(new SearchSubmitListener() {
@Override public boolean searchOnSearchKeySubmit() {
return false;
}

@Override public boolean hideAutocompleteOnSearchSubmit() {
return false;
}
});
}
}
127 changes: 98 additions & 29 deletions lib/src/main/java/com/mapzen/pelias/widget/PeliasSearchView.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public void run() {
final InputMethodManager imm =
(InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
editText.setCursorVisible(true);
HIDDEN_METHOD_INVOKER.showSoftInputUnchecked(imm, PeliasSearchView.this, 0);
}
}
Expand All @@ -59,6 +60,7 @@ public void run() {
final InputMethodManager imm =
(InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
editText.setCursorVisible(false);
imm.hideSoftInputFromWindow(getWindowToken(), 0);
}
}
Expand All @@ -70,6 +72,7 @@ public void run() {
}
};

private EditText editText;
private ListView autoCompleteListView;
private SavedSearch savedSearch;
private Pelias pelias;
Expand All @@ -86,6 +89,7 @@ public void run() {
private boolean cacheSearchResults = true;
private boolean autoKeyboardShow = true;
private SuggestFilter suggestFilter;
private boolean checkHideAutocompleteList = false;

private Callback<Result> suggestCallback = new Callback<Result>() {
@Override public void onResponse(Call<Result> call, Response<Result> response) {
Expand Down Expand Up @@ -114,6 +118,8 @@ public void run() {
}
};

private SearchSubmitListener searchSubmitListener;

/**
* Constructs a new search view given a context.
*/
Expand All @@ -135,6 +141,11 @@ private void setup() {
disableDefaultSoftKeyboardBehaviour();
setOnQueryTextListener(this);
setImeOptions(EditorInfo.IME_ACTION_SEARCH);
setupEditText();
}

private void setupEditText() {
editText = (EditText) findViewById(R.id.search_src_text);
}

/**
Expand All @@ -152,31 +163,7 @@ public void setAutoCompleteListView(final ListView listView) {
autoCompleteListView = listView;
setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
@Override public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
final Animation slideIn = loadAnimation(getContext(), R.anim.slide_in);
setAutoCompleteAdapterIcon(recentSearchIconResourceId);
loadSavedSearches();
listView.setVisibility(VISIBLE);
listView.setAnimation(slideIn);
if (autoKeyboardShow) {
postDelayed(showImeRunnable, 300);
}
setOnQueryTextListener(PeliasSearchView.this);
} else {
final Animation slideOut = loadAnimation(getContext(), R.anim.slide_out);
listView.setVisibility(GONE);
listView.setAnimation(slideOut);
postDelayed(hideImeRunnable, 300);
setOnQueryTextListener(null);
}

// Notify secondary listener
if (onPeliasFocusChangeListener != null) {
onPeliasFocusChangeListener.onFocusChange(view, hasFocus);
}

focusedViewHasFocus = hasFocus;
postDelayed(backPressedRunnable, 300);
PeliasSearchView.this.onFocusChange(view, hasFocus);
}
});

Expand All @@ -198,6 +185,63 @@ public void enableAutoKeyboardShow() {
autoKeyboardShow = true;
}

public void setSearchSubmitListener(SearchSubmitListener listener) {
searchSubmitListener = listener;
}

private void handleSearchGainingFocus() {
setAutoCompleteAdapterIcon(recentSearchIconResourceId);
loadSavedSearches();
safeShowAutocompleteList();
setOnQueryTextListener(PeliasSearchView.this);
}

private void handleSearchLosingFocus() {
safeHideAutocompleteList();
postDelayed(hideImeRunnable, 300);
setOnQueryTextListener(null);
}

private void safeShowAutocompleteList() {
if (autoCompleteListView == null) {
return;
}
if (autoCompleteListView.getVisibility() != VISIBLE) {
final Animation slideIn = loadAnimation(getContext(), R.anim.slide_in);
autoCompleteListView.setVisibility(VISIBLE);
autoCompleteListView.setAnimation(slideIn);
}
if (autoKeyboardShow) {
postDelayed(showImeRunnable, 300);
}
}

/**
* Checks whether autocomplete list should be hidden and if so, hides it.
*/
private void safeHideAutocompleteList() {
if (checkHideAutocompleteList) {
checkHideAutocompleteList = false;
if (searchSubmitListener.hideAutocompleteOnSearchSubmit()) {
hideAutocompleteList();
}
} else {
hideAutocompleteList();
}
}

/**
* Hides the autocomplete list with animation.
*/
private void hideAutocompleteList() {
if (autoCompleteListView == null) {
return;
}
final Animation slideOut = loadAnimation(getContext(), R.anim.slide_out);
autoCompleteListView.setVisibility(GONE);
autoCompleteListView.setAnimation(slideOut);
}

/**
* Overrides default behavior for showing soft keyboard. Enables manual control by this class.
*/
Expand All @@ -219,16 +263,21 @@ private void disableDefaultSoftKeyboardBehaviour() {

@Override public boolean onQueryTextSubmit(String query) {
if (pelias != null) {
pelias.search(query, callback);
if (searchSubmitListener == null || searchSubmitListener.searchOnSearchKeySubmit()) {
pelias.search(query, callback);
}
}

storeSavedSearch(query, null);

if (onSubmitListener != null) {
onSubmitListener.onSubmit();
}
if (searchSubmitListener != null) {
checkHideAutocompleteList = true;
}
textSubmitted = true;
clearFocus();
onFocusChange(this, false);
resetCursorPosition();
return false;
}
Expand Down Expand Up @@ -381,7 +430,6 @@ public interface OnSubmitListener {
}

private void resetCursorPosition() {
EditText editText = (EditText) findViewById(R.id.search_src_text);
if (editText != null) {
editText.setSelection(0);
}
Expand Down Expand Up @@ -423,7 +471,11 @@ public AdapterView.OnItemClickListener invoke() {
} else {
final Result result = new Result();
final ArrayList<Feature> features = new ArrayList<>(1);
clearFocus();
if (hasFocus()) {
clearFocus();
} else {
onFocusChange(PeliasSearchView.this, false);
}
setQuery(item.getText(), false);
resetCursorPosition();
features.add(item.getSimpleFeature().toFeature());
Expand All @@ -439,6 +491,22 @@ public AdapterView.OnItemClickListener invoke() {
}
}

private void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
handleSearchGainingFocus();
} else {
handleSearchLosingFocus();
}

// Notify secondary listener
if (onPeliasFocusChangeListener != null) {
onPeliasFocusChangeListener.onFocusChange(view, hasFocus);
}
focusedViewHasFocus = hasFocus;

postDelayed(backPressedRunnable, 300);
}

/**
* Listener to simulate when the SearchBar gains focus and then loses it when the user presses
* back without executing a search or clicking on an item in the autocomplete list view.
Expand Down Expand Up @@ -535,4 +603,5 @@ private void updateSavedSearch() {
Callback<Result> getSuggestCallback() {
return suggestCallback;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.mapzen.pelias.widget;

/**
* Interface to override what happens when the search key is pressed.
*/
public interface SearchSubmitListener {
/**
* Return true to have the {@link PeliasSearchView} execute a search when the search key is
* pressed, false to do no search.
* @return
*/
boolean searchOnSearchKeySubmit();

/**
* Return true to have the {@link PeliasSearchView} hide the autocomplete list view when the
* search key is pressed, false to have it remain visible.
* @return
*/
boolean hideAutocompleteOnSearchSubmit();

}
Loading

0 comments on commit 230fb58

Please sign in to comment.