From 1bd1ca9dc1045669444dce24b49f6441e5623ef6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20P=C3=B3=C5=82grabia?= Date: Mon, 19 Sep 2016 23:03:08 +0200 Subject: [PATCH] Fixes to basic navigation. --- .../urbanexplorer/MainActivity.java | 23 +++++++---- .../callbacks/WikiInfoRunBrowserCallback.java | 9 ++++- .../urbanexplorer/dto/MainActivityState.java | 40 +++++++++++++++++++ .../urbanexplorer/fragments/HomeFragment.java | 8 +++- .../fragments/WikiLocationsFragment.java | 2 +- .../urbanexplorer/utils/HelperUtils.java | 4 ++ 6 files changed, 75 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java index d43ead5..76b79a8 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java @@ -43,7 +43,7 @@ public class MainActivity extends ActionBarActivity { private GestureDetector.OnGestureListener swipeHandler; private PanoramioImageInfo photoInfo; private ProgressDialog progressDlg; - private MainActivityState oldFragmentId = MainActivityState.PANORAMIO + private MainActivityState oldFrag = MainActivityState.PANORAMIO_SHOWER; private boolean savedConfiguration; private static final Map fragTags = new HashMap<>(); @@ -78,7 +78,7 @@ public class MainActivity extends ActionBarActivity { lg.trace("onCreate"); setContentView(R.layout.activity_main); - HelperUtils.initErrorAndDebugHanlers(); + HelperUtils.initErrorAndDebugHanlers(this); NetUtils.setGlobalProxyAuth(this); currFrag = MainActivityState.PANORAMIO; @@ -198,9 +198,12 @@ public class MainActivity extends ActionBarActivity { private void switchFragment() { + if (currFrag == oldFrag) { + return; + } + if (!savedConfiguration) { photoInfo = null; - currFrag = MainActivityState.PANORAMIO; } switch (currFrag) { @@ -228,9 +231,9 @@ public class MainActivity extends ActionBarActivity { FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction ctx = fragmentManager.beginTransaction(); - lg.trace("old newFragment id: {}, current newFragment id: {}", oldFragmentId, currFrag); + lg.trace("old newFragment id: {}, current newFragment id: {}", oldFrag, currFrag); - HelperUtils.appendEffectToTransition(ctx, oldFragmentId, currFrag); + HelperUtils.appendEffectToTransition(ctx, oldFrag, currFrag); HelperUtils.traceAllAvailableFragments(fragmentManager); lg.trace("Trying to search newFragment by tag {}", tag); @@ -261,18 +264,22 @@ public class MainActivity extends ActionBarActivity { public void swipeLeft() { lg.debug("Swiped left"); - changeCurrentFragId((int)Math.max(AppConstants.MIN_FRAGMENT_ID, currFrag -1)); + changeCurrentFragId(currFrag.prev()); switchFragment(); } private void changeCurrentFragId(MainActivityState nextFragmentId) { - oldFragmentId = currFrag; + if (nextFragmentId == null) { + oldFrag = currFrag; + return; + } + oldFrag = currFrag; currFrag = nextFragmentId; } public void swipeRight() { lg.debug("Swiped right"); - changeCurrentFragId((int)Math.min(AppConstants.MAX_FRAGMENT_ID, currFrag +1)); + changeCurrentFragId(currFrag.next()); switchFragment(); } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/WikiInfoRunBrowserCallback.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/WikiInfoRunBrowserCallback.java index 06f1170..a162c10 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/WikiInfoRunBrowserCallback.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/WikiInfoRunBrowserCallback.java @@ -2,6 +2,7 @@ package pl.tpolgrabia.urbanexplorer.callbacks; import android.content.Intent; import android.net.Uri; +import android.support.v4.app.FragmentActivity; import android.widget.Toast; import com.androidquery.callback.AjaxCallback; import com.androidquery.callback.AjaxStatus; @@ -44,7 +45,13 @@ public class WikiInfoRunBrowserCallback extends AjaxCallback { .getString("fullurl"); Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(wikiUrl)); - wikiLocationsFragment.startActivity(intent); + FragmentActivity activity = wikiLocationsFragment.getActivity(); + if (activity == null) { + lg.debug("Fragment is not attached to activity"); + return; + } + + activity.startActivity(intent); } catch (JSONException e) { lg.error("JSON error", e); } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/dto/MainActivityState.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/dto/MainActivityState.java index 9f95e55..2092fc1 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/dto/MainActivityState.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/dto/MainActivityState.java @@ -1,5 +1,7 @@ package pl.tpolgrabia.urbanexplorer.dto; +import pl.tpolgrabia.urbanexplorer.MainActivity; + /** * Created by tpolgrabia on 19.09.16. */ @@ -17,4 +19,42 @@ public enum MainActivityState { public Integer getOrder() { return order; } + + public MainActivityState prev() { + int val = Integer.MIN_VALUE; + MainActivityState greatestSmaller = null; + + for (MainActivityState state : values()) { + if (state.getOrder() >= order || state.getOrder() < 0) { + continue; + } + + // we need the greatest smaller + + if (state.getOrder() > val) { + val = state.getOrder(); + greatestSmaller = state; + } + } + return greatestSmaller; + } + + public MainActivityState next() { + int val = Integer.MAX_VALUE; + MainActivityState smallestGreater = null; + + for (MainActivityState state : values()) { + if (state.getOrder() <= order || state.getOrder() < 0) { + continue; + } + + // we need the smallest greater + + if (state.getOrder() < val) { + val = state.getOrder(); + smallestGreater = state; + } + } + return smallestGreater; + } } 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 8d69e6d..20738ff 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java @@ -295,7 +295,13 @@ public class HomeFragment extends Fragment implements Refreshable { return; } - ListView locations = (ListView) getView().findViewById(R.id.locations); + final View view = getView(); + if (view == null) { + lg.debug("View still not initialized"); + return; + } + + ListView locations = (ListView) view.findViewById(R.id.locations); if (locations == null) { lg.trace("Empty locations"); return; 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 00948c9..7f7b354 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java @@ -49,7 +49,7 @@ public class WikiLocationsFragment extends Fragment implements Refreshable { private static final String WIKI_APP_OBJECTS = "WIKI_APP_OBJECTS"; private LocationManager locationService; private TextView currentLocation; - private ArrayList appObjects; + private ArrayList appObjects = new ArrayList<>(); private int lastFetchSize = -1; public WikiLocationsFragment() { 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 a75320b..d6332cd 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/HelperUtils.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/HelperUtils.java @@ -74,6 +74,10 @@ public class HelperUtils { } public static void appendEffectToTransition(FragmentTransaction ctx, MainActivityState old, MainActivityState curr) { + if (old.getOrder() == -1 || curr.getOrder() == -1) { + return; + } + if (old != curr) { if (curr.getOrder() < old.getOrder()) { // slide left animation