Merge pull request #849 from Bars107/sherpafyrefactor

Refactored states. Fixed null pointer on back pressed.
This commit is contained in:
vshcherb 2014-08-14 16:22:46 +02:00
commit 7cd33ebfdf

View file

@ -47,14 +47,19 @@ import com.actionbarsherlock.view.MenuItem.OnMenuItemClickListener;
*/ */
public class TourViewActivity extends SherlockFragmentActivity { public class TourViewActivity extends SherlockFragmentActivity {
private static final int STATE_LOADING = -1; private enum viewState {
private static final int STATE_SELECT_TOUR = 1; STATE_LOADING,
private static final int STATE_TOUR_VIEW = 2; STATE_SELECT_TOUR,
private static final int STATE_STAGE_OVERVIEW = 3; STATE_TOUR_VIEW,
private static final int STATE_DETAILED_OVERVIEW = 4; STATE_STAGE_OVERVIEW,
private static final int STATE_DETAILED_INSTRUCTIONS = 5; STATE_DETAILED_OVERVIEW,
private static final int STAGE_GALLERY = 6; STATE_DETAILED_INSTRUCTIONS,
private static int state = STATE_LOADING; STAGE_GALLERY,
STAGE_FAVORITES
}
private static viewState state = viewState.STATE_LOADING;
public static final int APP_EXIT_CODE = 4; public static final int APP_EXIT_CODE = 4;
public static final String APP_EXIT_KEY = "APP_EXIT_KEY"; public static final String APP_EXIT_KEY = "APP_EXIT_KEY";
@ -117,10 +122,10 @@ public class TourViewActivity extends SherlockFragmentActivity {
if (getMyApplication().isApplicationInitializing()) { if (getMyApplication().isApplicationInitializing()) {
FragmentManager fragmentManager = getSupportFragmentManager(); FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, new SherpafyLoadingFragment()).commit(); fragmentManager.beginTransaction().replace(R.id.content_frame, new SherpafyLoadingFragment()).commit();
} else if (state == STATE_DETAILED_INSTRUCTIONS) { } else if (state == viewState.STATE_DETAILED_INSTRUCTIONS) {
TourInformation tour = (TourInformation) selectedItem; TourInformation tour = (TourInformation) selectedItem;
showDetailedInstructions(getString(R.string.sherpafy_instructions), tour.getInstructions()); showDetailedInstructions(getString(R.string.sherpafy_instructions), tour.getInstructions());
} else if (state == STATE_DETAILED_OVERVIEW) { } else if (state == viewState.STATE_DETAILED_OVERVIEW) {
TourInformation tour = (TourInformation) selectedItem; TourInformation tour = (TourInformation) selectedItem;
showDetailedInstructions(getString(R.string.sherpafy_overview), tour.getFulldescription()); showDetailedInstructions(getString(R.string.sherpafy_overview), tour.getFulldescription());
} else { } else {
@ -130,16 +135,16 @@ public class TourViewActivity extends SherlockFragmentActivity {
@Override @Override
public void onBackPressed() { public void onBackPressed() {
if (state == STATE_SELECT_TOUR) { if (state == viewState.STATE_SELECT_TOUR) {
super.onBackPressed(); super.onBackPressed();
} else if (state == STATE_TOUR_VIEW) { } else if (state == viewState.STATE_TOUR_VIEW) {
selectMenu(R.string.sherpafy_tours); selectMenu(R.string.sherpafy_tours);
} else if (state == STATE_STAGE_OVERVIEW) { } else if (state == viewState.STATE_STAGE_OVERVIEW) {
SherpafyStageFragment fragment = (SherpafyStageFragment) getSupportFragmentManager().findFragmentByTag(String.valueOf(state)); SherpafyStageFragment fragment = (SherpafyStageFragment) getSupportFragmentManager().findFragmentByTag(String.valueOf(state));
if (fragment != null) { if (fragment != null) {
fragment.onBackPressed(); fragment.onBackPressed();
} }
} else if (state == STATE_DETAILED_OVERVIEW || state == STATE_DETAILED_INSTRUCTIONS) { } else if (state == viewState.STATE_DETAILED_OVERVIEW || state == viewState.STATE_DETAILED_INSTRUCTIONS || state == viewState.STAGE_FAVORITES) {
showSelectedItem(); showSelectedItem();
} }
} }
@ -189,11 +194,11 @@ public class TourViewActivity extends SherlockFragmentActivity {
public void updateActionBarTitle() { public void updateActionBarTitle() {
if (state == STATE_LOADING) { if (state == viewState.STATE_LOADING) {
getSupportActionBar().setTitle(R.string.app_name); getSupportActionBar().setTitle(R.string.app_name);
} else if (state == STATE_SELECT_TOUR) { } else if (state == viewState.STATE_SELECT_TOUR) {
getSupportActionBar().setTitle(R.string.sherpafy_tours); getSupportActionBar().setTitle(R.string.sherpafy_tours);
} else if (state == STATE_TOUR_VIEW) { } else if (state == viewState.STATE_TOUR_VIEW) {
} }
invalidateOptionsMenu(); invalidateOptionsMenu();
} }
@ -223,7 +228,7 @@ public class TourViewActivity extends SherlockFragmentActivity {
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
if(refreshListAfterDownload){ if (refreshListAfterDownload) {
refreshListAfterDownload = false; refreshListAfterDownload = false;
selectMenu(selectedItem == null ? R.string.sherpafy_tours : selectedItem); selectMenu(selectedItem == null ? R.string.sherpafy_tours : selectedItem);
} }
@ -262,16 +267,16 @@ public class TourViewActivity extends SherlockFragmentActivity {
FragmentManager fragmentManager = getSupportFragmentManager(); FragmentManager fragmentManager = getSupportFragmentManager();
Fragment fragment = fragments.get(item); Fragment fragment = fragments.get(item);
if (new Integer(R.string.sherpafy_tours).equals(item)) { if (new Integer(R.string.sherpafy_tours).equals(item)) {
if (fragment == null) { if (fragment == null || fragment.getActivity() == null) {
fragment = new SherpafySelectToursFragment(); fragment = new SherpafySelectToursFragment();
fragments.put(item, fragment); fragments.put(item, fragment);
} else { } else {
((SherpafySelectToursFragment) fragment).refreshAdapter(); ((SherpafySelectToursFragment) fragment).refreshAdapter();
} }
state = STATE_SELECT_TOUR; state = viewState.STATE_SELECT_TOUR;
setDrawerIndicatorVisible(true); setDrawerIndicatorVisible(true);
} else if (item instanceof TourInformation) { } else if (item instanceof TourInformation) {
state = STATE_TOUR_VIEW; state = viewState.STATE_TOUR_VIEW;
if (fragment == null) { if (fragment == null) {
fragment = new SherpafyTourFragment(); fragment = new SherpafyTourFragment();
Bundle bl = new Bundle(); Bundle bl = new Bundle();
@ -281,7 +286,7 @@ public class TourViewActivity extends SherlockFragmentActivity {
} }
setDrawerIndicatorVisible(true); setDrawerIndicatorVisible(true);
} else if (item instanceof StageInformation) { } else if (item instanceof StageInformation) {
state = STATE_STAGE_OVERVIEW; state = viewState.STATE_STAGE_OVERVIEW;
if (fragment == null) { if (fragment == null) {
fragment = new SherpafyStageFragment(); fragment = new SherpafyStageFragment();
Bundle bl = new Bundle(); Bundle bl = new Bundle();
@ -346,6 +351,7 @@ public class TourViewActivity extends SherlockFragmentActivity {
public void showFavoriteFragment(StageInformation stage, StageFavorite sf) { public void showFavoriteFragment(StageInformation stage, StageFavorite sf) {
state = viewState.STAGE_FAVORITES;
FragmentManager fragmentManager = getSupportFragmentManager(); FragmentManager fragmentManager = getSupportFragmentManager();
setDrawerIndicatorVisible(false); setDrawerIndicatorVisible(false);
SherpafyFavoriteFragment fragment = new SherpafyFavoriteFragment(); SherpafyFavoriteFragment fragment = new SherpafyFavoriteFragment();
@ -358,12 +364,12 @@ public class TourViewActivity extends SherlockFragmentActivity {
} }
public void showDetailedInstructions(String title, String cont) { public void showDetailedInstructions(String title, String cont) {
state = STATE_DETAILED_INSTRUCTIONS; state = viewState.STATE_DETAILED_INSTRUCTIONS;
showHtmlFragment(title, cont); showHtmlFragment(title, cont);
} }
public void showDetailedOverview(String title, String cont) { public void showDetailedOverview(String title, String cont) {
state = STATE_DETAILED_OVERVIEW; state = viewState.STATE_DETAILED_OVERVIEW;
showHtmlFragment(title, cont); showHtmlFragment(title, cont);
} }
@ -375,7 +381,7 @@ public class TourViewActivity extends SherlockFragmentActivity {
bl.putString(SherpafyHtmlFragment.HTML, cont); bl.putString(SherpafyHtmlFragment.HTML, cont);
bl.putString(SherpafyHtmlFragment.TITLE, title); bl.putString(SherpafyHtmlFragment.TITLE, title);
fragment.setArguments(bl); fragment.setArguments(bl);
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment, String.valueOf(STATE_DETAILED_OVERVIEW)).commit(); fragmentManager.beginTransaction().replace(R.id.content_frame, fragment, String.valueOf(viewState.STATE_DETAILED_OVERVIEW)).commit();
} }