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 ;
@@ -237,6 +242,9 @@ public class OCFileListFragment extends ExtendedListFragment implements
237242 protected String mLimitToMimeType ;
238243 private FloatingActionButton mFabMain ;
239244
245+ private SimpleListItemDividerDecoration simpleListItemDividerDecoration ;
246+ private MediaGridItemDecoration mediaGridItemDecoration ;
247+
240248 @ Inject DeviceInfo deviceInfo ;
241249
242250 protected enum MenuItemAddRemove {
@@ -250,6 +258,13 @@ protected enum MenuItemAddRemove {
250258
251259 private List <MenuItem > mOriginalMenuItems = new ArrayList <>();
252260
261+ private int maxColumnSizeLandscape = 5 ;
262+
263+ //this variable will help us to provide number of span count for grid view
264+ //the width for single item is approx to 360
265+ private static final int GRID_ITEM_DEFAULT_WIDTH = 360 ;
266+ private static final int DEFAULT_FALLBACK_SPAN_COUNT = 1 ;
267+
253268 @ Override
254269 public void onCreate (Bundle savedInstanceState ) {
255270 super .onCreate (savedInstanceState );
@@ -448,6 +463,10 @@ protected void setAdapter(Bundle args) {
448463 viewThemeUtils
449464 );
450465
466+ simpleListItemDividerDecoration = new SimpleListItemDividerDecoration (getContext (), R .drawable .item_divider , true );
467+ int spacing = getResources ().getDimensionPixelSize (R .dimen .media_grid_spacing );
468+ mediaGridItemDecoration = new MediaGridItemDecoration (spacing );
469+
451470 setRecyclerViewAdapter (mAdapter );
452471
453472 fastScrollUtils .applyFastScroll (getRecyclerView ());
@@ -1430,6 +1449,7 @@ public void switchToListView() {
14301449 if (isGridEnabled ()) {
14311450 switchLayoutManager (false );
14321451 }
1452+ addRemoveRecyclerViewItemDecorator ();
14331453 }
14341454
14351455 public void setGridAsPreferred () {
@@ -1441,6 +1461,33 @@ public void switchToGridView() {
14411461 if (!isGridEnabled ()) {
14421462 switchLayoutManager (true );
14431463 }
1464+ addRemoveRecyclerViewItemDecorator ();
1465+ }
1466+
1467+ private void addRemoveRecyclerViewItemDecorator () {
1468+ if (getRecyclerView ().getLayoutManager () instanceof GridLayoutManager ) {
1469+ removeItemDecorator ();
1470+ if (getRecyclerView ().getItemDecorationCount () == 0 ) {
1471+ getRecyclerView ().addItemDecoration (mediaGridItemDecoration );
1472+ int padding = getResources ().getDimensionPixelSize (R .dimen .grid_recyclerview_padding );
1473+ getRecyclerView ().setPadding (padding , padding , padding , padding );
1474+ }
1475+ } else {
1476+ removeItemDecorator ();
1477+ if (getRecyclerView ().getItemDecorationCount () == 0 && com .nmc .android .utils .DisplayUtils .isShowDividerForList ()) {
1478+ getRecyclerView ().addItemDecoration (simpleListItemDividerDecoration );
1479+ getRecyclerView ().setPadding (0 , 0 , 0 , 0 );
1480+ }
1481+ }
1482+ }
1483+
1484+ /**
1485+ * method to remove the item decorator
1486+ */
1487+ private void removeItemDecorator () {
1488+ while (getRecyclerView ().getItemDecorationCount () > 0 ) {
1489+ getRecyclerView ().removeItemDecorationAt (0 );
1490+ }
14441491 }
14451492
14461493 public void switchLayoutManager (boolean grid ) {
@@ -1471,12 +1518,40 @@ public int getSpanSize(int position) {
14711518 }
14721519
14731520 getRecyclerView ().setLayoutManager (layoutManager );
1521+ updateSpanCount (getResources ().getConfiguration ());
14741522 getRecyclerView ().scrollToPosition (position );
14751523 getAdapter ().setGridView (grid );
14761524 getRecyclerView ().setAdapter (getAdapter ());
14771525 getAdapter ().notifyDataSetChanged ();
14781526 }
14791527
1528+ /**
1529+ * method will calculate the number of spans required for grid item and will update the span accordingly
1530+ *
1531+ * @param isGrid
1532+ */
1533+ private void calculateAndUpdateSpanCount (boolean isGrid ) {
1534+ getRecyclerView ().getViewTreeObserver ().addOnGlobalLayoutListener (
1535+ new ViewTreeObserver .OnGlobalLayoutListener () {
1536+ @ Override
1537+ public void onGlobalLayout () {
1538+ getRecyclerView ().getViewTreeObserver ().removeOnGlobalLayoutListener (this );
1539+ if (isGrid ) {
1540+ int viewWidth = getRecyclerView ().getMeasuredWidth ();
1541+ int newSpanCount = viewWidth / GRID_ITEM_DEFAULT_WIDTH ;
1542+ RecyclerView .LayoutManager layoutManager = getRecyclerView ().getLayoutManager ();
1543+ if (layoutManager instanceof GridLayoutManager ) {
1544+ if (newSpanCount < 1 ) {
1545+ newSpanCount = DEFAULT_FALLBACK_SPAN_COUNT ;
1546+ }
1547+ ((GridLayoutManager ) layoutManager ).setSpanCount (newSpanCount );
1548+ layoutManager .requestLayout ();
1549+ }
1550+ }
1551+ }
1552+ });
1553+ }
1554+
14801555 public CommonOCFileListAdapterInterface getCommonAdapter () {
14811556 return mAdapter ;
14821557 }
@@ -2033,4 +2108,52 @@ public void setFabEnabled(final boolean enabled) {
20332108 public boolean isEmpty () {
20342109 return mAdapter == null || mAdapter .isEmpty ();
20352110 }
2111+
2112+ @ Override
2113+ public void onConfigurationChanged (@ NonNull Configuration newConfig ) {
2114+ super .onConfigurationChanged (newConfig );
2115+ if (getAdapter () != null ) {
2116+ getAdapter ().notifyDataSetChanged ();
2117+ }
2118+ updateSpanCount (newConfig );
2119+ }
2120+
2121+ /**
2122+ * method will update the span count on basis of device orientation for the file listing
2123+ *
2124+ * @param newConfig current configuration
2125+ */
2126+ private void updateSpanCount (Configuration newConfig ) {
2127+ //this should only run when current view is not media gallery
2128+ if (getAdapter () != null ) {
2129+ int maxColumnSize = (int ) AppPreferencesImpl .DEFAULT_GRID_COLUMN ;
2130+ if (newConfig .orientation == Configuration .ORIENTATION_LANDSCAPE ) {
2131+ //add the divider item decorator when orientation is landscape and device is not tablet
2132+ //because we don't have to add divider again as it is already added
2133+ if (!com .nmc .android .utils .DisplayUtils .isTablet ()) {
2134+ addRemoveRecyclerViewItemDecorator ();
2135+ }
2136+ maxColumnSize = maxColumnSizeLandscape ;
2137+ } else if (newConfig .orientation == Configuration .ORIENTATION_PORTRAIT ) {
2138+ //remove the divider item decorator when orientation is portrait and when device is not tablet
2139+ //because we have to show divider in both landscape and portrait mode
2140+ if (!com .nmc .android .utils .DisplayUtils .isTablet ()) {
2141+ removeItemDecorator ();
2142+ }
2143+ maxColumnSize = (int ) AppPreferencesImpl .DEFAULT_GRID_COLUMN ;
2144+ }
2145+
2146+ if (isGridEnabled ()) {
2147+ //for tablet calculate size on the basis of screen width
2148+ if (com .nmc .android .utils .DisplayUtils .isTablet ()) {
2149+ calculateAndUpdateSpanCount (true );
2150+ } else {
2151+ //and for phones directly show the hardcoded column size
2152+ if (getRecyclerView ().getLayoutManager () instanceof GridLayoutManager ) {
2153+ ((GridLayoutManager ) getRecyclerView ().getLayoutManager ()).setSpanCount (maxColumnSize );
2154+ }
2155+ }
2156+ }
2157+ }
2158+ }
20362159}
0 commit comments