Skip to content

Commit

Permalink
List divider added for tablet and landscape mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
surinder-tsys committed Jan 10, 2024
1 parent e625f45 commit a9f2f1f
Show file tree
Hide file tree
Showing 11 changed files with 437 additions and 2 deletions.
18 changes: 18 additions & 0 deletions app/src/main/java/com/nmc/android/utils/DisplayUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.nmc.android.utils

import android.content.res.Configuration
import com.owncloud.android.MainApp
import com.owncloud.android.R

object DisplayUtils {

@JvmStatic
fun isShowDividerForList(): Boolean = isTablet() || isLandscapeOrientation()

@JvmStatic
fun isTablet(): Boolean = MainApp.getAppContext().resources.getBoolean(R.bool.isTablet)

@JvmStatic
fun isLandscapeOrientation(): Boolean =
MainApp.getAppContext().resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
Expand Down Expand Up @@ -58,11 +59,13 @@
import com.owncloud.android.ui.adapter.UploadListAdapter;
import com.owncloud.android.ui.decoration.MediaGridItemDecoration;
import com.owncloud.android.utils.DisplayUtils;
import com.owncloud.android.ui.decoration.SimpleListItemDividerDecoration;
import com.owncloud.android.utils.FilesSyncHelper;
import com.owncloud.android.utils.theme.ViewThemeUtils;

import javax.inject.Inject;

import androidx.annotation.NonNull;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
Expand Down Expand Up @@ -110,6 +113,8 @@ public class UploadListActivity extends FileActivity {

private UploadListLayoutBinding binding;

private SimpleListItemDividerDecoration simpleListItemDividerDecoration;

public static Intent createIntent(OCFile file, User user, Integer flag, Context context) {
Intent intent = new Intent(context, UploadListActivity.class);
if (flag != null) {
Expand Down Expand Up @@ -174,6 +179,8 @@ private void setupContent() {
int spacing = getResources().getDimensionPixelSize(R.dimen.media_grid_spacing);
binding.list.addItemDecoration(new MediaGridItemDecoration(spacing));
binding.list.setLayoutManager(lm);
simpleListItemDividerDecoration = new SimpleListItemDividerDecoration(this, R.drawable.item_divider, true);
addListItemDecorator();
binding.list.setAdapter(uploadListAdapter);

viewThemeUtils.androidx.themeSwipeRefreshLayout(swipeListRefreshLayout);
Expand All @@ -182,6 +189,23 @@ private void setupContent() {
loadItems();
}

private void addListItemDecorator() {
if (com.nmc.android.utils.DisplayUtils.isShowDividerForList()) {
//check and remove divider item decorator if exist then add item decorator
removeListDividerDecorator();
binding.list.addItemDecoration(simpleListItemDividerDecoration);
}
}

/**
* method to remove the divider item decorator
*/
private void removeListDividerDecorator() {
if (binding.list.getItemDecorationCount() > 0) {
binding.list.removeItemDecoration(simpleListItemDividerDecoration);
}
}

private void loadItems() {
uploadListAdapter.loadUploadItemsFromDb();

Expand Down Expand Up @@ -372,4 +396,20 @@ public void onReceive(Context context, Intent intent) {
});
}
}

@Override
public void onConfigurationChanged(@NonNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
//this should only run when device is not tablet because we are adding dividers in tablet for both the
// orientations
if (!com.nmc.android.utils.DisplayUtils.isTablet()) {
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
//add the divider item decorator when orientation is landscape
addListItemDecorator();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
//remove the divider item decorator when orientation is portrait
removeListDividerDecorator();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import android.util.DisplayMetrics;
import android.view.View;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.RecyclerView;

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

private final Rect bounds = new Rect();
private Drawable divider;
private int leftPadding;
private int leftPadding = 0;
private boolean hasFooter;

/**
* Default divider will be used
Expand All @@ -52,6 +54,17 @@ public SimpleListItemDividerDecoration(Context context) {
styledAttributes.recycle();
}

/**
* Custom divider will be used
*
* @param hasFooter if recyclerview has footer and no divider should be shown for footer then pass true else false
*/
public SimpleListItemDividerDecoration(Context context, int resId, boolean hasFooter) {
super(context, DividerItemDecoration.VERTICAL);
this.hasFooter = hasFooter;
divider = ContextCompat.getDrawable(context, resId);
}

@Override
public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
canvas.save();
Expand All @@ -65,7 +78,12 @@ public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state)
right = parent.getWidth();
}

final int childCount = parent.getChildCount();
int childCount = parent.getChildCount();

if (hasFooter) {
childCount = childCount - 1;
}

for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
parent.getDecoratedBoundsWithMargins(child, bounds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
Expand All @@ -43,6 +44,7 @@
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.AbsListView;
import android.widget.Toast;

Expand All @@ -62,6 +64,7 @@
import com.nextcloud.client.jobs.BackgroundJobManager;
import com.nextcloud.client.network.ClientFactory;
import com.nextcloud.client.preferences.AppPreferences;
import com.nextcloud.client.preferences.AppPreferencesImpl;
import com.nextcloud.client.utils.Throttler;
import com.nextcloud.common.NextcloudClient;
import com.nextcloud.ui.fileactions.FileActionsBottomSheet;
Expand Down Expand Up @@ -95,6 +98,8 @@
import com.owncloud.android.ui.activity.UploadFilesActivity;
import com.owncloud.android.ui.adapter.CommonOCFileListAdapterInterface;
import com.owncloud.android.ui.adapter.OCFileListAdapter;
import com.owncloud.android.ui.decoration.MediaGridItemDecoration;
import com.owncloud.android.ui.decoration.SimpleListItemDividerDecoration;
import com.owncloud.android.ui.dialog.ChooseRichDocumentsTemplateDialogFragment;
import com.owncloud.android.ui.dialog.ChooseTemplateDialogFragment;
import com.owncloud.android.ui.dialog.ConfirmationDialogFragment;
Expand Down Expand Up @@ -237,6 +242,9 @@ public class OCFileListFragment extends ExtendedListFragment implements
protected String mLimitToMimeType;
private FloatingActionButton mFabMain;

private SimpleListItemDividerDecoration simpleListItemDividerDecoration;
private MediaGridItemDecoration mediaGridItemDecoration;

@Inject DeviceInfo deviceInfo;

protected enum MenuItemAddRemove {
Expand All @@ -250,6 +258,13 @@ protected enum MenuItemAddRemove {

private List<MenuItem> mOriginalMenuItems = new ArrayList<>();

private int maxColumnSizeLandscape = 5;

//this variable will help us to provide number of span count for grid view
//the width for single item is approx to 360
private static final int GRID_ITEM_DEFAULT_WIDTH = 360;
private static final int DEFAULT_FALLBACK_SPAN_COUNT = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -448,6 +463,10 @@ protected void setAdapter(Bundle args) {
viewThemeUtils
);

simpleListItemDividerDecoration = new SimpleListItemDividerDecoration(getContext(), R.drawable.item_divider, true);
int spacing = getResources().getDimensionPixelSize(R.dimen.media_grid_spacing);
mediaGridItemDecoration = new MediaGridItemDecoration(spacing);

setRecyclerViewAdapter(mAdapter);

fastScrollUtils.applyFastScroll(getRecyclerView());
Expand Down Expand Up @@ -1430,6 +1449,7 @@ public void switchToListView() {
if (isGridEnabled()) {
switchLayoutManager(false);
}
addRemoveRecyclerViewItemDecorator();
}

public void setGridAsPreferred() {
Expand All @@ -1441,6 +1461,33 @@ public void switchToGridView() {
if (!isGridEnabled()) {
switchLayoutManager(true);
}
addRemoveRecyclerViewItemDecorator();
}

private void addRemoveRecyclerViewItemDecorator() {
if (getRecyclerView().getLayoutManager() instanceof GridLayoutManager) {
removeItemDecorator();
if (getRecyclerView().getItemDecorationCount() == 0) {
getRecyclerView().addItemDecoration(mediaGridItemDecoration);
int padding = getResources().getDimensionPixelSize(R.dimen.grid_recyclerview_padding);
getRecyclerView().setPadding(padding, padding, padding, padding);
}
} else {
removeItemDecorator();
if (getRecyclerView().getItemDecorationCount() == 0 && com.nmc.android.utils.DisplayUtils.isShowDividerForList()) {
getRecyclerView().addItemDecoration(simpleListItemDividerDecoration);
getRecyclerView().setPadding(0, 0, 0, 0);
}
}
}

/**
* method to remove the item decorator
*/
private void removeItemDecorator() {
while (getRecyclerView().getItemDecorationCount() > 0) {
getRecyclerView().removeItemDecorationAt(0);
}
}

public void switchLayoutManager(boolean grid) {
Expand Down Expand Up @@ -1471,12 +1518,40 @@ public int getSpanSize(int position) {
}

getRecyclerView().setLayoutManager(layoutManager);
updateSpanCount(getResources().getConfiguration());
getRecyclerView().scrollToPosition(position);
getAdapter().setGridView(grid);
getRecyclerView().setAdapter(getAdapter());
getAdapter().notifyDataSetChanged();
}

/**
* method will calculate the number of spans required for grid item and will update the span accordingly
*
* @param isGrid
*/
private void calculateAndUpdateSpanCount(boolean isGrid) {
getRecyclerView().getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
getRecyclerView().getViewTreeObserver().removeOnGlobalLayoutListener(this);
if (isGrid) {
int viewWidth = getRecyclerView().getMeasuredWidth();
int newSpanCount = viewWidth / GRID_ITEM_DEFAULT_WIDTH;
RecyclerView.LayoutManager layoutManager = getRecyclerView().getLayoutManager();
if (layoutManager instanceof GridLayoutManager) {
if (newSpanCount < 1) {
newSpanCount = DEFAULT_FALLBACK_SPAN_COUNT;
}
((GridLayoutManager) layoutManager).setSpanCount(newSpanCount);
layoutManager.requestLayout();
}
}
}
});
}

public CommonOCFileListAdapterInterface getCommonAdapter() {
return mAdapter;
}
Expand Down Expand Up @@ -2033,4 +2108,52 @@ public void setFabEnabled(final boolean enabled) {
public boolean isEmpty() {
return mAdapter == null || mAdapter.isEmpty();
}

@Override
public void onConfigurationChanged(@NonNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (getAdapter() != null) {
getAdapter().notifyDataSetChanged();
}
updateSpanCount(newConfig);
}

/**
* method will update the span count on basis of device orientation for the file listing
*
* @param newConfig current configuration
*/
private void updateSpanCount(Configuration newConfig) {
//this should only run when current view is not media gallery
if (getAdapter() != null) {
int maxColumnSize = (int) AppPreferencesImpl.DEFAULT_GRID_COLUMN;
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
//add the divider item decorator when orientation is landscape and device is not tablet
//because we don't have to add divider again as it is already added
if (!com.nmc.android.utils.DisplayUtils.isTablet()) {
addRemoveRecyclerViewItemDecorator();
}
maxColumnSize = maxColumnSizeLandscape;
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
//remove the divider item decorator when orientation is portrait and when device is not tablet
//because we have to show divider in both landscape and portrait mode
if (!com.nmc.android.utils.DisplayUtils.isTablet()) {
removeItemDecorator();
}
maxColumnSize = (int) AppPreferencesImpl.DEFAULT_GRID_COLUMN;
}

if (isGridEnabled()) {
//for tablet calculate size on the basis of screen width
if (com.nmc.android.utils.DisplayUtils.isTablet()) {
calculateAndUpdateSpanCount(true);
} else {
//and for phones directly show the hardcoded column size
if (getRecyclerView().getLayoutManager() instanceof GridLayoutManager) {
((GridLayoutManager) getRecyclerView().getLayoutManager()).setSpanCount(maxColumnSize);
}
}
}
}
}
}
Loading

0 comments on commit a9f2f1f

Please sign in to comment.