Added README.txt and GPL Header to Source Files
[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.v4.app.Fragment;
20 import android.support.v4.app.FragmentActivity;
21 import android.support.v4.app.FragmentTransaction;
22 import android.view.Gravity;
23 import android.view.View;
24 import android.widget.LinearLayout;
25 import android.widget.PopupWindow;
26
27 import java.lang.reflect.Constructor;
28 import java.lang.reflect.InvocationTargetException;
29 import java.util.ArrayList;
30 import java.util.List;
31
32 import de.dhbwloe.campusapp.fragments.PopupFragment;
33
34 /**
35  * Created by pk910 on 19.01.2016.
36  */
37 public class NavigationManager {
38     private class NavPage {
39         String name;
40         Class<CampusAppFragment> fragmentClass;
41         int fragmentType; // 0 = normal; 1 = fullscreen; 2 = popup
42     };
43
44     private CampusAppContext AppContext;
45
46     // Derzeitig angezeigtes Fragment
47     private NavPage oCurrentPage;
48     private Fragment oCurrentFragment;
49
50     // Hintergrund Fragment (bei Popups)
51     private NavPage oParentPage;
52     private Fragment oParentFragment;
53
54     // Container ID
55     private int iFragmentContainerId;
56
57     // Alle registrierte Fragmente mit Name
58     private ArrayList<NavPage> lNavigationPages = new ArrayList<NavPage>();
59
60     public NavigationManager(CampusAppContext context, int fragmentContainer) {
61         AppContext = context;
62         iFragmentContainerId = fragmentContainer;
63         oCurrentPage = null;
64     }
65
66     public void registerPage(String name, Class<?> fragment, int fragmentType) {
67         NavPage page = new NavPage();
68         page.name = name;
69         page.fragmentClass = (Class<CampusAppFragment>) fragment;
70         page.fragmentType = fragmentType;
71         lNavigationPages.add(page);
72     }
73
74     public String getCurrentPageName() {
75         if(oCurrentPage == null)
76             return null;
77         if(oCurrentPage.fragmentType == 3 && oParentFragment != null){
78             if(oCurrentFragment.isAdded())
79                 return oCurrentPage.name;
80             else {
81                 closeDialog();
82             }
83         }
84         return oCurrentPage.name;
85     }
86
87     public CampusAppFragment getCurrentFragment() {
88         if(oCurrentPage == null)
89             return null;
90         if(oCurrentPage.fragmentType != 3)
91             return (CampusAppFragment)oCurrentFragment;
92         else
93             return ((PopupFragment)oCurrentFragment).getCurrentFragment();
94     }
95
96     public void navigatePage(String name) {
97         navigatePage(name, null, true);
98     }
99
100     public void navigatePage(String name, Bundle args) {
101         navigatePage(name, args, true);
102     }
103
104     public void navigatePage(String name, Bundle args, boolean history) {
105         // Suche Fragment
106         NavPage page = getPageByName(name);
107         if(page == null)
108             return;
109         Fragment fragment;
110
111         // Wenn das Fragment als Popup angeziegt werden soll, muss zunächst das Popup Fragment geladen werden.
112         if(page.fragmentType == 3) {
113             PopupFragment popupFragment = new PopupFragment();
114             if(args == null)
115                 args = new Bundle();
116             args.putString("target", "#"+page.name);
117             fragment = popupFragment;
118         } else {
119             fragment = getFragmentOfPage(page);
120         }
121
122         fragment.setArguments(args);
123
124         FragmentActivity fragmentActivity = (FragmentActivity) AppContext.getMainActivity();
125         FragmentTransaction transaction = fragmentActivity.getSupportFragmentManager().beginTransaction();
126
127         if (oCurrentPage != null && oCurrentPage.fragmentType == 3) {
128             transaction.remove(oCurrentFragment);
129             oCurrentPage = oParentPage;
130             oCurrentFragment = oParentFragment;
131         }
132         if (page.fragmentType == 3) {
133             transaction.add(fragment, "popup");
134             oParentPage = oCurrentPage;
135             oParentFragment = oCurrentFragment;
136             history = false;
137         } else {
138             // remove fragments
139             List<Fragment> al = fragmentActivity.getSupportFragmentManager().getFragments();
140             if(al != null) {
141                 for (Fragment frag : al) {
142                     if (frag != null && frag.isAdded()) {
143                         transaction.remove(frag);
144                     }
145                 }
146             }
147             transaction.add(iFragmentContainerId, fragment);
148         }
149         if (history) // Hinzufügen zur App History (Zurück Button)
150             transaction.addToBackStack(null);
151
152         oCurrentPage = page;
153         oCurrentFragment = fragment;
154
155         transaction.commit();
156     }
157
158     private NavPage getPageByName(String name) {
159         NavPage page = null;
160         for(int i = 0; i < lNavigationPages.size(); i++) {
161             if(lNavigationPages.get(i).name.equalsIgnoreCase(name)) {
162                 page = lNavigationPages.get(i);
163                 break;
164             }
165         }
166         if(page == null)
167             return null;
168
169         return page;
170     }
171
172     private CampusAppFragment getFragmentOfPage(NavPage page) {
173         Class<CampusAppFragment> fragmentClass = page.fragmentClass;
174         Constructor fragmentConstructor;
175         CampusAppFragment fragment;
176         try {
177             fragmentConstructor = fragmentClass.asSubclass(fragmentClass).getConstructor();
178             fragment = (CampusAppFragment)fragmentConstructor.newInstance(new Object[]{});
179         } catch (Exception e) {
180             return null;
181         }
182         return fragment;
183     }
184
185     public CampusAppFragment getPageFragment(String name) {
186         NavPage page = getPageByName(name);
187         if(page == null)
188             return null;
189         return getFragmentOfPage(page);
190     }
191
192     public PopupFragment getDialog() {
193         if(oCurrentPage != null && oCurrentPage.fragmentType == 3) {
194             PopupFragment fragment = (PopupFragment) oCurrentFragment;
195             return fragment;
196         }
197         return null;
198     }
199
200     public boolean closeDialog() {
201         // Popup schließen
202         if(oCurrentPage != null && oCurrentPage.fragmentType == 3) {
203             PopupFragment fragment = (PopupFragment) oCurrentFragment;
204             boolean wasAdded = false;
205             if(oCurrentFragment.isAdded()) {
206                 fragment.destroyView();
207
208                 FragmentActivity fragmentActivity = (FragmentActivity) AppContext.getMainActivity();
209                 FragmentTransaction transaction = fragmentActivity.getSupportFragmentManager().beginTransaction();
210
211                 transaction.remove(oCurrentFragment);
212                 transaction.commit();
213                 wasAdded = true;
214             }
215
216             oCurrentPage = oParentPage;
217             oCurrentFragment = oParentFragment;
218
219             return wasAdded;
220         }
221         return false;
222     }
223
224     public boolean back() {
225         return closeDialog();
226     }
227
228 }