Skip to content

Commit

Permalink
Merge pull request #56 from SchibstedSpain/autocomplete
Browse files Browse the repository at this point in the history
Added Auto-complete feature for the search view (related to issue #55)
  • Loading branch information
ferranpons committed Jan 31, 2017
2 parents dbfdc8d + a16eca8 commit 5d88a84
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -29,7 +29,7 @@ android:
- tools
- tools
- platform-tools
- build-tools-25.0.1
- build-tools-25.0.2
- android-25
- extra
- extra-google-google_play_services
Expand Down
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -294,6 +294,12 @@ Returns a ***List`<`Address`>`*** for the text introduced.
* ***getFromLocationName(String query, LatLng lowerLeft, LatLng upperRight):***
Returns a ***List`<`Address`>`*** for the text and the Rectangle introduced.

* ***getDebouncedFromLocationName(String query, int debounceTime):***
Returns a ***List`<`Address`>`*** for the text introduced. Useful if you want to implement your own search view with auto-complete.

* ***getDebouncedFromLocationName(String query, LatLng lowerLeft, LatLng upperRight, int debounceTime):***
Returns a ***List`<`Address`>`*** for the text and the Rectangle introduced. Useful if you want to implement your own search view with auto-complete.

* ***getInfoFromLocation(double latitude, double longitude):***
Returns a ***List`<`Address`>`*** based on a latitude and a longitude.

Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Expand Up @@ -3,7 +3,7 @@ apply from: '../quality.gradle'

android {
compileSdkVersion 25
buildToolsVersion '25.0.1'
buildToolsVersion '25.0.2'

defaultConfig {
applicationId "com.schibsted.mappicker"
Expand All @@ -22,6 +22,6 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:25.1.0'
compile 'com.android.support:appcompat-v7:25.1.1'
compile project(':leku')
}
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0-alpha2'
classpath 'com.android.tools.build:gradle:2.2.3'

classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:3.1.1"
Expand Down
4 changes: 2 additions & 2 deletions leku/build.gradle
Expand Up @@ -8,7 +8,7 @@ version = '2.3.1'

android {
compileSdkVersion 25
buildToolsVersion '25.0.1'
buildToolsVersion '25.0.2'

defaultConfig {
minSdkVersion 15
Expand Down Expand Up @@ -41,7 +41,7 @@ retrolambda {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])

def supportVersion = '25.1.0'
def supportVersion = '25.1.1'
compile "com.android.support:appcompat-v7:$supportVersion"
compile "com.android.support:design:$supportVersion"

Expand Down
Expand Up @@ -77,6 +77,7 @@ public class LocationPickerActivity extends AppCompatActivity
private static final int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
private static final int DEFAULT_ZOOM = 16;
private static final int WIDER_ZOOM = 6;
private static final int MIN_CHARACTERS = 2;

private GoogleMap map;
private GoogleApiClient googleApiClient;
Expand Down Expand Up @@ -190,7 +191,7 @@ public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2)
}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
public void onTextChanged(CharSequence charSequence, int start, int count, int after) {
if ("".equals(charSequence.toString())) {
adapter.clear();
adapter.notifyDataSetChanged();
Expand All @@ -202,6 +203,9 @@ public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
searchOption.setIcon(R.drawable.ic_mic);
}
} else {
if (charSequence.length() > MIN_CHARACTERS && after > count) {
retrieveLocationFrom(charSequence.toString(), 400);
}
if (clearSearchButton != null) {
clearSearchButton.setVisibility(View.VISIBLE);
}
Expand Down Expand Up @@ -444,6 +448,17 @@ public void showLocations(List<Address> addresses) {
}
}

@Override
public void showDebouncedLocations(List<Address> addresses) {
if (addresses != null) {
fillLocationList(addresses);
if (!addresses.isEmpty()) {
updateLocationNameList(addresses);
adapter.notifyDataSetChanged();
}
}
}

@Override
public void didLoadLocation() {
progressBar.setVisibility(View.GONE);
Expand Down Expand Up @@ -690,6 +705,14 @@ private void retrieveLocationFrom(String query) {
}
}

private void retrieveLocationFrom(String query, int debounceTime) {
if (searchZone != null && !searchZone.isEmpty()) {
retrieveDebouncedLocationFromZone(query, searchZone, debounceTime);
} else {
retrieveDebouncedLocationFromDefaultZone(query, debounceTime);
}
}

private void retrieveLocationFromDefaultZone(String query) {
if (CountryLocaleRect.getDefaultLowerLeft() != null) {
geocoderPresenter.getFromLocationName(query, CountryLocaleRect.getDefaultLowerLeft(),
Expand All @@ -709,6 +732,25 @@ private void retrieveLocationFromZone(String query, String zoneKey) {
}
}

private void retrieveDebouncedLocationFromDefaultZone(String query, int debounceTime) {
if (CountryLocaleRect.getDefaultLowerLeft() != null) {
geocoderPresenter.getDebouncedFromLocationName(query, CountryLocaleRect.getDefaultLowerLeft(),
CountryLocaleRect.getDefaultUpperRight(), debounceTime);
} else {
geocoderPresenter.getDebouncedFromLocationName(query, debounceTime);
}
}

private void retrieveDebouncedLocationFromZone(String query, String zoneKey, int debounceTime) {
Locale locale = new Locale(zoneKey);
if (CountryLocaleRect.getLowerLeftFromZone(locale) != null) {
geocoderPresenter.getDebouncedFromLocationName(query, CountryLocaleRect.getLowerLeftFromZone(locale),
CountryLocaleRect.getUpperRightFromZone(locale), debounceTime);
} else {
geocoderPresenter.getDebouncedFromLocationName(query, debounceTime);
}
}

private void returnCurrentPosition() {
if (currentLocation != null) {
Intent returnIntent = new Intent();
Expand Down
@@ -1,19 +1,19 @@
package com.schibstedspain.leku.geocoder;

import com.google.android.gms.maps.model.LatLng;
import java.util.concurrent.TimeUnit;
import pl.charmas.android.reactivelocation.ReactiveLocationProvider;
import rx.Scheduler;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

public class GeocoderPresenter {
private final GeocoderInteractorInterface interactor;
private GeocoderViewInterface view;
private final GeocoderViewInterface nullView = new GeocoderViewInterface.NullView();
private Subscription locationSubscription;
private Subscription locationNameSubscription;
private Subscription lastKnownLocationSubscription;
private CompositeSubscription compositeSubscription;
private final Scheduler scheduler;
private ReactiveLocationProvider locationProvider;

Expand All @@ -27,6 +27,7 @@ public GeocoderPresenter(ReactiveLocationProvider reactiveLocationProvider, Geoc
this.scheduler = scheduler;
this.locationProvider = reactiveLocationProvider;
this.interactor = interactor;
this.compositeSubscription = new CompositeSubscription();
}

public void setUI(GeocoderViewInterface geocoderViewInterface) {
Expand All @@ -35,47 +36,67 @@ public void setUI(GeocoderViewInterface geocoderViewInterface) {

public void stop() {
this.view = nullView;
if (lastKnownLocationSubscription != null) {
lastKnownLocationSubscription.unsubscribe();
}
if (locationNameSubscription != null) {
locationNameSubscription.unsubscribe();
}
if (locationSubscription != null) {
locationSubscription.unsubscribe();
if (compositeSubscription != null) {
compositeSubscription.clear();
}
}

public void getLastKnownLocation() {
lastKnownLocationSubscription =
Subscription lastKnownLocationSubscription =
locationProvider.getLastKnownLocation().subscribe(view::showLastLocation, throwable -> {
}, view::didGetLastLocation);
compositeSubscription.add(lastKnownLocationSubscription);
}

public void getFromLocationName(String query) {
view.willLoadLocation();
locationNameSubscription = interactor.getFromLocationName(query)
Subscription locationNameSubscription = interactor.getFromLocationName(query)
.subscribeOn(Schedulers.newThread())
.observeOn(scheduler)
.subscribe(view::showLocations, throwable -> view.showLoadLocationError(),
view::didLoadLocation);
compositeSubscription.add(locationNameSubscription);
}

public void getFromLocationName(String query, LatLng lowerLeft, LatLng upperRight) {
view.willLoadLocation();
locationNameSubscription = interactor.getFromLocationName(query, lowerLeft, upperRight)
Subscription locationNameSubscription = interactor.getFromLocationName(query, lowerLeft, upperRight)
.subscribeOn(Schedulers.newThread())
.observeOn(scheduler)
.subscribe(view::showLocations, throwable -> view.showLoadLocationError(),
view::didLoadLocation);
compositeSubscription.add(locationNameSubscription);
}

public void getDebouncedFromLocationName(String query, int debounceTime) {
view.willLoadLocation();
Subscription locationNameDebounceSubscription = interactor.getFromLocationName(query)
.debounce(debounceTime, TimeUnit.MILLISECONDS)
.subscribeOn(Schedulers.newThread())
.observeOn(scheduler)
.subscribe(view::showDebouncedLocations, throwable -> view.showLoadLocationError(),
view::didLoadLocation);
compositeSubscription.add(locationNameDebounceSubscription);
}

public void getDebouncedFromLocationName(String query, LatLng lowerLeft, LatLng upperRight, int debounceTime) {
view.willLoadLocation();
Subscription locationNameDebounceSubscription = interactor.getFromLocationName(query, lowerLeft, upperRight)
.debounce(debounceTime, TimeUnit.MILLISECONDS)
.subscribeOn(Schedulers.newThread())
.observeOn(scheduler)
.subscribe(view::showDebouncedLocations, throwable -> view.showLoadLocationError(),
view::didLoadLocation);
compositeSubscription.add(locationNameDebounceSubscription);
}

public void getInfoFromLocation(double latitude, double longitude) {
view.willGetLocationInfo();
locationSubscription = interactor.getFromLocation(latitude, longitude)
Subscription locationSubscription = interactor.getFromLocation(latitude, longitude)
.subscribeOn(Schedulers.newThread())
.observeOn(scheduler)
.subscribe(view::showLocationInfo, throwable -> view.showGetLocationInfoError(),
view::didGetLocationInfo);
compositeSubscription.add(locationSubscription);
}
}
Expand Up @@ -7,7 +7,9 @@
public interface GeocoderViewInterface {
void willLoadLocation();

void showLocations(List<Address> adresses);
void showLocations(List<Address> addresses);

void showDebouncedLocations(List<Address> addresses);

void didLoadLocation();

Expand All @@ -33,7 +35,12 @@ public void willLoadLocation() {
}

@Override
public void showLocations(List<Address> adresses) {
public void showLocations(List<Address> addresses) {

}

@Override
public void showDebouncedLocations(List<Address> addresses) {

}

Expand Down

0 comments on commit 5d88a84

Please sign in to comment.