Refactoring source code - some cleanups -> making smaller some classes.

master
Tomasz Półgrabia 2016-09-21 23:32:30 +02:00
parent c396759cec
commit 2fba983ec1
9 changed files with 226 additions and 61 deletions

View File

@ -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<MainActivityState, Runnable> switchFragmentActions = new HashMap<>();
private static final Map<Integer, String> 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<PanoramioImageInfo> 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<PanoramioImageInfo> photos) {
this.photos = photos;
}
public PanoramioImageInfo getPhotoInfo() {
return photoInfo;
}
}

View File

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

View File

@ -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<PanoramioImageInfo>());
homeFragment.setCurrentGeocodedLocation(null);
homeFragment.updateGeocodedLocation();
try {
homeFragment.fetchAdditionalPhotos();
} catch (InterruptedException e) {
lg.error("Failed trying acquring lock to load photos", e);
}
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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