From 348a6da9a3a019e1a73d7aba854ebb7c3a6e935e Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Wed, 18 Jun 2014 22:08:53 -0400 Subject: [PATCH] Beginning work on the actual Bible viewer! --- .../bspeice/minimalbible/MinimalBible.java | 24 ++++ .../activities/ActivityModules.java | 4 +- .../viewer/ActivityViewerModule.java | 15 +++ .../activities/viewer/BibleViewer.java | 121 ++++++++---------- .../activities/viewer/BookFragment.java | 104 +++++++++++++++ .../activities/viewer/BookManager.java | 50 ++++++++ .../res/layout/fragment_navigation_drawer.xml | 1 + ...ment_main.xml => fragment_viewer_main.xml} | 0 MinimalBible/src/main/res/menu/main.xml | 4 - 9 files changed, 249 insertions(+), 74 deletions(-) create mode 100644 MinimalBible/src/main/java/org/bspeice/minimalbible/activities/viewer/ActivityViewerModule.java create mode 100644 MinimalBible/src/main/java/org/bspeice/minimalbible/activities/viewer/BookFragment.java create mode 100644 MinimalBible/src/main/java/org/bspeice/minimalbible/activities/viewer/BookManager.java rename MinimalBible/src/main/res/layout/{fragment_main.xml => fragment_viewer_main.xml} (100%) diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/MinimalBible.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/MinimalBible.java index 90ee797..1f62f14 100644 --- a/MinimalBible/src/main/java/org/bspeice/minimalbible/MinimalBible.java +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/MinimalBible.java @@ -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()); + } } diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/ActivityModules.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/ActivityModules.java index de3a0d9..5cfe1fd 100644 --- a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/ActivityModules.java +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/ActivityModules.java @@ -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 { diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/viewer/ActivityViewerModule.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/viewer/ActivityViewerModule.java new file mode 100644 index 0000000..c6b4fc2 --- /dev/null +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/viewer/ActivityViewerModule.java @@ -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 { +} diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/viewer/BibleViewer.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/viewer/BibleViewer.java index 9376ba6..544deee 100644 --- a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/viewer/BibleViewer.java +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/viewer/BibleViewer.java @@ -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() { + @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(); + } } diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/viewer/BookFragment.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/viewer/BookFragment.java new file mode 100644 index 0000000..ff3f1f8 --- /dev/null +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/viewer/BookFragment.java @@ -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() { + @Override + public Boolean call(Book book) { + String mBookName = getArguments().getString(ARG_BOOK_NAME); + return book.getName().equals(mBookName); + } + }) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(new Action1() { + @Override + public void call(Book book) { + BookFragment.this.mBook = book; + displayBook(book); + } + }, new Action1() { + @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()); + } +} diff --git a/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/viewer/BookManager.java b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/viewer/BookManager.java new file mode 100644 index 0000000..ecb9785 --- /dev/null +++ b/MinimalBible/src/main/java/org/bspeice/minimalbible/activities/viewer/BookManager.java @@ -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 installedBooks; + private Boolean refreshComplete; + + @Inject + BookManager() { + installedBooks = Observable.from(Books.installed().getBooks()) + .cache(); + installedBooks.subscribeOn(Schedulers.io()) + .subscribe(new Action1() { + @Override + public void call(Book book) {} + }, new Action1() { + @Override + public void call(Throwable throwable) {} + }, new Action0() { + @Override + public void call() { + BookManager.this.refreshComplete = true; + } + }); + } + + public Observable getInstalledBooks() { + return installedBooks; + } + + public Boolean isRefreshComplete() { + return refreshComplete; + } + +} diff --git a/MinimalBible/src/main/res/layout/fragment_navigation_drawer.xml b/MinimalBible/src/main/res/layout/fragment_navigation_drawer.xml index fc2cb0b..78c322f 100644 --- a/MinimalBible/src/main/res/layout/fragment_navigation_drawer.xml +++ b/MinimalBible/src/main/res/layout/fragment_navigation_drawer.xml @@ -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" diff --git a/MinimalBible/src/main/res/layout/fragment_main.xml b/MinimalBible/src/main/res/layout/fragment_viewer_main.xml similarity index 100% rename from MinimalBible/src/main/res/layout/fragment_main.xml rename to MinimalBible/src/main/res/layout/fragment_viewer_main.xml diff --git a/MinimalBible/src/main/res/menu/main.xml b/MinimalBible/src/main/res/menu/main.xml index ee9c4d1..3f073e2 100644 --- a/MinimalBible/src/main/res/menu/main.xml +++ b/MinimalBible/src/main/res/menu/main.xml @@ -3,10 +3,6 @@ xmlns:tools="http://schemas.android.com/tools" tools:context="org.bspeice.minimalbible.MainActivity" > -