mirror of https://github.com/tasks/tasks
Convert place search to coroutines
parent
f0f6478dab
commit
14f5015fac
@ -1,87 +0,0 @@
|
|||||||
package org.tasks.location;
|
|
||||||
|
|
||||||
import static org.tasks.data.Place.newPlace;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.os.Bundle;
|
|
||||||
import androidx.annotation.Nullable;
|
|
||||||
import com.mapbox.api.geocoding.v5.MapboxGeocoding;
|
|
||||||
import com.mapbox.api.geocoding.v5.models.CarmenFeature;
|
|
||||||
import com.mapbox.api.geocoding.v5.models.GeocodingResponse;
|
|
||||||
import com.mapbox.geojson.Point;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import org.tasks.Callback;
|
|
||||||
import org.tasks.R;
|
|
||||||
import org.tasks.data.Place;
|
|
||||||
import retrofit2.Call;
|
|
||||||
import retrofit2.Response;
|
|
||||||
|
|
||||||
public class MapboxSearchProvider implements PlaceSearchProvider {
|
|
||||||
|
|
||||||
private final Context context;
|
|
||||||
private MapboxGeocoding.Builder builder;
|
|
||||||
|
|
||||||
public MapboxSearchProvider(Context context) {
|
|
||||||
this.context = context;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void restoreState(Bundle savedInstanceState) {}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void saveState(Bundle outState) {}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getAttributionRes(boolean dark) {
|
|
||||||
return R.drawable.mapbox_logo_icon;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void search(
|
|
||||||
String query,
|
|
||||||
@Nullable MapPosition bias,
|
|
||||||
Callback<List<PlaceSearchResult>> onSuccess,
|
|
||||||
Callback<String> onError) {
|
|
||||||
if (builder == null) {
|
|
||||||
String token = context.getString(R.string.mapbox_key);
|
|
||||||
builder = MapboxGeocoding.builder().autocomplete(true).accessToken(token);
|
|
||||||
if (bias != null) {
|
|
||||||
builder.proximity(Point.fromLngLat(bias.getLongitude(), bias.getLatitude()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
builder
|
|
||||||
.query(query)
|
|
||||||
.build()
|
|
||||||
.enqueueCall(
|
|
||||||
new retrofit2.Callback<GeocodingResponse>() {
|
|
||||||
@Override
|
|
||||||
public void onResponse(
|
|
||||||
Call<GeocodingResponse> call, Response<GeocodingResponse> response) {
|
|
||||||
List<PlaceSearchResult> results = new ArrayList<>();
|
|
||||||
results.clear();
|
|
||||||
for (CarmenFeature feature : response.body().features()) {
|
|
||||||
results.add(toSearchResult(feature));
|
|
||||||
}
|
|
||||||
onSuccess.call(results);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onFailure(Call<GeocodingResponse> call, Throwable t) {
|
|
||||||
onError.call(t.getMessage());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void fetch(
|
|
||||||
PlaceSearchResult placeSearchResult, Callback<Place> onSuccess, Callback<String> onError) {
|
|
||||||
onSuccess.call(placeSearchResult.getPlace());
|
|
||||||
}
|
|
||||||
|
|
||||||
private PlaceSearchResult toSearchResult(CarmenFeature feature) {
|
|
||||||
Place place = newPlace(feature);
|
|
||||||
return new PlaceSearchResult(feature.id(), place.getName(), place.getDisplayAddress(), place);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -0,0 +1,67 @@
|
|||||||
|
package org.tasks.location
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.os.Bundle
|
||||||
|
import com.mapbox.api.geocoding.v5.MapboxGeocoding
|
||||||
|
import com.mapbox.api.geocoding.v5.models.CarmenFeature
|
||||||
|
import com.mapbox.api.geocoding.v5.models.GeocodingResponse
|
||||||
|
import com.mapbox.geojson.Point
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
import org.tasks.R
|
||||||
|
import org.tasks.data.Place
|
||||||
|
import org.tasks.data.Place.Companion.newPlace
|
||||||
|
import retrofit2.Call
|
||||||
|
import retrofit2.Response
|
||||||
|
import java.util.*
|
||||||
|
import kotlin.coroutines.suspendCoroutine
|
||||||
|
|
||||||
|
class MapboxSearchProvider(private val context: Context) : PlaceSearchProvider {
|
||||||
|
private var builder: MapboxGeocoding.Builder? = null
|
||||||
|
|
||||||
|
override fun restoreState(savedInstanceState: Bundle?) {}
|
||||||
|
|
||||||
|
override fun saveState(outState: Bundle) {}
|
||||||
|
|
||||||
|
override fun getAttributionRes(dark: Boolean) = R.drawable.mapbox_logo_icon
|
||||||
|
|
||||||
|
override suspend fun search(query: String, bias: MapPosition?): List<PlaceSearchResult> =
|
||||||
|
withContext(Dispatchers.IO) {
|
||||||
|
suspendCoroutine { cont ->
|
||||||
|
if (builder == null) {
|
||||||
|
val token = context.getString(R.string.mapbox_key)
|
||||||
|
builder = MapboxGeocoding.builder().autocomplete(true).accessToken(token)
|
||||||
|
if (bias != null) {
|
||||||
|
builder?.proximity(Point.fromLngLat(bias.longitude, bias.latitude))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
builder
|
||||||
|
?.query(query)
|
||||||
|
?.build()
|
||||||
|
?.enqueueCall(
|
||||||
|
object : retrofit2.Callback<GeocodingResponse> {
|
||||||
|
override fun onResponse(
|
||||||
|
call: Call<GeocodingResponse>, response: Response<GeocodingResponse>) {
|
||||||
|
val results: MutableList<PlaceSearchResult> = ArrayList()
|
||||||
|
results.clear()
|
||||||
|
for (feature in response.body()!!.features()) {
|
||||||
|
results.add(toSearchResult(feature))
|
||||||
|
}
|
||||||
|
cont.resumeWith(Result.success(results))
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onFailure(call: Call<GeocodingResponse>, t: Throwable) {
|
||||||
|
cont.resumeWith(Result.failure(t))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun fetch(placeSearchResult: PlaceSearchResult): Place =
|
||||||
|
placeSearchResult.place
|
||||||
|
|
||||||
|
private fun toSearchResult(feature: CarmenFeature): PlaceSearchResult {
|
||||||
|
val place = newPlace(feature)
|
||||||
|
return PlaceSearchResult(feature.id(), place.name, place.displayAddress, place)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,25 +0,0 @@
|
|||||||
package org.tasks.location;
|
|
||||||
|
|
||||||
import android.os.Bundle;
|
|
||||||
import androidx.annotation.DrawableRes;
|
|
||||||
import androidx.annotation.Nullable;
|
|
||||||
import java.util.List;
|
|
||||||
import org.tasks.Callback;
|
|
||||||
import org.tasks.data.Place;
|
|
||||||
|
|
||||||
public interface PlaceSearchProvider {
|
|
||||||
void restoreState(Bundle savedInstanceState);
|
|
||||||
|
|
||||||
void saveState(Bundle outState);
|
|
||||||
|
|
||||||
@DrawableRes int getAttributionRes(boolean dark);
|
|
||||||
|
|
||||||
void search(
|
|
||||||
String query,
|
|
||||||
@Nullable MapPosition bias,
|
|
||||||
Callback<List<PlaceSearchResult>> onSuccess,
|
|
||||||
Callback<String> onError);
|
|
||||||
|
|
||||||
void fetch(
|
|
||||||
PlaceSearchResult placeSearchResult, Callback<Place> onSuccess, Callback<String> onError);
|
|
||||||
}
|
|
||||||
@ -0,0 +1,19 @@
|
|||||||
|
package org.tasks.location
|
||||||
|
|
||||||
|
import android.os.Bundle
|
||||||
|
import androidx.annotation.DrawableRes
|
||||||
|
import org.tasks.Callback
|
||||||
|
import org.tasks.data.Place
|
||||||
|
|
||||||
|
interface PlaceSearchProvider {
|
||||||
|
fun restoreState(savedInstanceState: Bundle?)
|
||||||
|
|
||||||
|
fun saveState(outState: Bundle)
|
||||||
|
|
||||||
|
@DrawableRes
|
||||||
|
fun getAttributionRes(dark: Boolean): Int
|
||||||
|
|
||||||
|
suspend fun search(query: String, bias: MapPosition?): List<PlaceSearchResult>
|
||||||
|
|
||||||
|
suspend fun fetch(placeSearchResult: PlaceSearchResult): Place
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue