Remember last used source. Closes #30

This commit is contained in:
inorichi 2016-02-06 19:03:15 +01:00
parent c35184abdc
commit 050b9c9fce
5 changed files with 49 additions and 137 deletions

View File

@ -92,6 +92,18 @@ public class PreferencesHelper {
return rxPrefs.getInteger(getKey(R.string.pref_image_scale_type_key), 1);
}
public Preference<Integer> imageDecoder() {
return rxPrefs.getInteger(getKey(R.string.pref_image_decoder_key), 0);
}
public Preference<Integer> zoomStart() {
return rxPrefs.getInteger(getKey(R.string.pref_zoom_start_key), 1);
}
public Preference<Integer> readerTheme() {
return rxPrefs.getInteger(getKey(R.string.pref_reader_theme_key), 0);
}
public Preference<Integer> portraitColumns() {
return rxPrefs.getInteger(getKey(R.string.pref_library_columns_portrait_key), 0);
}
@ -112,16 +124,8 @@ public class PreferencesHelper {
return prefs.getBoolean(getKey(R.string.pref_ask_update_manga_sync_key), false);
}
public Preference<Integer> imageDecoder() {
return rxPrefs.getInteger(getKey(R.string.pref_image_decoder_key), 0);
}
public Preference<Integer> zoomStart() {
return rxPrefs.getInteger(getKey(R.string.pref_zoom_start_key), 1);
}
public Preference<Integer> readerTheme() {
return rxPrefs.getInteger(getKey(R.string.pref_reader_theme_key), 0);
public Preference<Integer> lastUsedCatalogueSource() {
return rxPrefs.getInteger(getKey(R.string.pref_last_catalogue_source_key), -1);
}
public Preference<Boolean> catalogueAsList() {

View File

@ -20,6 +20,7 @@ import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.ViewSwitcher;
@ -66,7 +67,7 @@ public class CatalogueFragment extends BaseRxFragment<CataloguePresenter>
private EndlessListScrollListener listScrollListener;
@State String query = "";
@State int selectedIndex = -1;
@State int selectedIndex;
private final int SEARCH_TIMEOUT = 1000;
private PublishSubject<String> queryDebouncerSubject;
@ -122,25 +123,27 @@ public class CatalogueFragment extends BaseRxFragment<CataloguePresenter>
Context themedContext = getBaseActivity().getSupportActionBar() != null ?
getBaseActivity().getSupportActionBar().getThemedContext() : getActivity();
spinner = new Spinner(themedContext);
CatalogueSpinnerAdapter spinnerAdapter = new CatalogueSpinnerAdapter(themedContext,
ArrayAdapter<Source> spinnerAdapter = new ArrayAdapter<>(themedContext,
android.R.layout.simple_spinner_item, getPresenter().getEnabledSources());
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
if (savedState == null) selectedIndex = spinnerAdapter.getEmptyIndex();
if (savedState == null) {
selectedIndex = getPresenter().getLastUsedSourceIndex();
}
spinner.setAdapter(spinnerAdapter);
spinner.setSelection(selectedIndex);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Source source = spinnerAdapter.getItem(position);
// We add an empty source with id -1 that acts as a placeholder to show a hint
// that asks to select a source
if (source.getId() != -1 && (selectedIndex != position || adapter.isEmpty())) {
if (selectedIndex != position || adapter.isEmpty()) {
// Set previous selection if it's not a valid source and notify the user
if (!getPresenter().isValidSource(source)) {
spinner.setSelection(spinnerAdapter.getEmptyIndex());
spinner.setSelection(getPresenter().findFirstValidSource());
ToastUtil.showShort(getActivity(), R.string.source_requires_login);
} else {
selectedIndex = position;
getPresenter().setEnabledSource(selectedIndex);
showProgressBar();
glm.scrollToPositionWithOffset(0, 0);
llm.scrollToPositionWithOffset(0, 0);

View File

@ -32,6 +32,7 @@ public class CataloguePresenter extends BasePresenter<CatalogueFragment> {
@Inject CoverCache coverCache;
@Inject PreferencesHelper prefs;
private List<Source> sources;
private Source source;
@State int sourceId;
@ -56,6 +57,8 @@ public class CataloguePresenter extends BasePresenter<CatalogueFragment> {
source = sourceManager.get(sourceId);
}
sources = sourceManager.getSources();
mangaDetailSubject = PublishSubject.create();
pager = new RxPager<>();
@ -168,6 +171,14 @@ public class CataloguePresenter extends BasePresenter<CatalogueFragment> {
return lastMangasPage != null && lastMangasPage.nextPageUrl != null;
}
public int getLastUsedSourceIndex() {
int index = prefs.lastUsedCatalogueSource().get();
if (index < 0 || index >= sources.size() || !isValidSource(sources.get(index))) {
return findFirstValidSource();
}
return index;
}
public boolean isValidSource(Source source) {
if (!source.isLoginRequired() || source.isLogged())
return true;
@ -176,6 +187,19 @@ public class CataloguePresenter extends BasePresenter<CatalogueFragment> {
|| prefs.getSourcePassword(source).equals(""));
}
public int findFirstValidSource() {
for (int i = 0; i < sources.size(); i++) {
if (isValidSource(sources.get(i))) {
return i;
}
}
return 0;
}
public void setEnabledSource(int index) {
prefs.lastUsedCatalogueSource().set(index);
}
public List<Source> getEnabledSources() {
// TODO filter by enabled source
return sourceManager.getSources();

View File

@ -1,120 +0,0 @@
package eu.kanade.tachiyomi.ui.catalogue;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import org.jsoup.nodes.Document;
import java.util.List;
import eu.kanade.tachiyomi.R;
import eu.kanade.tachiyomi.data.database.models.Chapter;
import eu.kanade.tachiyomi.data.database.models.Manga;
import eu.kanade.tachiyomi.data.source.base.Source;
import eu.kanade.tachiyomi.data.source.model.MangasPage;
public class CatalogueSpinnerAdapter extends ArrayAdapter<Source> {
public CatalogueSpinnerAdapter(Context context, int resource, List<Source> sources) {
super(context, resource, sources);
sources.add(new SimpleSource());
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
if (position == getCount()) {
((TextView)v.findViewById(android.R.id.text1)).setText("");
((TextView)v.findViewById(android.R.id.text1)).setHint(getItem(getCount()).getName());
}
return v;
}
@Override
public int getCount() {
return super.getCount()-1; // you dont display last item. It is used as hint.
}
public int getEmptyIndex() {
return getCount();
}
private class SimpleSource extends Source {
@Override
public String getName() {
return getContext().getString(R.string.select_source);
}
@Override
public int getId() {
return -1;
}
@Override
public String getBaseUrl() {
return null;
}
@Override
public boolean isLoginRequired() {
return false;
}
@Override
protected String getInitialPopularMangasUrl() {
return null;
}
@Override
protected String getInitialSearchUrl(String query) {
return null;
}
@Override
protected List<Manga> parsePopularMangasFromHtml(Document parsedHtml) {
return null;
}
@Override
protected String parseNextPopularMangasUrl(Document parsedHtml, MangasPage page) {
return null;
}
@Override
protected List<Manga> parseSearchFromHtml(Document parsedHtml) {
return null;
}
@Override
protected String parseNextSearchUrl(Document parsedHtml, MangasPage page, String query) {
return null;
}
@Override
protected Manga parseHtmlToManga(String mangaUrl, String unparsedHtml) {
return null;
}
@Override
protected List<Chapter> parseHtmlToChapters(String unparsedHtml) {
return null;
}
@Override
protected List<String> parseHtmlToPageUrls(String unparsedHtml) {
return null;
}
@Override
protected String parseHtmlToImageUrl(String unparsedHtml) {
return null;
}
}
}

View File

@ -39,4 +39,5 @@
<string name="pref_build_time">pref_build_time</string>
<string name="pref_display_catalogue_as_list">pref_display_catalogue_as_list</string>
<string name="pref_last_catalogue_source_key">pref_last_catalogue_source_key</string>
</resources>