Skip to content

Commit 8d7653c

Browse files
committed
List divider added for tablet and landscape mode.
1 parent f580c64 commit 8d7653c

File tree

11 files changed

+437
-2
lines changed

11 files changed

+437
-2
lines changed
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;
@@ -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

Comments
 (0)