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;
72 public void navigatePage(String name) {
73 navigatePage(name, null, true);
76 public void navigatePage(String name, Bundle args) {
77 navigatePage(name, args, true);
80 public void navigatePage(String name, Bundle args, boolean history) {
82 NavPage page = getPageByName(name);
87 if(page.fragmentType == 3) {
88 PopupFragment popupFragment = new PopupFragment();
91 args.putString("target", "#"+page.name);
92 fragment = popupFragment;
94 fragment = getFragmentOfPage(page);
97 fragment.setArguments(args);
99 FragmentActivity fragmentActivity = (FragmentActivity) AppContext.getMainActivity();
100 FragmentTransaction transaction = fragmentActivity.getSupportFragmentManager().beginTransaction();
102 if (oCurrentPage != null && oCurrentPage.fragmentType == 3) {
103 transaction.remove(oCurrentFragment);
104 oCurrentPage = oParentPage;
105 oCurrentFragment = oParentFragment;
107 if (page.fragmentType == 3) {
108 transaction.add(fragment, "popup");
109 oParentPage = oCurrentPage;
110 oParentFragment = oCurrentFragment;
114 List<Fragment> al = fragmentActivity.getSupportFragmentManager().getFragments();
116 for (Fragment frag : al) {
117 if (frag != null && frag.isAdded()) {
118 transaction.remove(frag);
122 transaction.add(iFragmentContainerId, fragment);
125 transaction.addToBackStack(null);
128 oCurrentFragment = fragment;
130 transaction.commit();
133 private NavPage getPageByName(String name) {
135 for(int i = 0; i < lNavigationPages.size(); i++) {
136 if(lNavigationPages.get(i).name.equalsIgnoreCase(name)) {
137 page = lNavigationPages.get(i);
147 private CampusAppFragment getFragmentOfPage(NavPage page) {
148 Class<CampusAppFragment> fragmentClass = page.fragmentClass;
149 Constructor fragmentConstructor;
150 CampusAppFragment fragment;
152 fragmentConstructor = fragmentClass.asSubclass(fragmentClass).getConstructor();
153 fragment = (CampusAppFragment)fragmentConstructor.newInstance(new Object[]{});
154 } catch (Exception e) {
160 public CampusAppFragment getPageFragment(String name) {
161 NavPage page = getPageByName(name);
164 return getFragmentOfPage(page);
167 public PopupFragment getDialog() {
168 if(oCurrentPage != null && oCurrentPage.fragmentType == 3) {
169 PopupFragment fragment = (PopupFragment) oCurrentFragment;
175 public boolean closeDialog() {
176 if(oCurrentPage != null && oCurrentPage.fragmentType == 3) {
177 PopupFragment fragment = (PopupFragment) oCurrentFragment;
178 boolean wasAdded = false;
179 if(oCurrentFragment.isAdded()) {
180 fragment.destroyView();
182 FragmentActivity fragmentActivity = (FragmentActivity) AppContext.getMainActivity();
183 FragmentTransaction transaction = fragmentActivity.getSupportFragmentManager().beginTransaction();
185 transaction.remove(oCurrentFragment);
186 transaction.commit();
190 oCurrentPage = oParentPage;
191 oCurrentFragment = oParentFragment;
198 public boolean back() {
199 return closeDialog();