WikiWorker prepared to replace old utility functions

master
Tomasz Półgrabia 2016-11-01 20:35:56 +01:00
parent a29bcd8ed6
commit 0b4a9e3882
4 changed files with 210 additions and 24 deletions

View File

@ -0,0 +1,53 @@
package pl.tpolgrabia.urbanexplorer.dto.wiki;
/**
* Created by tpolgrabia on 01.11.16.
*/
public class WikiRequestDto {
private Double latitude;
private Double longitude;
private Double radius;
private Long limit;
public Double getLatitude() {
return latitude;
}
public void setLatitude(Double latitude) {
this.latitude = latitude;
}
public Double getLongitude() {
return longitude;
}
public void setLongitude(Double longitude) {
this.longitude = longitude;
}
public Double getRadius() {
return radius;
}
public void setRadius(Double radius) {
this.radius = radius;
}
public Long getLimit() {
return limit;
}
public void setLimit(Long limit) {
this.limit = limit;
}
@Override
public String toString() {
return "WikiRequestDto{" +
"latitude=" + latitude +
", longitude=" + longitude +
", radius=" + radius +
", limit=" + limit +
'}';
}
}

View File

@ -0,0 +1,139 @@
package pl.tpolgrabia.urbanexplorer.worker;
import android.content.Context;
import android.os.AsyncTask;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
import org.greenrobot.eventbus.EventBus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pl.tpolgrabia.urbanexplorer.MainActivity;
import pl.tpolgrabia.urbanexplorer.R;
import pl.tpolgrabia.urbanexplorer.adapters.WikiLocationsAdapter;
import pl.tpolgrabia.urbanexplorer.callbacks.wiki.FetchWikiLocationsCallback;
import pl.tpolgrabia.urbanexplorer.dto.wiki.WikiRequestDto;
import pl.tpolgrabia.urbanexplorer.fragments.WikiLocationsFragment;
import pl.tpolgrabia.urbanexplorerutils.events.DataLoadingFinishEvent;
import pl.tpolgrabia.wikibinding.dto.app.WikiAppObject;
import pl.tpolgrabia.wikibinding.dto.generator.WikiPage;
import pl.tpolgrabia.wikibinding.dto.generator.WikiResponse;
import pl.tpolgrabia.wikibinding.dto.geosearch.WikiGeoObject;
import pl.tpolgrabia.wikibinding.dto.geosearch.WikiGeoResponse;
import pl.tpolgrabia.wikibinding.utils.WikiUtils;
import retrofit2.Response;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by tpolgrabia on 01.11.16.
*/
public class WikiWorker extends AsyncTask<WikiRequestDto, Integer, List<WikiAppObject>> {
private static final Logger lg = LoggerFactory.getLogger(WikiWorker.class);
private final WikiUtils wikiUtils;
private final WikiLocationsFragment frag;
private boolean success = false;
private Context ctx;
public WikiWorker(Context ctx,
WikiLocationsFragment wikiLocationsFragment,
String countryCode) {
wikiUtils = new WikiUtils(ctx, countryCode);
this.frag = wikiLocationsFragment;
this.ctx = ctx;
}
@Override
protected List<WikiAppObject> doInBackground(WikiRequestDto... params) {
List<WikiAppObject> results = new ArrayList<>();
for (WikiRequestDto param : params) {
try {
Response<WikiGeoResponse> apiResult = wikiUtils.fetchGeoSearchWikiMetadata2(
param.getLatitude(),
param.getLongitude(),
param.getRadius(),
param.getLimit());
final int apiResponseCode = apiResult.code();
if (apiResponseCode != 200) {
lg.warn("Invalid error code {}. Response error message {}. Try it later again...",
apiResponseCode,
apiResult.message());
return results;
}
WikiGeoResponse apiGeoResponse = apiResult.body();
List<Long> pageIds = new ArrayList<>();
final List<WikiGeoObject> geoItems = apiGeoResponse.getQuery();
for (WikiGeoObject geoObject : geoItems) {
pageIds.add(geoObject.getPageId());
}
final Map<Long, WikiGeoObject> geoItemsMap = new HashMap<>();
for (WikiGeoObject geoItem : geoItems) {
geoItemsMap.put(geoItem.getPageId(), geoItem);
}
Response<WikiResponse> pageInfoResponse = wikiUtils.fetchPageInfos2(pageIds);
int pageInfoResponseCode = pageInfoResponse.code();
if (pageInfoResponseCode != 200) {
lg.warn("Invalid http code {}. Message: {}. Try it later again",
pageInfoResponseCode, pageInfoResponse.message());
return results;
}
WikiResponse wikiResponse = pageInfoResponse.body();
for (WikiPage page : wikiResponse.getPages()) {
WikiAppObject appObject = WikiUtils.convertWikiAppObject(geoItemsMap, page);
results.add(appObject);
}
} catch (IOException e) {
lg.error("I/O error", e);
}
}
return results;
}
@Override
protected void onPostExecute(List<WikiAppObject> objects) {
ArrayList<WikiAppObject> nobjects = new ArrayList<WikiAppObject>(objects);
frag.setAppObjects(nobjects);
// handling here wiki locations
if (success) {
Toast.makeText(ctx, "Sorry, currently we have problem with interfacing wiki" +
": " + success + ". Try again later", Toast.LENGTH_SHORT).show();
return;
}
// TODO on success
final View view = frag.getView();
if (view == null) {
return;
}
ListView locations = (ListView) view.findViewById(R.id.wiki_places);
locations.setOnItemLongClickListener(new FetchWikiLocationsCallback(frag));
locations.setAdapter(new WikiLocationsAdapter(frag.getActivity(), objects));
if (objects.isEmpty()) {
Toast.makeText(frag.getActivity(), "No results", Toast.LENGTH_SHORT).show();
}
MainActivity mainActivity = (MainActivity) frag.getActivity();
if (mainActivity == null) {
return;
}
EventBus.getDefault().post(new DataLoadingFinishEvent(this));
}
}

