Cleaning code.

master
Tomasz Półgrabia 2016-09-19 22:32:10 +02:00
parent 3ef358bfc3
commit 9e3d5428ab
3 changed files with 66 additions and 29 deletions

View File

@ -15,6 +15,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import pl.tpolgrabia.urbanexplorer.activities.SettingsActivity; import pl.tpolgrabia.urbanexplorer.activities.SettingsActivity;
import pl.tpolgrabia.urbanexplorer.callbacks.StandardLocationListener; import pl.tpolgrabia.urbanexplorer.callbacks.StandardLocationListener;
import pl.tpolgrabia.urbanexplorer.dto.MainActivityState;
import pl.tpolgrabia.urbanexplorer.dto.panoramio.PanoramioImageInfo; import pl.tpolgrabia.urbanexplorer.dto.panoramio.PanoramioImageInfo;
import pl.tpolgrabia.urbanexplorer.fragments.HomeFragment; import pl.tpolgrabia.urbanexplorer.fragments.HomeFragment;
import pl.tpolgrabia.urbanexplorer.fragments.PanoramioShowerFragment; import pl.tpolgrabia.urbanexplorer.fragments.PanoramioShowerFragment;
@ -36,13 +37,13 @@ public class MainActivity extends ActionBarActivity {
public static DisplayImageOptions options; public static DisplayImageOptions options;
private GestureDetectorCompat gestureDetector; private GestureDetectorCompat gestureDetector;
private int currentFragmentId = 0; private MainActivityState currFrag = MainActivityState.PANORAMIO;
private StandardLocationListener locationCallback; private StandardLocationListener locationCallback;
private boolean locationServicesActivated = false; private boolean locationServicesActivated = false;
private GestureDetector.OnGestureListener swipeHandler; private GestureDetector.OnGestureListener swipeHandler;
private PanoramioImageInfo photoInfo; private PanoramioImageInfo photoInfo;
private ProgressDialog progressDlg; private ProgressDialog progressDlg;
private int oldFragmentId = 0; private MainActivityState oldFragmentId = MainActivityState.PANORAMIO
private boolean savedConfiguration; private boolean savedConfiguration;
private static final Map<Integer, String> fragTags = new HashMap<>(); private static final Map<Integer, String> fragTags = new HashMap<>();
@ -80,7 +81,7 @@ public class MainActivity extends ActionBarActivity {
HelperUtils.initErrorAndDebugHanlers(); HelperUtils.initErrorAndDebugHanlers();
NetUtils.setGlobalProxyAuth(this); NetUtils.setGlobalProxyAuth(this);
currentFragmentId = 0; currFrag = MainActivityState.PANORAMIO;
progressDlg = new ProgressDialog(this); progressDlg = new ProgressDialog(this);
progressDlg.setCancelable(false); progressDlg.setCancelable(false);
@ -92,9 +93,12 @@ public class MainActivity extends ActionBarActivity {
locationCallback = new StandardLocationListener(); locationCallback = new StandardLocationListener();
// init fragments // init fragments
Integer fragId = savedInstanceState != null ? savedInstanceState.getInt(AppConstants.FRAG_ID) : null; MainActivityState fragId = savedInstanceState != null
? (MainActivityState)savedInstanceState.getSerializable(AppConstants.FRAG_ID)
: MainActivityState.PANORAMIO;
lg.trace("Restored orig frag id: {}", fragId); lg.trace("Restored orig frag id: {}", fragId);
currentFragmentId = fragId == null ? 0 : fragId; currFrag = fragId == null ? MainActivityState.PANORAMIO : fragId;
lg.trace("Set final frag id: {}", fragId); lg.trace("Set final frag id: {}", fragId);
photoInfo = savedInstanceState != null ? (PanoramioImageInfo) savedInstanceState.getSerializable(AppConstants.PHOTO_INFO) : null; photoInfo = savedInstanceState != null ? (PanoramioImageInfo) savedInstanceState.getSerializable(AppConstants.PHOTO_INFO) : null;
savedConfiguration = savedInstanceState != null ? savedInstanceState.getBoolean(AppConstants.SAVED_CONFIG_KEY) : false; savedConfiguration = savedInstanceState != null ? savedInstanceState.getBoolean(AppConstants.SAVED_CONFIG_KEY) : false;
@ -105,6 +109,20 @@ public class MainActivity extends ActionBarActivity {
HelperUtils.firstTimeNotification(this); HelperUtils.firstTimeNotification(this);
} }
@Override
public void onBackPressed() {
lg.debug("Back pressed");
switch(currFrag) {
case PANORAMIO_SHOWER:
photoInfo = null;
currFrag = MainActivityState.PANORAMIO;
break;
}
super.onBackPressed();
}
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater(); MenuInflater menuInflater = getMenuInflater();
@ -130,7 +148,7 @@ public class MainActivity extends ActionBarActivity {
} }
private void refreshFragment() { private void refreshFragment() {
final String tag = fragTags.get(currentFragmentId); final String tag = fragTags.get(currFrag);
if (tag == null) { if (tag == null) {
lg.warn("Unknown fragment id"); lg.warn("Unknown fragment id");
return; return;
@ -182,21 +200,20 @@ public class MainActivity extends ActionBarActivity {
if (!savedConfiguration) { if (!savedConfiguration) {
photoInfo = null; photoInfo = null;
currFrag = MainActivityState.PANORAMIO;
} }
if (photoInfo != null) { switch (currFrag) {
switchToPhoto(photoInfo); case PANORAMIO_SHOWER:
return; lg.debug("Switching to panoramio shower");
} switchToPhoto(photoInfo);
break;
switch (currentFragmentId) { case PANORAMIO:
case AppConstants.HOME_FRAGMENT_ID:
// switch to home fragment // switch to home fragment
lg.debug("Switching to home fragment"); lg.debug("Switching to home fragment");
final HomeFragment fragment = new HomeFragment(); switchFragment(new HomeFragment(), HomeFragment.TAG);
switchFragment(fragment, HomeFragment.TAG);
break; break;
case AppConstants.WIKI_FRAGMENT_ID: case WIKI:
// switch to wiki fragment // switch to wiki fragment
lg.debug("Switching to wiki fragment"); lg.debug("Switching to wiki fragment");
switchFragment(new WikiLocationsFragment(), WikiLocationsFragment.TAG); switchFragment(new WikiLocationsFragment(), WikiLocationsFragment.TAG);
@ -211,9 +228,9 @@ public class MainActivity extends ActionBarActivity {
FragmentManager fragmentManager = getSupportFragmentManager(); FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ctx = fragmentManager.beginTransaction(); FragmentTransaction ctx = fragmentManager.beginTransaction();
lg.trace("old newFragment id: {}, current newFragment id: {}", oldFragmentId, currentFragmentId); lg.trace("old newFragment id: {}, current newFragment id: {}", oldFragmentId, currFrag);
HelperUtils.appendEffectToTransition(ctx, oldFragmentId, currentFragmentId); HelperUtils.appendEffectToTransition(ctx, oldFragmentId, currFrag);
HelperUtils.traceAllAvailableFragments(fragmentManager); HelperUtils.traceAllAvailableFragments(fragmentManager);
lg.trace("Trying to search newFragment by tag {}", tag); lg.trace("Trying to search newFragment by tag {}", tag);
@ -244,18 +261,18 @@ public class MainActivity extends ActionBarActivity {
public void swipeLeft() { public void swipeLeft() {
lg.debug("Swiped left"); lg.debug("Swiped left");
changeCurrentFragId((int)Math.max(AppConstants.MIN_FRAGMENT_ID, currentFragmentId-1)); changeCurrentFragId((int)Math.max(AppConstants.MIN_FRAGMENT_ID, currFrag -1));
switchFragment(); switchFragment();
} }
private void changeCurrentFragId(int nextFragmentId) { private void changeCurrentFragId(MainActivityState nextFragmentId) {
oldFragmentId = currentFragmentId; oldFragmentId = currFrag;
currentFragmentId = nextFragmentId; currFrag = nextFragmentId;
} }
public void swipeRight() { public void swipeRight() {
lg.debug("Swiped right"); lg.debug("Swiped right");
changeCurrentFragId((int)Math.min(AppConstants.MAX_FRAGMENT_ID, currentFragmentId+1)); changeCurrentFragId((int)Math.min(AppConstants.MAX_FRAGMENT_ID, currFrag +1));
switchFragment(); switchFragment();
} }
@ -275,7 +292,6 @@ public class MainActivity extends ActionBarActivity {
} }
savedConfiguration = false; savedConfiguration = false;
photoInfo = null;
} }
@Override @Override
@ -313,11 +329,11 @@ public class MainActivity extends ActionBarActivity {
@Override @Override
protected void onSaveInstanceState(Bundle outState) { protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
lg.trace("1 Saving current fragment id: {}", currentFragmentId); lg.trace("1 Saving current fragment id: {}", currFrag);
outState.putSerializable(AppConstants.FRAG_ID, currentFragmentId); outState.putSerializable(AppConstants.FRAG_ID, currFrag);
outState.putSerializable(AppConstants.PHOTO_INFO, photoInfo); outState.putSerializable(AppConstants.PHOTO_INFO, photoInfo);
outState.putBoolean(AppConstants.SAVED_CONFIG_KEY, true); outState.putBoolean(AppConstants.SAVED_CONFIG_KEY, true);
lg.trace("2 Saving current fragment id: {}", currentFragmentId); lg.trace("2 Saving current fragment id: {}", currFrag);
} }
@Override @Override

View File

@ -0,0 +1,20 @@
package pl.tpolgrabia.urbanexplorer.dto;
/**
* Created by tpolgrabia on 19.09.16.
*/
public enum MainActivityState {
PANORAMIO(0),
WIKI(1),
PANORAMIO_SHOWER(-1);
private final Integer order;
MainActivityState(Integer order) {
this.order = order;
}
public Integer getOrder() {
return order;
}
}

View File

@ -23,6 +23,7 @@ import pl.tpolgrabia.urbanexplorer.AppConstants;
import pl.tpolgrabia.urbanexplorer.AppStage; import pl.tpolgrabia.urbanexplorer.AppStage;
import pl.tpolgrabia.urbanexplorer.MainActivity; import pl.tpolgrabia.urbanexplorer.MainActivity;
import pl.tpolgrabia.urbanexplorer.R; import pl.tpolgrabia.urbanexplorer.R;
import pl.tpolgrabia.urbanexplorer.dto.MainActivityState;
import java.util.List; import java.util.List;
@ -72,9 +73,9 @@ public class HelperUtils {
ImageLoader.getInstance().init(config); ImageLoader.getInstance().init(config);
} }
public static void appendEffectToTransition(FragmentTransaction ctx, int old, int curr) { public static void appendEffectToTransition(FragmentTransaction ctx, MainActivityState old, MainActivityState curr) {
if (old != curr) { if (old != curr) {
if (curr < old) { if (curr.getOrder() < old.getOrder()) {
// slide left animation // slide left animation
lg.trace("sliding left animation"); lg.trace("sliding left animation");
ctx.setCustomAnimations( ctx.setCustomAnimations(