Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor the corner view visibility #375

Merged
merged 1 commit into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Refactor the corner view visibility
  • Loading branch information
MGaetan89 committed May 2, 2021
commit 89ab174136a8e23a7cae565e5e7c58f88059f491
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