From 3dff7f90e70f666ed1fba17f843d28f5cd0830ab Mon Sep 17 00:00:00 2001 From: inorichi Date: Sun, 27 Dec 2015 14:58:36 +0100 Subject: [PATCH] Allow to change number of columns --- .../data/preference/PreferencesHelper.java | 8 ++ .../injection/component/AppComponent.java | 3 + .../ui/library/LibraryCategoryFragment.java | 38 ++++++++- .../ui/setting/SettingsActivity.java | 4 + .../ui/setting/SettingsGeneralFragment.java | 36 +++++++++ .../preference/LibraryColumnsDialog.java | 80 +++++++++++++++++++ .../mangafeed/widget/AutofitRecyclerView.java | 60 ++++++++++++++ .../res/layout/fragment_library_category.xml | 2 +- app/src/main/res/layout/item_catalogue.xml | 1 + .../main/res/layout/pref_library_columns.xml | 51 ++++++++++++ app/src/main/res/values/keys.xml | 5 ++ app/src/main/res/values/strings.xml | 8 ++ app/src/main/res/xml/pref_general.xml | 9 +++ app/src/main/res/xml/pref_main.xml | 5 ++ 14 files changed, 305 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/eu/kanade/mangafeed/ui/setting/SettingsGeneralFragment.java create mode 100644 app/src/main/java/eu/kanade/mangafeed/ui/setting/preference/LibraryColumnsDialog.java create mode 100644 app/src/main/java/eu/kanade/mangafeed/widget/AutofitRecyclerView.java create mode 100644 app/src/main/res/layout/pref_library_columns.xml create mode 100644 app/src/main/res/xml/pref_general.xml diff --git a/app/src/main/java/eu/kanade/mangafeed/data/preference/PreferencesHelper.java b/app/src/main/java/eu/kanade/mangafeed/data/preference/PreferencesHelper.java index 77992cce9..b610d2588 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/preference/PreferencesHelper.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/preference/PreferencesHelper.java @@ -91,6 +91,14 @@ public class PreferencesHelper { return Integer.parseInt(prefs.getString(getKey(R.string.pref_default_viewer_key), "1")); } + public Preference portraitColumns() { + return rxPrefs.getInteger(getKey(R.string.pref_library_columns_portrait_key), 0); + } + + public Preference 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(), ""); } diff --git a/app/src/main/java/eu/kanade/mangafeed/injection/component/AppComponent.java b/app/src/main/java/eu/kanade/mangafeed/injection/component/AppComponent.java index 5e5b39b44..5a6df1e9a 100644 --- a/app/src/main/java/eu/kanade/mangafeed/injection/component/AppComponent.java +++ b/app/src/main/java/eu/kanade/mangafeed/injection/component/AppComponent.java @@ -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); diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryCategoryFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryCategoryFragment.java index b1112c8ab..2ca2e30f7 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryCategoryFragment.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/library/LibraryCategoryFragment.java @@ -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 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(); diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/setting/SettingsActivity.java b/app/src/main/java/eu/kanade/mangafeed/ui/setting/SettingsActivity.java index a2b591074..9ff14e9d8 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/setting/SettingsActivity.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/setting/SettingsActivity.java @@ -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)); diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/setting/SettingsGeneralFragment.java b/app/src/main/java/eu/kanade/mangafeed/ui/setting/SettingsGeneralFragment.java new file mode 100644 index 000000000..f4443983a --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/ui/setting/SettingsGeneralFragment.java @@ -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; + } + +} diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/setting/preference/LibraryColumnsDialog.java b/app/src/main/java/eu/kanade/mangafeed/ui/setting/preference/LibraryColumnsDialog.java new file mode 100644 index 000000000..78b769af4 --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/ui/setting/preference/LibraryColumnsDialog.java @@ -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(); + } + +} diff --git a/app/src/main/java/eu/kanade/mangafeed/widget/AutofitRecyclerView.java b/app/src/main/java/eu/kanade/mangafeed/widget/AutofitRecyclerView.java new file mode 100644 index 000000000..97e382eb2 --- /dev/null +++ b/app/src/main/java/eu/kanade/mangafeed/widget/AutofitRecyclerView.java @@ -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); + } + } + +} \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_library_category.xml b/app/src/main/res/layout/fragment_library_category.xml index 1d124e5a7..fb2ceceaf 100644 --- a/app/src/main/res/layout/fragment_library_category.xml +++ b/app/src/main/res/layout/fragment_library_category.xml @@ -4,7 +4,7 @@ android:layout_width="match_parent" android:layout_height="match_parent"> - + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/keys.xml b/app/src/main/res/values/keys.xml index 0ea9054d6..a28d6a5c7 100644 --- a/app/src/main/res/values/keys.xml +++ b/app/src/main/res/values/keys.xml @@ -1,11 +1,16 @@ + pref_category_general_key pref_category_reader_key pref_category_accounts_key pref_category_downloads_key pref_category_cache_key pref_category_about_key + pref_library_columns_dialog_key + pref_library_columns_portrait_key + pref_library_columns_landscape_key + pref_default_viewer_key pref_hide_status_bar_key pref_lock_orientation_key diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d48e7051d..f93c3b3a7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -37,12 +37,20 @@ + General Reader Accounts Downloads Cache About + + Number of columns + Portrait + Landscape + Default + + Hide status bar Lock orientation diff --git a/app/src/main/res/xml/pref_general.xml b/app/src/main/res/xml/pref_general.xml new file mode 100644 index 000000000..c0ad44871 --- /dev/null +++ b/app/src/main/res/xml/pref_general.xml @@ -0,0 +1,9 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/xml/pref_main.xml b/app/src/main/res/xml/pref_main.xml index 71245cb60..af14da3b3 100644 --- a/app/src/main/res/xml/pref_main.xml +++ b/app/src/main/res/xml/pref_main.xml @@ -1,6 +1,11 @@ + +