Working language selector!

This commit is contained in:
Bradlee Speice 2014-11-07 00:11:05 -05:00
parent 88a40cbfbb
commit c0281cc8c7
3 changed files with 57 additions and 35 deletions

View File

@ -17,7 +17,6 @@ import android.widget.Toast;
import org.bspeice.minimalbible.Injector; import org.bspeice.minimalbible.Injector;
import org.bspeice.minimalbible.R; import org.bspeice.minimalbible.R;
import org.bspeice.minimalbible.activity.BaseFragment; import org.bspeice.minimalbible.activity.BaseFragment;
import org.bspeice.minimalbible.activity.downloader.manager.LocaleManager;
import org.bspeice.minimalbible.activity.downloader.manager.RefreshManager; import org.bspeice.minimalbible.activity.downloader.manager.RefreshManager;
import org.crosswire.jsword.book.Book; import org.crosswire.jsword.book.Book;
import org.crosswire.jsword.book.BookCategory; import org.crosswire.jsword.book.BookCategory;
@ -30,6 +29,7 @@ import javax.inject.Inject;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import butterknife.InjectView; import butterknife.InjectView;
import butterknife.OnItemSelected;
import rx.android.schedulers.AndroidSchedulers; import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1; import rx.functions.Action1;
import rx.functions.Func1; import rx.functions.Func1;
@ -47,11 +47,13 @@ public class BookListFragment extends BaseFragment {
protected ProgressDialog refreshDialog; protected ProgressDialog refreshDialog;
@Inject RefreshManager refreshManager; @Inject RefreshManager refreshManager;
@Inject @Inject
LocaleManager localeManager; List<String> availableLanguages;
@InjectView(R.id.lst_download_available) @InjectView(R.id.lst_download_available)
ListView downloadsAvailable; ListView downloadsAvailable;
@InjectView(R.id.spn_available_languages) @InjectView(R.id.spn_available_languages)
Spinner availableLanguages; Spinner languagesSpinner;
private LayoutInflater inflater; private LayoutInflater inflater;
/** /**
@ -80,6 +82,7 @@ public class BookListFragment extends BaseFragment {
false); false);
ButterKnife.inject(this, rootView); ButterKnife.inject(this, rootView);
displayModules(); displayModules();
return rootView; return rootView;
} }
@ -124,7 +127,32 @@ public class BookListFragment extends BaseFragment {
refreshDialog.show(); 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<Object> adapter = new ArrayAdapter<Object>(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() refreshManager.getFlatModules()
.filter(new Func1<Book, Boolean>() { .filter(new Func1<Book, Boolean>() {
@Override @Override
@ -134,6 +162,13 @@ public class BookListFragment extends BaseFragment {
.getString(ARG_BOOK_CATEGORY)); .getString(ARG_BOOK_CATEGORY));
} }
}) })
.filter(new Func1<Book, Boolean>() {
@Override
public Boolean call(Book book) {
return book.getLanguage().getName()
.equals(availableLanguages.get(position));
}
})
// Repack all the books // Repack all the books
.toSortedList(new Func2<Book, Book, Integer>() { .toSortedList(new Func2<Book, Book, Integer>() {
@Override @Override
@ -146,32 +181,12 @@ public class BookListFragment extends BaseFragment {
@Override @Override
public void call(List<Book> books) { public void call(List<Book> books) {
downloadsAvailable.setAdapter( downloadsAvailable.setAdapter(
new BookListAdapter(inflater, books, (DownloadActivity)getActivity())); new BookListAdapter(inflater, books,
availableLanguages.setAdapter(getLocaleSpinner()); (DownloadActivity) getActivity()));
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();
}
} }
}); });
} }
@SuppressWarnings("ConstantConditions")
// getAvailableLanguagesList() will not return null
SpinnerAdapter getLocaleSpinner() {
ArrayAdapter<Object> adapter = new ArrayAdapter<Object>(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 private class DownloadDialogListener implements
DialogInterface.OnClickListener { DialogInterface.OnClickListener {
@Override @Override

View File

@ -118,4 +118,10 @@ public class DownloadActivityModules {
LocaleManager provideLocaleManager(RefreshManager refreshManager) { LocaleManager provideLocaleManager(RefreshManager refreshManager) {
return new LocaleManager(refreshManager); return new LocaleManager(refreshManager);
} }
@Provides
List<String> availableLanguages(LocaleManager localeManager) {
final List<String> languages = new ArrayList<String>();
return localeManager.getSortedLanguagesList();
}
} }

View File

@ -10,7 +10,8 @@ class LocaleManager(val rM: RefreshManager) {
.groupBy { it.getName() } .groupBy { it.getName() }
val availableLanguages = languageModuleMap.map { it.getKey() } val availableLanguages = languageModuleMap.map { it.getKey() }
val availableLanguagesList = availableLanguages.toSortedList {(left, right) -> val sortedLanguagesList: List<String> =
availableLanguages.toSortedList {(left, right) ->
// Prioritize our current language first // Prioritize our current language first
if (left == currentLanguage) if (left == currentLanguage)
-1 -1