View File

@ -1,5 +1,6 @@
package pl.tpolgrabia.wikibinding; package pl.tpolgrabia.wikibinding;
import pl.tpolgrabia.wikibinding.dto.generator.WikiResponse;
import pl.tpolgrabia.wikibinding.dto.geosearch.WikiGeoResponse; import pl.tpolgrabia.wikibinding.dto.geosearch.WikiGeoResponse;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.http.GET; import retrofit2.http.GET;
@ -24,17 +25,6 @@ public interface WikiService {
"&pilimit=50" + "&pilimit=50" +
"&wbptterms=description" + "&wbptterms=description" +
"&format=json") "&format=json")
Call<String> fetchPageInfos( Call<WikiResponse> fetchPageInfos(
@Query("pageids") String pageIds); @Query("pageids") String pageIds);
// aq.ajax("https://" + countryCode + ".wikipedia.org/w/api.php" +
// "?action=query" +
// "&prop=coordinates%7Cpageimages%7Cpageterms" +
// "&colimit=50" +
// "&piprop=thumbnail" +
// "&pithumbsize=144" +
// "&pilimit=50" +
// "&wbptterms=description" +
// "&pageids=" + StringUtils.join(pageIds, "|") +
// "&format=json", JSONObject.class, new AjaxCallback<JSONObject>() {
} }

View File

@ -127,6 +127,20 @@ public class WikiUtils {
return wikiLocation; return wikiLocation;
} }
public static WikiAppObject convertWikiAppObject(Map<Long, WikiGeoObject> geoItemsMap, WikiPage page) {
WikiAppObject appObject = new WikiAppObject();
appObject.setTitle(page.getTitle());
appObject.setDistance(geoItemsMap.get(page.getPageId()).getDistance());
appObject.setLatitude(page.getCoordinates().get(0).getLatitude());
appObject.setLongitude(page.getCoordinates().get(0).getLongitude());
final WikiThumbnail thumbonail = page.getThumbnail();
final String thumSource = thumbonail != null ? thumbonail.getSource() : null;
appObject.setThumbnail(thumSource);
appObject.setUrl(thumSource);
appObject.setPageId(page.getPageId());
return appObject;
}
public Response<WikiGeoResponse> fetchGeoSearchWikiMetadata2(Double latitude, public Response<WikiGeoResponse> fetchGeoSearchWikiMetadata2(Double latitude,
Double longitude, Double longitude,
Double radius, Double radius,
@ -282,17 +296,7 @@ public class WikiUtils {
} }
for (WikiPage page : pages) { for (WikiPage page : pages) {
WikiAppObject appObject = new WikiAppObject(); results.add(convertWikiAppObject(geoItemsMap, page));
appObject.setTitle(page.getTitle());
appObject.setDistance(geoItemsMap.get(page.getPageId()).getDistance());
appObject.setLatitude(page.getCoordinates().get(0).getLatitude());
appObject.setLongitude(page.getCoordinates().get(0).getLongitude());
final WikiThumbnail thumbonail = page.getThumbnail();
final String thumSource = thumbonail != null ? thumbonail.getSource() : null;
appObject.setThumbnail(thumSource);
appObject.setUrl(thumSource);
appObject.setPageId(page.getPageId());
results.add(appObject);
} }
// TODO here add callback invocation with result // TODO here add callback invocation with result
@ -306,7 +310,7 @@ public class WikiUtils {
} }
public Response<String> fetchPageInfos2(List<Long> pageIds) throws IOException { public Response<WikiResponse> fetchPageInfos2(List<Long> pageIds) throws IOException {
OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
// TODO httpClient.addInterceptor(new RetrofitDebugInterceptor()); // TODO httpClient.addInterceptor(new RetrofitDebugInterceptor());