From 48ca02d059718872d1f96d2a3fc74f80c255b822 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20P=C3=B3=C5=82grabia?= Date: Sun, 18 Sep 2016 16:15:22 +0200 Subject: [PATCH] GUI fixes. - fixed hiding progress, when some issues encountered, - limited number of api requests. --- .../urbanexplorer/MainActivity.java | 6 ++ .../activities/SettingsActivity.java | 25 +++++++ .../urbanexplorer/fragments/HomeFragment.java | 67 +++++++++++++------ .../fragments/WikiLocationsFragment.java | 56 +++++++++++++++- .../urbanexplorer/utils/WikiUtils.java | 6 +- 5 files changed, 135 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java index 6f1145c..bdff32e 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java @@ -207,6 +207,12 @@ public class MainActivity extends ActionBarActivity { } private void switchFragment() { + + if (photoInfo != null) { + switchToPhoto(photoInfo); + return; + } + switch (currentFragmentId) { case HOME_FRAGMENT_ID: // switch to home fragment diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/activities/SettingsActivity.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/activities/SettingsActivity.java index 2672d46..62ec4a0 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/activities/SettingsActivity.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/activities/SettingsActivity.java @@ -3,11 +3,15 @@ package pl.tpolgrabia.urbanexplorer.activities; import android.os.Bundle; import android.preference.PreferenceFragment; import android.support.v7.app.ActionBarActivity; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import pl.tpolgrabia.urbanexplorer.R; import pl.tpolgrabia.urbanexplorer.fragments.SettingsFragment; public class SettingsActivity extends ActionBarActivity { + private static final Logger lg = LoggerFactory.getLogger(SettingsActivity.class); + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -19,5 +23,26 @@ public class SettingsActivity extends ActionBarActivity { getFragmentManager().beginTransaction() .replace(R.id.settings_fragments, new SettingsFragment()) .commit(); + + lg.trace("onCreate"); + } + + @Override + protected void onResume() { + super.onResume(); + lg.trace("onResume"); + } + + @Override + protected void onPause() { + super.onPause(); + + lg.trace("onPause"); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + lg.trace("onDestroy"); } } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java index cc27fac..96c73f3 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java @@ -39,7 +39,6 @@ public class HomeFragment extends Fragment { public static final String TAG = HomeFragment.class.getSimpleName(); private static final String PHOTO_LIST = "PHOTO_LIST_KEY"; - private LocationManager locationService; private boolean initialized = false; private View inflatedView; @@ -50,7 +49,14 @@ public class HomeFragment extends Fragment { public int getPanoramioBulkDataSize() { SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); - return sharedPrefs.getInt(AppConstants.PANORAMIO_BULK_SIZE_KEY, AppConstants.PANORAMIO_BULK_SIZE_DEF_VALUE); + final String sValue = sharedPrefs.getString(AppConstants.PANORAMIO_BULK_SIZE_KEY, + String.valueOf(AppConstants.PANORAMIO_BULK_SIZE_DEF_VALUE)); + try { + return Integer.parseInt(sValue); + } catch (NumberFormatException e) { + lg.warn("Invalid panoramio bulk data size {}", sValue, e); + return AppConstants.PANORAMIO_BULK_SIZE_DEF_VALUE; + } } public HomeFragment() { @@ -65,6 +71,7 @@ public class HomeFragment extends Fragment { loading = new Semaphore(1, true); noMorePhotos = false; + updateLocationInfo(); } @Override @@ -97,7 +104,11 @@ public class HomeFragment extends Fragment { // Inflate the layout for this fragment lg.trace("TAG: {}", getTag()); for (Fragment frag : getFragmentManager().getFragments()) { - lg.trace("Fragment TAG {}", frag.getTag()); + if (frag == null) { + lg.trace("Fragment is null"); + } else { + lg.trace("Fragment TAG {}", frag.getTag()); + } } inflatedView = inflater.inflate(R.layout.fragment_home, container, false); ListView locations = (ListView)inflatedView.findViewById(R.id.locations); @@ -125,6 +136,9 @@ public class HomeFragment extends Fragment { final Serializable serializable = savedInstanceState.getSerializable(PHOTO_LIST); lg.trace("Photo list serializable {}", serializable); photos = (ArrayList) serializable; + if (photos == null) { + photos = new ArrayList<>(); + } } } @@ -169,39 +183,48 @@ public class HomeFragment extends Fragment { } }); + ; return inflatedView; } private void fetchAdditionalPhotos() throws InterruptedException { - if (noMorePhotos) { - lg.trace("No more photos - last query was zero result"); - return; - } - - if (!initialized) { - lg.trace("Fetching additional photos blocked till system is initialized"); - return; - } - - - if (getView() == null) { - lg.trace("Application still not initialized"); - return; - } - final FragmentActivity activity = getActivity(); if (activity == null) { lg.trace("Activity shouldn't be null. No headless fragment"); return; } + MainActivity mainActivity = (MainActivity)getActivity(); + + if (noMorePhotos) { + lg.trace("No more photos - last query was zero result"); + mainActivity.hideProgress(); + return; + } + + if (!initialized) { + lg.trace("Fetching additional photos blocked till system is initialized"); + mainActivity.hideProgress(); + return; + } + + + if (getView() == null) { + lg.trace("Application still not initialized"); + mainActivity.hideProgress(); + return; + } + + + LocationManager locationService = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE); final Location location = locationService.getLastKnownLocation(LocationUtils.getDefaultLocation(activity)); if (location == null) { lg.info("Location still not available"); Toast.makeText(activity, "Location still not available", Toast.LENGTH_SHORT).show(); + mainActivity.hideProgress(); return; } @@ -265,9 +288,13 @@ public class HomeFragment extends Fragment { return; } + MainActivity mainActivity = (MainActivity) getActivity(); + + LocationManager locationService = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); final Location location = locationService.getLastKnownLocation(LocationUtils.getDefaultLocation(activity)); if (location == null) { lg.info("Location is still not available"); + mainActivity.hideProgress(); Toast.makeText(getActivity(), "Location is still not available", Toast.LENGTH_SHORT).show(); return; } @@ -351,7 +378,7 @@ public class HomeFragment extends Fragment { lg.warn("Activity should'nt be null. No headless fragment"); return; } - locationService = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE); + LocationManager locationService = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE); Location currLocation = locationService.getLastKnownLocation(LocationUtils.getDefaultLocation(activity)); lg.trace("Current location: {}, locationInfo: {}", currLocation, locationInfo); if (currLocation != null && locationInfo != null) { 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 7bda993..5b7e2d3 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java @@ -28,6 +28,7 @@ import pl.tpolgrabia.urbanexplorer.utils.NumberUtils; import pl.tpolgrabia.urbanexplorer.utils.WikiAppResponseCallback; import pl.tpolgrabia.urbanexplorer.utils.WikiUtils; +import java.util.ArrayList; import java.util.List; import static android.content.Context.LOCATION_SERVICE; @@ -42,13 +43,23 @@ public class WikiLocationsFragment extends Fragment { 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; private TextView currentLocation; + private ArrayList appObjects; + private int lastFetchSize = -1; public WikiLocationsFragment() { // Required empty public constructor } + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + lg.trace("onCreate"); + appObjects = savedInstanceState == null ? new ArrayList() + : (ArrayList)savedInstanceState.getSerializable(WIKI_APP_OBJECTS);; + } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, @@ -57,7 +68,11 @@ public class WikiLocationsFragment extends Fragment { final View inflatedView = inflater.inflate(R.layout.fragment_wiki_locations, container, false); lg.trace("TAG: {}", getTag()); for (Fragment frag : getFragmentManager().getFragments()) { - lg.trace("Fragment TAG {}", frag.getTag()); + if (frag == null) { + lg.trace("Got null fragment"); + } else { + lg.trace("Fragment TAG {}", frag.getTag()); + } } locationService = (LocationManager) getActivity().getSystemService(LOCATION_SERVICE); @@ -67,19 +82,41 @@ public class WikiLocationsFragment extends Fragment { mainActivity.getLocationCallback().addCallback(new StandardLocationListenerCallback() { @Override public void callback(Location location) { + lastFetchSize = -1; + appObjects = new ArrayList<>(); updateLocationInfo(); fetchWikiLocations(); } }); + ListView locations = (ListView) inflatedView.findViewById(R.id.wiki_places); + locations.setOnItemLongClickListener(new FetchWikiLocationsCallback(WikiLocationsFragment.this, appObjects)); + locations.setAdapter(new WikiLocationsAdapter(getActivity(), appObjects)); + return inflatedView; } public void fetchWikiLocations() { lg.trace("Fetch wiki locations"); + + MainActivity mainActivity = (MainActivity) getActivity(); + + if (lastFetchSize == 0) { + lg.trace("There is no results"); + mainActivity.hideProgress(); + return; + } + + if (!appObjects.isEmpty()) { + lg.trace("There are fetched objects"); + mainActivity.hideProgress(); + return; + } + final FragmentActivity activity = getActivity(); if (activity == null) { lg.warn("Activity shouldn't be null. No headless fragment"); + mainActivity.hideProgress(); return; } final Location location = locationService.getLastKnownLocation(LocationUtils.getDefaultLocation(activity)); @@ -87,11 +124,13 @@ public class WikiLocationsFragment extends Fragment { if (location == null) { lg.info("Sorry, location is still not available"); Toast.makeText(activity, "Sorry, location is still not available", Toast.LENGTH_SHORT).show(); + mainActivity.hideProgress(); return; } if (getView() == null) { lg.info("Wiki view is not yet initialized"); + mainActivity.hideProgress(); return; } @@ -101,8 +140,12 @@ public class WikiLocationsFragment extends Fragment { fetchRadiusLimit(), fetchSearchLimit(), new WikiAppResponseCallback() { + @Override - public void callback(WikiStatus status, final List appObjects) { + 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" + @@ -115,7 +158,7 @@ public class WikiLocationsFragment extends Fragment { ListView locations = (ListView) getView().findViewById(R.id.wiki_places); locations.setOnItemLongClickListener(new FetchWikiLocationsCallback(WikiLocationsFragment.this, appObjects)); locations.setAdapter(new WikiLocationsAdapter(activity, appObjects)); - if (appObjects.isEmpty()) { + if (objects.isEmpty()) { Toast.makeText(getActivity(), "No results", Toast.LENGTH_SHORT).show(); } @@ -171,4 +214,11 @@ public class WikiLocationsFragment extends Fragment { super.onPause(); } + @Override + public void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + lg.trace("onSaveInstanceState"); + + outState.putSerializable(WIKI_APP_OBJECTS, appObjects); + } } 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 dbb802b..746146b 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/WikiUtils.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/WikiUtils.java @@ -175,13 +175,15 @@ public class WikiUtils { } AQuery aq = NetUtils.createProxyAQueryInstance(ctx); - aq.ajax("https://en.wikipedia.org/w/api.php" + + final String queryUrl = "https://en.wikipedia.org/w/api.php" + "?action=query" + "&list=geosearch" + "&gscoord=" + latitude + "%7C" + longitude + "&gsradius=" + String.format("%.2f", radius) + "&gslimit=" + limit + - "&format=json", JSONObject.class, new AjaxCallback() { + "&format=json"; + lg.trace("GeoSearch wiki API url: {}", queryUrl); + aq.ajax(queryUrl, JSONObject.class, new AjaxCallback() { @Override public void callback(String url, JSONObject object, AjaxStatus status) { lg.trace("Finished waiting for {} with status {}:{} and response: {}",