Beginning work on the actual Bible viewer!

ugly-unit-test
Bradlee Speice 2014-06-18 22:08:53 -04:00
parent 8e54fdb86d
commit 348a6da9a3
9 changed files with 249 additions and 74 deletions

View File

@ -2,6 +2,11 @@ package org.bspeice.minimalbible;
import android.app.Application;
import android.content.Context;
import android.util.Log;
import org.crosswire.jsword.book.sword.SwordBookPath;
import java.io.File;
import dagger.ObjectGraph;
@ -18,6 +23,8 @@ public class MinimalBible extends Application {
*/
private static MinimalBible instance;
private String TAG = "MinimalBible";
/**
* Create the application, and persist the application Context
*/
@ -51,6 +58,7 @@ public class MinimalBible extends Application {
public void onCreate() {
//TODO: Is this necessary?
inject(this);
setJswordHome();
}
/**
@ -67,4 +75,20 @@ public class MinimalBible extends Application {
}
return graph;
}
/**
* Notify jSword that it needs to store files in the Android internal directory
* NOTE: Android will uninstall these files if you uninstall MinimalBible.
*/
@SuppressWarnings("null")
private void setJswordHome() {
// We need to set the download directory for jSword to stick with
// Android.
String home = MinimalBible.getAppContext().getFilesDir().toString();
Log.d(TAG, "Setting jsword.home to: " + home);
System.setProperty("jsword.home", home);
System.setProperty("sword.home", home);
SwordBookPath.setDownloadDir(new File(home));
Log.d(TAG, "Sword download path: " + SwordBookPath.getSwordDownloadDir());
}
}

View File

@ -1,6 +1,7 @@
package org.bspeice.minimalbible.activities;
import org.bspeice.minimalbible.activities.downloader.ActivityDownloaderModule;
import org.bspeice.minimalbible.activities.viewer.ActivityViewerModule;
import dagger.Module;
@ -9,7 +10,8 @@ import dagger.Module;
*/
@Module(
includes = {
ActivityDownloaderModule.class
ActivityDownloaderModule.class,
ActivityViewerModule.class
}
)
public class ActivityModules {

View File

@ -0,0 +1,15 @@
package org.bspeice.minimalbible.activities.viewer;
import dagger.Module;
/**
* Created by bspeice on 6/18/14.
*/
@Module(
injects = {
BibleViewer.class,
BookFragment.class
}
)
public class ActivityViewerModule {
}

View File

@ -1,27 +1,33 @@
package org.bspeice.minimalbible.activities.viewer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.view.LayoutInflater;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import org.bspeice.minimalbible.MinimalBible;
import org.bspeice.minimalbible.R;
import org.bspeice.minimalbible.activities.BaseActivity;
import org.bspeice.minimalbible.activities.BaseNavigationDrawerFragment;
import org.bspeice.minimalbible.activities.downloader.DownloadActivity;
import org.crosswire.jsword.book.Book;
import javax.inject.Inject;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
public class BibleViewer extends BaseActivity implements
BaseNavigationDrawerFragment.NavigationDrawerCallbacks {
@Inject BookManager bookManager;
/**
* Fragment managing the behaviors, interactions and presentation of the
* navigation drawer.
@ -37,6 +43,31 @@ public class BibleViewer extends BaseActivity implements
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MinimalBible.getApplication().inject(this);
// If no books are installed, we need to download one first.
int count = bookManager.getInstalledBooks()
.count()
.toBlocking()
.last();
if (count <= 0) {
Intent i = new Intent(this, DownloadActivity.class);
startActivityForResult(i, 0);
finish();
} else {
bookManager.getInstalledBooks()
.first()
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<Book>() {
@Override
public void call(Book book) {
Log.d("BibleViewer", "Subscribed to display book: " + book.getName());
displayMainBook(book);
}
});
}
setContentView(R.layout.activity_bible_viewer);
mNavigationDrawerFragment = (ViewerNavDrawerFragment) getSupportFragmentManager()
@ -45,32 +76,19 @@ public class BibleViewer extends BaseActivity implements
// Set up the drawer.
mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
(DrawerLayout) findViewById(R.id.drawer_layout));
}
@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager
.beginTransaction()
.replace(R.id.container,
PlaceholderFragment.newInstance(position + 1)).commit();
// Handle a navigation movement
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.title_section1);
break;
case 2:
mTitle = getString(R.string.title_section2);
break;
case 3:
mTitle = getString(R.string.title_section3);
break;
}
}
public void setActionBarTitle(String title) {
ActionBar actionBar = getSupportActionBar();
mTitle = title;
actionBar.setTitle(title);
}
public void restoreActionBar() {
ActionBar actionBar = getSupportActionBar();
@ -106,48 +124,13 @@ public class BibleViewer extends BaseActivity implements
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
TextView textView = (TextView) rootView
.findViewById(R.id.section_label);
textView.setText(Integer.toString(getArguments().getInt(
ARG_SECTION_NUMBER)));
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((BibleViewer) activity).onSectionAttached(getArguments().getInt(
ARG_SECTION_NUMBER));
}
}
private void displayMainBook(Book b) {
Log.d("BibleViewer", "Initializing main book: " + b.getName());
Log.d("MainThread?", Boolean.toString(Looper.myLooper() == Looper.getMainLooper()));
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment f = BookFragment.newInstance(b.getName());
fragmentManager.beginTransaction()
.replace(R.id.container, f)
.commit();
}
}

View File

@ -0,0 +1,104 @@
package org.bspeice.minimalbible.activities.viewer;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import org.bspeice.minimalbible.MinimalBible;
import org.bspeice.minimalbible.R;
import org.bspeice.minimalbible.activities.BaseFragment;
import org.crosswire.jsword.book.Book;
import javax.inject.Inject;
import butterknife.ButterKnife;
import butterknife.InjectView;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
/**
* A placeholder fragment containing a simple view.
*/
public class BookFragment extends BaseFragment {
@Inject BookManager bookManager;
@InjectView(R.id.section_label)
TextView sectionLabel;
private static final String ARG_BOOK_NAME = "book_name";
private Book mBook;
/**
* Returns a new instance of this fragment for the given section number.
*/
public static BookFragment newInstance(String bookName) {
BookFragment fragment = new BookFragment();
Bundle args = new Bundle();
args.putString(ARG_BOOK_NAME, bookName);
fragment.setArguments(args);
return fragment;
}
public BookFragment() {
}
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
MinimalBible.getApplication().inject(this);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_viewer_main, container,
false);
ButterKnife.inject(this, rootView);
// TODO: Load initial text from SharedPreferences
// And due to Observable async, we can kick off fetching the actual book asynchronously!
bookManager.getInstalledBooks()
.first(new Func1<Book, Boolean>() {
@Override
public Boolean call(Book book) {
String mBookName = getArguments().getString(ARG_BOOK_NAME);
return book.getName().equals(mBookName);
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<Book>() {
@Override
public void call(Book book) {
BookFragment.this.mBook = book;
displayBook(book);
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
Log.d("BookFragment", "No books installed?");
}
});
return rootView;
}
// TODO: Remove?
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
private void displayBook(Book b) {
Log.d("BookFragment", b.getName());
((BibleViewer)getActivity()).setActionBarTitle(b.getInitials());
sectionLabel.setText(b.getName());
}
}

View File

@ -0,0 +1,50 @@
package org.bspeice.minimalbible.activities.viewer;
import org.crosswire.jsword.book.Book;
import org.crosswire.jsword.book.Books;
import javax.inject.Inject;
import javax.inject.Singleton;
import rx.Observable;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
/**
* Created by bspeice on 6/18/14.
*/
@Singleton
public class BookManager {
private Observable<Book> installedBooks;
private Boolean refreshComplete;
@Inject
BookManager() {
installedBooks = Observable.from(Books.installed().getBooks())
.cache();
installedBooks.subscribeOn(Schedulers.io())
.subscribe(new Action1<Book>() {
@Override
public void call(Book book) {}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {}
}, new Action0() {
@Override
public void call() {
BookManager.this.refreshComplete = true;
}
});
}
public Observable<Book> getInstalledBooks() {
return installedBooks;
}
public Boolean isRefreshComplete() {
return refreshComplete;
}
}

View File

@ -3,6 +3,7 @@
android:id="@+id/list_nav_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cccccc"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"

View File

@ -3,10 +3,6 @@
xmlns:tools="http://schemas.android.com/tools"
tools:context="org.bspeice.minimalbible.MainActivity" >
<item
android:id="@+id/action_example"
android:title="@string/action_example"
app:showAsAction="withText|ifRoom"/>
<item
android:id="@+id/action_settings"
android:orderInCategory="100"