Skip to content

Commit

Permalink
Fixed issue with Title not showing on back press
Browse files Browse the repository at this point in the history
  • Loading branch information
ObjectiveTruth committed Jul 21, 2016
1 parent f36bb4b commit f1ee42c
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.objectivetruth.uoitlibrarybooking.app.UOITLibraryBookingApp;
import com.objectivetruth.uoitlibrarybooking.app.networking.MockHttpStack;
import com.objectivetruth.uoitlibrarybooking.data.DataModule;
import com.squareup.spoon.Spoon;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
Expand Down Expand Up @@ -67,38 +68,38 @@ protected RequestQueue providesRequestQueue() {

@Test
public void testDrawerGoesToCorrectScreens() {
//Spoon.screenshot(mActivity, "initial_screen");
Spoon.screenshot(mActivity, "initial_screen");

//onView(withId(android.R.id.button1)).perform(click());
//Spoon.screenshot(mActivity, "after_dismiss_dialog");
onView(withId(android.R.id.button1)).perform(click());
Spoon.screenshot(mActivity, "after_dismiss_dialog");
onView(withId(R.id.calendar_content_frame)).check(matches((isDisplayed())));

onView(withId(R.id.drawer_layout)).perform(DrawerActions.open());
//Spoon.screenshot(mActivity, "after_open_drawer");
Spoon.screenshot(mActivity, "after_open_drawer");

onView(withText("Library Booking Policies")).perform(click());
//Spoon.screenshot(mActivity, "after_open_guidelines_policies");
Spoon.screenshot(mActivity, "after_open_guidelines_policies");
onView(withText("Booking Guidelines")).check(matches(isDisplayed()));

onView(withId(R.id.drawer_layout)).perform(DrawerActions.open());
//Spoon.screenshot(mActivity, "after_open_drawer_2");
Spoon.screenshot(mActivity, "after_open_drawer_2");

onView(withText("About")).perform(click());
//Spoon.screenshot(mActivity, "after_open_about");
Spoon.screenshot(mActivity, "after_open_about");
onView(withText(R.string.about_description)).check(matches(isDisplayed()));

onView(withId(R.id.drawer_layout)).perform(DrawerActions.open());
//Spoon.screenshot(mActivity, "after_open_drawer_3");
Spoon.screenshot(mActivity, "after_open_drawer_3");

onView(withText("My Account")).perform(click());
//Spoon.screenshot(mActivity, "after_open_my_account");
Spoon.screenshot(mActivity, "after_open_my_account");
onView(withText("Login")).check(matches(isDisplayed()));

onView(withId(R.id.drawer_layout)).perform(DrawerActions.open());
//Spoon.screenshot(mActivity, "after_open_drawer_4");
Spoon.screenshot(mActivity, "after_open_drawer_4");

onView(withText("Calendar")).perform(click());
//Spoon.screenshot(mActivity, "after_open_calendar");
Spoon.screenshot(mActivity, "after_open_calendar");
onView(withId(R.id.calendar_content_frame)).check(matches((isDisplayed())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,23 @@
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.objectivetruth.uoitlibrarybooking.BuildConfig;
import com.objectivetruth.uoitlibrarybooking.MainActivity;
import com.objectivetruth.uoitlibrarybooking.R;
import mehdi.sakout.aboutpage.AboutPage;
import mehdi.sakout.aboutpage.Element;
import timber.log.Timber;

import java.util.HashMap;
import java.util.Map;

public class About extends Fragment{
private static final String ABOUT_TITLE = "About";

@Nullable
@Override
public View onCreateView(LayoutInflater inflater,
Expand Down Expand Up @@ -69,6 +74,23 @@ public View onCreateView(LayoutInflater inflater,
return aboutPage.create();
}

@Override
public void onHiddenChanged(boolean isNowHidden) {
if(isNowHidden) {
Timber.d(getClass().getSimpleName() + " isNowHidden");
}else {
Timber.d(getClass().getSimpleName() + " isNowVisible");
_setTitle(ABOUT_TITLE);
}
super.onHiddenChanged(isNowHidden);
}

private void _setTitle(String title) {
ActionBar actionBar = ((MainActivity) getActivity()).getSupportActionBar();
if(actionBar != null) {
actionBar.setTitle(title);
}
}

private Element _makeElement(String description) {
Element element = new Element();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.view.*;
import android.widget.Toast;
Expand All @@ -19,6 +20,7 @@
import com.google.gson.Gson;
import com.objectivetruth.uoitlibrarybooking.ActivityRoomInteraction;
import com.objectivetruth.uoitlibrarybooking.BuildConfig;
import com.objectivetruth.uoitlibrarybooking.MainActivity;
import com.objectivetruth.uoitlibrarybooking.R;
import com.objectivetruth.uoitlibrarybooking.app.UOITLibraryBookingApp;
import com.objectivetruth.uoitlibrarybooking.data.models.CalendarModel;
Expand All @@ -44,6 +46,7 @@
import static com.objectivetruth.uoitlibrarybooking.common.constants.SHARED_PREFERENCES_KEYS.HAS_LEARNED_HELP;

public class Calendar extends Fragment {
private static final String CALENDAR_TITLE = "Calendar";
@Inject CalendarModel calendarModel;
@Inject SharedPreferences mDefaultSharedPreferences;
@Inject SharedPreferences.Editor mDefaultSharedPreferencesEditor;
Expand Down Expand Up @@ -84,18 +87,19 @@ public void onStart() {
@Override
public void onHiddenChanged(boolean isNowHidden) {
if(isNowHidden) {
Timber.d("Calendar isNowHidden");
Timber.d(getClass().getSimpleName() + " isNowHidden");
_teardownViewBindings(_mSwipeLayout);
}else {
Timber.d("Calendar isNowVisible");
Timber.d(getClass().getSimpleName() + "isNowVisible");
_setTitle(CALENDAR_TITLE);
_setupViewBindings(_mSwipeLayout, calendarModel.getCalendarDataRefreshObservable());
}
super.onHiddenChanged(isNowHidden);
}

@Override
public void onStop() {
Timber.d("Calendar Stopped");
Timber.d(getClass().getSimpleName() + " Stopped");
_teardownViewBindings(_mSwipeLayout);
super.onStop();
}
Expand All @@ -106,6 +110,13 @@ public void onSaveInstanceState(Bundle outState) {
outState.putBoolean(HAS_SHOWN_INITIAL_SCREEN_BUNDLE_KEY, _hasShownInitialScreen);
}

private void _setTitle(String title) {
ActionBar actionBar = ((MainActivity) getActivity()).getSupportActionBar();
if(actionBar != null) {
actionBar.setTitle(title);
}
}

/**
* Undoes all the bindings to the view, this is important so events don't get fired when the view is no longer
* visible, or "in-view". This function is idempotent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public abstract class ActivityBase extends AppCompatActivity {
private DrawerLayout _mDrawerLayout;
private HashMap<String, Fragment> stringFragmentHashMap = new HashMap<String, Fragment>();
private int _lastMenuItemIDRequested;
private boolean _isFirstTimeSelectDrawerItem = true;
private NavigationView navigationView;
private String LAST_MENU_ITEM_ID_REQUESTED_BUNDLE_KEY = "LAST_MENU_ITEM_ID_REQUESTED";
private String MAIN_FRAGMENT_TAGS_BUNDLE_KEY = "MAIN_FRAGMENT_TAGS_BUNDLE_KEY";
Expand Down Expand Up @@ -244,32 +245,45 @@ protected boolean selectDrawerItem(MenuItem menuItem) {
}
if(fragmentTagIsFoundTriple == null) {return false;}

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
// If the fragment was already created(isFound), just show it. If not,
// Insert the fragment by replacing any existing fragment.
// Don't forget to tag it so it can be retrieved later without loading it again

if(_isFragmentFoundAndVisible(fragmentTagIsFoundTriple)) {
Timber.d("Drawer Item: " + menuItem.getTitle() + " already being shown. Not changing screen");
}else if(!_isFragmentFoundAndVisible(fragmentTagIsFoundTriple)) {
_hideAllVisibleFragments(stringFragmentHashMap);
fragmentManager.beginTransaction()
fragmentTransaction =
_addHideAllVisibleFragmentsToFragmentTransaction(stringFragmentHashMap, fragmentTransaction)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.show(fragmentTagIsFoundTriple.getLeft())
.show(fragmentTagIsFoundTriple.getLeft());
// On the first time opening the drawer there is a special case where the backstack will be empty, so adding
// will cause the user to see a blank screen with nothing if they press back
if(_isFirstTimeSelectDrawerItem) {
_isFirstTimeSelectDrawerItem = false;
}else{
fragmentTransaction.addToBackStack(null);
}

fragmentTransaction
.commit();
}else {
_hideAllVisibleFragments(stringFragmentHashMap);
fragmentManager.beginTransaction()
// To make sure the backstack works correctly, we split this into 2 separate transactions
// This one JUST adds it to the correct frame, and the 2nd one hides/unhides
getSupportFragmentManager().beginTransaction()
.add(R.id.mainactivity_content_frame,
fragmentTagIsFoundTriple.getLeft(),
fragmentTagIsFoundTriple.getMiddle())
.commit();

_addHideAllVisibleFragmentsToFragmentTransaction(stringFragmentHashMap, fragmentTransaction)
.addToBackStack(null)
.commit();
}

// Highlight the selected item
menuItem.setChecked(true);
// Set action bar title
if(getSupportActionBar() != null) {getSupportActionBar().setTitle(menuItem.getTitle());};
_mDrawerLayout.closeDrawers();
return true;
}
Expand All @@ -278,16 +292,16 @@ private boolean _isFragmentFoundAndVisible(Triple<Fragment, String, Boolean> fra
return fragmentTagIsFoundTriple.getRight() && fragmentTagIsFoundTriple.getLeft().isVisible();
}

private void _hideAllVisibleFragments(HashMap<String, Fragment> stringFragmentHashMap) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();

private FragmentTransaction _addHideAllVisibleFragmentsToFragmentTransaction(HashMap<String, Fragment>
stringFragmentHashMap,
FragmentTransaction fragmentTransaction) {
Collection<Fragment> fragmentCollection = stringFragmentHashMap.values();
for(Fragment fragment: fragmentCollection) {
if(fragment != null && fragment.isVisible()) {
fragmentTransaction = fragmentTransaction.hide(fragment);
}
}
fragmentTransaction.commit();
return fragmentTransaction;
}

protected ActionBarDrawerToggle getActionBarDrawerToggle() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.objectivetruth.uoitlibrarybooking.MainActivity;
import com.objectivetruth.uoitlibrarybooking.R;
import timber.log.Timber;

public class GuidelinesAndPolicies extends Fragment{
private static final String GUIDELINES_AND_POLICIES_TITLE = "Policies";
@Nullable
@Override
public View onCreateView(LayoutInflater inflater,
Expand All @@ -17,6 +21,24 @@ public View onCreateView(LayoutInflater inflater,
return inflater.inflate(R.layout.guidelines_policies, container, false);
}

@Override
public void onHiddenChanged(boolean isNowHidden) {
if(isNowHidden) {
Timber.d(getClass().getSimpleName() + " isNowHidden");
}else {
Timber.d(getClass().getSimpleName() + " isNowVisible");
_setTitle(GUIDELINES_AND_POLICIES_TITLE);
}
super.onHiddenChanged(isNowHidden);
}

private void _setTitle(String title) {
ActionBar actionBar = ((MainActivity) getActivity()).getSupportActionBar();
if(actionBar != null) {
actionBar.setTitle(title);
}
}

public static GuidelinesAndPolicies newInstance() {
return new GuidelinesAndPolicies();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.objectivetruth.uoitlibrarybooking.MainActivity;
import com.objectivetruth.uoitlibrarybooking.R;
import com.objectivetruth.uoitlibrarybooking.app.UOITLibraryBookingApp;
import com.objectivetruth.uoitlibrarybooking.data.models.UserModel;
Expand All @@ -23,6 +25,7 @@

public class MyAccount extends Fragment {
private Subscription myAccountDataLoginStateObservableSubscription;
private static final String MY_ACCOUNT_TITLE = "My Account";
public @Inject UserModel userModel;

@Override
Expand All @@ -40,25 +43,27 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,

@Override
public void onStart() {
Timber.d("MyAccount onStart");
Timber.d(getClass().getSimpleName() + " onStart");
_setupViewBindings(userModel.getLoginStateObservable());
super.onStart();
}

@Override
public void onHiddenChanged(boolean isNowHidden) {
if(isNowHidden) {
Timber.d("MyAccount isNowHidden");
Timber.d(getClass().getSimpleName() + " isNowHidden");
_teardownViewBindings();
}else {
Timber.d("MyAccount isNowVisible");
Timber.d(getClass().getSimpleName() + " isNowVisible");
_setTitle(MY_ACCOUNT_TITLE);
_setupViewBindings(userModel.getLoginStateObservable());
}
super.onHiddenChanged(isNowHidden);
}

@Override
public void onStop() {
Timber.d("MyAccount Stopped");
Timber.d(getClass().getSimpleName() + " Stopped");
_teardownViewBindings();
super.onStop();
}
Expand Down Expand Up @@ -116,7 +121,6 @@ public void onNext(MyAccountDataLoginState myAccountDataLoginState) {
private void _showFullscreenLoading() {
getChildFragmentManager().beginTransaction()
.replace(R.id.my_account_content_frame, Loading.newInstance())
.addToBackStack(null)
.commit();
}

Expand All @@ -126,6 +130,13 @@ private boolean _isMyAccountLoadedShowing() {
return currentFragmentInContentFrame instanceof MyAccountLoaded && currentFragmentInContentFrame.isVisible();
}

private void _setTitle(String title) {
ActionBar actionBar = ((MainActivity) getActivity()).getSupportActionBar();
if(actionBar != null) {
actionBar.setTitle(title);
}
}

private boolean _isMyAccountLoadedNOTShowing() {
return !_isMyAccountLoadedShowing();
}
Expand All @@ -144,7 +155,6 @@ private void _showMyAccountLoadedFragment(MyAccountDataLoginState myAccountDataL
//getActivity().invalidateOptionsMenu();
getChildFragmentManager().beginTransaction()
.replace(R.id.my_account_content_frame, myAccountLoadedFragment, MY_ACCOUNT_LOADED_FRAGMENT_TAG)
.addToBackStack(null)
.commit();
}

Expand All @@ -163,7 +173,6 @@ private void _showLoginFragment(MyAccountDataLoginState myAccountDataLoginState)

getChildFragmentManager().beginTransaction()
.replace(R.id.my_account_content_frame, myAccountLoginFragment, MY_ACCOUNT_LOGIN_FRAGMENT_TAG)
.addToBackStack(null)
.commit();
}

Expand Down

0 comments on commit f1ee42c

Please sign in to comment.