1818import android .app .Activity ;
1919import android .content .Context ;
2020import android .content .Intent ;
21+ import android .content .res .Configuration ;
2122import android .os .AsyncTask ;
2223import android .os .Bundle ;
2324import android .os .Handler ;
3233import android .view .MenuItem ;
3334import android .view .View ;
3435import android .view .ViewGroup ;
36+ import android .view .ViewTreeObserver ;
3537import android .widget .AbsListView ;
3638import android .widget .Toast ;
3739
5254import com .nextcloud .client .jobs .BackgroundJobManager ;
5355import com .nextcloud .client .network .ClientFactory ;
5456import com .nextcloud .client .preferences .AppPreferences ;
57+ import com .nextcloud .client .preferences .AppPreferencesImpl ;
5558import com .nextcloud .client .utils .Throttler ;
5659import com .nextcloud .common .NextcloudClient ;
5760import com .nextcloud .ui .fileactions .FileActionsBottomSheet ;
8790import com .owncloud .android .ui .activity .UploadFilesActivity ;
8891import com .owncloud .android .ui .adapter .CommonOCFileListAdapterInterface ;
8992import com .owncloud .android .ui .adapter .OCFileListAdapter ;
93+ import com .owncloud .android .ui .decoration .MediaGridItemDecoration ;
94+ import com .owncloud .android .ui .decoration .SimpleListItemDividerDecoration ;
9095import com .owncloud .android .ui .dialog .ChooseRichDocumentsTemplateDialogFragment ;
9196import com .owncloud .android .ui .dialog .ChooseTemplateDialogFragment ;
9297import com .owncloud .android .ui .dialog .ConfirmationDialogFragment ;
@@ -231,6 +236,9 @@ public class OCFileListFragment extends ExtendedListFragment implements
231236 protected String mLimitToMimeType ;
232237 private FloatingActionButton mFabMain ;
233238
239+ private SimpleListItemDividerDecoration simpleListItemDividerDecoration ;
240+ private MediaGridItemDecoration mediaGridItemDecoration ;
241+
234242 @ Inject DeviceInfo deviceInfo ;
235243
236244 protected enum MenuItemAddRemove {
@@ -244,6 +252,13 @@ protected enum MenuItemAddRemove {
244252
245253 private List <MenuItem > mOriginalMenuItems = new ArrayList <>();
246254
255+ private int maxColumnSizeLandscape = 5 ;
256+
257+ //this variable will help us to provide number of span count for grid view
258+ //the width for single item is approx to 360
259+ private static final int GRID_ITEM_DEFAULT_WIDTH = 360 ;
260+ private static final int DEFAULT_FALLBACK_SPAN_COUNT = 1 ;
261+
247262 @ Override
248263 public void onCreate (Bundle savedInstanceState ) {
249264 super .onCreate (savedInstanceState );
@@ -442,6 +457,10 @@ protected void setAdapter(Bundle args) {
442457 viewThemeUtils
443458 );
444459
460+ simpleListItemDividerDecoration = new SimpleListItemDividerDecoration (getContext (), R .drawable .item_divider , true );
461+ int spacing = getResources ().getDimensionPixelSize (R .dimen .media_grid_spacing );
462+ mediaGridItemDecoration = new MediaGridItemDecoration (spacing );
463+
445464 setRecyclerViewAdapter (mAdapter );
446465
447466 fastScrollUtils .applyFastScroll (getRecyclerView ());
@@ -1514,6 +1533,7 @@ public void switchToListView() {
15141533 if (isGridEnabled ()) {
15151534 switchLayoutManager (false );
15161535 }
1536+ addRemoveRecyclerViewItemDecorator ();
15171537 }
15181538
15191539 public void setGridAsPreferred () {
@@ -1525,6 +1545,33 @@ public void switchToGridView() {
15251545 if (!isGridEnabled ()) {
15261546 switchLayoutManager (true );
15271547 }
1548+ addRemoveRecyclerViewItemDecorator ();
1549+ }
1550+
1551+ private void addRemoveRecyclerViewItemDecorator () {
1552+ if (getRecyclerView ().getLayoutManager () instanceof GridLayoutManager ) {
1553+ removeItemDecorator ();
1554+ if (getRecyclerView ().getItemDecorationCount () == 0 ) {
1555+ getRecyclerView ().addItemDecoration (mediaGridItemDecoration );
1556+ int padding = getResources ().getDimensionPixelSize (R .dimen .grid_recyclerview_padding );
1557+ getRecyclerView ().setPadding (padding , padding , padding , padding );
1558+ }
1559+ } else {
1560+ removeItemDecorator ();
1561+ if (getRecyclerView ().getItemDecorationCount () == 0 && com .nmc .android .utils .DisplayUtils .isShowDividerForList ()) {
1562+ getRecyclerView ().addItemDecoration (simpleListItemDividerDecoration );
1563+ getRecyclerView ().setPadding (0 , 0 , 0 , 0 );
1564+ }
1565+ }
1566+ }
1567+
1568+ /**
1569+ * method to remove the item decorator
1570+ */
1571+ private void removeItemDecorator () {
1572+ while (getRecyclerView ().getItemDecorationCount () > 0 ) {
1573+ getRecyclerView ().removeItemDecorationAt (0 );
1574+ }
15281575 }
15291576
15301577 public void switchLayoutManager (boolean grid ) {
@@ -1555,12 +1602,40 @@ public int getSpanSize(int position) {
15551602 }
15561603
15571604 getRecyclerView ().setLayoutManager (layoutManager );
1605+ updateSpanCount (getResources ().getConfiguration ());
15581606 getRecyclerView ().scrollToPosition (position );
15591607 getAdapter ().setGridView (grid );
15601608 getRecyclerView ().setAdapter (getAdapter ());
15611609 getAdapter ().notifyDataSetChanged ();
15621610 }
15631611
1612+ /**
1613+ * method will calculate the number of spans required for grid item and will update the span accordingly
1614+ *
1615+ * @param isGrid
1616+ */
1617+ private void calculateAndUpdateSpanCount (boolean isGrid ) {
1618+ getRecyclerView ().getViewTreeObserver ().addOnGlobalLayoutListener (
1619+ new ViewTreeObserver .OnGlobalLayoutListener () {
1620+ @ Override
1621+ public void onGlobalLayout () {
1622+ getRecyclerView ().getViewTreeObserver ().removeOnGlobalLayoutListener (this );
1623+ if (isGrid ) {
1624+ int viewWidth = getRecyclerView ().getMeasuredWidth ();
1625+ int newSpanCount = viewWidth / GRID_ITEM_DEFAULT_WIDTH ;
1626+ RecyclerView .LayoutManager layoutManager = getRecyclerView ().getLayoutManager ();
1627+ if (layoutManager instanceof GridLayoutManager ) {
1628+ if (newSpanCount < 1 ) {
1629+ newSpanCount = DEFAULT_FALLBACK_SPAN_COUNT ;
1630+ }
1631+ ((GridLayoutManager ) layoutManager ).setSpanCount (newSpanCount );
1632+ layoutManager .requestLayout ();
1633+ }
1634+ }
1635+ }
1636+ });
1637+ }
1638+
15641639 public CommonOCFileListAdapterInterface getCommonAdapter () {
15651640 return mAdapter ;
15661641 }
@@ -2130,4 +2205,52 @@ private boolean isAPKorAAB(Set<OCFile> files) {
21302205 }
21312206 return false ;
21322207 }
2208+
2209+ @ Override
2210+ public void onConfigurationChanged (@ NonNull Configuration newConfig ) {
2211+ super .onConfigurationChanged (newConfig );
2212+ if (getAdapter () != null ) {
2213+ getAdapter ().notifyDataSetChanged ();
2214+ }
2215+ updateSpanCount (newConfig );
2216+ }
2217+
2218+ /**
2219+ * method will update the span count on basis of device orientation for the file listing
2220+ *
2221+ * @param newConfig current configuration
2222+ */
2223+ private void updateSpanCount (Configuration newConfig ) {
2224+ //this should only run when current view is not media gallery
2225+ if (getAdapter () != null ) {
2226+ int maxColumnSize = (int ) AppPreferencesImpl .DEFAULT_GRID_COLUMN ;
2227+ if (newConfig .orientation == Configuration .ORIENTATION_LANDSCAPE ) {
2228+ //add the divider item decorator when orientation is landscape and device is not tablet
2229+ //because we don't have to add divider again as it is already added
2230+ if (!com .nmc .android .utils .DisplayUtils .isTablet ()) {
2231+ addRemoveRecyclerViewItemDecorator ();
2232+ }
2233+ maxColumnSize = maxColumnSizeLandscape ;
2234+ } else if (newConfig .orientation == Configuration .ORIENTATION_PORTRAIT ) {
2235+ //remove the divider item decorator when orientation is portrait and when device is not tablet
2236+ //because we have to show divider in both landscape and portrait mode
2237+ if (!com .nmc .android .utils .DisplayUtils .isTablet ()) {
2238+ removeItemDecorator ();
2239+ }
2240+ maxColumnSize = (int ) AppPreferencesImpl .DEFAULT_GRID_COLUMN ;
2241+ }
2242+
2243+ if (isGridEnabled ()) {
2244+ //for tablet calculate size on the basis of screen width
2245+ if (com .nmc .android .utils .DisplayUtils .isTablet ()) {
2246+ calculateAndUpdateSpanCount (true );
2247+ } else {
2248+ //and for phones directly show the hardcoded column size
2249+ if (getRecyclerView ().getLayoutManager () instanceof GridLayoutManager ) {
2250+ ((GridLayoutManager ) getRecyclerView ().getLayoutManager ()).setSpanCount (maxColumnSize );
2251+ }
2252+ }
2253+ }
2254+ }
2255+ }
21332256}
0 commit comments