2929import android .app .Activity ;
3030import android .content .Context ;
3131import android .content .Intent ;
32+ import android .content .res .Configuration ;
3233import android .os .AsyncTask ;
3334import android .os .Bundle ;
3435import android .os .Handler ;
4344import android .view .MenuItem ;
4445import android .view .View ;
4546import android .view .ViewGroup ;
47+ import android .view .ViewTreeObserver ;
4648import android .widget .AbsListView ;
4749import android .widget .Toast ;
4850
6264import com .nextcloud .client .jobs .BackgroundJobManager ;
6365import com .nextcloud .client .network .ClientFactory ;
6466import com .nextcloud .client .preferences .AppPreferences ;
67+ import com .nextcloud .client .preferences .AppPreferencesImpl ;
6568import com .nextcloud .client .utils .Throttler ;
6669import com .nextcloud .common .NextcloudClient ;
6770import com .nextcloud .ui .fileactions .FileActionsBottomSheet ;
9598import com .owncloud .android .ui .activity .UploadFilesActivity ;
9699import com .owncloud .android .ui .adapter .CommonOCFileListAdapterInterface ;
97100import com .owncloud .android .ui .adapter .OCFileListAdapter ;
101+ import com .owncloud .android .ui .decoration .MediaGridItemDecoration ;
102+ import com .owncloud .android .ui .decoration .SimpleListItemDividerDecoration ;
98103import com .owncloud .android .ui .dialog .ChooseRichDocumentsTemplateDialogFragment ;
99104import com .owncloud .android .ui .dialog .ChooseTemplateDialogFragment ;
100105import com .owncloud .android .ui .dialog .ConfirmationDialogFragment ;
@@ -235,6 +240,9 @@ public class OCFileListFragment extends ExtendedListFragment implements
235240 protected String mLimitToMimeType ;
236241 private FloatingActionButton mFabMain ;
237242
243+ private SimpleListItemDividerDecoration simpleListItemDividerDecoration ;
244+ private MediaGridItemDecoration mediaGridItemDecoration ;
245+
238246 @ Inject DeviceInfo deviceInfo ;
239247
240248 protected enum MenuItemAddRemove {
@@ -248,6 +256,13 @@ protected enum MenuItemAddRemove {
248256
249257 private List <MenuItem > mOriginalMenuItems = new ArrayList <>();
250258
259+ private int maxColumnSizeLandscape = 5 ;
260+
261+ //this variable will help us to provide number of span count for grid view
262+ //the width for single item is approx to 360
263+ private static final int GRID_ITEM_DEFAULT_WIDTH = 360 ;
264+ private static final int DEFAULT_FALLBACK_SPAN_COUNT = 1 ;
265+
251266 @ Override
252267 public void onCreate (Bundle savedInstanceState ) {
253268 super .onCreate (savedInstanceState );
@@ -446,6 +461,10 @@ protected void setAdapter(Bundle args) {
446461 viewThemeUtils
447462 );
448463
464+ simpleListItemDividerDecoration = new SimpleListItemDividerDecoration (getContext (), R .drawable .item_divider , true );
465+ int spacing = getResources ().getDimensionPixelSize (R .dimen .media_grid_spacing );
466+ mediaGridItemDecoration = new MediaGridItemDecoration (spacing );
467+
449468 setRecyclerViewAdapter (mAdapter );
450469
451470 fastScrollUtils .applyFastScroll (getRecyclerView ());
@@ -1427,6 +1446,7 @@ public void switchToListView() {
14271446 if (isGridEnabled ()) {
14281447 switchLayoutManager (false );
14291448 }
1449+ addRemoveRecyclerViewItemDecorator ();
14301450 }
14311451
14321452 public void setGridAsPreferred () {
@@ -1438,6 +1458,33 @@ public void switchToGridView() {
14381458 if (!isGridEnabled ()) {
14391459 switchLayoutManager (true );
14401460 }
1461+ addRemoveRecyclerViewItemDecorator ();
1462+ }
1463+
1464+ private void addRemoveRecyclerViewItemDecorator () {
1465+ if (getRecyclerView ().getLayoutManager () instanceof GridLayoutManager ) {
1466+ removeItemDecorator ();
1467+ if (getRecyclerView ().getItemDecorationCount () == 0 ) {
1468+ getRecyclerView ().addItemDecoration (mediaGridItemDecoration );
1469+ int padding = getResources ().getDimensionPixelSize (R .dimen .grid_recyclerview_padding );
1470+ getRecyclerView ().setPadding (padding , padding , padding , padding );
1471+ }
1472+ } else {
1473+ removeItemDecorator ();
1474+ if (getRecyclerView ().getItemDecorationCount () == 0 && com .nmc .android .utils .DisplayUtils .isShowDividerForList ()) {
1475+ getRecyclerView ().addItemDecoration (simpleListItemDividerDecoration );
1476+ getRecyclerView ().setPadding (0 , 0 , 0 , 0 );
1477+ }
1478+ }
1479+ }
1480+
1481+ /**
1482+ * method to remove the item decorator
1483+ */
1484+ private void removeItemDecorator () {
1485+ while (getRecyclerView ().getItemDecorationCount () > 0 ) {
1486+ getRecyclerView ().removeItemDecorationAt (0 );
1487+ }
14411488 }
14421489
14431490 public void switchLayoutManager (boolean grid ) {
@@ -1468,12 +1515,40 @@ public int getSpanSize(int position) {
14681515 }
14691516
14701517 getRecyclerView ().setLayoutManager (layoutManager );
1518+ updateSpanCount (getResources ().getConfiguration ());
14711519 getRecyclerView ().scrollToPosition (position );
14721520 getAdapter ().setGridView (grid );
14731521 getRecyclerView ().setAdapter (getAdapter ());
14741522 getAdapter ().notifyDataSetChanged ();
14751523 }
14761524
1525+ /**
1526+ * method will calculate the number of spans required for grid item and will update the span accordingly
1527+ *
1528+ * @param isGrid
1529+ */
1530+ private void calculateAndUpdateSpanCount (boolean isGrid ) {
1531+ getRecyclerView ().getViewTreeObserver ().addOnGlobalLayoutListener (
1532+ new ViewTreeObserver .OnGlobalLayoutListener () {
1533+ @ Override
1534+ public void onGlobalLayout () {
1535+ getRecyclerView ().getViewTreeObserver ().removeOnGlobalLayoutListener (this );
1536+ if (isGrid ) {
1537+ int viewWidth = getRecyclerView ().getMeasuredWidth ();
1538+ int newSpanCount = viewWidth / GRID_ITEM_DEFAULT_WIDTH ;
1539+ RecyclerView .LayoutManager layoutManager = getRecyclerView ().getLayoutManager ();
1540+ if (layoutManager instanceof GridLayoutManager ) {
1541+ if (newSpanCount < 1 ) {
1542+ newSpanCount = DEFAULT_FALLBACK_SPAN_COUNT ;
1543+ }
1544+ ((GridLayoutManager ) layoutManager ).setSpanCount (newSpanCount );
1545+ layoutManager .requestLayout ();
1546+ }
1547+ }
1548+ }
1549+ });
1550+ }
1551+
14771552 public CommonOCFileListAdapterInterface getCommonAdapter () {
14781553 return mAdapter ;
14791554 }
@@ -2031,4 +2106,52 @@ public void setFabEnabled(final boolean enabled) {
20312106 public boolean isEmpty () {
20322107 return mAdapter == null || mAdapter .isEmpty ();
20332108 }
2109+
2110+ @ Override
2111+ public void onConfigurationChanged (@ NonNull Configuration newConfig ) {
2112+ super .onConfigurationChanged (newConfig );
2113+ if (getAdapter () != null ) {
2114+ getAdapter ().notifyDataSetChanged ();
2115+ }
2116+ updateSpanCount (newConfig );
2117+ }
2118+
2119+ /**
2120+ * method will update the span count on basis of device orientation for the file listing
2121+ *
2122+ * @param newConfig current configuration
2123+ */
2124+ private void updateSpanCount (Configuration newConfig ) {
2125+ //this should only run when current view is not media gallery
2126+ if (getAdapter () != null ) {
2127+ int maxColumnSize = (int ) AppPreferencesImpl .DEFAULT_GRID_COLUMN ;
2128+ if (newConfig .orientation == Configuration .ORIENTATION_LANDSCAPE ) {
2129+ //add the divider item decorator when orientation is landscape and device is not tablet
2130+ //because we don't have to add divider again as it is already added
2131+ if (!com .nmc .android .utils .DisplayUtils .isTablet ()) {
2132+ addRemoveRecyclerViewItemDecorator ();
2133+ }
2134+ maxColumnSize = maxColumnSizeLandscape ;
2135+ } else if (newConfig .orientation == Configuration .ORIENTATION_PORTRAIT ) {
2136+ //remove the divider item decorator when orientation is portrait and when device is not tablet
2137+ //because we have to show divider in both landscape and portrait mode
2138+ if (!com .nmc .android .utils .DisplayUtils .isTablet ()) {
2139+ removeItemDecorator ();
2140+ }
2141+ maxColumnSize = (int ) AppPreferencesImpl .DEFAULT_GRID_COLUMN ;
2142+ }
2143+
2144+ if (isGridEnabled ()) {
2145+ //for tablet calculate size on the basis of screen width
2146+ if (com .nmc .android .utils .DisplayUtils .isTablet ()) {
2147+ calculateAndUpdateSpanCount (true );
2148+ } else {
2149+ //and for phones directly show the hardcoded column size
2150+ if (getRecyclerView ().getLayoutManager () instanceof GridLayoutManager ) {
2151+ ((GridLayoutManager ) getRecyclerView ().getLayoutManager ()).setSpanCount (maxColumnSize );
2152+ }
2153+ }
2154+ }
2155+ }
2156+ }
20342157}
0 commit comments