From a4be4820840336912f61bf4c77bb94df5365d47a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20P=C3=B3=C5=82grabia?= Date: Tue, 27 Sep 2016 22:14:06 +0200 Subject: [PATCH] Refactored wiki utils into class non-static. --- .../urbanexplorer/AppConstants.java | 1 + .../wiki/FetchWikiLocationsCallback.java | 5 +- .../fragments/WikiLocationsFragment.java | 4 +- .../wikibinding/utils/WikiUtils.java | 71 ++++++++++--------- 4 files changed, 47 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java index 4492cd0..976ef06 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/AppConstants.java @@ -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"; } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/wiki/FetchWikiLocationsCallback.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/wiki/FetchWikiLocationsCallback.java index a93044b..b238ca5 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/wiki/FetchWikiLocationsCallback.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/wiki/FetchWikiLocationsCallback.java @@ -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 appObjects; public FetchWikiLocationsCallback(WikiLocationsFragment wikiLocationsFragment, List 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)); diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java index 094276c..1d764d6 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java @@ -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 diff --git a/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/utils/WikiUtils.java b/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/utils/WikiUtils.java index 0f1a656..5460531 100644 --- a/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/utils/WikiUtils.java +++ b/wikibinding/src/main/java/pl/tpolgrabia/wikibinding/utils/WikiUtils.java @@ -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,27 +36,34 @@ 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, - final double latitude, - final double longitude, - final Long resultsLimit, - final Long radiusLimit, - final WikiResponseCallback callback) { + public WikiUtils(Context ctx, String countryCode) { + this.ctx = ctx; + this.countryCode = countryCode; + } + + public void fetchNearPlaces( + final double latitude, + final double longitude, + final Long resultsLimit, + final Long radiusLimit, + final WikiResponseCallback callback) { final AQuery aq = NetUtils.createProxyAQueryInstance(ctx); aq.ajax("TODO", JSONObject.class, new AjaxCallback(){ @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,12 +171,12 @@ public class WikiUtils { return wikiLocation; } - public static void fetchGeoSearchWikiMetadata(Context ctx, - Double latitude, - Double longitude, - Double radius, - Long limit, - final WikiGeoResponseCallback callback) { + public void fetchGeoSearchWikiMetadata(Context ctx, + Double latitude, + Double longitude, + Double radius, + Long limit, + final WikiGeoResponseCallback callback) { lg.debug("Latitude: {}, longitude: {}, radius: {}, limit: {}", latitude, longitude, radius, limit); @@ -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,12 +251,12 @@ public class WikiUtils { return object; } - public static void fetchAppData(final Context ctx, - final Double latitude, - final Double longitude, - final Double radius, - final Long limit, - final WikiAppResponseCallback callback) { + public void fetchAppData( + final Double latitude, + final Double longitude, + final Double radius, + final Long limit, + final WikiAppResponseCallback callback) { lg.debug("Latitude: {}, longitude: {}, radius: {}, limit: {}", latitude, longitude, radius, limit); @@ -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 pageIds, final WikiResponseCallback callback) { + public void fetchPageInfos(List 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 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),