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;
15 import java.util.List;
17 import de.dhbwloe.campusapp.fragments.PopupFragment;
20 * Created by pk910 on 19.01.2016.
22 public class NavigationManager {
23 private class NavPage {
25 Class<CampusAppFragment> fragmentClass;
26 int fragmentType; // 0 = normal; 1 = fullscreen; 2 = popup
29 private CampusAppContext AppContext;
30 private NavPage oCurrentPage;
31 private NavPage oParentPage;
32 private Fragment oCurrentFragment;
33 private Fragment oParentFragment;
34 private int iFragmentContainerId;
35 private ArrayList<NavPage> lNavigationPages = new ArrayList<NavPage>();
37 public NavigationManager(CampusAppContext context, int fragmentContainer) {
39 iFragmentContainerId = fragmentContainer;
43 public void registerPage(String name, Class<?> fragment, int fragmentType) {
44 NavPage page = new NavPage();
46 page.fragmentClass = (Class<CampusAppFragment>) fragment;
47 page.fragmentType = fragmentType;
48 lNavigationPages.add(page);
51 public String getCurrentPageName() {
52 if(oCurrentPage == null)
54 if(oCurrentPage.fragmentType == 3 && oParentFragment != null){
55 if(oCurrentFragment.isAdded())
56 return oCurrentPage.name;
61 return oCurrentPage.name;
64 public CampusAppFragment getCurrentFragment() {
65 if(oCurrentPage == null)
67 if(oCurrentPage.fragmentType != 3)
68 return (CampusAppFragment)oCurrentFragment;
70 return ((PopupFragment)oCurrentFragment).getCurrentFragment();
73 public void navigatePage(String name) {
74 navigatePage(name, null, true);
77 public void navigatePage(String name, Bundle args) {
78 navigatePage(name, args, true);
81 public void navigatePage(String name, Bundle args, boolean history) {
83 NavPage page = getPageByName(name);
88 if(page.fragmentType == 3) {
89 PopupFragment popupFragment = new PopupFragment();
92 args.putString("target", "#"+page.name);
93 fragment = popupFragment;
95 fragment = getFragmentOfPage(page);
98 fragment.setArguments(args);
100 FragmentActivity fragmentActivity = (FragmentActivity) AppContext.getMainActivity();
101 FragmentTransaction transaction = fragmentActivity.getSupportFragmentManager().beginTransaction();
103 if (oCurrentPage != null && oCurrentPage.fragmentType == 3) {
104 transaction.remove(oCurrentFragment);
105 oCurrentPage = oParentPage;
106 oCurrentFragment = oParentFragment;
108 if (page.fragmentType == 3) {
109 transaction.add(fragment, "popup");
110 oParentPage = oCurrentPage;
111 oParentFragment = oCurrentFragment;
115 List<Fragment> al = fragmentActivity.getSupportFragmentManager().getFragments();
117 for (Fragment frag : al) {
118 if (frag != null && frag.isAdded()) {
119 transaction.remove(frag);
123 transaction.add(iFragmentContainerId, fragment);
126 transaction.addToBackStack(null);
129 oCurrentFragment = fragment;
131 transaction.commit();
134 private NavPage getPageByName(String name) {
136 for(int i = 0; i < lNavigationPages.size(); i++) {
137 if(lNavigationPages.get(i).name.equalsIgnoreCase(name)) {
138 page = lNavigationPages.get(i);
148 private CampusAppFragment getFragmentOfPage(NavPage page) {
149 Class<CampusAppFragment> fragmentClass = page.fragmentClass;
150 Constructor fragmentConstructor;
151 CampusAppFragment fragment;
153 fragmentConstructor = fragmentClass.asSubclass(fragmentClass).getConstructor();
154 fragment = (CampusAppFragment)fragmentConstructor.newInstance(new Object[]{});
155 } catch (Exception e) {
161 public CampusAppFragment getPageFragment(String name) {
162 NavPage page = getPageByName(name);
165 return getFragmentOfPage(page);
168 public PopupFragment getDialog() {
169 if(oCurrentPage != null && oCurrentPage.fragmentType == 3) {
170 PopupFragment fragment = (PopupFragment) oCurrentFragment;
176 public boolean closeDialog() {
177 if(oCurrentPage != null && oCurrentPage.fragmentType == 3) {
178 PopupFragment fragment = (PopupFragment) oCurrentFragment;
179 boolean wasAdded = false;
180 if(oCurrentFragment.isAdded()) {
181 fragment.destroyView();
183 FragmentActivity fragmentActivity = (FragmentActivity) AppContext.getMainActivity();
184 FragmentTransaction transaction = fragmentActivity.getSupportFragmentManager().beginTransaction();
186 transaction.remove(oCurrentFragment);
187 transaction.commit();
191 oCurrentPage = oParentPage;
192 oCurrentFragment = oParentFragment;
199 public boolean back() {
200 return closeDialog();