Refactored wiki utils into class non-static.

master
Tomasz Półgrabia 2016-09-27 22:14:06 +02:00
parent 0e5aa11b7a
commit a4be482084
4 changed files with 47 additions and 34 deletions

View File

@ -21,4 +21,5 @@ public class AppConstants {
static final String PHOTO_INFO = "PHOTO_INFO";
static final String SAVED_CONFIG_KEY = "SAVED_CONFIG_KEY";
public static final String GOOGLE_API_KEY = "AIzaSyBAJoK-pu_qnQ0U8EGjM1Zkz_g8oJV4w2g";
public static final String DEF_WIKI_COUNTRY_CODE = "en";
}

View File

@ -2,6 +2,7 @@ package pl.tpolgrabia.urbanexplorer.callbacks.wiki;
import android.view.View;
import android.widget.AdapterView;
import pl.tpolgrabia.urbanexplorer.AppConstants;
import pl.tpolgrabia.wikibinding.dto.app.WikiAppObject;
import pl.tpolgrabia.urbanexplorer.fragments.WikiLocationsFragment;
import pl.tpolgrabia.wikibinding.utils.WikiUtils;
@ -12,18 +13,20 @@ import java.util.List;
* Created by tpolgrabia on 14.09.16.
*/
public class FetchWikiLocationsCallback implements AdapterView.OnItemLongClickListener {
private final WikiUtils wikiUtils;
private WikiLocationsFragment wikiLocationsFragment;
private final List<WikiAppObject> appObjects;
public FetchWikiLocationsCallback(WikiLocationsFragment wikiLocationsFragment, List<WikiAppObject> appObjects) {
this.wikiLocationsFragment = wikiLocationsFragment;
this.wikiUtils = new WikiUtils(wikiLocationsFragment.getActivity(), AppConstants.DEF_WIKI_COUNTRY_CODE);
this.appObjects = appObjects;
}
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
final WikiAppObject item = appObjects.get(position);
WikiUtils.fetchSingleWikiInfoItemAndRunWikiPage(
wikiUtils.fetchSingleWikiInfoItemAndRunWikiPage(
wikiLocationsFragment.getActivity(),
item.getPageId(),
new WikiInfoRunBrowserCallback(wikiLocationsFragment, item));

View File

@ -46,6 +46,7 @@ public class WikiLocationsFragment extends Fragment {
private int lastFetchSize = -1;
private String currentGeocodedLocation;
private GeocoderUtils geocoderUtils;
private WikiUtils wikiUtils;
public WikiLocationsFragment() {
// Required empty public constructor
@ -63,6 +64,7 @@ public class WikiLocationsFragment extends Fragment {
super.onActivityCreated(savedInstanceState);
appObjects = WikiCacheUtils.loadWikiObjectsFromCache(getActivity(), savedInstanceState);
geocoderUtils = new GeocoderUtils(getActivity(), AppConstants.GOOGLE_API_KEY);
wikiUtils = new WikiUtils(getActivity(), AppConstants.DEF_WIKI_COUNTRY_CODE);
}
@Override
@ -110,7 +112,7 @@ public class WikiLocationsFragment extends Fragment {
return;
}
WikiUtils.fetchAppData(activity, new WikiFetchAppDataCallback(this, activity));
wikiUtils.fetchAppData(new WikiFetchAppDataCallback(this, activity));
}
@Override

View File

@ -13,6 +13,10 @@ import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pl.tpolgrabia.urbanexplorerutils.events.DataLoadingFinishEvent;
import pl.tpolgrabia.urbanexplorerutils.utils.LocationUtils;
import pl.tpolgrabia.urbanexplorerutils.utils.NetUtils;
import pl.tpolgrabia.urbanexplorerutils.utils.SettingsUtils;
import pl.tpolgrabia.wikibinding.callback.WikiAppResponseCallback;
import pl.tpolgrabia.wikibinding.callback.WikiResponseCallback;
import pl.tpolgrabia.wikibinding.callback.WikiStatus;
@ -24,10 +28,6 @@ import pl.tpolgrabia.wikibinding.dto.generator.WikiThumbnail;
import pl.tpolgrabia.wikibinding.dto.geosearch.WikiGeoObject;
import pl.tpolgrabia.wikibinding.dto.geosearch.WikiGeoResponse;
import pl.tpolgrabia.wikibinding.dto.geosearch.WikiGeoResponseCallback;
import pl.tpolgrabia.urbanexplorerutils.events.DataLoadingFinishEvent;
import pl.tpolgrabia.urbanexplorerutils.utils.LocationUtils;
import pl.tpolgrabia.urbanexplorerutils.utils.NetUtils;
import pl.tpolgrabia.urbanexplorerutils.utils.SettingsUtils;
import java.util.*;
@ -36,15 +36,22 @@ import java.util.*;
*/
public class WikiUtils {
private static final Logger lg = LoggerFactory.getLogger(WikiUtils.class);
private static final String CLASS_TAG = WikiUtils.class.getSimpleName();
private static final String WIKI_FORMAT = "json";
private static final long WIKI_MIN_RADIUS = 10L;
private static final Long WIKI_MAX_RESULTS_LIMIT = 500L;
private static final Long WIKI_MIN_RESULTS = 10L;
private static final Double WIKI_STD_RADIUS = 10000.0;
private static final Long WIKI_STD_LIMIT = 10L;
private final Context ctx;
private final String countryCode;
public static void fetchNearPlaces(Context ctx,
public WikiUtils(Context ctx, String countryCode) {
this.ctx = ctx;
this.countryCode = countryCode;
}
public void fetchNearPlaces(
final double latitude,
final double longitude,
final Long resultsLimit,
@ -56,7 +63,7 @@ public class WikiUtils {
@Override
public void callback(String url, JSONObject object, AjaxStatus status) {
// TODO handle response
final String qurl = "https://en.wikipedia.org/w/api.php?" +
final String qurl = "https://" + countryCode + ".wikipedia.org/w/api.php?" +
"action=query" +
"&prop=coordinates%7Cpageimages%7Cpageterms" +
"&colimit=50" +
@ -164,7 +171,7 @@ public class WikiUtils {
return wikiLocation;
}
public static void fetchGeoSearchWikiMetadata(Context ctx,
public void fetchGeoSearchWikiMetadata(Context ctx,
Double latitude,
Double longitude,
Double radius,
@ -182,7 +189,7 @@ public class WikiUtils {
}
AQuery aq = NetUtils.createProxyAQueryInstance(ctx);
final String queryUrl = "https://en.wikipedia.org/w/api.php" +
final String queryUrl = "https://" + countryCode + ".wikipedia.org/w/api.php" +
"?action=query" +
"&list=geosearch" +
"&gscoord=" + latitude + "%7C" + longitude +
@ -244,7 +251,7 @@ public class WikiUtils {
return object;
}
public static void fetchAppData(final Context ctx,
public void fetchAppData(
final Double latitude,
final Double longitude,
final Double radius,
@ -281,7 +288,7 @@ public class WikiUtils {
}
fetchPageInfos(ctx,
fetchPageInfos(
pageIds,
new WikiResponseCallback() {
@Override
@ -318,9 +325,9 @@ public class WikiUtils {
}
public static void fetchPageInfos(Context ctx, List<Long> pageIds, final WikiResponseCallback callback) {
public void fetchPageInfos(List<Long> pageIds, final WikiResponseCallback callback) {
AQuery aq = NetUtils.createProxyAQueryInstance(ctx);
aq.ajax("https://en.wikipedia.org/w/api.php" +
aq.ajax("https://" + countryCode + ".wikipedia.org/w/api.php" +
"?action=query" +
"&prop=coordinates%7Cpageimages%7Cpageterms" +
"&colimit=50" +
@ -347,18 +354,18 @@ public class WikiUtils {
}
public static void fetchSingleWikiInfoItemAndRunWikiPage(Context ctx,
public void fetchSingleWikiInfoItemAndRunWikiPage(Context ctx,
Long pageId,
AjaxCallback<JSONObject> callback) {
NetUtils.createProxyAQueryInstance(ctx).ajax(
"https://en.wikipedia.org/w/api.php?action=query&prop=info&pageids="
"https://" + countryCode + ".wikipedia.org/w/api.php?action=query&prop=info&pageids="
+ pageId + "&inprop=url&format=json",
JSONObject.class,
callback
);
}
public static void fetchAppData(Context ctx, WikiAppResponseCallback clbk) {
public void fetchAppData(WikiAppResponseCallback clbk) {
final Location location = LocationUtils.getLastKnownLocation(ctx);
if (location == null) {
@ -368,7 +375,7 @@ public class WikiUtils {
return;
}
fetchAppData(ctx,
fetchAppData(
location.getLatitude(),
location.getLongitude(),
SettingsUtils.fetchRadiusLimit(ctx),