From 2fba983ec15ab108a3c5f273c41f967cc353555a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20P=C3=B3=C5=82grabia?= Date: Wed, 21 Sep 2016 23:32:30 +0200 Subject: [PATCH] Refactoring source code - some cleanups -> making smaller some classes. --- .../urbanexplorer/MainActivity.java | 36 ++++++----- .../callbacks/GeocodedLocationCallback.java | 26 ++++++++ .../callbacks/PanoramioLocationCallback.java | 34 ++++++++++ .../callbacks/PanoramioProviderCallback.java | 25 ++++++++ .../urbanexplorer/fragments/HomeFragment.java | 62 +++++-------------- .../PanoramioItemLongClickHandler.java | 31 ++++++++++ .../PanoramioShowerSwitchHandler.java | 24 +++++++ .../handlers/PanoramioSwitchHandler.java | 25 ++++++++ .../handlers/WikiSwitchHandler.java | 24 +++++++ 9 files changed, 226 insertions(+), 61 deletions(-) create mode 100644 app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/GeocodedLocationCallback.java create mode 100644 app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/PanoramioLocationCallback.java create mode 100644 app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/PanoramioProviderCallback.java create mode 100644 app/src/main/java/pl/tpolgrabia/urbanexplorer/handlers/PanoramioItemLongClickHandler.java create mode 100644 app/src/main/java/pl/tpolgrabia/urbanexplorer/handlers/PanoramioShowerSwitchHandler.java create mode 100644 app/src/main/java/pl/tpolgrabia/urbanexplorer/handlers/PanoramioSwitchHandler.java create mode 100644 app/src/main/java/pl/tpolgrabia/urbanexplorer/handlers/WikiSwitchHandler.java diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java index 9d40e77..b48b171 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java @@ -21,7 +21,10 @@ import pl.tpolgrabia.urbanexplorer.fragments.HomeFragment; import pl.tpolgrabia.urbanexplorer.fragments.PanoramioShowerFragment; import pl.tpolgrabia.urbanexplorer.fragments.Refreshable; import pl.tpolgrabia.urbanexplorer.fragments.WikiLocationsFragment; +import pl.tpolgrabia.urbanexplorer.handlers.PanoramioShowerSwitchHandler; +import pl.tpolgrabia.urbanexplorer.handlers.PanoramioSwitchHandler; import pl.tpolgrabia.urbanexplorer.handlers.SwipeHandler; +import pl.tpolgrabia.urbanexplorer.handlers.WikiSwitchHandler; import pl.tpolgrabia.urbanexplorer.utils.HelperUtils; import pl.tpolgrabia.urbanexplorer.utils.LocationUtils; import pl.tpolgrabia.urbanexplorer.utils.NetUtils; @@ -47,6 +50,7 @@ public class MainActivity extends ActionBarActivity { private ProgressDialog progressDlg; private MainActivityState oldFrag = MainActivityState.PANORAMIO_SHOWER; private boolean savedConfiguration; + private static final Map switchFragmentActions = new HashMap<>(); private static final Map fragTags = new HashMap<>(); @@ -55,6 +59,12 @@ public class MainActivity extends ActionBarActivity { fragTags.put(AppConstants.WIKI_FRAGMENT_ID, WikiLocationsFragment.TAG); } + public MainActivity() { + switchFragmentActions.put(MainActivityState.PANORAMIO_SHOWER, new PanoramioShowerSwitchHandler(this)); + switchFragmentActions.put(MainActivityState.PANORAMIO, new PanoramioSwitchHandler(this)); + switchFragmentActions.put(MainActivityState.WIKI, new WikiSwitchHandler(this)); + } + private List photos; public StandardLocationListener getLocationCallback() { @@ -213,28 +223,18 @@ public class MainActivity extends ActionBarActivity { photoInfo = null; } - switch (currFrag) { - case PANORAMIO_SHOWER: - lg.debug("Switching to panoramio shower"); - switchToPhoto(photoInfo); - break; - case PANORAMIO: - // switch to home fragment - lg.debug("Switching to home fragment"); - switchFragment(new HomeFragment(), HomeFragment.TAG); - break; - case WIKI: - // switch to wiki fragment - lg.debug("Switching to wiki fragment"); - switchFragment(new WikiLocationsFragment(), WikiLocationsFragment.TAG); - break; + Runnable switchAction = switchFragmentActions.get(currFrag); + if (switchAction != null) { + switchAction.run(); + } else { + lg.warn("There is no valid switch action to the given fragment {}", currFrag); } savedConfiguration = false; } - private void switchFragment(Fragment newFragment, String tag) { + public void switchFragment(Fragment newFragment, String tag) { FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction ctx = fragmentManager.beginTransaction(); @@ -369,4 +369,8 @@ public class MainActivity extends ActionBarActivity { public void setPhotos(List photos) { this.photos = photos; } + + public PanoramioImageInfo getPhotoInfo() { + return photoInfo; + } } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/GeocodedLocationCallback.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/GeocodedLocationCallback.java new file mode 100644 index 0000000..d0b1444 --- /dev/null +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/GeocodedLocationCallback.java @@ -0,0 +1,26 @@ +package pl.tpolgrabia.urbanexplorer.callbacks; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import pl.tpolgrabia.urbanexplorer.fragments.HomeFragment; + +/** + * Created by tpolgrabia on 21.09.16. + */ +public class GeocodedLocationCallback implements LocationGeoCoderCallback { + private static final Logger lg = LoggerFactory.getLogger(GeocodedLocationCallback.class); + private HomeFragment homeFragment; + + public GeocodedLocationCallback(HomeFragment homeFragment) { + this.homeFragment = homeFragment; + } + + @Override + public void callback(int code, String message, String googleStatus, String geocodedLocation) { + lg.debug("Geocoded result code {}, message {}, status: {}, value {}", + code, message, googleStatus, geocodedLocation); + + homeFragment.setCurrentGeocodedLocation(geocodedLocation); + homeFragment.updateLocationInfo(); + } +} diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/PanoramioLocationCallback.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/PanoramioLocationCallback.java new file mode 100644 index 0000000..d0a5c86 --- /dev/null +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/PanoramioLocationCallback.java @@ -0,0 +1,34 @@ +package pl.tpolgrabia.urbanexplorer.callbacks; + +import android.location.Location; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import pl.tpolgrabia.urbanexplorer.dto.panoramio.PanoramioImageInfo; +import pl.tpolgrabia.urbanexplorer.fragments.HomeFragment; + +import java.util.ArrayList; + +/** + * Created by tpolgrabia on 21.09.16. + */ +public class PanoramioLocationCallback implements StandardLocationListenerCallback { + private static final Logger lg = LoggerFactory.getLogger(PanoramioLocationCallback.class); + private HomeFragment homeFragment; + + public PanoramioLocationCallback(HomeFragment homeFragment) { + this.homeFragment = homeFragment; + } + + @Override + public void callback(Location location) { + homeFragment.setNoMorePhotos(false); + homeFragment.setPhotos(new ArrayList()); + homeFragment.setCurrentGeocodedLocation(null); + homeFragment.updateGeocodedLocation(); + try { + homeFragment.fetchAdditionalPhotos(); + } catch (InterruptedException e) { + lg.error("Failed trying acquring lock to load photos", e); + } + } +} diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/PanoramioProviderCallback.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/PanoramioProviderCallback.java new file mode 100644 index 0000000..51a07bf --- /dev/null +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/PanoramioProviderCallback.java @@ -0,0 +1,25 @@ +package pl.tpolgrabia.urbanexplorer.callbacks; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import pl.tpolgrabia.urbanexplorer.fragments.HomeFragment; + +/** + * Created by tpolgrabia on 21.09.16. + */ +public class PanoramioProviderCallback implements ProviderStatusCallback { + private static final Logger lg = LoggerFactory.getLogger(PanoramioProviderCallback.class); + private HomeFragment homeFragment; + + public PanoramioProviderCallback(HomeFragment homeFragment) { + this.homeFragment = homeFragment; + } + + @Override + public void callback(String provider, boolean enabled) { + if (enabled) { + lg.trace("Handling provider enabling - refreshing panoramio listing"); + homeFragment.fetchPanoramioPhotos(); + } + } +} 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 cdaeb25..c9d33ef 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java @@ -10,7 +10,6 @@ import android.support.v4.app.FragmentActivity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.AdapterView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; @@ -24,6 +23,7 @@ import pl.tpolgrabia.urbanexplorer.R; import pl.tpolgrabia.urbanexplorer.callbacks.*; import pl.tpolgrabia.urbanexplorer.dto.panoramio.PanoramioCacheDto; import pl.tpolgrabia.urbanexplorer.dto.panoramio.PanoramioImageInfo; +import pl.tpolgrabia.urbanexplorer.handlers.PanoramioItemLongClickHandler; import pl.tpolgrabia.urbanexplorer.handlers.PanoramioLocationsScrollListener; import pl.tpolgrabia.urbanexplorer.utils.LocationUtils; import pl.tpolgrabia.urbanexplorer.utils.PanoramioUtils; @@ -88,33 +88,12 @@ public class HomeFragment extends Fragment implements Refreshable { private void initLocationCallback() { MainActivity mainActivity = ((MainActivity) getActivity()); mainActivity.getLocationCallback() - .addCallback(new StandardLocationListenerCallback() { - @Override - public void callback(Location location) { - noMorePhotos = false; - photos = new ArrayList<>(); - currentGeocodedLocation = null; - updateGeocodedLocation(); - try { - fetchAdditionalPhotos(); - } catch (InterruptedException e) { - lg.error("Failed trying acquring lock to load photos", e); - } - } - }); + .addCallback(new PanoramioLocationCallback(this)); mainActivity.getLocationCallback() - .addProviderCallback(new ProviderStatusCallback() { - @Override - public void callback(String provider, boolean enabled) { - if (enabled) { - lg.trace("Handling provider enabling - refreshing panoramio listing"); - fetchPanoramioPhotos(); - } - } - }); + .addProviderCallback(new PanoramioProviderCallback(this)); } - private void updateGeocodedLocation() { + public void updateGeocodedLocation() { if (getActivity() == null) { lg.debug("Activity still not attached"); return; @@ -127,16 +106,10 @@ public class HomeFragment extends Fragment implements Refreshable { return; } - LocationUtils.getGeoCodedLocation(getActivity(), currLocation.getLatitude(), currLocation.getLongitude(), new LocationGeoCoderCallback() { - @Override - public void callback(int code, String message, String googleStatus, String geocodedLocation) { - lg.debug("Geocoded result code {}, message {}, status: {}, value {}", - code, message, googleStatus, geocodedLocation); - - currentGeocodedLocation = geocodedLocation; - updateLocationInfo(); - } - }); + LocationUtils.getGeoCodedLocation(getActivity(), + currLocation.getLatitude(), + currLocation.getLongitude(), + new GeocodedLocationCallback(this)); } @@ -155,16 +128,7 @@ public class HomeFragment extends Fragment implements Refreshable { inflatedView = inflater.inflate(R.layout.fragment_home, container, false); ListView locations = (ListView)inflatedView.findViewById(R.id.locations); final ListView finalLocations = locations; - locations.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { - @Override - public boolean onItemLongClick(AdapterView adapterView, View view, int pos, long rowId) { - PanoramioAdapter panAdapter = (PanoramioAdapter) finalLocations.getAdapter(); - PanoramioImageInfo photoInfo = panAdapter.getItem(pos); - MainActivity activity = (MainActivity) getActivity(); - activity.switchToPhoto(photoInfo); - return false; - } - }); + locations.setOnItemLongClickListener(new PanoramioItemLongClickHandler(this, finalLocations)); initialized = true; @@ -459,4 +423,12 @@ public class HomeFragment extends Fragment implements Refreshable { photos.addAll(images); } + public void setCurrentGeocodedLocation(String currentGeocodedLocation) { + this.currentGeocodedLocation = currentGeocodedLocation; + } + + public void setPhotos(ArrayList photos) { + this.photos = photos; + } + } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/handlers/PanoramioItemLongClickHandler.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/handlers/PanoramioItemLongClickHandler.java new file mode 100644 index 0000000..92361e8 --- /dev/null +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/handlers/PanoramioItemLongClickHandler.java @@ -0,0 +1,31 @@ +package pl.tpolgrabia.urbanexplorer.handlers; + +import android.view.View; +import android.widget.AdapterView; +import android.widget.ListView; +import pl.tpolgrabia.urbanexplorer.MainActivity; +import pl.tpolgrabia.urbanexplorer.dto.panoramio.PanoramioImageInfo; +import pl.tpolgrabia.urbanexplorer.fragments.HomeFragment; +import pl.tpolgrabia.urbanexplorer.fragments.PanoramioAdapter; + +/** + * Created by tpolgrabia on 21.09.16. + */ +public class PanoramioItemLongClickHandler implements AdapterView.OnItemLongClickListener { + private HomeFragment homeFragment; + private final ListView finalLocations; + + public PanoramioItemLongClickHandler(HomeFragment homeFragment, ListView finalLocations) { + this.homeFragment = homeFragment; + this.finalLocations = finalLocations; + } + + @Override + public boolean onItemLongClick(AdapterView adapterView, View view, int pos, long rowId) { + PanoramioAdapter panAdapter = (PanoramioAdapter) finalLocations.getAdapter(); + PanoramioImageInfo photoInfo = panAdapter.getItem(pos); + MainActivity activity = (MainActivity) homeFragment.getActivity(); + activity.switchToPhoto(photoInfo); + return false; + } +} diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/handlers/PanoramioShowerSwitchHandler.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/handlers/PanoramioShowerSwitchHandler.java new file mode 100644 index 0000000..d710a2c --- /dev/null +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/handlers/PanoramioShowerSwitchHandler.java @@ -0,0 +1,24 @@ +package pl.tpolgrabia.urbanexplorer.handlers; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import pl.tpolgrabia.urbanexplorer.MainActivity; + +/** + * Created by tpolgrabia on 21.09.16. + */ +public class PanoramioShowerSwitchHandler implements Runnable { + + private static final Logger lg = LoggerFactory.getLogger(PanoramioShowerSwitchHandler.class); + private final MainActivity mainActivity; + + public PanoramioShowerSwitchHandler(MainActivity mainActivity) { + this.mainActivity = mainActivity; + } + + @Override + public void run() { + lg.debug("Switching to panoramio shower"); + mainActivity.switchToPhoto(mainActivity.getPhotoInfo()); + } +} diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/handlers/PanoramioSwitchHandler.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/handlers/PanoramioSwitchHandler.java new file mode 100644 index 0000000..12d5ca5 --- /dev/null +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/handlers/PanoramioSwitchHandler.java @@ -0,0 +1,25 @@ +package pl.tpolgrabia.urbanexplorer.handlers; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import pl.tpolgrabia.urbanexplorer.MainActivity; +import pl.tpolgrabia.urbanexplorer.fragments.HomeFragment; + +/** + * Created by tpolgrabia on 21.09.16. + */ +public class PanoramioSwitchHandler implements Runnable { + + private static final Logger lg = LoggerFactory.getLogger(PanoramioSwitchHandler.class); + private final MainActivity mainActivity; + + public PanoramioSwitchHandler(MainActivity mainActivity) { + this.mainActivity = mainActivity; + } + + @Override + public void run() { + lg.debug("Switching to home fragment"); + mainActivity.switchFragment(new HomeFragment(), HomeFragment.TAG); + } +} diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/handlers/WikiSwitchHandler.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/handlers/WikiSwitchHandler.java new file mode 100644 index 0000000..0416045 --- /dev/null +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/handlers/WikiSwitchHandler.java @@ -0,0 +1,24 @@ +package pl.tpolgrabia.urbanexplorer.handlers; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import pl.tpolgrabia.urbanexplorer.MainActivity; +import pl.tpolgrabia.urbanexplorer.fragments.WikiLocationsFragment; + +/** + * Created by tpolgrabia on 21.09.16. + */ +public class WikiSwitchHandler implements Runnable { + private final MainActivity mainActivity; + private static final Logger lg = LoggerFactory.getLogger(WikiSwitchHandler.class); + + public WikiSwitchHandler(MainActivity mainActivity) { + this.mainActivity = mainActivity; + } + + @Override + public void run() { + lg.debug("Switching to wiki fragment"); + mainActivity.switchFragment(new WikiLocationsFragment(), WikiLocationsFragment.TAG); + } +}