Zuordnung von Navigationsitem zu Zielseite in CampusAppContext verschoben - somit...
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / NavigationManager.java
1 /* NavigationManager.java
2  *
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.
7  *
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.
12  *
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/>.
15  */
16 package de.dhbwloe.campusapp;
17
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;
27
28 import java.lang.reflect.Constructor;
29 import java.lang.reflect.InvocationTargetException;
30 import java.util.ArrayList;
31 import java.util.List;
32
33 import de.dhbwloe.campusapp.fragments.PopupFragment;
34
35 /**
36  * Created by pk910 on 19.01.2016.
37  */
38 public class NavigationManager {
39     private class NavPage {
40         String name;
41         Class<CampusAppFragment> fragmentClass;
42         int fragmentType; // 0 = normal; 1 = fullscreen; 2 = popup
43     };
44
45     private CampusAppContext AppContext;
46
47     // Derzeitig angezeigtes Fragment
48     private NavPage oCurrentPage;
49     private Fragment oCurrentFragment;
50
51     // Hintergrund Fragment (bei Popups)
52     private NavPage oParentPage;
53     private Fragment oParentFragment;
54
55     // Container ID
56     private int iFragmentContainerId;
57     private int iNavigationViewId;
58
59     // Alle registrierte Fragmente mit Name
60     private ArrayList<NavPage> lNavigationPages = new ArrayList<NavPage>();
61
62     public NavigationManager(CampusAppContext context, int fragmentContainer, int navigationView) {
63         AppContext = context;
64         iFragmentContainerId = fragmentContainer;
65         iNavigationViewId = navigationView;
66         oCurrentPage = null;
67     }
68
69     public void registerPage(String name, Class<?> fragment, int fragmentType) {
70         NavPage page = new NavPage();
71         page.name = name;
72         page.fragmentClass = (Class<CampusAppFragment>) fragment;
73         page.fragmentType = fragmentType;
74         lNavigationPages.add(page);
75     }
76
77     public String getCurrentPageName() {
78         if(oCurrentPage == null)
79             return null;
80         if(oCurrentPage.fragmentType == 3 && oParentFragment != null){
81             if(oCurrentFragment.isAdded())
82                 return oCurrentPage.name;
83             else {
84                 closeDialog();
85             }
86         }
87         return oCurrentPage.name;
88     }
89
90     public CampusAppFragment getCurrentFragment() {
91         if(oCurrentPage == null)
92             return null;
93         if(oCurrentPage.fragmentType != 3)
94             return (CampusAppFragment)oCurrentFragment;
95         else
96             return ((PopupFragment)oCurrentFragment).getCurrentFragment();
97     }
98
99     public void navigatePage(String name) {
100         navigatePage(name, null, true);
101     }
102
103     public void navigatePage(String name, Bundle args) {
104         navigatePage(name, args, true);
105     }
106
107     public void navigatePage(String name, Bundle args, boolean history) {
108         // Suche Fragment
109         NavPage page = getPageByName(name);
110         if(page == null)
111             return;
112         Fragment fragment;
113
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();
117             if(args == null)
118                 args = new Bundle();
119             args.putString("target", "#"+page.name);
120             fragment = popupFragment;
121         } else {
122             fragment = getFragmentOfPage(page);
123         }
124
125         fragment.setArguments(args);
126
127         FragmentActivity fragmentActivity = (FragmentActivity) AppContext.getMainActivity();
128         FragmentTransaction transaction = fragmentActivity.getSupportFragmentManager().beginTransaction();
129
130         if (oCurrentPage != null && oCurrentPage.fragmentType == 3) {
131             transaction.remove(oCurrentFragment);
132             oCurrentPage = oParentPage;
133             oCurrentFragment = oParentFragment;
134         }
135         if (page.fragmentType == 3) {
136             transaction.add(fragment, "popup");
137             oParentPage = oCurrentPage;
138             oParentFragment = oCurrentFragment;
139             history = false;
140         } else {
141             // remove fragments
142             List<Fragment> al = fragmentActivity.getSupportFragmentManager().getFragments();
143             if(al != null) {
144                 for (Fragment frag : al) {
145                     if (frag != null && frag.isAdded()) {
146                         transaction.remove(frag);
147                     }
148                 }
149             }
150             transaction.add(iFragmentContainerId, fragment);
151         }
152         if (history) // Hinzufügen zur App History (Zurück Button)
153             transaction.addToBackStack(null);
154
155         oCurrentPage = page;
156         oCurrentFragment = fragment;
157
158         updateNavigationHighlight();
159
160         transaction.commit();
161     }
162
163     private NavPage getPageByName(String name) {
164         NavPage page = null;
165         for(int i = 0; i < lNavigationPages.size(); i++) {
166             if(lNavigationPages.get(i).name.equalsIgnoreCase(name)) {
167                 page = lNavigationPages.get(i);
168                 break;
169             }
170         }
171         if(page == null)
172             return null;
173
174         return page;
175     }
176
177     private CampusAppFragment getFragmentOfPage(NavPage page) {
178         Class<CampusAppFragment> fragmentClass = page.fragmentClass;
179         Constructor fragmentConstructor;
180         CampusAppFragment fragment;
181         try {
182             fragmentConstructor = fragmentClass.asSubclass(fragmentClass).getConstructor();
183             fragment = (CampusAppFragment)fragmentConstructor.newInstance(new Object[]{});
184         } catch (Exception e) {
185             return null;
186         }
187         return fragment;
188     }
189
190     public CampusAppFragment getPageFragment(String name) {
191         NavPage page = getPageByName(name);
192         if(page == null)
193             return null;
194         return getFragmentOfPage(page);
195     }
196
197     public PopupFragment getDialog() {
198         if(oCurrentPage != null && oCurrentPage.fragmentType == 3) {
199             PopupFragment fragment = (PopupFragment) oCurrentFragment;
200             return fragment;
201         }
202         return null;
203     }
204
205     public boolean closeDialog() {
206         // Popup schließen
207         if(oCurrentPage != null && oCurrentPage.fragmentType == 3) {
208             PopupFragment fragment = (PopupFragment) oCurrentFragment;
209             boolean wasAdded = false;
210             if(oCurrentFragment.isAdded()) {
211                 fragment.destroyView();
212
213                 FragmentActivity fragmentActivity = (FragmentActivity) AppContext.getMainActivity();
214                 FragmentTransaction transaction = fragmentActivity.getSupportFragmentManager().beginTransaction();
215
216                 transaction.remove(oCurrentFragment);
217                 transaction.commit();
218                 wasAdded = true;
219             }
220
221             oCurrentPage = oParentPage;
222             oCurrentFragment = oParentFragment;
223
224             return wasAdded;
225         }
226         return false;
227     }
228
229     public boolean back() {
230         return closeDialog();
231     }
232
233     private void updateNavigationHighlight() {
234         NavigationView navigationView = (NavigationView) AppContext.getMainActivity().findViewById(iNavigationViewId);
235
236         if(oCurrentPage == null)
237             return;
238
239         int activeItemId = 0;
240         for(CampusAppContext.NavigationItem navitem : AppContext.NAVIGATION_TARGETS) {
241             if(oCurrentPage.name.equalsIgnoreCase(navitem.navTarget)) {
242                 activeItemId = navitem.navItemId;
243             }
244         }
245         if(activeItemId == 0)
246             return;
247
248         navigationView.setCheckedItem(activeItemId);
249     }
250
251 }