1 /* NavigationManager.java
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 package de.dhbwloe.campusapp;
18 import android.os.Bundle;
19 import android.support.design.widget.NavigationView;
20 import android.support.v4.app.Fragment;
21 import android.support.v4.app.FragmentActivity;
22 import android.support.v4.app.FragmentTransaction;
23 import android.view.Gravity;
24 import android.view.View;
25 import android.widget.LinearLayout;
26 import android.widget.PopupWindow;
28 import java.lang.reflect.Constructor;
29 import java.lang.reflect.InvocationTargetException;
30 import java.util.ArrayList;
31 import java.util.List;
33 import de.dhbwloe.campusapp.fragments.PopupFragment;
36 * Created by pk910 on 19.01.2016.
38 public class NavigationManager {
39 private class NavPage {
41 Class<CampusAppFragment> fragmentClass;
42 int fragmentType; // 0 = normal; 1 = fullscreen; 2 = popup
45 private CampusAppContext AppContext;
47 // Derzeitig angezeigtes Fragment
48 private NavPage oCurrentPage;
49 private Fragment oCurrentFragment;
51 // Hintergrund Fragment (bei Popups)
52 private NavPage oParentPage;
53 private Fragment oParentFragment;
56 private int iFragmentContainerId;
57 private int iNavigationViewId;
59 // Alle registrierte Fragmente mit Name
60 private ArrayList<NavPage> lNavigationPages = new ArrayList<NavPage>();
62 public NavigationManager(CampusAppContext context, int fragmentContainer, int navigationView) {
64 iFragmentContainerId = fragmentContainer;
65 iNavigationViewId = navigationView;
69 public void registerPage(String name, Class<?> fragment, int fragmentType) {
70 NavPage page = new NavPage();
72 page.fragmentClass = (Class<CampusAppFragment>) fragment;
73 page.fragmentType = fragmentType;
74 lNavigationPages.add(page);
77 public String getCurrentPageName() {
78 if(oCurrentPage == null)
80 if(oCurrentPage.fragmentType == 3 && oParentFragment != null){
81 if(oCurrentFragment.isAdded())
82 return oCurrentPage.name;
87 return oCurrentPage.name;
90 public CampusAppFragment getCurrentFragment() {
91 if(oCurrentPage == null)
93 if(oCurrentPage.fragmentType != 3)
94 return (CampusAppFragment)oCurrentFragment;
96 return ((PopupFragment)oCurrentFragment).getCurrentFragment();
99 public void navigatePage(String name) {
100 navigatePage(name, null, true);
103 public void navigatePage(String name, Bundle args) {
104 navigatePage(name, args, true);
107 public void navigatePage(String name, Bundle args, boolean history) {
109 NavPage page = getPageByName(name);
114 // Wenn das Fragment als Popup angeziegt werden soll, muss zunächst das Popup Fragment geladen werden.
115 if(page.fragmentType == 3) {
116 PopupFragment popupFragment = new PopupFragment();
119 args.putString("target", "#"+page.name);
120 fragment = popupFragment;
122 fragment = getFragmentOfPage(page);
125 fragment.setArguments(args);
127 FragmentActivity fragmentActivity = (FragmentActivity) AppContext.getMainActivity();
128 FragmentTransaction transaction = fragmentActivity.getSupportFragmentManager().beginTransaction();
130 if (oCurrentPage != null && oCurrentPage.fragmentType == 3) {
131 transaction.remove(oCurrentFragment);
132 oCurrentPage = oParentPage;
133 oCurrentFragment = oParentFragment;
135 if (page.fragmentType == 3) {
136 transaction.add(fragment, "popup");
137 oParentPage = oCurrentPage;
138 oParentFragment = oCurrentFragment;
142 List<Fragment> al = fragmentActivity.getSupportFragmentManager().getFragments();
144 for (Fragment frag : al) {
145 if (frag != null && frag.isAdded()) {
146 transaction.remove(frag);
150 transaction.add(iFragmentContainerId, fragment);
152 if (history) // Hinzufügen zur App History (Zurück Button)
153 transaction.addToBackStack(null);
156 oCurrentFragment = fragment;
158 updateNavigationHighlight();
160 transaction.commit();
163 private NavPage getPageByName(String name) {
165 for(int i = 0; i < lNavigationPages.size(); i++) {
166 if(lNavigationPages.get(i).name.equalsIgnoreCase(name)) {
167 page = lNavigationPages.get(i);
177 private CampusAppFragment getFragmentOfPage(NavPage page) {
178 Class<CampusAppFragment> fragmentClass = page.fragmentClass;
179 Constructor fragmentConstructor;
180 CampusAppFragment fragment;
182 fragmentConstructor = fragmentClass.asSubclass(fragmentClass).getConstructor();
183 fragment = (CampusAppFragment)fragmentConstructor.newInstance(new Object[]{});
184 } catch (Exception e) {
190 public CampusAppFragment getPageFragment(String name) {
191 NavPage page = getPageByName(name);
194 return getFragmentOfPage(page);
197 public PopupFragment getDialog() {
198 if(oCurrentPage != null && oCurrentPage.fragmentType == 3) {
199 PopupFragment fragment = (PopupFragment) oCurrentFragment;
205 public boolean closeDialog() {
207 if(oCurrentPage != null && oCurrentPage.fragmentType == 3) {
208 PopupFragment fragment = (PopupFragment) oCurrentFragment;
209 boolean wasAdded = false;
210 if(oCurrentFragment.isAdded()) {
211 fragment.destroyView();
213 FragmentActivity fragmentActivity = (FragmentActivity) AppContext.getMainActivity();
214 FragmentTransaction transaction = fragmentActivity.getSupportFragmentManager().beginTransaction();
216 transaction.remove(oCurrentFragment);
217 transaction.commit();
221 oCurrentPage = oParentPage;
222 oCurrentFragment = oParentFragment;
229 public boolean back() {
230 return closeDialog();
233 private void updateNavigationHighlight() {
234 NavigationView navigationView = (NavigationView) AppContext.getMainActivity().findViewById(iNavigationViewId);
236 if(oCurrentPage == null)
239 int activeItemId = 0;
240 for(CampusAppContext.NavigationItem navitem : AppContext.NAVIGATION_TARGETS) {
241 if(oCurrentPage.name.equalsIgnoreCase(navitem.navTarget)) {
242 activeItemId = navitem.navItemId;
245 if(activeItemId == 0)
248 navigationView.setCheckedItem(activeItemId);