Switch LruCache to DI

This commit is contained in:
Bradlee Speice 2014-09-01 08:28:50 -04:00
parent cc0c681bae
commit aa29caebb3
4 changed files with 43 additions and 16 deletions

View File

@ -2,9 +2,9 @@ package org.bspeice.minimalbible.activity.viewer;
import android.util.Log; import android.util.Log;
import org.bspeice.minimalbible.Injector;
import org.bspeice.minimalbible.activity.navigation.ExpListNavAdapter; import org.bspeice.minimalbible.activity.navigation.ExpListNavAdapter;
import org.bspeice.minimalbible.activity.viewer.bookutil.VersificationUtil; import org.bspeice.minimalbible.activity.viewer.bookutil.VersificationUtil;
import org.bspeice.minimalbible.service.book.VerseLookupModules;
import org.crosswire.jsword.book.Book; import org.crosswire.jsword.book.Book;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
@ -28,7 +28,7 @@ import rx.functions.Func1;
ExpListNavDrawerFragment.class, ExpListNavDrawerFragment.class,
ExpListNavAdapter.class ExpListNavAdapter.class
}, },
library = true includes = VerseLookupModules.class
) )
public class BibleViewerModules { public class BibleViewerModules {
BibleViewer activity; BibleViewer activity;
@ -37,12 +37,6 @@ public class BibleViewerModules {
this.activity = activity; this.activity = activity;
} }
@Provides
@Singleton
Injector provideInjector() {
return activity;
}
@Provides @Provides
@Singleton @Singleton
BibleViewerPreferences providePrefs() { BibleViewerPreferences providePrefs() {

View File

@ -70,9 +70,10 @@ public class BookFragment extends BaseFragment {
Bundle savedInstanceState) { Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_viewer_main, container, View rootView = inflater.inflate(R.layout.fragment_viewer_main, container,
false); false);
((Injector)getActivity()).inject(this); Injector i = (Injector) getActivity();
i.inject(this);
// TODO: Defer lookup until after webview created? When exactly is WebView created? // TODO: Defer lookup until after webview created? When exactly is WebView created?
this.lookupService = new VerseLookupService(mBook.get()); this.lookupService = new VerseLookupService(i, mBook.get());
ButterKnife.inject(this, rootView); ButterKnife.inject(this, rootView);
mainContent.getSettings().setJavaScriptEnabled(true); mainContent.getSettings().setJavaScriptEnabled(true);
@ -98,7 +99,7 @@ public class BookFragment extends BaseFragment {
* Do the initial work of displaying a book. Requires setting up WebView, etc. * Do the initial work of displaying a book. Requires setting up WebView, etc.
* TODO: Get initial content from cache? * TODO: Get initial content from cache?
* *
* @param b * @param b The book we want to display
*/ */
private void displayBook(Book b) { private void displayBook(Book b) {
Log.d("BookFragment", b.getName()); Log.d("BookFragment", b.getName());
@ -117,10 +118,11 @@ public class BookFragment extends BaseFragment {
} }
/** /**
* Do the heavy listing of getting the actual text for a verse * Do the heavy lifting of getting the actual text for a verse
* *
* @param v * @param v The verse to display
*/ */
@SuppressWarnings("unused")
public void displayVerse(Verse v) { public void displayVerse(Verse v) {
Book b = mBook.get(); Book b = mBook.get();
lookupService.getHTMLVerse(v); lookupService.getHTMLVerse(v);
@ -135,6 +137,7 @@ public class BookFragment extends BaseFragment {
mainContent.loadUrl("javascript:" + function + "('" + arg.toString() + "')"); mainContent.loadUrl("javascript:" + function + "('" + arg.toString() + "')");
} }
@SuppressWarnings("unused")
private void invokeJavascript(String function, List<Object> args) { private void invokeJavascript(String function, List<Object> args) {
mainContent.loadUrl("javascript:" + function + "(" + joinString(",", args.toArray()) + ")"); mainContent.loadUrl("javascript:" + function + "(" + joinString(",", args.toArray()) + ")");
} }

View File

@ -0,0 +1,25 @@
package org.bspeice.minimalbible.service.book;
import android.support.v4.util.LruCache;
import dagger.Module;
import dagger.Provides;
/**
* Created by bspeice on 9/1/14.
*/
@Module(injects = VerseLookupService.class)
public class VerseLookupModules {
private static final int MAX_SIZE = 1000000; // 1MB
/**
* Create a new LruCache. We're free to create new ones since they're all backed by the file
* system anyways.
*
* @return The LruCache to use
*/
@Provides
LruCache<String, String> getLruCache() {
return new LruCache<String, String>(MAX_SIZE);
}
}

View File

@ -3,6 +3,7 @@ package org.bspeice.minimalbible.service.book;
import android.support.v4.util.LruCache; import android.support.v4.util.LruCache;
import android.util.Log; import android.util.Log;
import org.bspeice.minimalbible.Injector;
import org.bspeice.minimalbible.service.format.osistohtml.OsisToHtmlParameters; import org.bspeice.minimalbible.service.format.osistohtml.OsisToHtmlParameters;
import org.bspeice.minimalbible.service.format.osistohtml.OsisToHtmlSaxHandler; import org.bspeice.minimalbible.service.format.osistohtml.OsisToHtmlSaxHandler;
import org.crosswire.common.xml.SAXEventProvider; import org.crosswire.common.xml.SAXEventProvider;
@ -12,6 +13,8 @@ import org.crosswire.jsword.book.BookException;
import org.crosswire.jsword.passage.Verse; import org.crosswire.jsword.passage.Verse;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import javax.inject.Inject;
import rx.functions.Action1; import rx.functions.Action1;
import rx.schedulers.Schedulers; import rx.schedulers.Schedulers;
import rx.subjects.PublishSubject; import rx.subjects.PublishSubject;
@ -29,8 +32,10 @@ import rx.subjects.PublishSubject;
*/ */
public class VerseLookupService implements Action1<Verse> { public class VerseLookupService implements Action1<Verse> {
private static final int MAX_SIZE = 1000000; // 1MB
Book book; Book book;
@Inject
LruCache<String, String> cache; LruCache<String, String> cache;
/** /**
* The listener is responsible for delegating calls to cache verses. * The listener is responsible for delegating calls to cache verses.
@ -39,11 +44,11 @@ public class VerseLookupService implements Action1<Verse> {
*/ */
private PublishSubject<Verse> listener = PublishSubject.create(); private PublishSubject<Verse> listener = PublishSubject.create();
public VerseLookupService(Book b) { public VerseLookupService(Injector i, Book b) {
listener.subscribeOn(Schedulers.io()) listener.subscribeOn(Schedulers.io())
.subscribe(this); .subscribe(this);
this.book = b; this.book = b;
this.cache = new LruCache<String, String>(MAX_SIZE); i.inject(this);
} }
/** /**