From 4b3050c8cd32803f1fcbc30816524c4f97ed469a Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Thu, 2 Apr 2015 20:26:29 -0400 Subject: [PATCH] Handle navigating on clicking a search item --- .../activity/viewer/BibleViewer.java | 29 +++++++++++++--- .../activity/search/SearchResultsView.kt | 33 +++++++++++++++---- .../activity/viewer/BookScrollEvent.kt | 11 ++++++- .../activity/viewer/ViewerIntent.kt | 27 +++++++++++++++ 4 files changed, 88 insertions(+), 12 deletions(-) create mode 100644 app/src/main/kotlin/org/bspeice/minimalbible/activity/viewer/ViewerIntent.kt diff --git a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewer.java b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewer.java index d63ca48..4a2b93d 100644 --- a/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewer.java +++ b/app/src/main/java/org/bspeice/minimalbible/activity/viewer/BibleViewer.java @@ -124,12 +124,11 @@ public class BibleViewer extends BaseActivity implements Injector { // If a new chapter is selected, make sure we close the drawer - // We can't specify `this` as the subscriber since we can't - // extend Action1 + // It's a long lookup chain, but still holds to Law of Demeter scrollEventPublisher.subscribe(new Action1() { @Override public void call(BookScrollEvent bookScrollEvent) { - closeMenu(); + BibleViewer.this.drawerLayout.closeDrawers(); } }); @@ -137,8 +136,28 @@ public class BibleViewer extends BaseActivity implements Injector { bibleContent.doInitialize(mainBook, prefs, scrollEventPublisher); } - public void closeMenu() { - drawerLayout.closeDrawers(); + /** + * Re-start the activity + * Mostly this is used for handling a search completing, and we need to + * display the result. + */ + @Override + protected void onStart() { + super.onStart(); + handleSearchIntent(getIntent()); + } + + /** + * Handle navigating to the chapter if we are started by searching. + * The scrollNum will be -1 if we weren't started by search. + * + * @param i The intent the activity was started with + */ + public void handleSearchIntent(Intent i) { + Integer scrollNum = ViewerIntent.Companion.decodeSearchResult(i); + if (scrollNum > 0) { + scrollEventPublisher.onNext(new BookScrollEvent(mainBook, scrollNum)); + } } @Override diff --git a/app/src/main/kotlin/org/bspeice/minimalbible/activity/search/SearchResultsView.kt b/app/src/main/kotlin/org/bspeice/minimalbible/activity/search/SearchResultsView.kt index 4ba05e3..f0c7232 100644 --- a/app/src/main/kotlin/org/bspeice/minimalbible/activity/search/SearchResultsView.kt +++ b/app/src/main/kotlin/org/bspeice/minimalbible/activity/search/SearchResultsView.kt @@ -5,13 +5,16 @@ import android.support.v7.widget.LinearLayoutManager import android.support.v7.widget.RecyclerView import android.util.AttributeSet import android.view.LayoutInflater +import android.view.View import android.view.ViewGroup import android.widget.LinearLayout import android.widget.TextView import org.bspeice.minimalbible.R +import org.bspeice.minimalbible.activity.viewer.ViewerIntent import org.bspeice.minimalbible.service.format.osisparser.OsisParser import org.crosswire.jsword.book.Book import org.crosswire.jsword.passage.Verse +import kotlin.properties.Delegates /** * Created by bspeice on 2/26/15. @@ -36,13 +39,13 @@ class SearchResultsListView(val ctx: Context, val attrs: AttributeSet) : LinearL class SearchResultsAdapter(val b: Book, val results: List) : RecyclerView.Adapter() { - override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ResultViewHolder? { + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ResultViewHolder? { val resultView = SearchResultView(parent) return ResultViewHolder(resultView) } - override fun onBindViewHolder(holder: ResultViewHolder?, position: Int) { - holder?.bind(b, results[position]) + override fun onBindViewHolder(holder: ResultViewHolder, position: Int) { + holder.bind(b, results[position]) } override fun getItemCount(): Int = results.size() @@ -52,7 +55,14 @@ class SearchResultsAdapter(val b: Book, val results: List) * The ViewHolder object for an individual search result * TODO: Bold the text found in the query */ -class ResultViewHolder(val view: SearchResultView) : RecyclerView.ViewHolder(view.contentView) { +class ResultViewHolder(val view: SearchResultView) : + RecyclerView.ViewHolder(view.contentView), View.OnClickListener { + + var verse: Verse by Delegates.notNull() + + init { + view.setOnClickListener(this) + } // TODO: Need a nicer way of displaying the book name - currently is ALL CAPS fun buildVerseName(v: Verse) = "${v.getBook().name()} ${v.getChapter()}:${v.getVerse()}" @@ -60,18 +70,29 @@ class ResultViewHolder(val view: SearchResultView) : RecyclerView.ViewHolder(vie fun buildVerseContent(b: Book, v: Verse, o: OsisParser) = o.parseVerse(b, v) fun bind(b: Book, verse: Verse) { + this.verse = verse view.verseName setText buildVerseName(verse) view.verseContent setText buildVerseContent(b, verse, OsisParser()) } + + override fun onClick(v: View) { + val context = v.getContext() + val intent = ViewerIntent.fromSearchResult(context, verse) + context.startActivity(intent) + } } /** * A custom view to wrap showing a search result */ -class SearchResultView(val group: ViewGroup?) { - val inflater = LayoutInflater.from(group!!.getContext()) +class SearchResultView(val group: ViewGroup) { + val inflater = LayoutInflater.from(group.getContext()) val contentView = inflater.inflate(R.layout.view_search_result, group, false) val verseName = contentView.findViewById(R.id.verseName) as TextView val verseContent = contentView.findViewById(R.id.verseContent) as TextView + + fun setOnClickListener(listener: View.OnClickListener) { + contentView.setOnClickListener(listener) + } } 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 index 82c04ea..fc21d9f 100644 --- a/app/src/main/kotlin/org/bspeice/minimalbible/activity/viewer/BookScrollEvent.kt +++ b/app/src/main/kotlin/org/bspeice/minimalbible/activity/viewer/BookScrollEvent.kt @@ -1,8 +1,17 @@ package org.bspeice.minimalbible.activity.viewer +import org.crosswire.jsword.book.Book +import org.crosswire.jsword.book.getVersification +import org.crosswire.jsword.passage.Verse import org.crosswire.jsword.versification.BibleBook /** * Created by bspeice on 11/26/14. */ -data class BookScrollEvent(val b: BibleBook, val chapter: Int) {} +data class BookScrollEvent(val b: BibleBook, val chapter: Int) { + constructor(v: Verse) : this(v.getBook(), v.getChapter()) { + } + + constructor(b: Book, ordinal: Int) : this(b.getVersification().decodeOrdinal(ordinal)) { + } +} diff --git a/app/src/main/kotlin/org/bspeice/minimalbible/activity/viewer/ViewerIntent.kt b/app/src/main/kotlin/org/bspeice/minimalbible/activity/viewer/ViewerIntent.kt new file mode 100644 index 0000000..f006ca7 --- /dev/null +++ b/app/src/main/kotlin/org/bspeice/minimalbible/activity/viewer/ViewerIntent.kt @@ -0,0 +1,27 @@ +package org.bspeice.minimalbible.activity.viewer + +import android.content.Context +import android.content.Intent +import org.crosswire.jsword.passage.Verse + +/** + * Created by bspeice on 4/2/15. + */ +class ViewerIntent() { + + companion object { + val VERSE_RESULT_KEY = "VERSE_RESULT" + + fun fromSearchResult(ctx: Context, verse: Verse): Intent { + val i = Intent(ctx, javaClass()) + i.putExtra(VERSE_RESULT_KEY, verse.getOrdinal()) + return i + } + + /** + * Attempt to get the result of a search out of the intent + * Returns -1 if no search result was found + */ + fun decodeSearchResult(i: Intent) = i.getIntExtra(VERSE_RESULT_KEY, -1) + } +}