Small refactor to clean the initialization process

This commit is contained in:
Bradlee Speice 2015-01-18 22:21:16 -05:00
parent 3721ef5ee0
commit 82b8564402
3 changed files with 14 additions and 27 deletions

View File

@ -116,9 +116,6 @@ public class BibleViewer extends BaseActivity implements Injector {
setInsetToolbar(toolbar); setInsetToolbar(toolbar);
// Currently you must set up the menu in exactly this order. Do I need to refactor this?
bibleMenu.setScrollEventPublisher(scrollEventPublisher);
bibleMenu.setBible(mainBook);
// 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 // We can't specify `this` as the subscriber since we can't
@ -130,9 +127,8 @@ public class BibleViewer extends BaseActivity implements Injector {
} }
}); });
// Set up the view to respond to scroll events as well. Again, exact order is needed. bibleMenu.doInitialize(mainBook, scrollEventPublisher);
bibleContent.setScrollPublisher(scrollEventPublisher); bibleContent.doInitialize(mainBook, prefs, scrollEventPublisher);
bibleContent.setBook(mainBook, prefs);
} }
public void closeMenu() { public void closeMenu() {

View File

@ -17,26 +17,19 @@ import rx.subjects.PublishSubject
import android.widget.LinearLayout import android.widget.LinearLayout
import android.app.Activity import android.app.Activity
import android.util.AttributeSet import android.util.AttributeSet
import kotlin.properties.Delegates
import org.bspeice.minimalbible.activity.setInset import org.bspeice.minimalbible.activity.setInset
import android.support.annotation.LayoutRes import android.support.annotation.LayoutRes
import org.crosswire.jsword.versification.BibleBook import org.crosswire.jsword.versification.BibleBook
class BibleMenu(val ctx: Context, val attrs: AttributeSet) : LinearLayout(ctx, attrs) { class BibleMenu(val ctx: Context, val attrs: AttributeSet) : LinearLayout(ctx, attrs) {
var menuContent: ExpandableListView by Delegates.notNull()
var scrollEventPublisher: PublishSubject<BookScrollEvent> by Delegates.notNull();
{
val inflater = ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater val inflater = ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
inflater.inflate(R.layout.view_bible_menu, this, true) val contentView = inflater.inflate(R.layout.view_bible_menu, this, true)
val menuContent = findViewById(R.id._bible_menu) as ExpandableListView
menuContent = findViewById(R.id._bible_menu) as ExpandableListView fun doInitialize(b: Book, publisher: PublishSubject<BookScrollEvent>) {
} val adapter = BibleAdapter(b, publisher)
fun setBible(b: Book) {
val adapter = BibleAdapter(b, scrollEventPublisher)
menuContent setAdapter adapter menuContent setAdapter adapter
scrollEventPublisher subscribe { publisher subscribe {
menuContent.collapseGroup(adapter.getGroupIdForBook(it.b)) menuContent.collapseGroup(adapter.getGroupIdForBook(it.b))
} }
} }

View File

@ -17,27 +17,25 @@ import org.bspeice.minimalbible.service.format.osisparser.OsisParser
import android.util.Log import android.util.Log
import android.content.Context import android.content.Context
import android.util.AttributeSet import android.util.AttributeSet
import kotlin.properties.Delegates
import android.support.v7.widget.LinearLayoutManager import android.support.v7.widget.LinearLayoutManager
import android.widget.LinearLayout import android.widget.LinearLayout
import android.view.View
class BibleView(val ctx: Context, val attrs: AttributeSet) : LinearLayout(ctx, attrs) { class BibleView(val ctx: Context, val attrs: AttributeSet) : LinearLayout(ctx, attrs) {
var bibleContent: RecyclerView by Delegates.notNull()
var scrollPublisher: PublishSubject<BookScrollEvent> by Delegates.notNull()
val layoutManager: LinearLayoutManager = LinearLayoutManager(ctx) val layoutManager: LinearLayoutManager = LinearLayoutManager(ctx)
val inflater = ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater; val inflater = ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater;
val contentView: View = inflater.inflate(R.layout.view_bible, this, true)
val bibleContent = contentView.findViewById(R.id.bible_content) as RecyclerView
{ {
val rootView = inflater.inflate(R.layout.view_bible, this, true)
bibleContent = rootView.findViewById(R.id.bible_content) as RecyclerView
bibleContent setLayoutManager layoutManager bibleContent setLayoutManager layoutManager
} }
fun setBook(b: Book, prefs: BibleViewerPreferences) { fun doInitialize(b: Book, prefs: BibleViewerPreferences,
publisher: PublishSubject<BookScrollEvent>) {
val adapter = BookAdapter(b, prefs) val adapter = BookAdapter(b, prefs)
adapter.bindScrollHandler(scrollPublisher, layoutManager) adapter.bindScrollHandler(publisher, layoutManager)
bibleContent setAdapter adapter bibleContent setAdapter adapter
bibleContent scrollToPosition prefs.currentChapter() bibleContent scrollToPosition prefs.currentChapter()
} }