mirror of
				https://github.com/mihonapp/mihon.git
				synced 2025-10-31 14:27:57 +01:00 
			
		
		
		
	Allow to change number of columns
This commit is contained in:
		| @@ -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(), ""); | ||||
|     } | ||||
|   | ||||
| @@ -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); | ||||
|   | ||||
| @@ -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(); | ||||
|   | ||||
| @@ -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)); | ||||
|   | ||||
| @@ -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; | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -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(); | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -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); | ||||
|         } | ||||
|     } | ||||
|  | ||||
| } | ||||
		Reference in New Issue
	
	Block a user