1 package de.dhbwloe.campusapp;
3 import android.os.Bundle;
4 import android.support.v4.app.Fragment;
5 import android.support.v4.app.FragmentActivity;
6 import android.support.v4.app.FragmentTransaction;
7 import android.view.Gravity;
8 import android.view.View;
9 import android.widget.LinearLayout;
10 import android.widget.PopupWindow;
12 import java.lang.reflect.Constructor;
13 import java.lang.reflect.InvocationTargetException;
14 import java.util.ArrayList;
16 import de.dhbwloe.campusapp.fragments.PopupFragment;
19 * Created by pk910 on 19.01.2016.
21 public class NavigationManager {
22 private class NavPage {
24 Class<CampusAppFragment> fragmentClass;
25 int fragmentType; // 0 = normal; 1 = fullscreen; 2 = popup
28 private CampusAppContext AppContext;
29 private NavPage oCurrentPage;
30 private NavPage oParentPage;
31 private Fragment oCurrentFragment;
32 private Fragment oParentFragment;
33 private int iFragmentContainerId;
34 private ArrayList<NavPage> lNavigationPages = new ArrayList<NavPage>();
36 public NavigationManager(CampusAppContext context, int fragmentContainer) {
38 iFragmentContainerId = fragmentContainer;
42 public void registerPage(String name, Class<?> fragment, int fragmentType) {
43 NavPage page = new NavPage();
45 page.fragmentClass = (Class<CampusAppFragment>) fragment;
46 page.fragmentType = fragmentType;
47 lNavigationPages.add(page);
50 public String getCurrentPageName() {
51 if(oCurrentPage == null)
53 if(oCurrentPage.fragmentType == 3 && oParentFragment != null){
54 if(oCurrentFragment.isAdded())
55 return oCurrentPage.name;
60 return oCurrentPage.name;
63 public CampusAppFragment getCurrentFragment() {
64 if(oCurrentPage == null)
66 if(oCurrentPage.fragmentType != 3)
67 return (CampusAppFragment)oCurrentFragment;
71 public void navigatePage(String name) {
72 navigatePage(name, null, true);
75 public void navigatePage(String name, Bundle args) {
76 navigatePage(name, args, true);
79 public void navigatePage(String name, Bundle args, boolean history) {
81 NavPage page = getPageByName(name);
86 if(page.fragmentType == 3) {
87 PopupFragment popupFragment = new PopupFragment();
90 args.putString("target", "#"+page.name);
91 fragment = popupFragment;
93 fragment = getFragmentOfPage(page);
96 fragment.setArguments(args);
98 FragmentActivity fragmentActivity = (FragmentActivity) AppContext.getMainActivity();
99 FragmentTransaction transaction = fragmentActivity.getSupportFragmentManager().beginTransaction();
101 if (oCurrentPage != null && oCurrentPage.fragmentType == 3) {
102 transaction.remove(oCurrentFragment);
103 oCurrentPage = oParentPage;
104 oCurrentFragment = oParentFragment;
106 if (page.fragmentType == 3) {
107 transaction.add(fragment, "popup");
108 oParentPage = oCurrentPage;
109 oParentFragment = oCurrentFragment;
111 } else if (oCurrentPage != null) {
112 transaction.replace(iFragmentContainerId, fragment);
114 transaction.add(iFragmentContainerId, fragment);
117 transaction.addToBackStack(null);
120 oCurrentFragment = fragment;
122 transaction.commit();
125 private NavPage getPageByName(String name) {
127 for(int i = 0; i < lNavigationPages.size(); i++) {
128 if(lNavigationPages.get(i).name.equalsIgnoreCase(name)) {
129 page = lNavigationPages.get(i);
139 private CampusAppFragment getFragmentOfPage(NavPage page) {
140 Class<CampusAppFragment> fragmentClass = page.fragmentClass;
141 Constructor fragmentConstructor;
142 CampusAppFragment fragment;
144 fragmentConstructor = fragmentClass.asSubclass(fragmentClass).getConstructor();
145 fragment = (CampusAppFragment)fragmentConstructor.newInstance(new Object[]{});
146 } catch (Exception e) {
152 public CampusAppFragment getPageFragment(String name) {
153 NavPage page = getPageByName(name);
156 return getFragmentOfPage(page);
159 public PopupFragment getDialog() {
160 if(oCurrentPage != null && oCurrentPage.fragmentType == 3) {
161 PopupFragment fragment = (PopupFragment) oCurrentFragment;
167 public boolean closeDialog() {
168 if(oCurrentPage != null && oCurrentPage.fragmentType == 3) {
169 PopupFragment fragment = (PopupFragment) oCurrentFragment;
170 if(oCurrentFragment.isAdded()) {
171 fragment.destroyView();
173 FragmentActivity fragmentActivity = (FragmentActivity) AppContext.getMainActivity();
174 FragmentTransaction transaction = fragmentActivity.getSupportFragmentManager().beginTransaction();
176 transaction.remove(oCurrentFragment);
177 transaction.commit();
180 oCurrentPage = oParentPage;
181 oCurrentFragment = oParentFragment;
189 public boolean back() {
190 return closeDialog();