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