diff --git a/app/src/androidTest/java/com/mapbox/mapboxsdk/plugins/places/autocomplete/ui/PlaceAutocompleteFragmentTest.java b/app/src/androidTest/java/com/mapbox/mapboxsdk/plugins/places/autocomplete/ui/PlaceAutocompleteFragmentTest.java index cc1d63509..948e627d5 100644 --- a/app/src/androidTest/java/com/mapbox/mapboxsdk/plugins/places/autocomplete/ui/PlaceAutocompleteFragmentTest.java +++ b/app/src/androidTest/java/com/mapbox/mapboxsdk/plugins/places/autocomplete/ui/PlaceAutocompleteFragmentTest.java @@ -28,6 +28,7 @@ import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.not; import static org.junit.Assert.assertThat; +import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -36,6 +37,7 @@ @Ignore public class PlaceAutocompleteFragmentTest { private static final String ACCESS_TOKEN = "pk.XXX"; + private static final Integer HISTORY_COUNT = 5; private PlaceAutocompleteFragment placeAutocompleteFragment; @@ -53,6 +55,7 @@ public void init() { // For style test builder.backgroundColor(Color.BLUE); + builder.historyCount(HISTORY_COUNT); builder.hint("foobar"); placeAutocompleteFragment = PlaceAutocompleteFragment.newInstance( @@ -68,6 +71,11 @@ public void newInstance_doesPutAccessTokenInBundle() throws Exception { assertThat(placeAutocompleteFragment.getAccessToken(), equalTo(ACCESS_TOKEN)); } + @Test + public void styleView_setHistoryCountIsCorrect() throws Exception { + assertThat(placeAutocompleteFragment.getHistoryCount(), equalTo(HISTORY_COUNT)); + } + @Test public void onCreateView_doesInflateCorrectModeView() throws Exception { onView(withId(R.id.cardView)).check(matches(isDisplayed())); diff --git a/plugin-places/src/main/java/com/mapbox/mapboxsdk/plugins/places/autocomplete/model/PlaceOptions.java b/plugin-places/src/main/java/com/mapbox/mapboxsdk/plugins/places/autocomplete/model/PlaceOptions.java index 534f97f56..8e1fd7690 100644 --- a/plugin-places/src/main/java/com/mapbox/mapboxsdk/plugins/places/autocomplete/model/PlaceOptions.java +++ b/plugin-places/src/main/java/com/mapbox/mapboxsdk/plugins/places/autocomplete/model/PlaceOptions.java @@ -84,6 +84,16 @@ public abstract class PlaceOptions implements Parcelable { */ public abstract int limit(); + /** + * Limit the number of results returned. The default is the maximum number of + * historical searches already saved by the Places plugin. + * + * @return the number of past search results returned by the geocoder + * @since 0.9.0 + */ + @Nullable + public abstract Integer historyCount(); + /** * Limit results to a bounding box. Options are in the format {@code minX,minY,maxX,maxY}. * @@ -245,6 +255,15 @@ public Builder geocodingTypes(@NonNull @GeocodingTypeCriteria String... geocodin */ public abstract Builder limit(@IntRange(from = 1, to = 10) int limit); + /** + * Limit the number of past search results shown. + * + * @param historyCount the number of past historical searches shown before a search starts. + * @return this builder instance for chaining options together + * @since 0.9.0 + */ + public abstract Builder historyCount(@Nullable @IntRange(from = 0) Integer historyCount); + /** * Limit results to a bounding box. * diff --git a/plugin-places/src/main/java/com/mapbox/mapboxsdk/plugins/places/autocomplete/ui/PlaceAutocompleteFragment.java b/plugin-places/src/main/java/com/mapbox/mapboxsdk/plugins/places/autocomplete/ui/PlaceAutocompleteFragment.java index 6149702eb..eb3520211 100644 --- a/plugin-places/src/main/java/com/mapbox/mapboxsdk/plugins/places/autocomplete/ui/PlaceAutocompleteFragment.java +++ b/plugin-places/src/main/java/com/mapbox/mapboxsdk/plugins/places/autocomplete/ui/PlaceAutocompleteFragment.java @@ -44,6 +44,7 @@ public class PlaceAutocompleteFragment extends Fragment implements ResultClickCa private SearchView searchView; private View dropShadowView; private String accessToken; + private Integer historyCount; private View rootView; private int mode; @@ -204,8 +205,15 @@ private void bindViews() { void updateSearchHistoryView(@Nullable List searchHistoryEntities) { searchHistoryView.getResultsList().clear(); if (searchHistoryEntities != null) { - for (SearchHistoryEntity entity : searchHistoryEntities) { - searchHistoryView.getResultsList().add(entity.getCarmenFeature()); + if (placeOptions.historyCount() != null) { + historyCount = placeOptions.historyCount(); + for (int x = 0; x < historyCount; x++) { + searchHistoryView.getResultsList().add(searchHistoryEntities.get(x).getCarmenFeature()); + } + } else { + for (SearchHistoryEntity entity : searchHistoryEntities) { + searchHistoryView.getResultsList().add(entity.getCarmenFeature()); + } } } searchHistoryView.notifyDataSetChanged(); @@ -269,4 +277,8 @@ public void onChanged(@Nullable List searchHistoryEntities) public String getAccessToken() { return accessToken; } + + public Integer getHistoryCount() { + return historyCount; + } }