Skip to content

Commit

Permalink
Merge pull request #375 from MGaetan89/corner_view_visibility
Browse files Browse the repository at this point in the history
Refactor the corner view visibility
  • Loading branch information
evrencoskun authored Jun 30, 2021
2 parents 4810ec4 + 89ab174 commit f46ef65
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package com.evrencoskun.tableview.test.adapters;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static java.util.Collections.emptyList;

import android.view.View;

import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;

import com.evrencoskun.tableview.TableView;
import com.evrencoskun.tableview.test.TestActivity;
import com.evrencoskun.tableview.test.data.SimpleData;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
public class AbstractTableAdapterTest {

@Rule
public ActivityScenarioRule<TestActivity> mActivityTestRule =
new ActivityScenarioRule<>(TestActivity.class);

private SimpleData mData;
private TableView mTableView;
private SimpleTestAdapter mAdapter;

@Before
public void before() {
mData = new SimpleData(5);
mTableView = new TableView(InstrumentationRegistry.getInstrumentation().getContext());

mAdapter = new SimpleTestAdapter();
mAdapter.setTableView(mTableView);
}

@Test
public void testCornerViewStateWithDisabledCorner() {
mTableView.setShowCornerView(false);

assertNull(mAdapter.getCornerView());

mAdapter.setAllItems(null, null, null);

assertNull(mAdapter.getCornerView());

mAdapter.setAllItems(mData.getColumnHeaders(), mData.getRowHeaders(), mData.getCells());

assertNotNull(mAdapter.getCornerView());
assertEquals(View.VISIBLE, mAdapter.getCornerView().getVisibility());

mAdapter.setAllItems(emptyList(), emptyList(), emptyList());

assertNotNull(mAdapter.getCornerView());
assertEquals(View.GONE, mAdapter.getCornerView().getVisibility());
}

@Test
public void testCornerViewStateWithEnabledCorners() {
mTableView.setShowCornerView(true);

assertNull(mAdapter.getCornerView());

mAdapter.setAllItems(mData.getColumnHeaders(), mData.getRowHeaders(), mData.getCells());

assertNotNull(mAdapter.getCornerView());
assertEquals(View.VISIBLE, mAdapter.getCornerView().getVisibility());

mAdapter.setAllItems(null, null, null);

assertNotNull(mAdapter.getCornerView());
assertEquals(View.GONE, mAdapter.getCornerView().getVisibility());

// We set some data, that we then reset to empty
mAdapter.setAllItems(mData.getColumnHeaders(), mData.getRowHeaders(), mData.getCells());
mAdapter.setAllItems(emptyList(), emptyList(), emptyList());

assertNotNull(mAdapter.getCornerView());
assertEquals(View.GONE, mAdapter.getCornerView().getVisibility());

mAdapter.setAllItems(mData.getColumnHeaders(), null, null);

assertNotNull(mAdapter.getCornerView());
assertEquals(View.VISIBLE, mAdapter.getCornerView().getVisibility());

mAdapter.setAllItems(null, mData.getRowHeaders(), null);

assertNotNull(mAdapter.getCornerView());
assertEquals(View.GONE, mAdapter.getCornerView().getVisibility());

mAdapter.setAllItems(mData.getColumnHeaders(), mData.getRowHeaders(), null);

assertNotNull(mAdapter.getCornerView());
assertEquals(View.VISIBLE, mAdapter.getCornerView().getVisibility());
}

@Test
public void testCornerViewStateWithToggledCorners() {
mTableView.setShowCornerView(true);

assertNull(mAdapter.getCornerView());

mAdapter.setAllItems(mData.getColumnHeaders(), mData.getRowHeaders(), mData.getCells());

assertNotNull(mAdapter.getCornerView());
assertEquals(View.VISIBLE, mAdapter.getCornerView().getVisibility());

mTableView.setShowCornerView(false);
mAdapter.setAllItems(mData.getColumnHeaders(), mData.getRowHeaders(), mData.getCells());

assertNotNull(mAdapter.getCornerView());
assertEquals(View.VISIBLE, mAdapter.getCornerView().getVisibility());

mAdapter.setAllItems(null, null, null);

assertNotNull(mAdapter.getCornerView());
assertEquals(View.GONE, mAdapter.getCornerView().getVisibility());

// We set some data, that we then reset to empty
mAdapter.setAllItems(mData.getColumnHeaders(), mData.getRowHeaders(), mData.getCells());
mAdapter.setAllItems(emptyList(), emptyList(), emptyList());

assertNotNull(mAdapter.getCornerView());
assertEquals(View.GONE, mAdapter.getCornerView().getVisibility());

mAdapter.setAllItems(mData.getColumnHeaders(), null, null);

assertNotNull(mAdapter.getCornerView());
assertEquals(View.GONE, mAdapter.getCornerView().getVisibility());

mAdapter.setAllItems(null, mData.getRowHeaders(), null);

assertNotNull(mAdapter.getCornerView());
assertEquals(View.GONE, mAdapter.getCornerView().getVisibility());

mAdapter.setAllItems(mData.getColumnHeaders(), mData.getRowHeaders(), null);

assertNotNull(mAdapter.getCornerView());
assertEquals(View.VISIBLE, mAdapter.getCornerView().getVisibility());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,41 +121,58 @@ public void setCellItems(@Nullable List<List<C>> cellItems) {
dispatchCellDataSetChangesToListeners(mCellItems);
}

public void setAllItems(@Nullable List<CH> columnHeaderItems, @Nullable List<RH> rowHeaderItems, @Nullable List<List<C>>
cellItems) {
public void setAllItems(
@Nullable List<CH> columnHeaderItems,
@Nullable List<RH> rowHeaderItems,
@Nullable List<List<C>> cellItems
) {
// Set all items
setColumnHeaderItems(columnHeaderItems);
setRowHeaderItems(rowHeaderItems);
setCellItems(cellItems);

// Control corner view
if (mCornerView == null){
if (columnHeaderItems != null && !columnHeaderItems.isEmpty() && mTableView != null) {
// Check to see if the corner view show be shown with column headers
if (!mTableView.getShowCornerView()){
// Don't show corner view if there are column headers but
// No row headers or cell data
// (Original behaviour)
if (!(rowHeaderItems != null && !rowHeaderItems.isEmpty() &&
cellItems != null && !cellItems.isEmpty())) {
// There are no row headers or cell items so no corner view is needed
return;
}
}
// Create corner view
mCornerView = onCreateCornerView((ViewGroup) mTableView);

// Set the corner location
mTableView.addView(mCornerView, new FrameLayout.LayoutParams(mRowHeaderWidth,
mColumnHeaderHeight, mTableView.getGravity()));
updateCornerViewState(columnHeaderItems, rowHeaderItems);
}

private void updateCornerViewState(
@Nullable List<CH> columnHeaderItems,
@Nullable List<RH> rowHeaderItems
) {
boolean hasColumnHeaders = columnHeaderItems != null && !columnHeaderItems.isEmpty();
boolean hasRowHeaders = rowHeaderItems != null && !rowHeaderItems.isEmpty();
boolean showCornerView = mTableView != null && mTableView.getShowCornerView();
boolean needCornerSpace = hasColumnHeaders && (hasRowHeaders || showCornerView);

// Create the corner view if we need it
if (mCornerView == null && needCornerSpace) {
// No TableView is associated with this Adapter, so we can't create the corner view
if (mTableView == null) {
return;
}

FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
mRowHeaderWidth,
mColumnHeaderHeight,
mTableView.getGravity()
);

// Create corner view
mCornerView = onCreateCornerView((ViewGroup) mTableView);

// Set the corner location
mTableView.addView(mCornerView, layoutParams);
}

// We don't have any corner view to update
if (mCornerView == null) {
return;
}

if (needCornerSpace) {
mCornerView.setVisibility(View.VISIBLE);
} else {
// Change corner view visibility
if (rowHeaderItems != null && !rowHeaderItems.isEmpty()) {
mCornerView.setVisibility(View.VISIBLE);
} else {
mCornerView.setVisibility(View.GONE);
}
mCornerView.setVisibility(View.GONE);
}
}

Expand Down

0 comments on commit f46ef65

Please sign in to comment.