diff --git a/app/src/main/java/org/schabi/newpipe/local/BaseLocalListFragment.java b/app/src/main/java/org/schabi/newpipe/local/BaseLocalListFragment.java index 5192aa2ab..abdf82353 100644 --- a/app/src/main/java/org/schabi/newpipe/local/BaseLocalListFragment.java +++ b/app/src/main/java/org/schabi/newpipe/local/BaseLocalListFragment.java @@ -1,8 +1,13 @@ package org.schabi.newpipe.local; +import android.content.SharedPreferences; +import android.content.res.Configuration; +import android.content.res.Resources; import android.os.Bundle; +import android.preference.PreferenceManager; import android.support.v4.app.Fragment; import android.support.v7.app.ActionBar; +import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; @@ -25,7 +30,7 @@ import static org.schabi.newpipe.util.AnimationUtils.animateView; * called and is memory efficient when in backstack. * */ public abstract class BaseLocalListFragment extends BaseStateFragment - implements ListViewContract { + implements ListViewContract, SharedPreferences.OnSharedPreferenceChangeListener { /*////////////////////////////////////////////////////////////////////////// // Views @@ -36,6 +41,9 @@ public abstract class BaseLocalListFragment extends BaseStateFragment protected LocalItemListAdapter itemListAdapter; protected RecyclerView itemsList; + private int updateFlags = 0; + + private static final int LIST_MODE_UPDATE_FLAG = 0x32; /*////////////////////////////////////////////////////////////////////////// // Lifecycle - Creation @@ -45,6 +53,29 @@ public abstract class BaseLocalListFragment extends BaseStateFragment public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); + PreferenceManager.getDefaultSharedPreferences(activity) + .registerOnSharedPreferenceChangeListener(this); + } + + @Override + public void onDestroy() { + super.onDestroy(); + PreferenceManager.getDefaultSharedPreferences(activity) + .unregisterOnSharedPreferenceChangeListener(this); + } + + @Override + public void onResume() { + super.onResume(); + if (updateFlags != 0) { + if ((updateFlags & LIST_MODE_UPDATE_FLAG) != 0) { + final boolean useGrid = isGridLayout(); + itemsList.setLayoutManager(useGrid ? getGridLayoutManager() : getListLayoutManager()); + itemListAdapter.setGridItemVariants(useGrid); + itemListAdapter.notifyDataSetChanged(); + } + updateFlags = 0; + } } /*////////////////////////////////////////////////////////////////////////// @@ -59,6 +90,16 @@ public abstract class BaseLocalListFragment extends BaseStateFragment return activity.getLayoutInflater().inflate(R.layout.pignate_footer, itemsList, false); } + protected RecyclerView.LayoutManager getGridLayoutManager() { + final Resources resources = activity.getResources(); + int width = resources.getDimensionPixelSize(R.dimen.video_item_grid_thumbnail_image_width); + width += (24 * resources.getDisplayMetrics().density); + final int spanCount = (int) Math.floor(resources.getDisplayMetrics().widthPixels / (double)width); + final GridLayoutManager lm = new GridLayoutManager(activity, spanCount); + lm.setSpanSizeLookup(itemListAdapter.getSpanSizeLookup(spanCount)); + return lm; + } + protected RecyclerView.LayoutManager getListLayoutManager() { return new LinearLayoutManager(activity); } @@ -67,10 +108,13 @@ public abstract class BaseLocalListFragment extends BaseStateFragment protected void initViews(View rootView, Bundle savedInstanceState) { super.initViews(rootView, savedInstanceState); - itemsList = rootView.findViewById(R.id.items_list); - itemsList.setLayoutManager(getListLayoutManager()); - itemListAdapter = new LocalItemListAdapter(activity); + + final boolean useGrid = isGridLayout(); + itemsList = rootView.findViewById(R.id.items_list); + itemsList.setLayoutManager(useGrid ? getGridLayoutManager() : getListLayoutManager()); + + itemListAdapter.setGridItemVariants(useGrid); itemListAdapter.setHeader(headerRootView = getListHeader()); itemListAdapter.setFooter(footerRootView = getListFooter()); @@ -174,4 +218,22 @@ public abstract class BaseLocalListFragment extends BaseStateFragment resetFragment(); return super.onError(exception); } + + @Override + public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { + if (key.equals(getString(R.string.list_view_mode_key))) { + updateFlags |= LIST_MODE_UPDATE_FLAG; + } + } + + protected boolean isGridLayout() { + final String list_mode = PreferenceManager.getDefaultSharedPreferences(activity).getString(getString(R.string.list_view_mode_key), getString(R.string.list_view_mode_value)); + if ("auto".equals(list_mode)) { + final Configuration configuration = getResources().getConfiguration(); + return configuration.orientation == Configuration.ORIENTATION_LANDSCAPE + && configuration.isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_LARGE); + } else { + return "grid".equals(list_mode); + } + } } diff --git a/app/src/main/java/org/schabi/newpipe/local/LocalItemListAdapter.java b/app/src/main/java/org/schabi/newpipe/local/LocalItemListAdapter.java index 99937b58c..e298dedd3 100644 --- a/app/src/main/java/org/schabi/newpipe/local/LocalItemListAdapter.java +++ b/app/src/main/java/org/schabi/newpipe/local/LocalItemListAdapter.java @@ -1,18 +1,21 @@ package org.schabi.newpipe.local; import android.app.Activity; +import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.View; import android.view.ViewGroup; import org.schabi.newpipe.database.LocalItem; -import org.schabi.newpipe.local.HeaderFooterHolder; -import org.schabi.newpipe.local.LocalItemBuilder; import org.schabi.newpipe.local.holder.LocalItemHolder; +import org.schabi.newpipe.local.holder.LocalPlaylistGridItemHolder; import org.schabi.newpipe.local.holder.LocalPlaylistItemHolder; +import org.schabi.newpipe.local.holder.LocalPlaylistStreamGridItemHolder; import org.schabi.newpipe.local.holder.LocalPlaylistStreamItemHolder; +import org.schabi.newpipe.local.holder.LocalStatisticStreamGridItemHolder; import org.schabi.newpipe.local.holder.LocalStatisticStreamItemHolder; +import org.schabi.newpipe.local.holder.RemotePlaylistGridItemHolder; import org.schabi.newpipe.local.holder.RemotePlaylistItemHolder; import org.schabi.newpipe.util.FallbackViewHolder; import org.schabi.newpipe.util.Localization; @@ -52,14 +55,19 @@ public class LocalItemListAdapter extends RecyclerView.Adapter localItems; private final DateFormat dateFormat; private boolean showFooter = false; + private boolean useGridVariant = false; private View header = null; private View footer = null; @@ -134,6 +142,10 @@ public class LocalItemListAdapter extends RecyclerView.Adapter + + + + + + + + + + + + + \ No newline at end of file