Skip to content

Commit a9f2f1f

Browse files
committed
List divider added for tablet and landscape mode.
1 parent e625f45 commit a9f2f1f

11 files changed

Lines changed: 437 additions & 2 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.nmc.android.utils
2+
3+
import android.content.res.Configuration
4+
import com.owncloud.android.MainApp
5+
import com.owncloud.android.R
6+
7+
object DisplayUtils {
8+
9+
@JvmStatic
10+
fun isShowDividerForList(): Boolean = isTablet() || isLandscapeOrientation()
11+
12+
@JvmStatic
13+
fun isTablet(): Boolean = MainApp.getAppContext().resources.getBoolean(R.bool.isTablet)
14+
15+
@JvmStatic
16+
fun isLandscapeOrientation(): Boolean =
17+
MainApp.getAppContext().resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE
18+
}

app/src/main/java/com/owncloud/android/ui/activity/UploadListActivity.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import android.content.Intent;
3131
import android.content.IntentFilter;
3232
import android.content.ServiceConnection;
33+
import android.content.res.Configuration;
3334
import android.os.Bundle;
3435
import android.os.IBinder;
3536
import android.view.Menu;
@@ -58,11 +59,13 @@
5859
import com.owncloud.android.ui.adapter.UploadListAdapter;
5960
import com.owncloud.android.ui.decoration.MediaGridItemDecoration;
6061
import com.owncloud.android.utils.DisplayUtils;
62+
import com.owncloud.android.ui.decoration.SimpleListItemDividerDecoration;
6163
import com.owncloud.android.utils.FilesSyncHelper;
6264
import com.owncloud.android.utils.theme.ViewThemeUtils;
6365

6466
import javax.inject.Inject;
6567

68+
import androidx.annotation.NonNull;
6669
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
6770
import androidx.recyclerview.widget.GridLayoutManager;
6871
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
@@ -110,6 +113,8 @@ public class UploadListActivity extends FileActivity {
110113

111114
private UploadListLayoutBinding binding;
112115

116+
private SimpleListItemDividerDecoration simpleListItemDividerDecoration;
117+
113118
public static Intent createIntent(OCFile file, User user, Integer flag, Context context) {
114119
Intent intent = new Intent(context, UploadListActivity.class);
115120
if (flag != null) {
@@ -174,6 +179,8 @@ private void setupContent() {
174179
int spacing = getResources().getDimensionPixelSize(R.dimen.media_grid_spacing);
175180
binding.list.addItemDecoration(new MediaGridItemDecoration(spacing));
176181
binding.list.setLayoutManager(lm);
182+
simpleListItemDividerDecoration = new SimpleListItemDividerDecoration(this, R.drawable.item_divider, true);
183+
addListItemDecorator();
177184
binding.list.setAdapter(uploadListAdapter);
178185

179186
viewThemeUtils.androidx.themeSwipeRefreshLayout(swipeListRefreshLayout);
@@ -182,6 +189,23 @@ private void setupContent() {
182189
loadItems();
183190
}
184191

192+
private void addListItemDecorator() {
193+
if (com.nmc.android.utils.DisplayUtils.isShowDividerForList()) {
194+
//check and remove divider item decorator if exist then add item decorator
195+
removeListDividerDecorator();
196+
binding.list.addItemDecoration(simpleListItemDividerDecoration);
197+
}
198+
}
199+
200+
/**
201+
* method to remove the divider item decorator
202+
*/
203+
private void removeListDividerDecorator() {
204+
if (binding.list.getItemDecorationCount() > 0) {
205+
binding.list.removeItemDecoration(simpleListItemDividerDecoration);
206+
}
207+
}
208+
185209
private void loadItems() {
186210
uploadListAdapter.loadUploadItemsFromDb();
187211

@@ -372,4 +396,20 @@ public void onReceive(Context context, Intent intent) {
372396
});
373397
}
374398
}
399+
400+
@Override
401+
public void onConfigurationChanged(@NonNull Configuration newConfig) {
402+
super.onConfigurationChanged(newConfig);
403+
//this should only run when device is not tablet because we are adding dividers in tablet for both the
404+
// orientations
405+
if (!com.nmc.android.utils.DisplayUtils.isTablet()) {
406+
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
407+
//add the divider item decorator when orientation is landscape
408+
addListItemDecorator();
409+
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
410+
//remove the divider item decorator when orientation is portrait
411+
removeListDividerDecorator();
412+
}
413+
}
414+
}
375415
}

