1919import android .app .Activity ;
2020import android .content .Context ;
2121import android .content .Intent ;
22+ import android .content .res .Configuration ;
2223import android .os .AsyncTask ;
2324import android .os .Bundle ;
2425import android .os .Handler ;
3334import android .view .MenuItem ;
3435import android .view .View ;
3536import android .view .ViewGroup ;
37+ import android .view .ViewTreeObserver ;
3638import android .widget .AbsListView ;
3739import android .widget .Toast ;
3840
5153import com .nextcloud .client .editimage .EditImageActivity ;
5254import com .nextcloud .client .jobs .BackgroundJobManager ;
5355import com .nextcloud .client .network .ClientFactory ;
56+ import com .nextcloud .client .preferences .AppPreferencesImpl ;
5457import com .nextcloud .client .utils .Throttler ;
5558import com .nextcloud .common .NextcloudClient ;
5659import com .nextcloud .ui .fileactions .FileActionsBottomSheet ;
8891import com .owncloud .android .ui .activity .UploadFilesActivity ;
8992import com .owncloud .android .ui .adapter .CommonOCFileListAdapterInterface ;
9093import com .owncloud .android .ui .adapter .OCFileListAdapter ;
94+ import com .owncloud .android .ui .decoration .MediaGridItemDecoration ;
95+ import com .owncloud .android .ui .decoration .SimpleListItemDividerDecoration ;
9196import com .owncloud .android .ui .dialog .ChooseRichDocumentsTemplateDialogFragment ;
9297import com .owncloud .android .ui .dialog .ChooseTemplateDialogFragment ;
9398import com .owncloud .android .ui .dialog .ConfirmationDialogFragment ;
@@ -233,6 +238,9 @@ public class OCFileListFragment extends ExtendedListFragment implements
233238 protected String mLimitToMimeType ;
234239 private FloatingActionButton mFabMain ;
235240
241+ private SimpleListItemDividerDecoration simpleListItemDividerDecoration ;
242+ private MediaGridItemDecoration mediaGridItemDecoration ;
243+
236244 @ Inject DeviceInfo deviceInfo ;
237245
238246 protected enum MenuItemAddRemove {
@@ -246,6 +254,13 @@ protected enum MenuItemAddRemove {
246254
247255 private List <MenuItem > mOriginalMenuItems = new ArrayList <>();
248256
257+ private int maxColumnSizeLandscape = 5 ;
258+
259+ //this variable will help us to provide number of span count for grid view
260+ //the width for single item is approx to 360
261+ private static final int GRID_ITEM_DEFAULT_WIDTH = 360 ;
262+ private static final int DEFAULT_FALLBACK_SPAN_COUNT = 1 ;
263+
249264 @ Override
250265 public void onCreate (Bundle savedInstanceState ) {
251266 super .onCreate (savedInstanceState );
@@ -459,6 +474,10 @@ protected void setAdapter(Bundle args) {
459474 viewThemeUtils
460475 );
461476
477+ simpleListItemDividerDecoration = new SimpleListItemDividerDecoration (getContext (), R .drawable .item_divider , true );
478+ int spacing = getResources ().getDimensionPixelSize (R .dimen .media_grid_spacing );
479+ mediaGridItemDecoration = new MediaGridItemDecoration (spacing );
480+
462481 setRecyclerViewAdapter (mAdapter );
463482
464483 fastScrollUtils .applyFastScroll (getRecyclerView ());
@@ -1649,6 +1668,7 @@ public void switchToListView() {
16491668 if (isGridEnabled ()) {
16501669 switchLayoutManager (false );
16511670 }
1671+ addRemoveRecyclerViewItemDecorator ();
16521672 }
16531673
16541674 public void setGridAsPreferred () {
@@ -1660,6 +1680,33 @@ public void switchToGridView() {
16601680 if (!isGridEnabled ()) {
16611681 switchLayoutManager (true );
16621682 }
1683+ addRemoveRecyclerViewItemDecorator ();
1684+ }
1685+
1686+ private void addRemoveRecyclerViewItemDecorator () {
1687+ if (getRecyclerView ().getLayoutManager () instanceof GridLayoutManager ) {
1688+ removeItemDecorator ();
1689+ if (getRecyclerView ().getItemDecorationCount () == 0 ) {
1690+ getRecyclerView ().addItemDecoration (mediaGridItemDecoration );
1691+ int padding = getResources ().getDimensionPixelSize (R .dimen .grid_recyclerview_padding );
1692+ getRecyclerView ().setPadding (padding , padding , padding , padding );
1693+ }
1694+ } else {
1695+ removeItemDecorator ();
1696+ if (getRecyclerView ().getItemDecorationCount () == 0 && com .nmc .android .utils .DisplayUtils .isShowDividerForList ()) {
1697+ getRecyclerView ().addItemDecoration (simpleListItemDividerDecoration );
1698+ getRecyclerView ().setPadding (0 , 0 , 0 , 0 );
1699+ }
1700+ }
1701+ }
1702+
1703+ /**
1704+ * method to remove the item decorator
1705+ */
1706+ private void removeItemDecorator () {
1707+ while (getRecyclerView ().getItemDecorationCount () > 0 ) {
1708+ getRecyclerView ().removeItemDecorationAt (0 );
1709+ }
16631710 }
16641711
16651712 public void switchLayoutManager (boolean grid ) {
@@ -1690,12 +1737,40 @@ public int getSpanSize(int position) {
16901737 }
16911738
16921739 getRecyclerView ().setLayoutManager (layoutManager );
1740+ updateSpanCount (getResources ().getConfiguration ());
16931741 getRecyclerView ().scrollToPosition (position );
16941742 getAdapter ().setGridView (grid );
16951743 getRecyclerView ().setAdapter (getAdapter ());
16961744 getAdapter ().notifyDataSetChanged ();
16971745 }
16981746
1747+ /**
1748+ * method will calculate the number of spans required for grid item and will update the span accordingly
1749+ *
1750+ * @param isGrid
1751+ */
1752+ private void calculateAndUpdateSpanCount (boolean isGrid ) {
1753+ getRecyclerView ().getViewTreeObserver ().addOnGlobalLayoutListener (
1754+ new ViewTreeObserver .OnGlobalLayoutListener () {
1755+ @ Override
1756+ public void onGlobalLayout () {
1757+ getRecyclerView ().getViewTreeObserver ().removeOnGlobalLayoutListener (this );
1758+ if (isGrid ) {
1759+ int viewWidth = getRecyclerView ().getMeasuredWidth ();
1760+ int newSpanCount = viewWidth / GRID_ITEM_DEFAULT_WIDTH ;
1761+ RecyclerView .LayoutManager layoutManager = getRecyclerView ().getLayoutManager ();
1762+ if (layoutManager instanceof GridLayoutManager ) {
1763+ if (newSpanCount < 1 ) {
1764+ newSpanCount = DEFAULT_FALLBACK_SPAN_COUNT ;
1765+ }
1766+ ((GridLayoutManager ) layoutManager ).setSpanCount (newSpanCount );
1767+ layoutManager .requestLayout ();
1768+ }
1769+ }
1770+ }
1771+ });
1772+ }
1773+
16991774 public CommonOCFileListAdapterInterface getCommonAdapter () {
17001775 return mAdapter ;
17011776 }
@@ -2275,4 +2350,52 @@ private boolean isAPKorAAB(Set<OCFile> files) {
22752350 }
22762351 return false ;
22772352 }
2353+
2354+ @ Override
2355+ public void onConfigurationChanged (@ NonNull Configuration newConfig ) {
2356+ super .onConfigurationChanged (newConfig );
2357+ if (getAdapter () != null ) {
2358+ getAdapter ().notifyDataSetChanged ();
2359+ }
2360+ updateSpanCount (newConfig );
2361+ }
2362+
2363+ /**
2364+ * method will update the span count on basis of device orientation for the file listing
2365+ *
2366+ * @param newConfig current configuration
2367+ */
2368+ private void updateSpanCount (Configuration newConfig ) {
2369+ //this should only run when current view is not media gallery
2370+ if (getAdapter () != null ) {
2371+ int maxColumnSize = (int ) AppPreferencesImpl .DEFAULT_GRID_COLUMN ;
2372+ if (newConfig .orientation == Configuration .ORIENTATION_LANDSCAPE ) {
2373+ //add the divider item decorator when orientation is landscape and device is not tablet
2374+ //because we don't have to add divider again as it is already added
2375+ if (!com .nmc .android .utils .DisplayUtils .isTablet ()) {
2376+ addRemoveRecyclerViewItemDecorator ();
2377+ }
2378+ maxColumnSize = maxColumnSizeLandscape ;
2379+ } else if (newConfig .orientation == Configuration .ORIENTATION_PORTRAIT ) {
2380+ //remove the divider item decorator when orientation is portrait and when device is not tablet
2381+ //because we have to show divider in both landscape and portrait mode
2382+ if (!com .nmc .android .utils .DisplayUtils .isTablet ()) {
2383+ removeItemDecorator ();
2384+ }
2385+ maxColumnSize = (int ) AppPreferencesImpl .DEFAULT_GRID_COLUMN ;
2386+ }
2387+
2388+ if (isGridEnabled ()) {
2389+ //for tablet calculate size on the basis of screen width
2390+ if (com .nmc .android .utils .DisplayUtils .isTablet ()) {
2391+ calculateAndUpdateSpanCount (true );
2392+ } else {
2393+ //and for phones directly show the hardcoded column size
2394+ if (getRecyclerView ().getLayoutManager () instanceof GridLayoutManager ) {
2395+ ((GridLayoutManager ) getRecyclerView ().getLayoutManager ()).setSpanCount (maxColumnSize );
2396+ }
2397+ }
2398+ }
2399+ }
2400+ }
22782401}
0 commit comments