diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java index e7b1d70..d43ead5 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java @@ -15,6 +15,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import pl.tpolgrabia.urbanexplorer.activities.SettingsActivity; import pl.tpolgrabia.urbanexplorer.callbacks.StandardLocationListener; +import pl.tpolgrabia.urbanexplorer.dto.MainActivityState; import pl.tpolgrabia.urbanexplorer.dto.panoramio.PanoramioImageInfo; import pl.tpolgrabia.urbanexplorer.fragments.HomeFragment; import pl.tpolgrabia.urbanexplorer.fragments.PanoramioShowerFragment; @@ -36,13 +37,13 @@ public class MainActivity extends ActionBarActivity { public static DisplayImageOptions options; private GestureDetectorCompat gestureDetector; - private int currentFragmentId = 0; + private MainActivityState currFrag = MainActivityState.PANORAMIO; private StandardLocationListener locationCallback; private boolean locationServicesActivated = false; private GestureDetector.OnGestureListener swipeHandler; private PanoramioImageInfo photoInfo; private ProgressDialog progressDlg; - private int oldFragmentId = 0; + private MainActivityState oldFragmentId = MainActivityState.PANORAMIO private boolean savedConfiguration; private static final Map fragTags = new HashMap<>(); @@ -80,7 +81,7 @@ public class MainActivity extends ActionBarActivity { HelperUtils.initErrorAndDebugHanlers(); NetUtils.setGlobalProxyAuth(this); - currentFragmentId = 0; + currFrag = MainActivityState.PANORAMIO; progressDlg = new ProgressDialog(this); progressDlg.setCancelable(false); @@ -92,9 +93,12 @@ public class MainActivity extends ActionBarActivity { locationCallback = new StandardLocationListener(); // 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); - currentFragmentId = fragId == null ? 0 : fragId; + currFrag = fragId == null ? MainActivityState.PANORAMIO : fragId; lg.trace("Set final frag id: {}", fragId); photoInfo = savedInstanceState != null ? (PanoramioImageInfo) savedInstanceState.getSerializable(AppConstants.PHOTO_INFO) : null; savedConfiguration = savedInstanceState != null ? savedInstanceState.getBoolean(AppConstants.SAVED_CONFIG_KEY) : false; @@ -105,6 +109,20 @@ public class MainActivity extends ActionBarActivity { 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 public boolean onCreateOptionsMenu(Menu menu) { MenuInflater menuInflater = getMenuInflater(); @@ -130,7 +148,7 @@ public class MainActivity extends ActionBarActivity { } private void refreshFragment() { - final String tag = fragTags.get(currentFragmentId); + final String tag = fragTags.get(currFrag); if (tag == null) { lg.warn("Unknown fragment id"); return; @@ -182,21 +200,20 @@ public class MainActivity extends ActionBarActivity { if (!savedConfiguration) { photoInfo = null; + currFrag = MainActivityState.PANORAMIO; } - if (photoInfo != null) { - switchToPhoto(photoInfo); - return; - } - - switch (currentFragmentId) { - case AppConstants.HOME_FRAGMENT_ID: + 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"); - final HomeFragment fragment = new HomeFragment(); - switchFragment(fragment, HomeFragment.TAG); + switchFragment(new HomeFragment(), HomeFragment.TAG); break; - case AppConstants.WIKI_FRAGMENT_ID: + case WIKI: // switch to wiki fragment lg.debug("Switching to wiki fragment"); switchFragment(new WikiLocationsFragment(), WikiLocationsFragment.TAG); @@ -211,9 +228,9 @@ public class MainActivity extends ActionBarActivity { FragmentManager fragmentManager = getSupportFragmentManager(); 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); lg.trace("Trying to search newFragment by tag {}", tag); @@ -244,18 +261,18 @@ public class MainActivity extends ActionBarActivity { public void swipeLeft() { 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(); } - private void changeCurrentFragId(int nextFragmentId) { - oldFragmentId = currentFragmentId; - currentFragmentId = nextFragmentId; + private void changeCurrentFragId(MainActivityState nextFragmentId) { + oldFragmentId = currFrag; + currFrag = nextFragmentId; } public void swipeRight() { 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(); } @@ -275,7 +292,6 @@ public class MainActivity extends ActionBarActivity { } savedConfiguration = false; - photoInfo = null; } @Override @@ -313,11 +329,11 @@ public class MainActivity extends ActionBarActivity { @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); - lg.trace("1 Saving current fragment id: {}", currentFragmentId); - outState.putSerializable(AppConstants.FRAG_ID, currentFragmentId); + lg.trace("1 Saving current fragment id: {}", currFrag); + outState.putSerializable(AppConstants.FRAG_ID, currFrag); outState.putSerializable(AppConstants.PHOTO_INFO, photoInfo); outState.putBoolean(AppConstants.SAVED_CONFIG_KEY, true); - lg.trace("2 Saving current fragment id: {}", currentFragmentId); + lg.trace("2 Saving current fragment id: {}", currFrag); } @Override diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/dto/MainActivityState.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/dto/MainActivityState.java new file mode 100644 index 0000000..9f95e55 --- /dev/null +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/dto/MainActivityState.java @@ -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; + } +} diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/HelperUtils.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/HelperUtils.java index 8c88e16..a75320b 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/HelperUtils.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/HelperUtils.java @@ -23,6 +23,7 @@ import pl.tpolgrabia.urbanexplorer.AppConstants; import pl.tpolgrabia.urbanexplorer.AppStage; import pl.tpolgrabia.urbanexplorer.MainActivity; import pl.tpolgrabia.urbanexplorer.R; +import pl.tpolgrabia.urbanexplorer.dto.MainActivityState; import java.util.List; @@ -72,9 +73,9 @@ public class HelperUtils { 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 (curr < old) { + if (curr.getOrder() < old.getOrder()) { // slide left animation lg.trace("sliding left animation"); ctx.setCustomAnimations(