[broken] Significant refactoring work

Last step is getting the downloader back up to speed.
This commit is contained in:
Bradlee Speice 2014-07-23 23:02:48 -04:00
parent f3cd9b8de4
commit 787cc4aa24
12 changed files with 557 additions and 529 deletions

View File

@ -1,301 +0,0 @@
package org.bspeice.minimalbible.activity;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ListView;
import com.readystatesoftware.systembartint.SystemBarTintManager;
import org.bspeice.minimalbible.R;
/**
* Fragment used for managing interactions for and presentation of a navigation
* drawer. See the <a href=
* "https://developer.android.com/design/patterns/navigation-drawer.html#Interaction"
* > design guidelines</a> for a complete explanation of the behaviors
* implemented here.
* TODO: Refactor to allow ExpandableListView
*/
public class BaseNavigationDrawerFragment extends Fragment {
/**
* Remember the position of the selected item.
*/
private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position";
/**
* Per the design guidelines, you should show the drawer on launch until the
* user manually expands it. This shared preference tracks this.
*/
private static final String PREF_USER_LEARNED_DRAWER = "navigation_drawer_learned";
protected ListView mDrawerListView;
protected int mCurrentSelectedPosition = 0;
/**
* A pointer to the current callbacks instance (the Activity).
*/
private NavigationDrawerCallbacks mCallbacks;
/**
* Helper component that ties the action bar to the navigation drawer.
*/
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private View mFragmentContainerView;
private boolean mFromSavedInstanceState;
private boolean mUserLearnedDrawer;
public BaseNavigationDrawerFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Read in the flag indicating whether or not the user has demonstrated
// awareness of the
// drawer. See PREF_USER_LEARNED_DRAWER for details.
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(getActivity());
mUserLearnedDrawer = sp.getBoolean(PREF_USER_LEARNED_DRAWER, false);
if (savedInstanceState != null) {
mCurrentSelectedPosition = savedInstanceState
.getInt(STATE_SELECTED_POSITION);
mFromSavedInstanceState = true;
}
// Select either the default item (0) or the last selected item.
selectItem(mCurrentSelectedPosition);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Indicate that this fragment would like to influence the set of
// actions in the action bar.
setHasOptionsMenu(true);
}
public boolean isDrawerOpen() {
return mDrawerLayout != null
&& mDrawerLayout.isDrawerOpen(mFragmentContainerView);
}
/**
* Users of this fragment must call this method to set up the navigation
* drawer interactions.
*
* @param fragmentId
* The android:id of this fragment in its activity's layout.
* @param drawerLayout
* The DrawerLayout containing this fragment's UI.
*/
public void setUp(int fragmentId, DrawerLayout drawerLayout) {
mFragmentContainerView = getActivity().findViewById(fragmentId);
mDrawerLayout = drawerLayout;
// set a custom shadow that overlays the main content when the drawer
// opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
// set up the drawer's list view with items and click listener
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions
// between the navigation drawer and the action bar app icon.
mDrawerToggle = new ActionBarDrawerToggle(getActivity(), /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.navigation_drawer_open, /*
* "open drawer" description for
* accessibility
*/
R.string.navigation_drawer_close /*
* "close drawer" description for
* accessibility
*/
) {
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
if (!isAdded()) {
return;
}
getActivity().supportInvalidateOptionsMenu(); // calls
// onPrepareOptionsMenu()
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
if (!isAdded()) {
return;
}
if (!mUserLearnedDrawer) {
// The user manually opened the drawer; store this flag to
// prevent auto-showing
// the navigation drawer automatically in the future.
mUserLearnedDrawer = true;
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(getActivity());
sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true)
.commit();
}
getActivity().supportInvalidateOptionsMenu(); // calls
// onPrepareOptionsMenu()
}
};
// If the user hasn't 'learned' about the drawer, open it to introduce
// them to the drawer,
// per the navigation drawer design guidelines.
if (!mUserLearnedDrawer && !mFromSavedInstanceState) {
mDrawerLayout.openDrawer(mFragmentContainerView);
}
// Defer code dependent on restoration of previous instance state.
mDrawerLayout.post(new Runnable() {
@Override
public void run() {
mDrawerToggle.syncState();
}
});
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
public void selectItem(int position) {
mCurrentSelectedPosition = position;
if (mDrawerListView != null) {
mDrawerListView.setItemChecked(position, true);
((NavDrawerAdapter<String>)mDrawerListView.getAdapter()).setCurrentlyHighlighted(position);
}
if (mDrawerLayout != null) {
mDrawerLayout.closeDrawer(mFragmentContainerView);
}
if (mCallbacks != null) {
mCallbacks.onNavigationDrawerItemSelected(position);
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallbacks = (NavigationDrawerCallbacks) activity;
} catch (ClassCastException e) {
throw new ClassCastException(
"Activity must implement NavigationDrawerCallbacks.");
}
}
@Override
public void onDetach() {
super.onDetach();
mCallbacks = null;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Forward the new configuration the drawer toggle component.
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// If the drawer is open, show the global app actions in the action bar.
// See also
// showGlobalContextActionBar, which controls the top-left area of the
// action bar.
if (mDrawerLayout != null && isDrawerOpen()) {
inflater.inflate(R.menu.global, menu);
showGlobalContextActionBar();
}
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* Per the navigation drawer design guidelines, updates the action bar to
* show the global app 'context', rather than just what's in the current
* screen.
*/
private void showGlobalContextActionBar() {
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
// actionBar.setTitle(R.string.app_name);
}
protected ActionBar getActionBar() {
return ((ActionBarActivity) getActivity()).getSupportActionBar();
}
public void setInsets(View view) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT)
return;
SystemBarTintManager tintManager = new SystemBarTintManager(getActivity());
SystemBarTintManager.SystemBarConfig config = tintManager.getConfig();
view.setPadding(0, config.getPixelInsetTop(true),
config.getPixelInsetRight(), config.getPixelInsetBottom());
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// This could also be a ScrollView
ExpandableListView list = (ExpandableListView) view.findViewById(R.id.list_nav_drawer);
// This could also be set in your layout, allows the list items to
// scroll through the bottom padded area (navigation bar)
list.setClipToPadding(false);
// Sets the padding to the insets (include action bar and navigation bar
// padding for the current device and orientation)
setInsets(list);
}
/**
* Callbacks interface that all activities using this fragment must
* implement.
*/
public static interface NavigationDrawerCallbacks {
/**
* Called when an item in the navigation drawer is selected.
*/
void onNavigationDrawerItemSelected(int position);
}
}

