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 ;
@@ -232,6 +237,9 @@ public class OCFileListFragment extends ExtendedListFragment implements
232237 protected String mLimitToMimeType ;
233238 private FloatingActionButton mFabMain ;
234239
240+ private SimpleListItemDividerDecoration simpleListItemDividerDecoration ;
241+ private MediaGridItemDecoration mediaGridItemDecoration ;
242+
235243 @ Inject DeviceInfo deviceInfo ;
236244
237245 protected enum MenuItemAddRemove {
@@ -245,6 +253,13 @@ protected enum MenuItemAddRemove {
245253
246254 private List <MenuItem > mOriginalMenuItems = new ArrayList <>();
247255
256+ private int maxColumnSizeLandscape = 5 ;
257+
258+ //this variable will help us to provide number of span count for grid view
259+ //the width for single item is approx to 360
260+ private static final int GRID_ITEM_DEFAULT_WIDTH = 360 ;
261+ private static final int DEFAULT_FALLBACK_SPAN_COUNT = 1 ;
262+
248263 @ Override
249264 public void onCreate (Bundle savedInstanceState ) {
250265 super .onCreate (savedInstanceState );
@@ -443,6 +458,10 @@ protected void setAdapter(Bundle args) {
443458 viewThemeUtils
444459 );
445460
461+ simpleListItemDividerDecoration = new SimpleListItemDividerDecoration (getContext (), R .drawable .item_divider , true );
462+ int spacing = getResources ().getDimensionPixelSize (R .dimen .media_grid_spacing );
463+ mediaGridItemDecoration = new MediaGridItemDecoration (spacing );
464+
446465 setRecyclerViewAdapter (mAdapter );
447466
448467 fastScrollUtils .applyFastScroll (getRecyclerView ());
@@ -1520,6 +1539,7 @@ public void switchToListView() {
15201539 if (isGridEnabled ()) {
15211540 switchLayoutManager (false );
15221541 }
1542+ addRemoveRecyclerViewItemDecorator ();
15231543 }
15241544
15251545 public void setGridAsPreferred () {
@@ -1531,6 +1551,33 @@ public void switchToGridView() {
15311551 if (!isGridEnabled ()) {
15321552 switchLayoutManager (true );
15331553 }
1554+ addRemoveRecyclerViewItemDecorator ();
1555+ }
1556+
1557+ private void addRemoveRecyclerViewItemDecorator () {
1558+ if (getRecyclerView ().getLayoutManager () instanceof GridLayoutManager ) {
1559+ removeItemDecorator ();
1560+ if (getRecyclerView ().getItemDecorationCount () == 0 ) {
1561+ getRecyclerView ().addItemDecoration (mediaGridItemDecoration );
1562+ int padding = getResources ().getDimensionPixelSize (R .dimen .grid_recyclerview_padding );
1563+ getRecyclerView ().setPadding (padding , padding , padding , padding );
1564+ }
1565+ } else {
1566+ removeItemDecorator ();
1567+ if (getRecyclerView ().getItemDecorationCount () == 0 && com .nmc .android .utils .DisplayUtils .isShowDividerForList ()) {
1568+ getRecyclerView ().addItemDecoration (simpleListItemDividerDecoration );
1569+ getRecyclerView ().setPadding (0 , 0 , 0 , 0 );
1570+ }
1571+ }
1572+ }
1573+
1574+ /**
1575+ * method to remove the item decorator
1576+ */
1577+ private void removeItemDecorator () {
1578+ while (getRecyclerView ().getItemDecorationCount () > 0 ) {
1579+ getRecyclerView ().removeItemDecorationAt (0 );
1580+ }
15341581 }
15351582
15361583 public void switchLayoutManager (boolean grid ) {
@@ -1561,12 +1608,40 @@ public int getSpanSize(int position) {
15611608 }
15621609
15631610 getRecyclerView ().setLayoutManager (layoutManager );
1611+ updateSpanCount (getResources ().getConfiguration ());
15641612 getRecyclerView ().scrollToPosition (position );
15651613 getAdapter ().setGridView (grid );
15661614 getRecyclerView ().setAdapter (getAdapter ());
15671615 getAdapter ().notifyDataSetChanged ();
15681616 }
15691617
1618+ /**
1619+ * method will calculate the number of spans required for grid item and will update the span accordingly
1620+ *
1621+ * @param isGrid
1622+ */
1623+ private void calculateAndUpdateSpanCount (boolean isGrid ) {
1624+ getRecyclerView ().getViewTreeObserver ().addOnGlobalLayoutListener (
1625+ new ViewTreeObserver .OnGlobalLayoutListener () {
1626+ @ Override
1627+ public void onGlobalLayout () {
1628+ getRecyclerView ().getViewTreeObserver ().removeOnGlobalLayoutListener (this );
1629+ if (isGrid ) {
1630+ int viewWidth = getRecyclerView ().getMeasuredWidth ();
1631+ int newSpanCount = viewWidth / GRID_ITEM_DEFAULT_WIDTH ;
1632+ RecyclerView .LayoutManager layoutManager = getRecyclerView ().getLayoutManager ();
1633+ if (layoutManager instanceof GridLayoutManager ) {
1634+ if (newSpanCount < 1 ) {
1635+ newSpanCount = DEFAULT_FALLBACK_SPAN_COUNT ;
1636+ }
1637+ ((GridLayoutManager ) layoutManager ).setSpanCount (newSpanCount );
1638+ layoutManager .requestLayout ();
1639+ }
1640+ }
1641+ }
1642+ });
1643+ }
1644+
15701645 public CommonOCFileListAdapterInterface getCommonAdapter () {
15711646 return mAdapter ;
15721647 }
@@ -2139,4 +2214,52 @@ private boolean isAPKorAAB(Set<OCFile> files) {
21392214 }
21402215 return false ;
21412216 }
2217+
2218+ @ Override
2219+ public void onConfigurationChanged (@ NonNull Configuration newConfig ) {
2220+ super .onConfigurationChanged (newConfig );
2221+ if (getAdapter () != null ) {
2222+ getAdapter ().notifyDataSetChanged ();
2223+ }
2224+ updateSpanCount (newConfig );
2225+ }
2226+
2227+ /**
2228+ * method will update the span count on basis of device orientation for the file listing
2229+ *
2230+ * @param newConfig current configuration
2231+ */
2232+ private void updateSpanCount (Configuration newConfig ) {
2233+ //this should only run when current view is not media gallery
2234+ if (getAdapter () != null ) {
2235+ int maxColumnSize = (int ) AppPreferencesImpl .DEFAULT_GRID_COLUMN ;
2236+ if (newConfig .orientation == Configuration .ORIENTATION_LANDSCAPE ) {
2237+ //add the divider item decorator when orientation is landscape and device is not tablet
2238+ //because we don't have to add divider again as it is already added
2239+ if (!com .nmc .android .utils .DisplayUtils .isTablet ()) {
2240+ addRemoveRecyclerViewItemDecorator ();
2241+ }
2242+ maxColumnSize = maxColumnSizeLandscape ;
2243+ } else if (newConfig .orientation == Configuration .ORIENTATION_PORTRAIT ) {
2244+ //remove the divider item decorator when orientation is portrait and when device is not tablet
2245+ //because we have to show divider in both landscape and portrait mode
2246+ if (!com .nmc .android .utils .DisplayUtils .isTablet ()) {
2247+ removeItemDecorator ();
2248+ }
2249+ maxColumnSize = (int ) AppPreferencesImpl .DEFAULT_GRID_COLUMN ;
2250+ }
2251+
2252+ if (isGridEnabled ()) {
2253+ //for tablet calculate size on the basis of screen width
2254+ if (com .nmc .android .utils .DisplayUtils .isTablet ()) {
2255+ calculateAndUpdateSpanCount (true );
2256+ } else {
2257+ //and for phones directly show the hardcoded column size
2258+ if (getRecyclerView ().getLayoutManager () instanceof GridLayoutManager ) {
2259+ ((GridLayoutManager ) getRecyclerView ().getLayoutManager ()).setSpanCount (maxColumnSize );
2260+ }
2261+ }
2262+ }
2263+ }
2264+ }
21422265}
0 commit comments