Handle navigating on clicking a search item

This commit is contained in:
Bradlee Speice 2015-04-02 20:26:29 -04:00
parent 8533cf523c
commit 4b3050c8cd
4 changed files with 88 additions and 12 deletions

View File

@ -124,12 +124,11 @@ public class BibleViewer extends BaseActivity implements Injector {
// If a new chapter is selected, make sure we close the drawer // If a new chapter is selected, make sure we close the drawer
// We can't specify `this` as the subscriber since we can't // It's a long lookup chain, but still holds to Law of Demeter
// extend Action1
scrollEventPublisher.subscribe(new Action1<BookScrollEvent>() { scrollEventPublisher.subscribe(new Action1<BookScrollEvent>() {
@Override @Override
public void call(BookScrollEvent bookScrollEvent) { 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); 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 @Override

View File

@ -5,13 +5,16 @@ import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView import android.support.v7.widget.RecyclerView
import android.util.AttributeSet import android.util.AttributeSet
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.LinearLayout import android.widget.LinearLayout
import android.widget.TextView import android.widget.TextView
import org.bspeice.minimalbible.R import org.bspeice.minimalbible.R
import org.bspeice.minimalbible.activity.viewer.ViewerIntent
import org.bspeice.minimalbible.service.format.osisparser.OsisParser import org.bspeice.minimalbible.service.format.osisparser.OsisParser
import org.crosswire.jsword.book.Book import org.crosswire.jsword.book.Book
import org.crosswire.jsword.passage.Verse import org.crosswire.jsword.passage.Verse
import kotlin.properties.Delegates
/** /**
* Created by bspeice on 2/26/15. * 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<Verse>) class SearchResultsAdapter(val b: Book, val results: List<Verse>)
: RecyclerView.Adapter<ResultViewHolder>() { : RecyclerView.Adapter<ResultViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ResultViewHolder? { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ResultViewHolder? {
val resultView = SearchResultView(parent) val resultView = SearchResultView(parent)
return ResultViewHolder(resultView) return ResultViewHolder(resultView)
} }
override fun onBindViewHolder(holder: ResultViewHolder?, position: Int) { override fun onBindViewHolder(holder: ResultViewHolder, position: Int) {
holder?.bind(b, results[position]) holder.bind(b, results[position])
} }
override fun getItemCount(): Int = results.size() override fun getItemCount(): Int = results.size()
@ -52,7 +55,14 @@ class SearchResultsAdapter(val b: Book, val results: List<Verse>)
* The ViewHolder object for an individual search result * The ViewHolder object for an individual search result
* TODO: Bold the text found in the query * 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 // 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()}" 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 buildVerseContent(b: Book, v: Verse, o: OsisParser) = o.parseVerse(b, v)
fun bind(b: Book, verse: Verse) { fun bind(b: Book, verse: Verse) {
this.verse = verse
view.verseName setText buildVerseName(verse) view.verseName setText buildVerseName(verse)
view.verseContent setText buildVerseContent(b, verse, OsisParser()) 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 * A custom view to wrap showing a search result
*/ */
class SearchResultView(val group: ViewGroup?) { class SearchResultView(val group: ViewGroup) {
val inflater = LayoutInflater.from(group!!.getContext()) val inflater = LayoutInflater.from(group.getContext())
val contentView = inflater.inflate(R.layout.view_search_result, group, false) val contentView = inflater.inflate(R.layout.view_search_result, group, false)
val verseName = contentView.findViewById(R.id.verseName) as TextView val verseName = contentView.findViewById(R.id.verseName) as TextView
val verseContent = contentView.findViewById(R.id.verseContent) as TextView val verseContent = contentView.findViewById(R.id.verseContent) as TextView
fun setOnClickListener(listener: View.OnClickListener) {
contentView.setOnClickListener(listener)
}
} }

View File

@ -1,8 +1,17 @@
package org.bspeice.minimalbible.activity.viewer 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 import org.crosswire.jsword.versification.BibleBook
/** /**
* Created by bspeice on 11/26/14. * 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)) {
}
}

View File

@ -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<BibleViewer>())
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)
}
}