app/src/main/java/com/owncloud/android/ui/decoration/SimpleListItemDividerDecoration.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import android.util.DisplayMetrics;
2929
import android.view.View;
3030

31+
import androidx.core.content.ContextCompat;
3132
import androidx.recyclerview.widget.DividerItemDecoration;
3233
import androidx.recyclerview.widget.RecyclerView;
3334

@@ -39,7 +40,8 @@ public class SimpleListItemDividerDecoration extends DividerItemDecoration {
3940

4041
private final Rect bounds = new Rect();
4142
private Drawable divider;
42-
private int leftPadding;
43+
private int leftPadding = 0;
44+
private boolean hasFooter;
4345

4446
/**
4547
* Default divider will be used
@@ -52,6 +54,17 @@ public SimpleListItemDividerDecoration(Context context) {
5254
styledAttributes.recycle();
5355
}
5456

57+
/**
58+
* Custom divider will be used
59+
*
60+
* @param hasFooter if recyclerview has footer and no divider should be shown for footer then pass true else false
61+
*/
62+
public SimpleListItemDividerDecoration(Context context, int resId, boolean hasFooter) {
63+
super(context, DividerItemDecoration.VERTICAL);
64+
this.hasFooter = hasFooter;
65+
divider = ContextCompat.getDrawable(context, resId);
66+
}
67+
5568
@Override
5669
public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
5770
canvas.save();
@@ -65,7 +78,12 @@ public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state)
6578
right = parent.getWidth();
6679
}
6780

68-
final int childCount = parent.getChildCount();
81+
int childCount = parent.getChildCount();
82+
83+
if (hasFooter) {
84+
childCount = childCount - 1;
85+
}
86+
6987
for (int i = 0; i < childCount; i++) {
7088
final View child = parent.getChildAt(i);
7189
parent.getDecoratedBoundsWithMargins(child, bounds);

app/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import android.app.Activity;
3030
import android.content.Context;
3131
import android.content.Intent;
32+
import android.content.res.Configuration;
3233
import android.os.AsyncTask;
3334
import android.os.Bundle;
3435
import android.os.Handler;
@@ -43,6 +44,7 @@
4344
import android.view.MenuItem;
4445
import android.view.View;
4546
import android.view.ViewGroup;
47+
import android.view.ViewTreeObserver;
4648
import android.widget.AbsListView;
4749
import android.widget.Toast;
4850

@@ -62,6 +64,7 @@
6264
import com.nextcloud.client.jobs.BackgroundJobManager;
6365
import com.nextcloud.client.network.ClientFactory;
6466
import com.nextcloud.client.preferences.AppPreferences;
67+
import com.nextcloud.client.preferences.AppPreferencesImpl;
6568
import com.nextcloud.client.utils.Throttler;
6669
import com.nextcloud.common.NextcloudClient;
6770
import com.nextcloud.ui.fileactions.FileActionsBottomSheet;
@@ -95,6 +98,8 @@
9598
import com.owncloud.android.ui.activity.UploadFilesActivity;
9699
import com.owncloud.android.ui.adapter.CommonOCFileListAdapterInterface;
97100
import com.owncloud.android.ui.adapter.OCFileListAdapter;
101+
import com.owncloud.android.ui.decoration.MediaGridItemDecoration;
102+
import com.owncloud.android.ui.decoration.SimpleListItemDividerDecoration;
98103
import com.owncloud.android.ui.dialog.ChooseRichDocumentsTemplateDialogFragment;
99104
import com.owncloud.android.ui.dialog.ChooseTemplateDialogFragment;
100105
import 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

Comments
 (0)