From 92638eab029d0179c10f39de3a8aab69f41c3817 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20P=C3=B3=C5=82grabia?= Date: Sat, 24 Sep 2016 11:02:21 +0200 Subject: [PATCH] Cleaning and refactoring WikiLocationsFragment. --- .../callbacks/WikiFetchAppDataCallback.java | 59 +++++++++ .../callbacks/WikiLocationCallback.java | 26 ++++ .../WikiLocationProviderStatusCallback.java | 25 ++++ .../fragments/WikiLocationsFragment.java | 122 +++--------------- .../urbanexplorer/utils/CacheUtils.java | 34 +++++ .../urbanexplorer/utils/SettingsUtils.java | 17 ++- 6 files changed, 176 insertions(+), 107 deletions(-) create mode 100644 app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/WikiFetchAppDataCallback.java create mode 100644 app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/WikiLocationCallback.java create mode 100644 app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/WikiLocationProviderStatusCallback.java diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/WikiFetchAppDataCallback.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/WikiFetchAppDataCallback.java new file mode 100644 index 0000000..4b6d109 --- /dev/null +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/WikiFetchAppDataCallback.java @@ -0,0 +1,59 @@ +package pl.tpolgrabia.urbanexplorer.callbacks; + +import android.support.v4.app.FragmentActivity; +import android.widget.ListView; +import android.widget.Toast; +import org.greenrobot.eventbus.EventBus; +import pl.tpolgrabia.urbanexplorer.MainActivity; +import pl.tpolgrabia.urbanexplorer.R; +import pl.tpolgrabia.urbanexplorer.adapters.WikiLocationsAdapter; +import pl.tpolgrabia.urbanexplorer.dto.wiki.app.WikiAppObject; +import pl.tpolgrabia.urbanexplorer.events.DataLoadingFinishEvent; +import pl.tpolgrabia.urbanexplorer.fragments.WikiLocationsFragment; +import pl.tpolgrabia.urbanexplorer.utils.WikiAppResponseCallback; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by tpolgrabia on 24.09.16. + */ +public class WikiFetchAppDataCallback implements WikiAppResponseCallback { + + private WikiLocationsFragment wikiLocationsFragment; + private final FragmentActivity activity; + + public WikiFetchAppDataCallback(WikiLocationsFragment wikiLocationsFragment, FragmentActivity activity) { + this.wikiLocationsFragment = wikiLocationsFragment; + this.activity = activity; + } + + @Override + public void callback(WikiStatus status, final List objects) { + ArrayList nobjects = new ArrayList(objects); + wikiLocationsFragment.setAppObjects(nobjects); + + // handling here wiki locations + if (status != WikiStatus.SUCCESS) { + Toast.makeText(activity, "Sorry, currently we have problem with interfacing wiki" + + ": " + status + ". Try again later", Toast.LENGTH_SHORT).show(); + return; + } + + // TODO on success + + ListView locations = (ListView) wikiLocationsFragment.getView().findViewById(R.id.wiki_places); + locations.setOnItemLongClickListener(new FetchWikiLocationsCallback(wikiLocationsFragment, nobjects)); + locations.setAdapter(new WikiLocationsAdapter(activity, objects)); + if (objects.isEmpty()) { + Toast.makeText(wikiLocationsFragment.getActivity(), "No results", Toast.LENGTH_SHORT).show(); + } + + MainActivity mainActivity = (MainActivity) wikiLocationsFragment.getActivity(); + if (mainActivity == null) { + return; + } + + EventBus.getDefault().post(new DataLoadingFinishEvent(this)); + } +} diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/WikiLocationCallback.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/WikiLocationCallback.java new file mode 100644 index 0000000..dabb213 --- /dev/null +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/WikiLocationCallback.java @@ -0,0 +1,26 @@ +package pl.tpolgrabia.urbanexplorer.callbacks; + +import android.location.Location; +import pl.tpolgrabia.urbanexplorer.dto.wiki.app.WikiAppObject; +import pl.tpolgrabia.urbanexplorer.fragments.WikiLocationsFragment; + +import java.util.ArrayList; + +/** + * Created by tpolgrabia on 24.09.16. + */ +public class WikiLocationCallback implements StandardLocationListenerCallback { + private WikiLocationsFragment wikiLocationsFragment; + + public WikiLocationCallback(WikiLocationsFragment wikiLocationsFragment) { + this.wikiLocationsFragment = wikiLocationsFragment; + } + + @Override + public void callback(Location location) { + wikiLocationsFragment.setLastFetchSize(-1); + wikiLocationsFragment.setAppObjects(new ArrayList()); + wikiLocationsFragment.updateLocationInfo(); + wikiLocationsFragment.fetchWikiLocations(); + } +} diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/WikiLocationProviderStatusCallback.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/WikiLocationProviderStatusCallback.java new file mode 100644 index 0000000..4341486 --- /dev/null +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/WikiLocationProviderStatusCallback.java @@ -0,0 +1,25 @@ +package pl.tpolgrabia.urbanexplorer.callbacks; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import pl.tpolgrabia.urbanexplorer.fragments.WikiLocationsFragment; + +/** + * Created by tpolgrabia on 24.09.16. + */ +public class WikiLocationProviderStatusCallback implements ProviderStatusCallback { + private static final Logger lg = LoggerFactory.getLogger(WikiLocationProviderStatusCallback.class); + private WikiLocationsFragment wikiLocationsFragment; + + public WikiLocationProviderStatusCallback(WikiLocationsFragment wikiLocationsFragment) { + this.wikiLocationsFragment = wikiLocationsFragment; + } + + @Override + public void callback(String provider, boolean enabled) { + if (enabled) { + lg.trace("Handling provider enabling - refreshing wiki listing"); + wikiLocationsFragment.fetchWikiLocations(); + } + } +} 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 56a12b3..a14820d 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java @@ -1,11 +1,9 @@ package pl.tpolgrabia.urbanexplorer.fragments; -import android.content.SharedPreferences; import android.location.Location; import android.location.LocationManager; import android.os.Bundle; -import android.preference.PreferenceManager; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.view.LayoutInflater; @@ -27,14 +25,11 @@ import pl.tpolgrabia.urbanexplorer.callbacks.*; import pl.tpolgrabia.urbanexplorer.dto.wiki.WikiCacheDto; import pl.tpolgrabia.urbanexplorer.dto.wiki.app.WikiAppObject; import pl.tpolgrabia.urbanexplorer.events.DataLoadingFinishEvent; -import pl.tpolgrabia.urbanexplorer.events.DataLoadingStartEvent; import pl.tpolgrabia.urbanexplorer.events.RefreshEvent; import pl.tpolgrabia.urbanexplorer.utils.*; import java.io.*; import java.util.ArrayList; -import java.util.GregorianCalendar; -import java.util.List; import static android.content.Context.LOCATION_SERVICE; @@ -44,9 +39,6 @@ import static android.content.Context.LOCATION_SERVICE; public class WikiLocationsFragment extends Fragment { private static final Logger lg = LoggerFactory.getLogger(WikiLocationsFragment.class); - private static final String CLASS_TAG = WikiLocationsFragment.class.getSimpleName(); - private static final double WIKI_DEF_RADIUS = 10.0; - private static final long WIKI_DEF_LIMIT = 100; public static final String TAG = WikiLocationsFragment.class.getSimpleName(); private static final String WIKI_APP_OBJECTS = "WIKI_APP_OBJECTS"; private LocationManager locationService; @@ -90,37 +82,15 @@ public class WikiLocationsFragment extends Fragment { // Inflate the layout for this fragment final View inflatedView = inflater.inflate(R.layout.fragment_wiki_locations, container, false); lg.trace("TAG: {}", getTag()); - for (Fragment frag : getFragmentManager().getFragments()) { - if (frag == null) { - lg.trace("Got null fragment"); - } else { - lg.trace("Fragment TAG {}", frag.getTag()); - } - } + DebugUtils.dumpFragments(getFragmentManager().getFragments()); locationService = (LocationManager) getActivity().getSystemService(LOCATION_SERVICE); currentLocation = (TextView) inflatedView.findViewById(R.id.wiki_current_location); MainActivity mainActivity = (MainActivity) getActivity(); - mainActivity.getLocationCallback().addCallback(new StandardLocationListenerCallback() { - @Override - public void callback(Location location) { - lastFetchSize = -1; - appObjects = new ArrayList<>(); - updateLocationInfo(); - fetchWikiLocations(); - } - }); + mainActivity.getLocationCallback().addCallback(new WikiLocationCallback(this)); - mainActivity.getLocationCallback().addProviderCallback(new ProviderStatusCallback() { - @Override - public void callback(String provider, boolean enabled) { - if (enabled) { - lg.trace("Handling provider enabling - refreshing wiki listing"); - fetchWikiLocations(); - } - } - }); + mainActivity.getLocationCallback().addProviderCallback(new WikiLocationProviderStatusCallback(this)); ListView locations = (ListView) inflatedView.findViewById(R.id.wiki_places); locations.setOnItemLongClickListener(new FetchWikiLocationsCallback(WikiLocationsFragment.this, appObjects)); @@ -170,55 +140,12 @@ public class WikiLocationsFragment extends Fragment { WikiUtils.fetchAppData(activity, location.getLatitude(), location.getLongitude(), - fetchRadiusLimit(), - fetchSearchLimit(), - new WikiAppResponseCallback() { - - @Override - public void callback(WikiStatus status, final List objects) { - appObjects.clear(); - appObjects.addAll(objects); - - // handling here wiki locations - if (status != WikiStatus.SUCCESS) { - Toast.makeText(activity, "Sorry, currently we have problem with interfacing wiki" + - ": " + status + ". Try again later", Toast.LENGTH_SHORT).show(); - return; - } - - // TODO on success - - ListView locations = (ListView) getView().findViewById(R.id.wiki_places); - locations.setOnItemLongClickListener(new FetchWikiLocationsCallback(WikiLocationsFragment.this, appObjects)); - locations.setAdapter(new WikiLocationsAdapter(activity, appObjects)); - if (objects.isEmpty()) { - Toast.makeText(getActivity(), "No results", Toast.LENGTH_SHORT).show(); - } - - MainActivity mainActivity = (MainActivity) getActivity(); - if (mainActivity == null) { - return; - } - - EventBus.getDefault().post(new DataLoadingFinishEvent(this)); - } - } + SettingsUtils.fetchRadiusLimit(getActivity()), + SettingsUtils.fetchSearchLimit(getActivity()), + new WikiFetchAppDataCallback(this, activity) ); } - private Double fetchRadiusLimit() { - SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); - final String prefWikiRadius = sharedPreferences.getString("pref_wiki_radius", String.valueOf(WIKI_DEF_RADIUS)); - lg.debug("Pref wiki radius limit {}", prefWikiRadius); - return NumberUtils.safeParseDouble(prefWikiRadius)*1000.0; // in m, settings are in km unit - } - private Long fetchSearchLimit() { - SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); - final String prefWikiResultsLimit = sharedPreferences.getString("pref_wiki_limit", String.valueOf(WIKI_DEF_LIMIT)); - lg.debug("Pref wiki search results limit {}", prefWikiResultsLimit); - return NumberUtils.safeParseLong(prefWikiResultsLimit); - } - @Override public void onResume() { super.onResume(); @@ -287,33 +214,7 @@ public class WikiLocationsFragment extends Fragment { EventBus.getDefault().unregister(this); lg.trace("onDestroy {}", System.identityHashCode(this)); - try (BufferedWriter bw = new BufferedWriter( - new OutputStreamWriter( - new FileOutputStream( - new File(getActivity().getCacheDir(), - AppConstants.WIKI_CACHE_FILENAME))))) { - - WikiCacheDto dto = new WikiCacheDto(); - dto.setAppObject(appObjects); - if (getActivity() != null) { - Location location = LocationUtils.getLastKnownLocation(getActivity()); - if (location != null) { - dto.setLongitude(location.getLongitude()); - dto.setLatitude(location.getLatitude()); - dto.setAltitude(location.getAltitude()); - } - } - - dto.setFetchedAt(new GregorianCalendar().getTime()); - // FIXME should be a fetched time, not persist time - - new Gson().toJson(bw); - - } catch (FileNotFoundException e) { - lg.error("File not found", e); - } catch (IOException e) { - lg.error("I/O error", e); - } + CacheUtils.saveWikiObjectsToCache(getActivity(), appObjects); } @Subscribe @@ -321,4 +222,13 @@ public class WikiLocationsFragment extends Fragment { appObjects.clear(); fetchWikiLocations(); } + + public void setLastFetchSize(int lastFetchSize) { + this.lastFetchSize = lastFetchSize; + } + + public void setAppObjects(ArrayList appObjects) { + this.appObjects = appObjects; + } + } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/CacheUtils.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/CacheUtils.java index 8643f6b..1043c3f 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/CacheUtils.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/CacheUtils.java @@ -10,11 +10,15 @@ import org.slf4j.LoggerFactory; import pl.tpolgrabia.urbanexplorer.AppConstants; import pl.tpolgrabia.urbanexplorer.dto.panoramio.PanoramioCacheDto; import pl.tpolgrabia.urbanexplorer.dto.panoramio.PanoramioImageInfo; +import pl.tpolgrabia.urbanexplorer.dto.wiki.WikiCacheDto; +import pl.tpolgrabia.urbanexplorer.dto.wiki.app.WikiAppObject; import pl.tpolgrabia.urbanexplorer.fragments.HomeFragment; +import pl.tpolgrabia.urbanexplorer.fragments.WikiLocationsFragment; import java.io.*; import java.util.ArrayList; import java.util.GregorianCalendar; +import java.util.List; /** * Created by tpolgrabia on 24.09.16. @@ -101,4 +105,34 @@ public class CacheUtils { } } } + + public static void saveWikiObjectsToCache(Context ctx, List appObjects) { + try (BufferedWriter bw = new BufferedWriter( + new OutputStreamWriter( + new FileOutputStream( + new File(ctx.getCacheDir(), + AppConstants.WIKI_CACHE_FILENAME))))) { + + WikiCacheDto dto = new WikiCacheDto(); + dto.setAppObject(appObjects); + if (ctx != null) { + Location location = LocationUtils.getLastKnownLocation(ctx); + if (location != null) { + dto.setLongitude(location.getLongitude()); + dto.setLatitude(location.getLatitude()); + dto.setAltitude(location.getAltitude()); + } + } + + dto.setFetchedAt(new GregorianCalendar().getTime()); + // FIXME should be a fetched time, not persist time + + new Gson().toJson(bw); + + } catch (FileNotFoundException e) { + lg.error("File not found", e); + } catch (IOException e) { + lg.error("I/O error", e); + } + } } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/SettingsUtils.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/SettingsUtils.java index 2e36bfc..6ea9d66 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/SettingsUtils.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/SettingsUtils.java @@ -6,12 +6,13 @@ import android.preference.PreferenceManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import pl.tpolgrabia.urbanexplorer.AppConstants; -import pl.tpolgrabia.urbanexplorer.fragments.HomeFragment; /** * Created by tpolgrabia on 24.09.16. */ public class SettingsUtils { + public static final double WIKI_DEF_RADIUS = 10.0; + public static final long WIKI_DEF_LIMIT = 100; private static final Logger lg = LoggerFactory.getLogger(SettingsUtils.class); public static Double fetchRadiusY(Context ctx) { @@ -45,4 +46,18 @@ public class SettingsUtils { return AppConstants.PANORAMIO_BULK_SIZE_DEF_VALUE; } } + + public static Double fetchRadiusLimit(Context ctx) { + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(ctx); + final String prefWikiRadius = sharedPreferences.getString("pref_wiki_radius", String.valueOf(WIKI_DEF_RADIUS)); + lg.debug("Pref wiki radius limit {}", prefWikiRadius); + return NumberUtils.safeParseDouble(prefWikiRadius)*1000.0; // in m, settings are in km unit + } + + public static Long fetchSearchLimit(Context ctx) { + SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(ctx); + final String prefWikiResultsLimit = sharedPreferences.getString("pref_wiki_limit", String.valueOf(WIKI_DEF_LIMIT)); + lg.debug("Pref wiki search results limit {}", prefWikiResultsLimit); + return NumberUtils.safeParseLong(prefWikiResultsLimit); + } }