Cleaning and refactoring WikiLocationsFragment.

master
Tomasz Półgrabia 2016-09-24 11:02:21 +02:00
parent 90c0a70b40
commit 92638eab02
6 changed files with 176 additions and 107 deletions

View File

@ -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<WikiAppObject> objects) {
ArrayList<WikiAppObject> nobjects = new ArrayList<WikiAppObject>(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));
}
}

View File

@ -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<WikiAppObject>());
wikiLocationsFragment.updateLocationInfo();
wikiLocationsFragment.fetchWikiLocations();
}
}

View File

@ -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();
}
}
}

View File

@ -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<WikiAppObject> 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<WikiAppObject> appObjects) {
this.appObjects = appObjects;
}
}

View File

@ -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<WikiAppObject> 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);
}
}
}

View File

@ -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);
}
}