Allow to change number of columns

This commit is contained in:
inorichi 2015-12-27 14:58:36 +01:00
parent 1c86828b65
commit 3dff7f90e7
14 changed files with 305 additions and 5 deletions

View File

@ -91,6 +91,14 @@ public class PreferencesHelper {
return Integer.parseInt(prefs.getString(getKey(R.string.pref_default_viewer_key), "1"));
}
public Preference<Integer> portraitColumns() {
return rxPrefs.getInteger(getKey(R.string.pref_library_columns_portrait_key), 0);
}
public Preference<Integer> landscapeColumns() {
return rxPrefs.getInteger(getKey(R.string.pref_library_columns_landscape_key), 0);
}
public String getSourceUsername(Source source) {
return prefs.getString(SOURCE_ACCOUNT_USERNAME + source.getId(), "");
}

View File

@ -14,6 +14,7 @@ import eu.kanade.mangafeed.injection.module.AppModule;
import eu.kanade.mangafeed.injection.module.DataModule;
import eu.kanade.mangafeed.ui.catalogue.CataloguePresenter;
import eu.kanade.mangafeed.ui.download.DownloadPresenter;
import eu.kanade.mangafeed.ui.library.LibraryCategoryFragment;
import eu.kanade.mangafeed.ui.library.LibraryPresenter;
import eu.kanade.mangafeed.ui.manga.MangaActivity;
import eu.kanade.mangafeed.ui.manga.MangaPresenter;
@ -48,6 +49,8 @@ public interface AppComponent {
void inject(SettingsAccountsFragment settingsAccountsFragment);
void inject(SettingsActivity settingsActivity);
void inject(LibraryCategoryFragment libraryCategoryFragment);
void inject(Source source);
void inject(MyAnimeList myAnimeList);

View File

@ -1,41 +1,52 @@
package eu.kanade.mangafeed.ui.library;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v7.view.ActionMode;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import com.f2prateek.rx.preferences.Preference;
import java.util.List;
import javax.inject.Inject;
import butterknife.Bind;
import butterknife.ButterKnife;
import eu.kanade.mangafeed.App;
import eu.kanade.mangafeed.R;
import eu.kanade.mangafeed.data.database.models.Category;
import eu.kanade.mangafeed.data.database.models.Manga;
import eu.kanade.mangafeed.data.preference.PreferencesHelper;
import eu.kanade.mangafeed.event.LibraryMangasEvent;
import eu.kanade.mangafeed.ui.base.activity.BaseActivity;
import eu.kanade.mangafeed.ui.base.adapter.FlexibleViewHolder;
import eu.kanade.mangafeed.ui.base.fragment.BaseFragment;
import eu.kanade.mangafeed.ui.manga.MangaActivity;
import eu.kanade.mangafeed.util.EventBusHook;
import eu.kanade.mangafeed.widget.AutofitRecyclerView;
import icepick.Icepick;
import icepick.State;
import rx.Subscription;
public class LibraryCategoryFragment extends BaseFragment implements
ActionMode.Callback, FlexibleViewHolder.OnListItemClickListener {
@Bind(R.id.library_mangas) RecyclerView recycler;
@Inject PreferencesHelper preferences;
@Bind(R.id.library_mangas) AutofitRecyclerView recycler;
@State Category category;
private LibraryCategoryAdapter adapter;
private ActionMode actionMode;
private Subscription numColumnsSubscription;
private static final int INVALID_POSITION = -1;
public static LibraryCategoryFragment newInstance(Category category) {
@ -44,6 +55,12 @@ public class LibraryCategoryFragment extends BaseFragment implements
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
App.get(getActivity()).getComponent().inject(this);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) {
// Inflate the layout for this fragment
@ -52,14 +69,27 @@ public class LibraryCategoryFragment extends BaseFragment implements
Icepick.restoreInstanceState(this, savedState);
recycler.setHasFixedSize(true);
recycler.setLayoutManager(new GridLayoutManager(getActivity(), 4));
adapter = new LibraryCategoryAdapter(this);
recycler.setAdapter(adapter);
Preference<Integer> columnsPref = getResources().getConfiguration()
.orientation == Configuration.ORIENTATION_PORTRAIT ?
preferences.portraitColumns() :
preferences.landscapeColumns();
numColumnsSubscription = columnsPref.asObservable()
.subscribe(recycler::setSpanCount);
return view;
}
@Override
public void onDestroyView() {
numColumnsSubscription.unsubscribe();
super.onDestroyView();
}
@Override
public void onResume() {
super.onResume();

View File

@ -48,6 +48,10 @@ public class SettingsActivity extends BaseActivity {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_main);
registerSubpreference(R.string.pref_category_general_key,
SettingsGeneralFragment.newInstance(
R.xml.pref_general, R.string.pref_category_general));
registerSubpreference(R.string.pref_category_reader_key,
SettingsNestedFragment.newInstance(
R.xml.pref_reader, R.string.pref_category_reader));

View File

@ -0,0 +1,36 @@
package eu.kanade.mangafeed.ui.setting;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import eu.kanade.mangafeed.R;
import eu.kanade.mangafeed.data.preference.PreferencesHelper;
import eu.kanade.mangafeed.ui.setting.preference.LibraryColumnsDialog;
public class SettingsGeneralFragment extends SettingsNestedFragment {
private LibraryColumnsDialog columnsDialog;
public static SettingsNestedFragment newInstance(int resourcePreference, int resourceTitle) {
SettingsNestedFragment fragment = new SettingsGeneralFragment();
fragment.setArgs(resourcePreference, resourceTitle);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) {
View view = super.onCreateView(inflater, container, savedState);
PreferencesHelper preferences = getSettingsActivity().preferences;
columnsDialog = (LibraryColumnsDialog) findPreference(
getString(R.string.pref_library_columns_dialog_key));
columnsDialog.setPreferencesHelper(preferences);
return view;
}
}

View File

@ -0,0 +1,80 @@
package eu.kanade.mangafeed.ui.setting.preference;
import android.content.Context;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.NumberPicker;
import butterknife.Bind;
import butterknife.ButterKnife;
import eu.kanade.mangafeed.R;
import eu.kanade.mangafeed.data.preference.PreferencesHelper;
public class LibraryColumnsDialog extends DialogPreference {
private Context context;
private PreferencesHelper preferences;
@Bind(R.id.portrait_columns) NumberPicker portraitColumns;
@Bind(R.id.landscape_columns) NumberPicker landscapeColumns;
public LibraryColumnsDialog(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public LibraryColumnsDialog(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context context) {
this.context = context;
setDialogLayoutResource(R.layout.pref_library_columns);
}
@Override
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
ButterKnife.bind(this, view);
portraitColumns.setValue(preferences.portraitColumns().get());
landscapeColumns.setValue(preferences.landscapeColumns().get());
}
@Override
protected void onDialogClosed(boolean positiveResult) {
super.onDialogClosed(positiveResult);
if (positiveResult) {
preferences.portraitColumns().set(portraitColumns.getValue());
preferences.landscapeColumns().set(landscapeColumns.getValue());
updateSummary();
}
}
private void updateSummary() {
setSummary(getColumnsSummary());
}
private String getColumnsSummary() {
return String.format("%s: %s, %s: %s",
context.getString(R.string.portrait),
getColumnValue(preferences.portraitColumns().get()),
context.getString(R.string.landscape),
getColumnValue(preferences.landscapeColumns().get()));
}
private String getColumnValue(int value) {
return value == 0 ? context.getString(R.string.default_columns) : value + "";
}
public void setPreferencesHelper(PreferencesHelper preferences) {
this.preferences = preferences;
// Set initial summary when the preferences helper is provided
updateSummary();
}
}

View File

@ -0,0 +1,60 @@
package eu.kanade.mangafeed.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
public class AutofitRecyclerView extends RecyclerView {
private GridLayoutManager manager;
private int columnWidth = -1;
private int spanCount = 0;
public AutofitRecyclerView(Context context) {
super(context);
init(context, null);
}
public AutofitRecyclerView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public AutofitRecyclerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
if (attrs != null) {
int[] attrsArray = {
android.R.attr.columnWidth
};
TypedArray array = context.obtainStyledAttributes(attrs, attrsArray);
columnWidth = array.getDimensionPixelSize(0, -1);
array.recycle();
}
manager = new GridLayoutManager(getContext(), 1);
setLayoutManager(manager);
}
@Override
protected void onMeasure(int widthSpec, int heightSpec) {
super.onMeasure(widthSpec, heightSpec);
if (spanCount == 0 && columnWidth > 0) {
int spanCount = Math.max(1, getMeasuredWidth() / columnWidth);
manager.setSpanCount(spanCount);
}
}
public void setSpanCount(int spanCount) {
this.spanCount = spanCount;
if (spanCount > 0) {
manager.setSpanCount(spanCount);
}
}
}

View File

@ -4,7 +4,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
<eu.kanade.mangafeed.widget.AutofitRecyclerView
android:id="@+id/library_mangas"
style="@style/AppTheme.GridView"
android:columnWidth="140dp"

View File

@ -63,6 +63,7 @@
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
app:typeface="ptsansNarrowBold"
android:lineSpacingExtra="-4dp"
android:ellipsize="end"
android:maxLines="2"
android:paddingLeft="8dp"

View File

@ -0,0 +1,51 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="15dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/portrait"/>
<eu.kanade.mangafeed.widget.MinMaxNumberPicker
android:id="@+id/portrait_columns"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:min="0"
app:max="10"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/landscape"/>
<eu.kanade.mangafeed.widget.MinMaxNumberPicker
android:id="@+id/landscape_columns"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:min="0"
app:max="10"/>
</LinearLayout>
</LinearLayout>

View File

@ -1,11 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="pref_category_general_key">pref_category_general_key</string>
<string name="pref_category_reader_key">pref_category_reader_key</string>
<string name="pref_category_accounts_key">pref_category_accounts_key</string>
<string name="pref_category_downloads_key">pref_category_downloads_key</string>
<string name="pref_category_cache_key">pref_category_cache_key</string>
<string name="pref_category_about_key">pref_category_about_key</string>
<string name="pref_library_columns_dialog_key">pref_library_columns_dialog_key</string>
<string name="pref_library_columns_portrait_key">pref_library_columns_portrait_key</string>
<string name="pref_library_columns_landscape_key">pref_library_columns_landscape_key</string>
<string name="pref_default_viewer_key">pref_default_viewer_key</string>
<string name="pref_hide_status_bar_key">pref_hide_status_bar_key</string>
<string name="pref_lock_orientation_key">pref_lock_orientation_key</string>

View File

@ -37,12 +37,20 @@
<!-- Preferences -->
<!-- Subsections -->
<string name="pref_category_general">General</string>
<string name="pref_category_reader">Reader</string>
<string name="pref_category_accounts">Accounts</string>
<string name="pref_category_downloads">Downloads</string>
<string name="pref_category_cache">Cache</string>
<string name="pref_category_about">About</string>
<!-- General section -->
<string name="pref_library_columns">Number of columns</string>
<string name="portrait">Portrait</string>
<string name="landscape">Landscape</string>
<string name="default_columns">Default</string>
<!-- Reader section -->
<string name="pref_hide_status_bar">Hide status bar</string>
<string name="pref_lock_orientation">Lock orientation</string>

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<eu.kanade.mangafeed.ui.setting.preference.LibraryColumnsDialog
android:key="@string/pref_library_columns_dialog_key"
android:persistent="false"
android:title="@string/pref_library_columns"/>
</PreferenceScreen>

View File

@ -1,6 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<Preference
android:key="@string/pref_category_general_key"
android:persistent="false"
android:title="@string/pref_category_general" />
<Preference
android:key="@string/pref_category_reader_key"
android:persistent="false"