+/* 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;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
+import java.util.List;
import de.dhbwloe.campusapp.fragments.PopupFragment;
};
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;
}
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;
}
return null;
if(oCurrentPage.fragmentType != 3)
return (CampusAppFragment)oCurrentFragment;
- return null;
+ else
+ return ((PopupFragment)oCurrentFragment).getCurrentFragment();
}
public void navigatePage(String name) {
}
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)
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();
}
}
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;
}
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);
+ }
+
}