Zuordnung von Navigationsitem zu Zielseite in CampusAppContext verschoben - somit...
[DHBWCampusApp.git] / app / src / main / java / de / dhbwloe / campusapp / NavigationManager.java
index 33d2f1c541938de315e4281b30e7bb31fb07e013..073bf190bab359bda77f0c1fd2c33ed42840e275 100644 (file)
@@ -1,6 +1,22 @@
+/* NavigationManager.java
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
 package de.dhbwloe.campusapp;
 
 import android.os.Bundle;
+import android.support.design.widget.NavigationView;
 import android.support.v4.app.Fragment;
 import android.support.v4.app.FragmentActivity;
 import android.support.v4.app.FragmentTransaction;
@@ -12,6 +28,7 @@ import android.widget.PopupWindow;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
 import java.util.ArrayList;
+import java.util.List;
 
 import de.dhbwloe.campusapp.fragments.PopupFragment;
 
@@ -26,16 +43,26 @@ public class NavigationManager {
     };
 
     private CampusAppContext AppContext;
+
+    // Derzeitig angezeigtes Fragment
     private NavPage oCurrentPage;
-    private NavPage oParentPage;
     private Fragment oCurrentFragment;
+
+    // Hintergrund Fragment (bei Popups)
+    private NavPage oParentPage;
     private Fragment oParentFragment;
+
+    // Container ID
     private int iFragmentContainerId;
+    private int iNavigationViewId;
+
+    // Alle registrierte Fragmente mit Name
     private ArrayList<NavPage> lNavigationPages = new ArrayList<NavPage>();
 
-    public NavigationManager(CampusAppContext context, int fragmentContainer) {
+    public NavigationManager(CampusAppContext context, int fragmentContainer, int navigationView) {
         AppContext = context;
         iFragmentContainerId = fragmentContainer;
+        iNavigationViewId = navigationView;
         oCurrentPage = null;
     }
 
@@ -50,6 +77,13 @@ public class NavigationManager {
     public String getCurrentPageName() {
         if(oCurrentPage == null)
             return null;
+        if(oCurrentPage.fragmentType == 3 && oParentFragment != null){
+            if(oCurrentFragment.isAdded())
+                return oCurrentPage.name;
+            else {
+                closeDialog();
+            }
+        }
         return oCurrentPage.name;
     }
 
@@ -58,7 +92,8 @@ public class NavigationManager {
             return null;
         if(oCurrentPage.fragmentType != 3)
             return (CampusAppFragment)oCurrentFragment;
-        return null;
+        else
+            return ((PopupFragment)oCurrentFragment).getCurrentFragment();
     }
 
     public void navigatePage(String name) {
@@ -70,12 +105,13 @@ public class NavigationManager {
     }
 
     public void navigatePage(String name, Bundle args, boolean history) {
-
+        // Suche Fragment
         NavPage page = getPageByName(name);
         if(page == null)
             return;
         Fragment fragment;
 
+        // Wenn das Fragment als Popup angeziegt werden soll, muss zunächst das Popup Fragment geladen werden.
         if(page.fragmentType == 3) {
             PopupFragment popupFragment = new PopupFragment();
             if(args == null)
@@ -101,17 +137,26 @@ public class NavigationManager {
             oParentPage = oCurrentPage;
             oParentFragment = oCurrentFragment;
             history = false;
-        } else if (oCurrentPage != null) {
-            transaction.replace(iFragmentContainerId, fragment);
         } else {
+            // remove fragments
+            List<Fragment> al = fragmentActivity.getSupportFragmentManager().getFragments();
+            if(al != null) {
+                for (Fragment frag : al) {
+                    if (frag != null && frag.isAdded()) {
+                        transaction.remove(frag);
+                    }
+                }
+            }
             transaction.add(iFragmentContainerId, fragment);
         }
-        if (history)
+        if (history) // Hinzufügen zur App History (Zurück Button)
             transaction.addToBackStack(null);
 
         oCurrentPage = page;
         oCurrentFragment = fragment;
 
+        updateNavigationHighlight();
+
         transaction.commit();
     }
 
@@ -158,20 +203,25 @@ public class NavigationManager {
     }
 
     public boolean closeDialog() {
+        // Popup schließen
         if(oCurrentPage != null && oCurrentPage.fragmentType == 3) {
             PopupFragment fragment = (PopupFragment) oCurrentFragment;
-            fragment.destroyView();
+            boolean wasAdded = false;
+            if(oCurrentFragment.isAdded()) {
+                fragment.destroyView();
 
-            FragmentActivity fragmentActivity = (FragmentActivity) AppContext.getMainActivity();
-            FragmentTransaction transaction = fragmentActivity.getSupportFragmentManager().beginTransaction();
+                FragmentActivity fragmentActivity = (FragmentActivity) AppContext.getMainActivity();
+                FragmentTransaction transaction = fragmentActivity.getSupportFragmentManager().beginTransaction();
 
-            transaction.remove(oCurrentFragment);
+                transaction.remove(oCurrentFragment);
+                transaction.commit();
+                wasAdded = true;
+            }
 
             oCurrentPage = oParentPage;
             oCurrentFragment = oParentFragment;
 
-            transaction.commit();
-            return true;
+            return wasAdded;
         }
         return false;
     }
@@ -180,4 +230,22 @@ public class NavigationManager {
         return closeDialog();
     }
 
+    private void updateNavigationHighlight() {
+        NavigationView navigationView = (NavigationView) AppContext.getMainActivity().findViewById(iNavigationViewId);
+
+        if(oCurrentPage == null)
+            return;
+
+        int activeItemId = 0;
+        for(CampusAppContext.NavigationItem navitem : AppContext.NAVIGATION_TARGETS) {
+            if(oCurrentPage.name.equalsIgnoreCase(navitem.navTarget)) {
+                activeItemId = navitem.navItemId;
+            }
+        }
+        if(activeItemId == 0)
+            return;
+
+        navigationView.setCheckedItem(activeItemId);
+    }
+
 }