From 96c5c895e90f5bb3569e1d318a2d16ce6f08ce36 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Wed, 26 Nov 2014 22:41:55 -0500 Subject: [PATCH] Seek-to working! For some reason, things at 1st Timothy onward don't work, but everything up to that point does. I'll call that enough for now. PLEASE NOTE: This code needs serious refactoring and testing, dependencies are all over the place. Functionally it's OK for now though. --- .../activity/viewer/BibleViewerModules.java | 7 ++++++ .../viewer/BookChapterNavFragment.java | 9 +++++++- .../activity/viewer/BookFragment.java | 19 +++++----------- .../minimalbible/activity/viewer/BibleMenu.kt | 20 ++++++++++++++++- .../activity/viewer/BookAdapter.kt | 22 ++++++++++++++----- .../activity/viewer/BookScrollEvent.kt | 8 +++++++ 6 files changed, 65 insertions(+), 20 deletions(-) create mode 100644 app/src/main/kotlin/org/bspeice/minimalbible/activity/viewer/BookScrollEvent.kt diff --git a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewerModules.java b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewerModules.java index bbf7204..ba6ca53 100644 --- a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewerModules.java +++ b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewerModules.java @@ -17,6 +17,7 @@ import dagger.Provides; import de.devland.esperandro.Esperandro; import rx.functions.Action1; import rx.functions.Func1; +import rx.subjects.PublishSubject; /** * Modules used for the BibleViewer activity @@ -101,4 +102,10 @@ public class BibleViewerModules { VerseLookup verseLookup(@Named("MainBook") Book b) { return new VerseLookup(b); } + + @Provides + @Singleton + PublishSubject scrollEventProvider() { + return PublishSubject.create(); + } } \ No newline at end of file diff --git a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BookChapterNavFragment.java b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BookChapterNavFragment.java index 74edc3c..76725bb 100644 --- a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BookChapterNavFragment.java +++ b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BookChapterNavFragment.java @@ -14,6 +14,8 @@ import org.crosswire.jsword.book.Book; import javax.inject.Inject; import javax.inject.Named; +import rx.subjects.PublishSubject; + /** * ExpandableListView for managing books of the Bible. * We extend from @link{BaseNavigationDrawerFragment} so we can inherit some of the lifecycle @@ -27,6 +29,9 @@ public class BookChapterNavFragment extends NavDrawerFragment { @Inject @Named("MainBook") Book mainBook; + @Inject + PublishSubject scrollListener; + ExpandableListView mActualListView; @Override @@ -35,9 +40,11 @@ public class BookChapterNavFragment extends NavDrawerFragment { Injector i = (Injector) getActivity(); i.inject(this); + BibleMenu menu = new BibleMenu(mainBook); mActualListView = (ExpandableListView) inflater.inflate( R.layout.fragment_expandable_navigation_drawer, container, false); - mActualListView.setAdapter(new BibleMenu(mainBook)); + mActualListView.setAdapter(menu); + mActualListView.setOnChildClickListener(menu.getMenuClickListener(scrollListener)); mActualListView.setItemChecked(mCurrentSelectedPosition, true); return mActualListView; diff --git a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BookFragment.java b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BookFragment.java index 192f920..8728910 100644 --- a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BookFragment.java +++ b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BookFragment.java @@ -1,20 +1,17 @@ package org.bspeice.minimalbible.activity.viewer; -import android.annotation.SuppressLint; import android.app.Activity; -import android.os.Build; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.support.v7.widget.RecyclerView; import org.bspeice.minimalbible.Injector; import org.bspeice.minimalbible.R; import org.bspeice.minimalbible.activity.BaseFragment; -import org.bspeice.minimalbible.service.lookup.VerseLookup; import org.crosswire.jsword.book.Book; import javax.inject.Inject; @@ -22,10 +19,6 @@ import javax.inject.Named; import butterknife.ButterKnife; import butterknife.InjectView; -import rx.Observable; -import rx.android.schedulers.AndroidSchedulers; -import rx.functions.Action1; -import rx.schedulers.Schedulers; import rx.subjects.PublishSubject; /** @@ -40,14 +33,11 @@ public class BookFragment extends BaseFragment { @Named("MainBook") Book mBook; @Inject - VerseLookup verseLookup; + PublishSubject scrollEventProvider; @InjectView(R.id.book_content) RecyclerView bookContent; - - PublishSubject titleReceiver = PublishSubject.create(); - /** * Returns a new instance of this fragment for the given book. */ @@ -101,7 +91,10 @@ public class BookFragment extends BaseFragment { ((BibleViewer)getActivity()).setActionBarTitle(b.getInitials()); final RecyclerView.LayoutManager manager = new LinearLayoutManager(getActivity()); + BookAdapter adapter = new BookAdapter(b); bookContent.setLayoutManager(manager); - bookContent.setAdapter(new BookAdapter(b)); + bookContent.setAdapter(adapter); + + adapter.bindScrollHandler(scrollEventProvider, manager); } } diff --git a/app/src/main/kotlin/org/bspeice/minimalbible/activity/viewer/BibleMenu.kt b/app/src/main/kotlin/org/bspeice/minimalbible/activity/viewer/BibleMenu.kt index 271aa21..d8f3ff7 100644 --- a/app/src/main/kotlin/org/bspeice/minimalbible/activity/viewer/BibleMenu.kt +++ b/app/src/main/kotlin/org/bspeice/minimalbible/activity/viewer/BibleMenu.kt @@ -13,6 +13,8 @@ import android.content.Context import android.view.LayoutInflater import android.content.res.Resources import android.support.annotation.IdRes +import android.widget.ExpandableListView +import rx.subjects.PublishSubject /** * Created by bspeice on 10/24/14. @@ -25,6 +27,22 @@ class BibleMenu(val b: Book) : BaseExpandableListAdapter() { Pair(it, b.getVersification().getLastChapter(it)) } + /** + * The listener that should be registered to receive click events + * It's created here because we need access to the menuMappings + */ + fun getMenuClickListener(listener: PublishSubject) = + object : ExpandableListView.OnChildClickListener { + override fun onChildClick(listView: ExpandableListView?, childView: View?, + groupPosition: Int, childPosition: Int, id: Long): Boolean { + + val map = menuMappings[groupPosition] + listener onNext BookScrollEvent(map.first, map.second) + + return true; // Event was handled + } + } + var groupHighlighted: Int = 0 var childHighlighted: Int = 0 @@ -82,4 +100,4 @@ class BibleMenu(val b: Book) : BaseExpandableListAdapter() { content setTextColor getHighlightedColor(highlighted) } } -} \ No newline at end of file +} diff --git a/app/src/main/kotlin/org/bspeice/minimalbible/activity/viewer/BookAdapter.kt b/app/src/main/kotlin/org/bspeice/minimalbible/activity/viewer/BookAdapter.kt index 2418f6e..f6a00f6 100644 --- a/app/src/main/kotlin/org/bspeice/minimalbible/activity/viewer/BookAdapter.kt +++ b/app/src/main/kotlin/org/bspeice/minimalbible/activity/viewer/BookAdapter.kt @@ -3,8 +3,6 @@ package org.bspeice.minimalbible.activity.viewer import android.support.v7.widget.RecyclerView import android.view.ViewGroup import org.crosswire.jsword.book.Book -import org.crosswire.jsword.passage.Verse -import android.view.View import android.view.LayoutInflater import org.bspeice.minimalbible.R import android.widget.TextView @@ -13,14 +11,15 @@ import org.crosswire.jsword.book.getVersification import org.crosswire.jsword.versification.getBooks import org.crosswire.jsword.versification.BibleBook import org.bspeice.minimalbible.activity.viewer.BookAdapter.ChapterInfo -import android.util.Log +import rx.subjects.PublishSubject /** * Adapter used for displaying a book * Displays one chapter at a time, * as each TextView widget is it's own line break */ -class BookAdapter(val b: Book) : RecyclerView.Adapter() { +class BookAdapter(val b: Book) +: RecyclerView.Adapter() { val versification = b.getVersification() val bookList = versification.getBooks() @@ -48,7 +47,7 @@ class BookAdapter(val b: Book) : RecyclerView.Adapter() { val verseCount = versification.getLastVerse(currentBook, it) ChapterInfo(b, it, currentBook, firstVerse, firstVerse + verseCount) } - } + }; /** * I'm not sure what the position argument actually represents, @@ -73,6 +72,19 @@ class BookAdapter(val b: Book) : RecyclerView.Adapter() { * Get the number of chapters in the book */ override fun getItemCount(): Int = chapterCount + + fun bindScrollHandler(provider: PublishSubject, + lM: RecyclerView.LayoutManager) { + provider subscribe { + val event = it + chapterList.withIndices() + .filter { + event.b == it.second.bibleBook && + event.chapter == it.second.chapter + } + .forEach { lM scrollToPosition it.first } + } + } } class PassageView(val v: TextView) : RecyclerView.ViewHolder(v) { diff --git a/app/src/main/kotlin/org/bspeice/minimalbible/activity/viewer/BookScrollEvent.kt b/app/src/main/kotlin/org/bspeice/minimalbible/activity/viewer/BookScrollEvent.kt new file mode 100644 index 0000000..82c04ea --- /dev/null +++ b/app/src/main/kotlin/org/bspeice/minimalbible/activity/viewer/BookScrollEvent.kt @@ -0,0 +1,8 @@ +package org.bspeice.minimalbible.activity.viewer + +import org.crosswire.jsword.versification.BibleBook + +/** + * Created by bspeice on 11/26/14. + */ +data class BookScrollEvent(val b: BibleBook, val chapter: Int) {}