Factorized methods

This commit is contained in:
Kanahia 2024-07-07 07:47:28 +05:30
parent cfb1575c41
commit 438aa3b41b
4 changed files with 22 additions and 59 deletions

View file

@ -398,43 +398,15 @@ public class OkHttpJsonApiClient {
}
/**
* Retrieves place based on Entity Id.
* Retrieves a list of places based on the provided list of places and language.
*
* @param entityId Id of Wikidata Entity
* @param language The language for the query.
* @return A nearby place.
* @throws Exception If an error occurs during the retrieval process.
* @param placeList A list of Place objects for which to fetch information.
* @param language The language code to use for the query.
* @return A list of Place objects with additional information retrieved from Wikidata, or null
* if an error occurs.
* @throws IOException If there is an issue with reading the resource file or executing the HTTP
* request.
*/
@Nullable
public Place getNearbyPlace(
final String entityId, final String language)
throws Exception {
final String wikidataQuery = FileUtils.readFromResource("/queries/query_for_item.rq");
final String query = wikidataQuery
.replace("${ENTITY}", "wd:"+entityId)
.replace("${LANG}", language);
final HttpUrl.Builder urlBuilder = HttpUrl
.parse(sparqlQueryUrl)
.newBuilder()
.addQueryParameter("query", query)
.addQueryParameter("format", "json");
final Request request = new Request.Builder()
.url(urlBuilder.build())
.build();
final Response response = okHttpClient.newCall(request).execute();
if (response.body() != null && response.isSuccessful()) {
final String json = response.body().string();
final NearbyResponse nearbyResponse = gson.fromJson(json, NearbyResponse.class);
final List<NearbyResultItem> bindings = nearbyResponse.getResults().getBindings();
NearbyResultItem item = bindings.get(0);
final Place placeFromNearbyItem = Place.from(item);
return placeFromNearbyItem;
}
throw new Exception(response.message());
}
@Nullable
public List<Place> getPlaces(
final List<Place> placeList, final String language) throws IOException {

View file

@ -135,13 +135,6 @@ public class NearbyController extends MapController {
return nearbyPlaces.getPlaces(placeList, Locale.getDefault().getLanguage());
}
public Place getPlace(String entity) throws Exception {
return nearbyPlaces.getPlaceFromWikidataQuery(
entity,
Locale.getDefault().getLanguage()
);
}
public static LatLng calculateNorthEast(double latitude, double longitude, double distance) {
double lat1 = Math.toRadians(latitude);
double deltaLat = distance * 0.008;

View file

@ -121,19 +121,15 @@ public class NearbyPlaces {
}
/**
* Retrieves a place from a Wikidata query based on Entity Id
* Retrieves a list of places based on the provided list of places and language.
*
* @param entityId Id of Wikidata Entity
* @param lang The language for the query.
* @return A place obtained from the Wikidata query.
* This method fetches place information from a Wikidata query using the specified language.
*
* @param placeList A list of Place objects for which to fetch information.
* @param lang The language code to use for the query.
* @return A list of Place objects obtained from the Wikidata query.
* @throws Exception If an error occurs during the retrieval process.
*/
public Place getPlaceFromWikidataQuery(final String entityId,
final String lang) throws Exception {
return okHttpJsonApiClient
.getNearbyPlace(entityId, lang);
}
public List<Place> getPlaces(final List<Place> placeList,
final String lang) throws Exception {
return okHttpJsonApiClient

View file

@ -1224,14 +1224,14 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
}
private void getPlaceData(String entity, Place place, Marker marker, Boolean isBookMarked) {
final Observable<Place> getPlaceObservable = Observable
final Observable<List<Place>> getPlaceObservable = Observable
.fromCallable(() -> nearbyController
.getPlace(entity));
.getPlaces(List.of(place)));
compositeDisposable.add(getPlaceObservable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(p -> {
Place updatedPlace = p;
.subscribe(placeList -> {
Place updatedPlace = placeList.get(0);
updatedPlace.distance = place.distance;
updatedPlace.location = place.location;
marker.setTitle(updatedPlace.name);
@ -1240,14 +1240,15 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
? getTextBetweenParentheses(
updatedPlace.getLongDescription()) : updatedPlace.getLongDescription());
marker.showInfoWindow();
for (int i =0; i < updatedPlaceList.size(); i++) {
for (int i = 0; i < updatedPlaceList.size(); i++) {
Place pl = updatedPlaceList.get(i);
if (pl.location == updatedPlace.location){
if (pl.location == updatedPlace.location) {
updatedPlaceList.set(i, updatedPlace);
savePlaceToDB(place);
}
}
Drawable icon = ContextCompat.getDrawable(getContext(), getIconFor(updatedPlace, isBookMarked));
Drawable icon = ContextCompat.getDrawable(getContext(),
getIconFor(updatedPlace, isBookMarked));
marker.setIcon(icon);
binding.map.invalidate();
binding.bottomSheetDetails.dataCircularProgress.setVisibility(View.GONE);
@ -1343,6 +1344,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
setFilterState();
}));
}
public void loadPlacesDataAsync(List<Place> placeList, LatLng curLatLng) {
List<Place> places = new ArrayList<>(placeList);
int batchSize = 3;