mirror of
				https://github.com/MinimalBible/MinimalBible
				synced 2025-10-31 09:30:25 -04:00 
			
		
		
		
	Refactor main menu to Kotlin
So much less code to write and maintain... love it.
This commit is contained in:
		| @ -14,8 +14,6 @@ import java.util.List; | ||||
| import javax.inject.Inject; | ||||
| import javax.inject.Singleton; | ||||
|  | ||||
| import rx.Observable; | ||||
|  | ||||
| /** | ||||
|  * Manager to keep track of which books have been installed | ||||
|  */ | ||||
| @ -23,6 +21,7 @@ import rx.Observable; | ||||
| public class InstalledManager implements BooksListener { | ||||
|  | ||||
|     @Inject Books installedBooks; | ||||
|     // TODO: Why is this injected if we initialize in the constructor? | ||||
|     @Inject List<Book> installedBooksList; | ||||
|  | ||||
|     private String TAG = "InstalledManager"; | ||||
| @ -38,12 +37,6 @@ public class InstalledManager implements BooksListener { | ||||
|         return installedBooksList.contains(b); | ||||
|     } | ||||
|  | ||||
|     public Observable<Book> getInstalledBooks() { | ||||
|         // This method is needed to provide a fresher copy of what's installed | ||||
|         // than Books.getInstalled() does. | ||||
|         return Observable.from(installedBooksList); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void bookAdded(BooksEvent booksEvent) { | ||||
|         Log.d(TAG, "Book added: " + booksEvent.getBook().toString()); | ||||
|  | ||||
| @ -1,185 +0,0 @@ | ||||
| package org.bspeice.minimalbible.activity.navigation; | ||||
|  | ||||
| import android.content.Context; | ||||
| import android.view.LayoutInflater; | ||||
| import android.view.View; | ||||
| import android.view.ViewGroup; | ||||
| import android.widget.BaseExpandableListAdapter; | ||||
| import android.widget.TextView; | ||||
|  | ||||
| import org.bspeice.minimalbible.R; | ||||
|  | ||||
| import java.util.HashMap; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
|  | ||||
| import butterknife.ButterKnife; | ||||
| import butterknife.InjectView; | ||||
|  | ||||
| /** | ||||
|  * ExpandableListView Navigation Drawer | ||||
|  * 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 ExpListNavAdapter<T1, T2> extends BaseExpandableListAdapter { | ||||
|  | ||||
|     // 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. | ||||
|     Map<Integer, T1> indexableBibleBooks; | ||||
|     Map<T1, List<T2>> chaptersForBook; | ||||
|  | ||||
|     private int groupHighlighted; | ||||
|     private int childHighlighted; | ||||
|  | ||||
|     public ExpListNavAdapter(List<T1> groups, Map<T1, List<T2>> children) { | ||||
|  | ||||
|         // Let the map know ahead of time how big it will be | ||||
|         // int bookCount = versification.getBookCount(); | ||||
|         indexableBibleBooks = new HashMap<Integer, T1>(groups.size()); | ||||
|         chaptersForBook = new HashMap<T1, List<T2>>(groups.size()); | ||||
|  | ||||
|         // Is it terrible that I don't like using an actual for loop? | ||||
|         for (int index = 0; index < groups.size(); index++) { | ||||
|             T1 gItem = groups.get(index); | ||||
|             indexableBibleBooks.put(index, gItem); | ||||
|             chaptersForBook.put(gItem, children.get(gItem)); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public int getGroupCount() { | ||||
|         return indexableBibleBooks.size(); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public int getChildrenCount(int i) { | ||||
|         return chaptersForBook.get(indexableBibleBooks.get(i)).size(); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public T1 getGroup(int i) { | ||||
|         return indexableBibleBooks.get(i); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @param i  The group position | ||||
|      * @param i2 The child position | ||||
|      * @return The child chapter value | ||||
|      */ | ||||
|     @Override | ||||
|     public T2 getChild(int i, int i2) { | ||||
|         return chaptersForBook.get(indexableBibleBooks.get(i)).get(i2); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public long getGroupId(int i) { | ||||
|         return i; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public long getChildId(int i, int i2) { | ||||
|         return i2; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public boolean hasStableIds() { | ||||
|         return true; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Get the views for this group | ||||
|      * | ||||
|      * @param position | ||||
|      * @param expanded | ||||
|      * @param convertView | ||||
|      * @param parent | ||||
|      * @return | ||||
|      */ | ||||
|     @Override | ||||
|     public View getGroupView(int position, boolean expanded, | ||||
|                              View convertView, ViewGroup parent) { | ||||
|         NavItemHolder<T1> bookHolder; | ||||
|         if (convertView == null || convertView.getTag() == null) { | ||||
|             LayoutInflater inflater = (LayoutInflater) parent.getContext() | ||||
|                     .getSystemService(Context.LAYOUT_INFLATER_SERVICE); | ||||
|             convertView = inflater.inflate(R.layout.list_navigation_drawer, | ||||
|                     parent, false); | ||||
|             bookHolder = new NavItemHolder<T1>(convertView); | ||||
|             convertView.setTag(bookHolder); | ||||
|         } else { | ||||
|             bookHolder = (NavItemHolder<T1>) convertView.getTag(); | ||||
|         } | ||||
|  | ||||
|         bookHolder.bind(getGroup(position), position == groupHighlighted); | ||||
|         return convertView; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Get the view for a child | ||||
|      * | ||||
|      * @param groupPosition | ||||
|      * @param childPosition | ||||
|      * @param isLastChild | ||||
|      * @param convertView | ||||
|      * @param parent | ||||
|      * @return | ||||
|      */ | ||||
|     @Override | ||||
|     public View getChildView(int groupPosition, int childPosition, | ||||
|                              boolean isLastChild, View convertView, ViewGroup parent) { | ||||
|         NavItemHolder<T2> chapterHolder; | ||||
|         if (convertView == null || convertView.getTag() == null) { | ||||
|             LayoutInflater inflater = (LayoutInflater) parent.getContext() | ||||
|                     .getSystemService(Context.LAYOUT_INFLATER_SERVICE); | ||||
|             convertView = inflater.inflate(R.layout.list_navigation_drawer, | ||||
|                     parent, false); | ||||
|             chapterHolder = new NavItemHolder<T2>(convertView); | ||||
|             convertView.setTag(chapterHolder); | ||||
|         } else { | ||||
|             chapterHolder = (NavItemHolder<T2>) convertView.getTag(); | ||||
|         } | ||||
|  | ||||
|         chapterHolder.bind(getChild(groupPosition, childPosition), | ||||
|                 childPosition == childHighlighted); | ||||
|         return convertView; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public boolean isChildSelectable(int i, int i2) { | ||||
|         return true; | ||||
|     } | ||||
|  | ||||
|     public void setGroupHighlighted(int groupHighlighted) { | ||||
|         this.groupHighlighted = groupHighlighted; | ||||
|     } | ||||
|  | ||||
|     public void setChildHighlighted(int childHighlighted) { | ||||
|         this.childHighlighted = childHighlighted; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 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<T3> { | ||||
|         @InjectView(R.id.navlist_content) | ||||
|         TextView content; | ||||
|  | ||||
|         View v; | ||||
|  | ||||
|         public NavItemHolder(View v) { | ||||
|             this.v = v; // Needed for resolving colors below | ||||
|             ButterKnife.inject(this, v); | ||||
|         } | ||||
|  | ||||
|         public void bind(T3 object, boolean highlighted) { | ||||
|             content.setText(object.toString()); | ||||
|             if (highlighted) { | ||||
|                 content.setTextColor(v.getResources().getColor(R.color.navbar_highlight)); | ||||
|             } else { | ||||
|                 content.setTextColor(v.getResources().getColor(R.color.navbar_unhighlighted)); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @ -2,11 +2,9 @@ package org.bspeice.minimalbible.activity.viewer; | ||||
|  | ||||
| import android.util.Log; | ||||
|  | ||||
| import org.bspeice.minimalbible.activity.navigation.ExpListNavAdapter; | ||||
| import org.bspeice.minimalbible.service.book.VerseLookupModules; | ||||
| import org.bspeice.minimalbible.service.manager.BookManager; | ||||
| import org.crosswire.jsword.book.Book; | ||||
| import org.crosswire.jsword.versification.VersificationUtil; | ||||
|  | ||||
| import java.util.NoSuchElementException; | ||||
| import java.util.concurrent.atomic.AtomicReference; | ||||
| @ -21,17 +19,17 @@ import rx.functions.Action1; | ||||
| import rx.functions.Func1; | ||||
|  | ||||
| /** | ||||
|  * Created by bspeice on 6/18/14. | ||||
|  * Modules used for the BibleViewer activity | ||||
|  */ | ||||
| @Module( | ||||
|         injects = { | ||||
|                 BibleViewer.class, | ||||
|                 BookFragment.class, | ||||
|                 ExpListNavDrawerFragment.class, | ||||
|                 ExpListNavAdapter.class | ||||
|                 ExpListNavDrawerFragment.class | ||||
|         }, | ||||
|         includes = VerseLookupModules.class | ||||
| ) | ||||
| @SuppressWarnings("unused") | ||||
| public class BibleViewerModules { | ||||
|     BibleViewer activity; | ||||
|  | ||||
| @ -98,9 +96,4 @@ public class BibleViewerModules { | ||||
|     BookManager bookManager() { | ||||
|         return new BookManager(); | ||||
|     } | ||||
|  | ||||
|     @Provides | ||||
|     VersificationUtil provideVersificationUtil() { | ||||
|         return new VersificationUtil(); | ||||
|     } | ||||
| } | ||||
| @ -8,22 +8,12 @@ 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.crosswire.jsword.book.Book; | ||||
| import org.crosswire.jsword.versification.BibleBook; | ||||
| import org.crosswire.jsword.versification.VersificationUtil; | ||||
|  | ||||
| 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.Action1; | ||||
|  | ||||
| /** | ||||
|  * ExpandableListView for managing books of the Bible. | ||||
|  * We extend from @link{BaseNavigationDrawerFragment} so we can inherit some of the lifecycle | ||||
| @ -34,7 +24,6 @@ import rx.functions.Action1; | ||||
|  */ | ||||
| public class ExpListNavDrawerFragment extends NavDrawerFragment { | ||||
|  | ||||
|     @Inject VersificationUtil vUtil; | ||||
|     @Inject @Named("MainBook") | ||||
|     Book mainBook; | ||||
|  | ||||
| @ -48,48 +37,7 @@ public class ExpListNavDrawerFragment extends NavDrawerFragment { | ||||
|  | ||||
|         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; | ||||
|         if (mainBook != null) { | ||||
|             bibleBooks = vUtil.getBookNames(mainBook) | ||||
|                     .toList().toBlocking().first(); | ||||
|         } else { | ||||
|             bibleBooks = new ArrayList<String>(); | ||||
|         } | ||||
|  | ||||
|  | ||||
|         // I really don't like how we build the chapters, but I'm not adding Guava just for Range. | ||||
|         // This isn't a totally functional style map-reduce, but the reduce step is | ||||
|         // unnecessarily verbose. Like this comment. | ||||
|         final Map<String, List<Integer>> chapterMap = new HashMap<String, List<Integer>>(); | ||||
|         if (mainBook != null) { | ||||
|             vUtil.getBooks(mainBook).forEach(new Action1<BibleBook>() { | ||||
|                 @Override | ||||
|                 public void call(BibleBook bibleBook) { | ||||
|                     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 | ||||
|                     } | ||||
|                     chapterMap.put(vUtil.getBookName(mainBook, bibleBook), chapterList); | ||||
|                 } | ||||
|             }); | ||||
|  | ||||
|             ExpListNavAdapter<String, Integer> adapter = | ||||
|                     new ExpListNavAdapter<String, Integer>(bibleBooks, chapterMap); | ||||
|  | ||||
|             mActualListView.setAdapter(adapter); | ||||
|         } | ||||
|         mActualListView.setAdapter(new BibleMenu(mainBook)); | ||||
|  | ||||
|         mActualListView.setItemChecked(mCurrentSelectedPosition, true); | ||||
|         return mActualListView; | ||||
|  | ||||
| @ -0,0 +1,7 @@ | ||||
| package org.bspeice.minimalbible.exception; | ||||
|  | ||||
| /** | ||||
|  * Error to be thrown when no books are currently installed. | ||||
|  */ | ||||
| public class NoBooksInstalledException extends Exception { | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 Bradlee Speice
					Bradlee Speice