View File

@ -13,7 +13,7 @@ import org.bspeice.minimalbible.MinimalBible;
import org.bspeice.minimalbible.OGHolder; import org.bspeice.minimalbible.OGHolder;
import org.bspeice.minimalbible.R; import org.bspeice.minimalbible.R;
import org.bspeice.minimalbible.activity.BaseActivity; import org.bspeice.minimalbible.activity.BaseActivity;
import org.bspeice.minimalbible.activity.BaseNavigationDrawerFragment; import org.bspeice.minimalbible.activity.navigation.NavDrawerFragment;
import org.crosswire.jsword.book.BookCategory; import org.crosswire.jsword.book.BookCategory;
import java.util.List; import java.util.List;
@ -24,26 +24,23 @@ import javax.inject.Named;
import dagger.ObjectGraph; import dagger.ObjectGraph;
public class DownloadActivity extends BaseActivity implements public class DownloadActivity extends BaseActivity implements
BaseNavigationDrawerFragment.NavigationDrawerCallbacks, NavDrawerFragment.NavigationDrawerCallbacks,
Injector { Injector {
private final String TAG = "DownloadActivity"; private final String TAG = "DownloadActivity";
@Inject
@Named("ValidCategories")
List<BookCategory> validCategories;
/** /**
* Fragment managing the behaviors, interactions and presentation of the * Fragment managing the behaviors, interactions and presentation of the
* navigation drawer. * navigation drawer.
*/ */
private DownloadNavDrawerFragment mNavigationDrawerFragment; private DownloadNavDrawerFragment mNavigationDrawerFragment;
/** /**
* Used to store the last screen title. For use in * Used to store the last screen title. For use in
* {@link #restoreActionBar()}. * {@link #restoreActionBar()}.
*/ */
private CharSequence mTitle; private CharSequence mTitle;
@Inject @Named("ValidCategories")
List<BookCategory> validCategories;
private ObjectGraph daObjectGraph; private ObjectGraph daObjectGraph;
/** /**

View File

@ -1,18 +1,16 @@
package org.bspeice.minimalbible.activity.downloader; package org.bspeice.minimalbible.activity.downloader;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView; import android.widget.ListView;
import org.bspeice.minimalbible.Injector; import org.bspeice.minimalbible.Injector;
import org.bspeice.minimalbible.R; import org.bspeice.minimalbible.R;
import org.bspeice.minimalbible.activity.BaseNavigationDrawerFragment; import org.bspeice.minimalbible.activity.navigation.ListNavAdapter;
import org.bspeice.minimalbible.activity.NavDrawerAdapter; import org.bspeice.minimalbible.activity.navigation.NavDrawerFragment;
import org.crosswire.jsword.book.BookCategory; import org.crosswire.jsword.book.BookCategory;
import java.util.List; import java.util.List;
@ -20,9 +18,10 @@ import java.util.List;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
public class DownloadNavDrawerFragment extends BaseNavigationDrawerFragment { public class DownloadNavDrawerFragment extends NavDrawerFragment {
@Inject @Named("ValidCategories") @Inject
@Named("ValidCategories")
List<BookCategory> validCategories; List<BookCategory> validCategories;
@Override @Override
@ -41,7 +40,7 @@ public class DownloadNavDrawerFragment extends BaseNavigationDrawerFragment {
} }
}); });
mDrawerListView.setAdapter(new NavDrawerAdapter<BookCategory>(getActivity(), mDrawerListView.setAdapter(new ListNavAdapter<BookCategory>(getActivity(),
validCategories)); validCategories));
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true); mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
return mDrawerListView; return mDrawerListView;

View File

@ -1,4 +1,4 @@
package org.bspeice.minimalbible.activity.viewer; package org.bspeice.minimalbible.activity.navigation;
import android.content.Context; import android.content.Context;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -7,61 +7,44 @@ import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter; import android.widget.BaseExpandableListAdapter;
import android.widget.TextView; import android.widget.TextView;
import org.bspeice.minimalbible.Injector;
import org.bspeice.minimalbible.R; import org.bspeice.minimalbible.R;
import org.bspeice.minimalbible.activity.viewer.bookutil.VersificationUtil;
import org.crosswire.jsword.book.Book;
import org.crosswire.jsword.versification.BibleBook;
import org.crosswire.jsword.versification.Versification;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import javax.inject.Inject;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import butterknife.InjectView; import butterknife.InjectView;
import rx.functions.Action1;
/** /**
* ExpandableListView Navigation Drawer * ExpandableListView Navigation Drawer
* TODO: Refactor out to ExpandableNavDrawerAdapter? * T1 represents Group objects, T2 is child objects
* Not sure if I'll ever actually need to re-use this, but go ahead and make it generic.
* TODO: Document this.
*/ */
public class BibleNavAdapter extends BaseExpandableListAdapter { public class ExpListNavAdapter<T1, T2> extends BaseExpandableListAdapter {
@Inject
VersificationUtil vUtil;
Versification versification;
Book book;
// Now we could technically implement this structure using a LinkedHashMap, but // Now we could technically implement this structure using a LinkedHashMap, but
// it's easier both to understand and program if we implement this using two maps. // it's easier both to understand and program if we implement this using two maps.
Map<Integer, BibleBook> indexableBibleBooks; Map<Integer, T1> indexableBibleBooks;
Map<BibleBook, Integer> chaptersForBook; Map<T1, List<T2>> chaptersForBook;
private int groupHighlighted; private int groupHighlighted;
private int childHighlighted; private int childHighlighted;
public BibleNavAdapter(final Book b, Injector injector) { public ExpListNavAdapter(List<T1> groups, Map<T1, List<T2>> children) {
injector.inject(this);
this.book = b;
versification = vUtil.getVersification(book);
// Let the map know ahead of time how big it will be // Let the map know ahead of time how big it will be
// int bookCount = versification.getBookCount(); // int bookCount = versification.getBookCount();
indexableBibleBooks = new HashMap<Integer, BibleBook>(); indexableBibleBooks = new HashMap<Integer, T1>(groups.size());
chaptersForBook = new HashMap<BibleBook, Integer>(); chaptersForBook = new HashMap<T1, List<T2>>(groups.size());
final AtomicInteger counter = new AtomicInteger(0); // Is it terrible that I don't like using an actual for loop?
vUtil.getBooks(book) for (int index = 0; index < groups.size(); index++) {
.forEach(new Action1<BibleBook>() { T1 gItem = groups.get(index);
@Override indexableBibleBooks.put(index, gItem);
public void call(BibleBook bibleBook) { chaptersForBook.put(gItem, children.get(gItem));
indexableBibleBooks.put(counter.getAndIncrement(), bibleBook);
chaptersForBook.put(bibleBook, vUtil.getChapterCount(b, bibleBook));
} }
});
} }
@Override @Override
@ -71,25 +54,22 @@ public class BibleNavAdapter extends BaseExpandableListAdapter {
@Override @Override
public int getChildrenCount(int i) { public int getChildrenCount(int i) {
return chaptersForBook.get(indexableBibleBooks.get(i)); return chaptersForBook.get(indexableBibleBooks.get(i)).size();
} }
@Override @Override
public String getGroup(int i) { public T1 getGroup(int i) {
return vUtil.getBookName(book, indexableBibleBooks.get(i)); return indexableBibleBooks.get(i);
} }
/** /**
* Take a shortcut - since the second item is the (indexed) chapter number,
* we just need to add one to remove the off-by-one
*
* @param i The group position * @param i The group position
* @param i2 The child position * @param i2 The child position
* @return The child chapter value * @return The child chapter value
*/ */
@Override @Override
public Integer getChild(int i, int i2) { public T2 getChild(int i, int i2) {
return i2 + 1; return chaptersForBook.get(indexableBibleBooks.get(i)).get(i2);
} }
@Override @Override
@ -104,7 +84,7 @@ public class BibleNavAdapter extends BaseExpandableListAdapter {
@Override @Override
public boolean hasStableIds() { public boolean hasStableIds() {
return false; return true;
} }
/** /**
@ -119,16 +99,16 @@ public class BibleNavAdapter extends BaseExpandableListAdapter {
@Override @Override
public View getGroupView(int position, boolean expanded, public View getGroupView(int position, boolean expanded,
View convertView, ViewGroup parent) { View convertView, ViewGroup parent) {
NavItemHolder<String> bookHolder; NavItemHolder<T1> bookHolder;
if (convertView == null || convertView.getTag() == null) { if (convertView == null || convertView.getTag() == null) {
LayoutInflater inflater = (LayoutInflater) parent.getContext() LayoutInflater inflater = (LayoutInflater) parent.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE); .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_navigation_drawer, convertView = inflater.inflate(R.layout.list_navigation_drawer,
parent, false); parent, false);
bookHolder = new NavItemHolder<String>(convertView); bookHolder = new NavItemHolder<T1>(convertView);
convertView.setTag(bookHolder); convertView.setTag(bookHolder);
} else { } else {
bookHolder = (NavItemHolder<String>) convertView.getTag(); bookHolder = (NavItemHolder<T1>) convertView.getTag();
} }
bookHolder.bind(getGroup(position), position == groupHighlighted); bookHolder.bind(getGroup(position), position == groupHighlighted);
@ -148,16 +128,16 @@ public class BibleNavAdapter extends BaseExpandableListAdapter {
@Override @Override
public View getChildView(int groupPosition, int childPosition, public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) { boolean isLastChild, View convertView, ViewGroup parent) {
NavItemHolder<Integer> chapterHolder; NavItemHolder<T2> chapterHolder;
if (convertView == null || convertView.getTag() == null) { if (convertView == null || convertView.getTag() == null) {
LayoutInflater inflater = (LayoutInflater) parent.getContext() LayoutInflater inflater = (LayoutInflater) parent.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE); .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_navigation_drawer, convertView = inflater.inflate(R.layout.list_navigation_drawer,
parent, false); parent, false);
chapterHolder = new NavItemHolder<Integer>(convertView); chapterHolder = new NavItemHolder<T2>(convertView);
convertView.setTag(chapterHolder); convertView.setTag(chapterHolder);
} else { } else {
chapterHolder = (NavItemHolder<Integer>) convertView.getTag(); chapterHolder = (NavItemHolder<T2>) convertView.getTag();
} }
chapterHolder.bind(getChild(groupPosition, childPosition), chapterHolder.bind(getChild(groupPosition, childPosition),
@ -180,8 +160,9 @@ public class BibleNavAdapter extends BaseExpandableListAdapter {
/** /**
* Class to hold elements for the navbar - doesn't matter if they're group or child. * Class to hold elements for the navbar - doesn't matter if they're group or child.
* T3 is either T1 or T2, doesn't matter.
*/ */
class NavItemHolder<T> { class NavItemHolder<T3> {
@InjectView(R.id.navlist_content) @InjectView(R.id.navlist_content)
TextView content; TextView content;
@ -192,7 +173,7 @@ public class BibleNavAdapter extends BaseExpandableListAdapter {
ButterKnife.inject(this, v); ButterKnife.inject(this, v);
} }
public void bind(T object, boolean highlighted) { public void bind(T3 object, boolean highlighted) {
content.setText(object.toString()); content.setText(object.toString());
if (highlighted) { if (highlighted) {
content.setTextColor(v.getResources().getColor(R.color.navbar_highlight)); content.setTextColor(v.getResources().getColor(R.color.navbar_highlight));

View File

@ -1,4 +1,4 @@
package org.bspeice.minimalbible.activity; package org.bspeice.minimalbible.activity.navigation;
import android.content.Context; import android.content.Context;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -19,24 +19,24 @@ import butterknife.InjectView;
* This class (and its usage) needs some work refactoring, * This class (and its usage) needs some work refactoring,
* but the PoC is looking good! * but the PoC is looking good!
*/ */
public class NavDrawerAdapter<T> extends BaseAdapter { public class ListNavAdapter<T> extends BaseAdapter {
Context context; Context context;
List<T> objects; List<T> objects;
int currentlyHighlighted; int currentlyHighlighted;
public NavDrawerAdapter(Context context, List<T> objects) { public ListNavAdapter(Context context, List<T> objects) {
this.context = context; this.context = context;
this.objects = objects; this.objects = objects;
} }
public void setCurrentlyHighlighted(int currentlyHighlighted) {
this.currentlyHighlighted = currentlyHighlighted;
}
public int getCurrentlyHighlighted() { public int getCurrentlyHighlighted() {
return this.currentlyHighlighted; return this.currentlyHighlighted;
} }
public void setCurrentlyHighlighted(int currentlyHighlighted) {
this.currentlyHighlighted = currentlyHighlighted;
}
@Override @Override
public int getCount() { public int getCount() {
return objects.size(); return objects.size();

View File

@ -0,0 +1,299 @@
package org.bspeice.minimalbible.activity.navigation;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ListView;
import com.readystatesoftware.systembartint.SystemBarTintManager;
import org.bspeice.minimalbible.R;
/**
* Fragment used for managing interactions for and presentation of a navigation
* drawer. See the <a href=
* "https://developer.android.com/design/patterns/navigation-drawer.html#Interaction"
* > design guidelines</a> for a complete explanation of the behaviors
* implemented here.
* TODO: Refactor to allow ExpandableListView
*/
public class NavDrawerFragment extends Fragment {
/**
* Remember the position of the selected item.
*/
private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position";
/**
* Per the design guidelines, you should show the drawer on launch until the
* user manually expands it. This shared preference tracks this.
*/
private static final String PREF_USER_LEARNED_DRAWER = "navigation_drawer_learned";
protected ListView mDrawerListView;
protected int mCurrentSelectedPosition = 0;
/**
* A pointer to the current callbacks instance (the Activity).
*/
private NavigationDrawerCallbacks mCallbacks;
/**
* Helper component that ties the action bar to the navigation drawer.
*/
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private View mFragmentContainerView;
private boolean mFromSavedInstanceState;
private boolean mUserLearnedDrawer;
public NavDrawerFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Read in the flag indicating whether or not the user has demonstrated
// awareness of the
// drawer. See PREF_USER_LEARNED_DRAWER for details.
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(getActivity());
mUserLearnedDrawer = sp.getBoolean(PREF_USER_LEARNED_DRAWER, false);
if (savedInstanceState != null) {
mCurrentSelectedPosition = savedInstanceState
.getInt(STATE_SELECTED_POSITION);
mFromSavedInstanceState = true;
}
// Select either the default item (0) or the last selected item.
selectItem(mCurrentSelectedPosition);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Indicate that this fragment would like to influence the set of
// actions in the action bar.
setHasOptionsMenu(true);
}
public boolean isDrawerOpen() {
return mDrawerLayout != null
&& mDrawerLayout.isDrawerOpen(mFragmentContainerView);
}
/**
* Users of this fragment must call this method to set up the navigation
* drawer interactions.
*
* @param fragmentId The android:id of this fragment in its activity's layout.
* @param drawerLayout The DrawerLayout containing this fragment's UI.
*/
public void setUp(int fragmentId, DrawerLayout drawerLayout) {
mFragmentContainerView = getActivity().findViewById(fragmentId);
mDrawerLayout = drawerLayout;
// set a custom shadow that overlays the main content when the drawer
// opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
// set up the drawer's list view with items and click listener
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions
// between the navigation drawer and the action bar app icon.
mDrawerToggle = new ActionBarDrawerToggle(getActivity(), /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.navigation_drawer_open, /*
* "open drawer" description for
* accessibility
*/
R.string.navigation_drawer_close /*
* "close drawer" description for
* accessibility
*/
) {
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
if (!isAdded()) {
return;
}
getActivity().supportInvalidateOptionsMenu(); // calls
// onPrepareOptionsMenu()
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
if (!isAdded()) {
return;
}
if (!mUserLearnedDrawer) {
// The user manually opened the drawer; store this flag to
// prevent auto-showing
// the navigation drawer automatically in the future.
mUserLearnedDrawer = true;
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(getActivity());
sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true)
.commit();
}
getActivity().supportInvalidateOptionsMenu(); // calls
// onPrepareOptionsMenu()
}
};
// If the user hasn't 'learned' about the drawer, open it to introduce
// them to the drawer,
// per the navigation drawer design guidelines.
if (!mUserLearnedDrawer && !mFromSavedInstanceState) {
mDrawerLayout.openDrawer(mFragmentContainerView);
}
// Defer code dependent on restoration of previous instance state.
mDrawerLayout.post(new Runnable() {
@Override
public void run() {
mDrawerToggle.syncState();
}
});
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
public void selectItem(int position) {
mCurrentSelectedPosition = position;
if (mDrawerListView != null) {
mDrawerListView.setItemChecked(position, true);
((ListNavAdapter<String>) mDrawerListView.getAdapter()).setCurrentlyHighlighted(position);
}
if (mDrawerLayout != null) {
mDrawerLayout.closeDrawer(mFragmentContainerView);
}
if (mCallbacks != null) {
mCallbacks.onNavigationDrawerItemSelected(position);
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallbacks = (NavigationDrawerCallbacks) activity;
} catch (ClassCastException e) {
throw new ClassCastException(
"Activity must implement NavigationDrawerCallbacks.");
}
}
@Override
public void onDetach() {
super.onDetach();
mCallbacks = null;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Forward the new configuration the drawer toggle component.
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// If the drawer is open, show the global app actions in the action bar.
// See also
// showGlobalContextActionBar, which controls the top-left area of the
// action bar.
if (mDrawerLayout != null && isDrawerOpen()) {
inflater.inflate(R.menu.global, menu);
showGlobalContextActionBar();
}
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* Per the navigation drawer design guidelines, updates the action bar to
* show the global app 'context', rather than just what's in the current
* screen.
*/
private void showGlobalContextActionBar() {
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
// actionBar.setTitle(R.string.app_name);
}
protected ActionBar getActionBar() {
return ((ActionBarActivity) getActivity()).getSupportActionBar();
}
public void setInsets(View view) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT)
return;
SystemBarTintManager tintManager = new SystemBarTintManager(getActivity());
SystemBarTintManager.SystemBarConfig config = tintManager.getConfig();
view.setPadding(0, config.getPixelInsetTop(true),
config.getPixelInsetRight(), config.getPixelInsetBottom());
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// This could also be a ScrollView
ExpandableListView list = (ExpandableListView) view.findViewById(R.id.list_nav_drawer);
// This could also be set in your layout, allows the list items to
// scroll through the bottom padded area (navigation bar)
list.setClipToPadding(false);
// Sets the padding to the insets (include action bar and navigation bar
// padding for the current device and orientation)
setInsets(list);
}
/**
* Callbacks interface that all activities using this fragment must
* implement.
*/
public static interface NavigationDrawerCallbacks {
/**
* Called when an item in the navigation drawer is selected.
*/
void onNavigationDrawerItemSelected(int position);
}
}

View File

@ -16,8 +16,8 @@ import org.bspeice.minimalbible.MinimalBible;
import org.bspeice.minimalbible.OGHolder; import org.bspeice.minimalbible.OGHolder;
import org.bspeice.minimalbible.R; import org.bspeice.minimalbible.R;
import org.bspeice.minimalbible.activity.BaseActivity; import org.bspeice.minimalbible.activity.BaseActivity;
import org.bspeice.minimalbible.activity.BaseNavigationDrawerFragment;
import org.bspeice.minimalbible.activity.downloader.DownloadActivity; import org.bspeice.minimalbible.activity.downloader.DownloadActivity;
import org.bspeice.minimalbible.activity.navigation.NavDrawerFragment;
import org.crosswire.jsword.book.Book; import org.crosswire.jsword.book.Book;
import javax.inject.Inject; import javax.inject.Inject;
@ -27,7 +27,7 @@ import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1; import rx.functions.Action1;
public class BibleViewer extends BaseActivity implements public class BibleViewer extends BaseActivity implements
BaseNavigationDrawerFragment.NavigationDrawerCallbacks, NavDrawerFragment.NavigationDrawerCallbacks,
Injector { Injector {
@Inject BookManager bookManager; @Inject BookManager bookManager;
@ -37,7 +37,7 @@ public class BibleViewer extends BaseActivity implements
* Fragment managing the behaviors, interactions and presentation of the * Fragment managing the behaviors, interactions and presentation of the
* navigation drawer. * navigation drawer.
*/ */
private ViewerNavDrawerFragment mNavigationDrawerFragment; private ExpListNavDrawerFragment mNavigationDrawerFragment;
/** /**
* Used to store the last screen title. For use in * Used to store the last screen title. For use in
* {@link #restoreActionBar()}. * {@link #restoreActionBar()}.
@ -50,13 +50,11 @@ public class BibleViewer extends BaseActivity implements
private void buildObjGraph() { private void buildObjGraph() {
if (bvObjectGraph == null) { if (bvObjectGraph == null) {
OGHolder holder = OGHolder.get(this); OGHolder holder = OGHolder.get(this);
ObjectGraph holderGraph = holder.fetchGraph(); bvObjectGraph = holder.fetchGraph();
if (holderGraph == null) { if (bvObjectGraph == null) {
bvObjectGraph = MinimalBible.get(this) bvObjectGraph = MinimalBible.get(this)
.plus(new BibleViewerModules(this)); .plus(new BibleViewerModules(this));
holder.persistGraph(holderGraph); holder.persistGraph(bvObjectGraph);
} else {
bvObjectGraph = holderGraph;
} }
} }
bvObjectGraph.inject(this); bvObjectGraph.inject(this);
@ -98,7 +96,7 @@ public class BibleViewer extends BaseActivity implements
setContentView(R.layout.activity_bible_viewer); setContentView(R.layout.activity_bible_viewer);
mNavigationDrawerFragment = (ViewerNavDrawerFragment) getSupportFragmentManager() mNavigationDrawerFragment = (ExpListNavDrawerFragment) getSupportFragmentManager()
.findFragmentById(R.id.navigation_drawer); .findFragmentById(R.id.navigation_drawer);
mTitle = getTitle(); mTitle = getTitle();

View File

@ -3,6 +3,7 @@ package org.bspeice.minimalbible.activity.viewer;
import android.util.Log; import android.util.Log;
import org.bspeice.minimalbible.Injector; import org.bspeice.minimalbible.Injector;
import org.bspeice.minimalbible.activity.navigation.ExpListNavAdapter;
import org.bspeice.minimalbible.activity.viewer.bookutil.VersificationUtil; import org.bspeice.minimalbible.activity.viewer.bookutil.VersificationUtil;
import org.crosswire.jsword.book.Book; import org.crosswire.jsword.book.Book;
@ -24,8 +25,8 @@ import rx.functions.Func1;
injects = { injects = {
BibleViewer.class, BibleViewer.class,
BookFragment.class, BookFragment.class,
ViewerNavDrawerFragment.class, ExpListNavDrawerFragment.class,
BibleNavAdapter.class ExpListNavAdapter.class
}, },
library = true library = true
) )

View File

@ -0,0 +1,110 @@
package org.bspeice.minimalbible.activity.viewer;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import org.bspeice.minimalbible.Injector;
import org.bspeice.minimalbible.R;
import org.bspeice.minimalbible.activity.navigation.ExpListNavAdapter;
import org.bspeice.minimalbible.activity.navigation.NavDrawerFragment;
import org.bspeice.minimalbible.activity.viewer.bookutil.VersificationUtil;
import org.crosswire.jsword.book.Book;
import org.crosswire.jsword.versification.BibleBook;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Named;
import rx.functions.Func1;
import rx.functions.Func2;
/**
* ExpandableListView for managing books of the Bible.
* We extend from @link{BaseNavigationDrawerFragment} so we can inherit some of the lifecycle
* pieces, but the actual view inflation is done by us.
* TODO: Extend BaseExpNavigationDrawerFragment?
*/
public class ExpListNavDrawerFragment extends NavDrawerFragment {
@Inject VersificationUtil vUtil;
@Inject @Named("MainBook")
Book mainBook;
ExpandableListView mActualListView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Injector i = (Injector) getActivity();
i.inject(this);
mActualListView = (ExpandableListView) inflater.inflate(
R.layout.fragment_expandable_navigation_drawer, container, false);
/*
mDrawerListView
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
selectItem(position);
}
});
*/
List<String> bibleBooks = vUtil.getBookNames(mainBook)
.toList().toBlocking().first();
// I really don't like how we build the chapters, but I'm not adding Guava just for Range.
// RXJava does get ridiculous with the angle brackets, you have me there. But Intellij
// folds nicely.
Map<String, List<Integer>> chapterMap =
vUtil.getBooks(mainBook).map(new Func1<BibleBook, Map<String, List<Integer>>>() {
@Override
public Map<String, List<Integer>> call(BibleBook bibleBook) {
// These lines are important
int bookCount = vUtil.getChapterCount(mainBook, bibleBook);
List<Integer> chapterList = new ArrayList<Integer>(bookCount);
for (int i = 0; i < bookCount; i++) {
chapterList.add(i + 1); // Index to chapter number
}
// </important>
Map<String, List<Integer>> bookListMap =
new HashMap<String, List<Integer>>(1);
bookListMap.put(vUtil.getBookName(mainBook, bibleBook), chapterList);
return bookListMap;
}
})
.reduce(new Func2<Map<String, List<Integer>>,
Map<String, List<Integer>>,
Map<String, List<Integer>>>() {
@Override
public Map<String, List<Integer>>
call(Map<String, List<Integer>> acc,
Map<String, List<Integer>> value) {
// These lines are important
acc.putAll(value);
return acc;
// </important>
}
})
.toBlocking()
.first();
ExpListNavAdapter<String, Integer> adapter =
new ExpListNavAdapter<String, Integer>(bibleBooks, chapterMap);
mActualListView.setAdapter(adapter);
mActualListView.setItemChecked(mCurrentSelectedPosition, true);
return mActualListView;
}
}

View File

@ -1,56 +0,0 @@
package org.bspeice.minimalbible.activity.viewer;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import org.bspeice.minimalbible.Injector;
import org.bspeice.minimalbible.R;
import org.bspeice.minimalbible.activity.BaseNavigationDrawerFragment;
import org.bspeice.minimalbible.activity.viewer.bookutil.VersificationUtil;
import org.crosswire.jsword.book.Book;
import javax.inject.Inject;
import javax.inject.Named;
/**
* ExpandableListView for managing books of the Bible.
* We extend from @link{BaseNavigationDrawerFragment} so we can inherit some of the lifecycle
* pieces, but the actual view inflation is done by us.
* TODO: Extend BaseExpNavigationDrawerFragment?
*/
public class ViewerNavDrawerFragment extends BaseNavigationDrawerFragment {
@Inject VersificationUtil vUtil;
@Inject @Named("MainBook")
Book mainBook;
ExpandableListView mActualListView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Injector i = (Injector) getActivity();
i.inject(this);
mActualListView = (ExpandableListView) inflater.inflate(
R.layout.fragment_expandable_navigation_drawer, container, false);
/*
mDrawerListView
.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
selectItem(position);
}
});
*/
mActualListView.setAdapter(new BibleNavAdapter(mainBook, i));
mActualListView.setItemChecked(mCurrentSelectedPosition, true);
return mActualListView;
}
}

View File

@ -15,8 +15,8 @@
android:id="@+id/container" android:id="@+id/container"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true" android:clipToPadding="false"
android:clipToPadding="false" /> android:fitsSystemWindows="true" />
<!-- <!--
android:layout_gravity="start" tells DrawerLayout to treat android:layout_gravity="start" tells DrawerLayout to treat
@ -32,11 +32,11 @@
<fragment <fragment
android:id="@+id/navigation_drawer" android:id="@+id/navigation_drawer"
android:name="org.bspeice.minimalbible.activity.viewer.ViewerNavDrawerFragment" android:name="org.bspeice.minimalbible.activity.viewer.ExpListNavDrawerFragment"
android:layout_width="@dimen/navigation_drawer_width" android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true" android:layout_gravity="start"
android:clipToPadding="false" android:clipToPadding="false"
android:layout_gravity="start" /> android:fitsSystemWindows="true" />
</android.support.v4.widget.DrawerLayout> </android.support.v4.widget.DrawerLayout>

View File

@ -7,6 +7,6 @@
android:choiceMode="singleChoice" android:choiceMode="singleChoice"
android:divider="@android:color/transparent" android:divider="@android:color/transparent"
android:dividerHeight="0dp" android:dividerHeight="0dp"
tools:context="org.bspeice.minimalbible.activity.viewer.ViewerNavDrawerFragment"> tools:context="org.bspeice.minimalbible.activity.viewer.ExpListNavDrawerFragment">
</ExpandableListView> </ExpandableListView>