Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added cancel method to autocomplete widget #226

Merged
merged 2 commits into from
Dec 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ protected void onSaveInstanceState(Bundle outState) {
@Override
protected void onDestroy() {
super.onDestroy();
autocomplete.cancelApiCall();
mapView.onDestroy();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mapbox.services.android.geocoder.ui;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -19,6 +20,7 @@
import java.io.IOException;
import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

/**
Expand All @@ -37,6 +39,7 @@ public class GeocoderAdapter extends BaseAdapter implements Filterable {
private double[] bbox;
private Position position;
private int limit;
private Call call;

private GeocoderFilter geocoderFilter;

Expand Down Expand Up @@ -225,6 +228,18 @@ public void setLimit(int limit) {
this.limit = limit;
}

/**
* Can be used to cancel any calls currently in progress. It's a good idea to include in onDestroy() to prevent
* memory leaks
*
* @since 2.0.0
*/
public void cancelApiCall() {
if (call != null) {
call.cancel();
}
}

/*
* Required by BaseAdapter
*/
Expand Down Expand Up @@ -366,6 +381,8 @@ protected FilterResults performFiltering(CharSequence constraint) {
builder.setLimit(limit);
}

call = builder.build().getCall();

// Do request
response = builder.build().executeCall();
} catch (IOException ioException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ public boolean onTouch(View view, MotionEvent event) {
});
}

/**
* Can be used to cancel any calls currently in progress. It's a good idea to include in onDestroy() to prevent
* memory leaks
*
* @since 2.0.0
*/
public void cancelApiCall() {
adapter.cancelApiCall();
}

/*
* Setters
*/
Expand Down