diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/dto/WikiPage.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/dto/WikiPage.java index d25cc0a..9443f33 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/dto/WikiPage.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/dto/WikiPage.java @@ -1,5 +1,7 @@ package pl.tpolgrabia.urbanexplorer.dto; +import java.util.List; + /** * Created by tpolgrabia on 28.08.16. */ @@ -10,4 +12,64 @@ public class WikiPage { private Long pageId; private WikiThumbnail thumbnail; private String title; + + public List getCoordinates() { + return coordinates; + } + + public void setCoordinates(List coordinates) { + this.coordinates = coordinates; + } + + public Long getIndex() { + return index; + } + + public void setIndex(Long index) { + this.index = index; + } + + public Long getNs() { + return ns; + } + + public void setNs(Long ns) { + this.ns = ns; + } + + public Long getPageId() { + return pageId; + } + + public void setPageId(Long pageId) { + this.pageId = pageId; + } + + public WikiThumbnail getThumbnail() { + return thumbnail; + } + + public void setThumbnail(WikiThumbnail thumbnail) { + this.thumbnail = thumbnail; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + @Override + public String toString() { + return "WikiPage{" + + "coordinates=" + coordinates + + ", index=" + index + + ", ns=" + ns + + ", pageId=" + pageId + + ", thumbnail=" + thumbnail + + ", title='" + title + '\'' + + '}'; + } } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/WikiUtils.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/WikiUtils.java index ad8a32e..5178986 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/WikiUtils.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/WikiUtils.java @@ -1,21 +1,38 @@ package pl.tpolgrabia.urbanexplorer.utils; import android.content.Context; +import android.util.Log; import android.view.View; import com.androidquery.AQuery; import com.androidquery.callback.AjaxCallback; import com.androidquery.callback.AjaxStatus; +import org.json.JSONArray; +import org.json.JSONException; import org.json.JSONObject; import pl.tpolgrabia.urbanexplorer.callbacks.WikiResponseCallback; +import pl.tpolgrabia.urbanexplorer.callbacks.WikiStatus; +import pl.tpolgrabia.urbanexplorer.dto.WikiLocation; +import pl.tpolgrabia.urbanexplorer.dto.WikiPage; +import pl.tpolgrabia.urbanexplorer.dto.WikiResponse; +import pl.tpolgrabia.urbanexplorer.dto.WikiThumbnail; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; /** * Created by tpolgrabia on 28.08.16. */ public class WikiUtils { + private static final String CLASS_TAG = WikiUtils.class.getSimpleName(); private static final String WIKI_FORMAT = "json"; - public static void fetchNearPlaces(Context ctx, final double latitude, final double longitude, final WikiResponseCallback callback) { - AQuery aq = new AQuery(ctx); + public static void fetchNearPlaces(Context ctx, + final double latitude, + final double longitude, + final long resultsLimit, + final WikiResponseCallback callback) { + final AQuery aq = new AQuery(ctx); aq.ajax("TODO", JSONObject.class, new AjaxCallback(){ @Override public void callback(String url, JSONObject object, AjaxStatus status) { @@ -31,10 +48,81 @@ public class WikiUtils { "&generator=geosearch" + "&ggscoord=" + latitude + "%7C" + longitude + "&ggsradius=10000" + - "&ggslimit=50" + + "&ggslimit=" + resultsLimit + "&format" + WIKI_FORMAT; - callback.callback(null, null); + aq.ajax(qurl, JSONObject.class, new AjaxCallback() { + @Override + public void callback(String url, JSONObject object, AjaxStatus status) { + if (status.getCode() == 200) { + callback.callback(WikiStatus.SUCCESS, null); + } else { + try { + callback.callback(WikiStatus.NETWORK_ERROR, fetchWikiResponse(object)); + } catch (JSONException e) { + Log.e(CLASS_TAG, "General error", e); + callback.callback(WikiStatus.GENERAL_ERROR, null); + } + } + } + }); + } }); } + + public static WikiResponse fetchWikiResponse(JSONObject object) throws JSONException { + WikiResponse wikiResponse = new WikiResponse(); + wikiResponse.setBatchComplete(Boolean.valueOf(object.getString("batchcomplete"))); + wikiResponse.setPages(fetchPages(object.getJSONObject("query").getJSONObject("pages"))); + return wikiResponse; + } + + public static List fetchPages(JSONObject jpages) throws JSONException { + List pages = new ArrayList<>(); + Iterator pagesIds = jpages.keys(); + while (pagesIds.hasNext()) { + String pageId = pagesIds.next(); + pages.add(fetchPage(jpages.getJSONObject(pageId))); + } + return pages; + } + + public static WikiPage fetchPage(JSONObject jpage) throws JSONException { + WikiPage wikiPage = new WikiPage(); + wikiPage.setCoordinates(fetchCoordinates(jpage.getJSONArray("coordinates"))); + wikiPage.setIndex(jpage.getLong("index")); + wikiPage.setNs(jpage.getLong("ns")); + wikiPage.setPageId(jpage.getLong("pageId")); + wikiPage.setThumbnail(fetchThumbnail(jpage.getJSONObject("thumbnail"))); + wikiPage.setTitle(jpage.getString("title")); + return null; + } + + public static WikiThumbnail fetchThumbnail(JSONObject jthumbnail) throws JSONException { + WikiThumbnail wikiThumbnail = new WikiThumbnail(); + wikiThumbnail.setWidth(jthumbnail.getLong("width")); + wikiThumbnail.setHeight(jthumbnail.getLong("height")); + wikiThumbnail.setSource(jthumbnail.getString("source")); + return wikiThumbnail; + } + + public static List fetchCoordinates(JSONArray jcoordinates) throws JSONException { + List wikiLocations = new ArrayList(); + + int n = jcoordinates.length(); + for (int i = 0; i < n; i++) { + wikiLocations.add(fetchCoordinate(jcoordinates.getJSONObject(i))) + } + + return wikiLocations; + } + + public static WikiLocation fetchCoordinate(JSONObject jlocation) throws JSONException { + WikiLocation wikiLocation = new WikiLocation(); + wikiLocation.setLatitude(jlocation.getDouble("lat")); + wikiLocation.setLongitude(jlocation.getDouble("lon")); + wikiLocation.setPrimary(jlocation.getString("primary")); + wikiLocation.setGlobe(jlocation.getString("globe")); + return wikiLocation; + } }