Grundaufbau der App
[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         return oCurrentPage.name;
54     }
55
56     public CampusAppFragment getCurrentFragment() {
57         if(oCurrentPage == null)
58             return null;
59         if(oCurrentPage.fragmentType != 3)
60             return (CampusAppFragment)oCurrentFragment;
61         return null;
62     }
63
64     public void navigatePage(String name) {
65         navigatePage(name, null, true);
66     }
67
68     public void navigatePage(String name, Bundle args) {
69         navigatePage(name, args, true);
70     }
71
72     public void navigatePage(String name, Bundle args, boolean history) {
73
74         NavPage page = getPageByName(name);
75         if(page == null)
76             return;
77         Fragment fragment;
78
79         if(page.fragmentType == 3) {
80             PopupFragment popupFragment = new PopupFragment();
81             if(args == null)
82                 args = new Bundle();
83             args.putString("target", "#"+page.name);
84             fragment = popupFragment;
85         } else {
86             fragment = getFragmentOfPage(page);
87         }
88
89         fragment.setArguments(args);
90
91         FragmentActivity fragmentActivity = (FragmentActivity) AppContext.getMainActivity();
92         FragmentTransaction transaction = fragmentActivity.getSupportFragmentManager().beginTransaction();
93
94         if (oCurrentPage != null && oCurrentPage.fragmentType == 3) {
95             transaction.remove(oCurrentFragment);
96             oCurrentPage = oParentPage;
97             oCurrentFragment = oParentFragment;
98         }
99         if (page.fragmentType == 3) {
100             transaction.add(fragment, "popup");
101             oParentPage = oCurrentPage;
102             oParentFragment = oCurrentFragment;
103             history = false;
104         } else if (oCurrentPage != null) {
105             transaction.replace(iFragmentContainerId, fragment);
106         } else {
107             transaction.add(iFragmentContainerId, fragment);
108         }
109         if (history)
110             transaction.addToBackStack(null);
111
112         oCurrentPage = page;
113         oCurrentFragment = fragment;
114
115         transaction.commit();
116     }
117
118     private NavPage getPageByName(String name) {
119         NavPage page = null;
120         for(int i = 0; i < lNavigationPages.size(); i++) {
121             if(lNavigationPages.get(i).name.equalsIgnoreCase(name)) {
122                 page = lNavigationPages.get(i);
123                 break;
124             }
125         }
126         if(page == null)
127             return null;
128
129         return page;
130     }
131
132     private CampusAppFragment getFragmentOfPage(NavPage page) {
133         Class<CampusAppFragment> fragmentClass = page.fragmentClass;
134         Constructor fragmentConstructor;
135         CampusAppFragment fragment;
136         try {
137             fragmentConstructor = fragmentClass.asSubclass(fragmentClass).getConstructor();
138             fragment = (CampusAppFragment)fragmentConstructor.newInstance(new Object[]{});
139         } catch (Exception e) {
140             return null;
141         }
142         return fragment;
143     }
144
145     public CampusAppFragment getPageFragment(String name) {
146         NavPage page = getPageByName(name);
147         if(page == null)
148             return null;
149         return getFragmentOfPage(page);
150     }
151
152     public PopupFragment getDialog() {
153         if(oCurrentPage != null && oCurrentPage.fragmentType == 3) {
154             PopupFragment fragment = (PopupFragment) oCurrentFragment;
155             return fragment;
156         }
157         return null;
158     }
159
160     public boolean closeDialog() {
161         if(oCurrentPage != null && oCurrentPage.fragmentType == 3) {
162             PopupFragment fragment = (PopupFragment) oCurrentFragment;
163             fragment.destroyView();
164
165             FragmentActivity fragmentActivity = (FragmentActivity) AppContext.getMainActivity();
166             FragmentTransaction transaction = fragmentActivity.getSupportFragmentManager().beginTransaction();
167
168             transaction.remove(oCurrentFragment);
169
170             oCurrentPage = oParentPage;
171             oCurrentFragment = oParentFragment;
172
173             transaction.commit();
174             return true;
175         }
176         return false;
177     }
178
179     public boolean back() {
180         return closeDialog();
181     }
182
183 }