some fixes
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / NavigationManager.java
1 package de.dhbwloe.campusapp;
2
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;
11
12 import java.lang.reflect.Constructor;
13 import java.lang.reflect.InvocationTargetException;
14 import java.util.ArrayList;
15
16 import de.dhbwloe.campusapp.fragments.PopupFragment;
17
18 /**
19  * Created by pk910 on 19.01.2016.
20  */
21 public class NavigationManager {
22     private class NavPage {
23         String name;
24         Class<CampusAppFragment> fragmentClass;
25         int fragmentType; // 0 = normal; 1 = fullscreen; 2 = popup
26     };
27
28     private CampusAppContext AppContext;
29     private NavPage oCurrentPage;
30     private NavPage oParentPage;
31     private Fragment oCurrentFragment;
32     private Fragment oParentFragment;
33     private int iFragmentContainerId;
34     private ArrayList<NavPage> lNavigationPages = new ArrayList<NavPage>();
35
36     public NavigationManager(CampusAppContext context, int fragmentContainer) {
37         AppContext = context;
38         iFragmentContainerId = fragmentContainer;
39         oCurrentPage = null;
40     }
41
42     public void registerPage(String name, Class<?> fragment, int fragmentType) {
43         NavPage page = new NavPage();
44         page.name = name;
45         page.fragmentClass = (Class<CampusAppFragment>) fragment;
46         page.fragmentType = fragmentType;
47         lNavigationPages.add(page);
48     }
49
50     public String getCurrentPageName() {
51         if(oCurrentPage == null)
52             return null;
53         if(oCurrentPage.fragmentType == 3 && oParentFragment != null){
54             if(oCurrentFragment.isAdded())
55                 return oCurrentPage.name;
56             else {
57                 closeDialog();
58             }
59         }
60         return oCurrentPage.name;
61     }
62
63     public CampusAppFragment getCurrentFragment() {
64         if(oCurrentPage == null)
65             return null;
66         if(oCurrentPage.fragmentType != 3)
67             return (CampusAppFragment)oCurrentFragment;
68         return null;
69     }
70
71     public void navigatePage(String name) {
72         navigatePage(name, null, true);
73     }
74
75     public void navigatePage(String name, Bundle args) {
76         navigatePage(name, args, true);
77     }
78
79     public void navigatePage(String name, Bundle args, boolean history) {
80
81         NavPage page = getPageByName(name);
82         if(page == null)
83             return;
84         Fragment fragment;
85
86         if(page.fragmentType == 3) {
87             PopupFragment popupFragment = new PopupFragment();
88             if(args == null)
89                 args = new Bundle();
90             args.putString("target", "#"+page.name);
91             fragment = popupFragment;
92         } else {
93             fragment = getFragmentOfPage(page);
94         }
95
96         fragment.setArguments(args);
97
98         FragmentActivity fragmentActivity = (FragmentActivity) AppContext.getMainActivity();
99         FragmentTransaction transaction = fragmentActivity.getSupportFragmentManager().beginTransaction();
100
101         if (oCurrentPage != null && oCurrentPage.fragmentType == 3) {
102             transaction.remove(oCurrentFragment);
103             oCurrentPage = oParentPage;
104             oCurrentFragment = oParentFragment;
105         }
106         if (page.fragmentType == 3) {
107             transaction.add(fragment, "popup");
108             oParentPage = oCurrentPage;
109             oParentFragment = oCurrentFragment;
110             history = false;
111         } else if (oCurrentPage != null) {
112             transaction.replace(iFragmentContainerId, fragment);
113         } else {
114             transaction.add(iFragmentContainerId, fragment);
115         }
116         if (history)
117             transaction.addToBackStack(null);
118
119         oCurrentPage = page;
120         oCurrentFragment = fragment;
121
122         transaction.commit();
123     }
124
125     private NavPage getPageByName(String name) {
126         NavPage page = null;
127         for(int i = 0; i < lNavigationPages.size(); i++) {
128             if(lNavigationPages.get(i).name.equalsIgnoreCase(name)) {
129                 page = lNavigationPages.get(i);
130                 break;
131             }
132         }
133         if(page == null)
134             return null;
135
136         return page;
137     }
138
139     private CampusAppFragment getFragmentOfPage(NavPage page) {
140         Class<CampusAppFragment> fragmentClass = page.fragmentClass;
141         Constructor fragmentConstructor;
142         CampusAppFragment fragment;
143         try {
144             fragmentConstructor = fragmentClass.asSubclass(fragmentClass).getConstructor();
145             fragment = (CampusAppFragment)fragmentConstructor.newInstance(new Object[]{});
146         } catch (Exception e) {
147             return null;
148         }
149         return fragment;
150     }
151
152     public CampusAppFragment getPageFragment(String name) {
153         NavPage page = getPageByName(name);
154         if(page == null)
155             return null;
156         return getFragmentOfPage(page);
157     }
158
159     public PopupFragment getDialog() {
160         if(oCurrentPage != null && oCurrentPage.fragmentType == 3) {
161             PopupFragment fragment = (PopupFragment) oCurrentFragment;
162             return fragment;
163         }
164         return null;
165     }
166
167     public boolean closeDialog() {
168         if(oCurrentPage != null && oCurrentPage.fragmentType == 3) {
169             PopupFragment fragment = (PopupFragment) oCurrentFragment;
170             boolean wasAdded = false;
171             if(oCurrentFragment.isAdded()) {
172                 fragment.destroyView();
173
174                 FragmentActivity fragmentActivity = (FragmentActivity) AppContext.getMainActivity();
175                 FragmentTransaction transaction = fragmentActivity.getSupportFragmentManager().beginTransaction();
176
177                 transaction.remove(oCurrentFragment);
178                 transaction.commit();
179                 wasAdded = true;
180             }
181
182             oCurrentPage = oParentPage;
183             oCurrentFragment = oParentFragment;
184
185             return wasAdded;
186         }
187         return false;
188     }
189
190     public boolean back() {
191         return closeDialog();
192     }
193
194 }