From c0281cc8c723aa84a95c55b7781709162c6a9e31 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Fri, 7 Nov 2014 00:11:05 -0500 Subject: [PATCH] Working language selector! --- .../activity/downloader/BookListFragment.java | 67 ++++++++++++------- .../downloader/DownloadActivityModules.java | 6 ++ .../downloader/manager/LocaleManager.kt | 19 +++--- 3 files changed, 57 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/org/bspeice/minimalbible/activity/downloader/BookListFragment.java b/app/src/main/java/org/bspeice/minimalbible/activity/downloader/BookListFragment.java index e2f3db1..f2a371f 100644 --- a/app/src/main/java/org/bspeice/minimalbible/activity/downloader/BookListFragment.java +++ b/app/src/main/java/org/bspeice/minimalbible/activity/downloader/BookListFragment.java @@ -17,7 +17,6 @@ import android.widget.Toast; import org.bspeice.minimalbible.Injector; import org.bspeice.minimalbible.R; import org.bspeice.minimalbible.activity.BaseFragment; -import org.bspeice.minimalbible.activity.downloader.manager.LocaleManager; import org.bspeice.minimalbible.activity.downloader.manager.RefreshManager; import org.crosswire.jsword.book.Book; import org.crosswire.jsword.book.BookCategory; @@ -30,6 +29,7 @@ import javax.inject.Inject; import butterknife.ButterKnife; import butterknife.InjectView; +import butterknife.OnItemSelected; import rx.android.schedulers.AndroidSchedulers; import rx.functions.Action1; import rx.functions.Func1; @@ -47,11 +47,13 @@ public class BookListFragment extends BaseFragment { protected ProgressDialog refreshDialog; @Inject RefreshManager refreshManager; @Inject - LocaleManager localeManager; + List availableLanguages; + @InjectView(R.id.lst_download_available) ListView downloadsAvailable; @InjectView(R.id.spn_available_languages) - Spinner availableLanguages; + Spinner languagesSpinner; + private LayoutInflater inflater; /** @@ -80,6 +82,7 @@ public class BookListFragment extends BaseFragment { false); ButterKnife.inject(this, rootView); displayModules(); + return rootView; } @@ -124,7 +127,32 @@ public class BookListFragment extends BaseFragment { refreshDialog.show(); } - // Listen for the books! + languagesSpinner.setAdapter(getLocaleSpinner()); + if (BookListFragment.this.getActivity() != null) { + // On a screen rotate, getActivity() will be null. But, the activity + // will already have been set up correctly, so we don't need to worry + // about it. + // If not null, we need to set it up now. + setInsetsSpinner(BookListFragment.this.getActivity(), languagesSpinner); + } + if (refreshDialog != null) { + refreshDialog.cancel(); + } + } + + @SuppressWarnings("ConstantConditions") + // getAvailableLanguagesList() will not return null + SpinnerAdapter getLocaleSpinner() { + ArrayAdapter adapter = new ArrayAdapter(this.getActivity(), + android.R.layout.simple_spinner_item, + availableLanguages.toArray()); + adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + return adapter; + } + + @SuppressWarnings("unused") + @OnItemSelected(R.id.spn_available_languages) + public void onClick(final int position) { refreshManager.getFlatModules() .filter(new Func1() { @Override @@ -134,6 +162,13 @@ public class BookListFragment extends BaseFragment { .getString(ARG_BOOK_CATEGORY)); } }) + .filter(new Func1() { + @Override + public Boolean call(Book book) { + return book.getLanguage().getName() + .equals(availableLanguages.get(position)); + } + }) // Repack all the books .toSortedList(new Func2() { @Override @@ -146,30 +181,10 @@ public class BookListFragment extends BaseFragment { @Override public void call(List books) { downloadsAvailable.setAdapter( - new BookListAdapter(inflater, books, (DownloadActivity)getActivity())); - availableLanguages.setAdapter(getLocaleSpinner()); - if (BookListFragment.this.getActivity() != null) { - // On a screen rotate, getActivity() will be null. But, the activity - // will already have been set up correctly, so we don't need to worry - // about it. - // If not null, we need to set it up now. - setInsetsSpinner(BookListFragment.this.getActivity(), availableLanguages); - } - if (refreshDialog != null) { - refreshDialog.cancel(); - } + new BookListAdapter(inflater, books, + (DownloadActivity) getActivity())); } }); - } - - @SuppressWarnings("ConstantConditions") - // getAvailableLanguagesList() will not return null - SpinnerAdapter getLocaleSpinner() { - ArrayAdapter adapter = new ArrayAdapter(this.getActivity(), - android.R.layout.simple_spinner_item, - localeManager.getAvailableLanguagesList().toArray()); - adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); - return adapter; } private class DownloadDialogListener implements diff --git a/app/src/main/java/org/bspeice/minimalbible/activity/downloader/DownloadActivityModules.java b/app/src/main/java/org/bspeice/minimalbible/activity/downloader/DownloadActivityModules.java index bb940e7..fd8a62d 100644 --- a/app/src/main/java/org/bspeice/minimalbible/activity/downloader/DownloadActivityModules.java +++ b/app/src/main/java/org/bspeice/minimalbible/activity/downloader/DownloadActivityModules.java @@ -118,4 +118,10 @@ public class DownloadActivityModules { LocaleManager provideLocaleManager(RefreshManager refreshManager) { return new LocaleManager(refreshManager); } + + @Provides + List availableLanguages(LocaleManager localeManager) { + final List languages = new ArrayList(); + return localeManager.getSortedLanguagesList(); + } } diff --git a/app/src/main/kotlin/org/bspeice/minimalbible/activity/downloader/manager/LocaleManager.kt b/app/src/main/kotlin/org/bspeice/minimalbible/activity/downloader/manager/LocaleManager.kt index b711423..0a26004 100644 --- a/app/src/main/kotlin/org/bspeice/minimalbible/activity/downloader/manager/LocaleManager.kt +++ b/app/src/main/kotlin/org/bspeice/minimalbible/activity/downloader/manager/LocaleManager.kt @@ -10,13 +10,14 @@ class LocaleManager(val rM: RefreshManager) { .groupBy { it.getName() } val availableLanguages = languageModuleMap.map { it.getKey() } - val availableLanguagesList = availableLanguages.toSortedList {(left, right) -> - // Prioritize our current language first - if (left == currentLanguage) - -1 - else if (right == currentLanguage) - 1 - else - left.compareTo(right) - }.toBlocking().first() + val sortedLanguagesList: List = + availableLanguages.toSortedList {(left, right) -> + // Prioritize our current language first + if (left == currentLanguage) + -1 + else if (right == currentLanguage) + 1 + else + left.compareTo(right) + }.toBlocking().first() } \ No newline at end of file