Skip to content

Commit

Permalink
fix(SESI-261): clamp max width column to 75% of visualization width (#…
Browse files Browse the repository at this point in the history
…192)


Co-authored-by: Vittorio Cellucci <[email protected]>
  • Loading branch information
vcellu and vcellu authored Feb 23, 2023
1 parent 2ad7c9b commit 0c2ab33
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,46 +43,46 @@ public void loadWidths(int frameWidth, List<DataColumn> dataColumns, List<DataRo
}
}

private void loadDefaultWidths(int frameWidth, List<DataRow> rows) {
private void loadDefaultWidths(float frameWidth, List<DataRow> rows) {
int runningTotal = 0;
for (DataColumn col : dataColumns) {
col.width = resizeColumnByAverage(col, rows, false);
col.width = resizeColumnByAverage(col, rows, false, frameWidth);
runningTotal += col.width;
}

if (runningTotal < frameWidth) {
int defaultWidth = frameWidth / dataColumns.size();
int defaultWidth = (int) (frameWidth / dataColumns.size());
for (DataColumn column : dataColumns) {
column.width = defaultWidth;
}
}
}

private boolean loadWidthsFromStorage() {
String key = buildTableKey();
String value = preferences.getString(key, null);
if(value == null) {
return false;
}
try {
JSONArray jsonArray = new JSONArray(value);
for(int i = 0; i< jsonArray.length(); i++) {
JSONArray jsonWidths = jsonArray.getJSONArray(i);
if(jsonWidths.length() != dataColumns.size()) {
return false;
}
for(int j = 0; j < jsonWidths.length(); j++) {
dataColumns.get(j).width = jsonWidths.getInt(j);
}
}
return true;
} catch (Exception exception) {
Log.e("ColumnWidths", exception.getMessage());
}
String key = buildTableKey();
String value = preferences.getString(key, null);
if(value == null) {
return false;
}
try {
JSONArray jsonArray = new JSONArray(value);
for(int i = 0; i< jsonArray.length(); i++) {
JSONArray jsonWidths = jsonArray.getJSONArray(i);
if(jsonWidths.length() != dataColumns.size()) {
return false;
}
for(int j = 0; j < jsonWidths.length(); j++) {
dataColumns.get(j).width = jsonWidths.getInt(j);
}
}
return true;
} catch (Exception exception) {
Log.e("ColumnWidths", exception.getMessage());
}
return false;
}

public int resizeColumnByAverage(DataColumn column, List<DataRow> rows, boolean shouldAddWidth) {
public int resizeColumnByAverage(DataColumn column, List<DataRow> rows, boolean shouldAddWidth, float frameWidth) {
int runningTotal = 0;
Paint paint = new Paint();
for(DataRow row : rows) {
Expand All @@ -96,7 +96,7 @@ public int resizeColumnByAverage(DataColumn column, List<DataRow> rows, boolean
}
}
}

int averageTextSize = rows.size() > 0 ? runningTotal / rows.size() : 1;
// Create a string with max text
String tempString = new String(new char[averageTextSize]).replace("\0", "X");
Expand All @@ -106,6 +106,7 @@ public int resizeColumnByAverage(DataColumn column, List<DataRow> rows, boolean

float width = paint.measureText(tempString, 0, tempString.length());
width = Math.max(DataProvider.minWidth * 1.5f, PixelUtils.dpToPx(width));
width = Math.min(width, frameWidth * 0.75f);
if(shouldAddWidth) {
widths.add((int)width);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ public class CustomLinearLayoutManger extends LinearLayoutManager {
public void onLayoutCompleted(RecyclerView.State state) {
super.onLayoutCompleted(state);
if(!initialized && recyclerView != null) {
if(recyclerView.testTextWrap()) {
if(recyclerView.getChildCount() == 0) {
return;
}
if(recyclerView.testTextWrap(true)) {
recyclerView = null;
initialized = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public void updateLineHeight(DataColumn column) {

}

public boolean testTextWrap() {
public boolean testTextWrap( boolean recursive ) {
if(!tableView.cellContentStyle.wrap) {
// don't test but tell whoever is calling
// that it's done testing
Expand Down Expand Up @@ -196,7 +196,9 @@ public void run() {
tableView.rootLayout.requestLayout();
}
requestLayout();
dataProvider.notifyDataSetChanged();
if(recursive) {
dataProvider.notifyDataSetChanged();
}
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public void setDataColumns(List<DataColumn> cols) {
.filter(dataCol -> dataCol.dataColIdx == col.dataColIdx)
.findAny()
.orElse(col);
col.width = column.width == 0 ? columnWidths.resizeColumnByAverage(column, rows, true) : column.width;
col.width = column.width == 0 ? columnWidths.resizeColumnByAverage(column, rows, true, tableView.totalWidth) : column.width;
}).collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ void createRecyclerView() {

void invalidateLayout() {
tableViewFactory.invalidateLayout();
updateHeaderViewLineCount();
requestLayout();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public void createAll() {

updateFirstColumnHeaderHeight();
updateTotalsViewHeight();
tempListenForLayoutChanges();

dataProvider.notifyDataSetChanged();

Expand All @@ -91,10 +92,25 @@ public void run() {
updateGrabbers();
updateScrollbarBounds();
updateRecyclerViewMargins();
testTextWrap();
}
});
}

private void tempListenForLayoutChanges() {
// add a temporary listener to do text wrapping
if(coupledRecyclerView != null) {
coupledRecyclerView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
if(coupledRecyclerView.testTextWrap(false)) {
coupledRecyclerView.removeOnLayoutChangeListener(this);
}
}
});
}
}

private void setMockScrollLayouts() {
FrameLayout.LayoutParams verticalFrameLayout = new FrameLayout.LayoutParams((int) PixelUtils.dpToPx(5), FrameLayout.LayoutParams.MATCH_PARENT);
verticalFrameLayout.gravity = Gravity.RIGHT;
Expand Down Expand Up @@ -488,4 +504,15 @@ public void run() {
});
}
}

private void testTextWrap() {
if(firstColumnRecyclerView != null) {
firstColumnRecyclerView.testTextWrap(true);
}
if(coupledRecyclerView != null) {
coupledRecyclerView.testTextWrap(true);
}
}
}


9 changes: 5 additions & 4 deletions ios/ColumnWidths.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class ColumnWidths {

func loadDefaultWidths(_ frame: CGRect, columnCount: Int, dataRows: [DataRow], dataCols: [DataColumn]) {
if !loadFromStorage(columnCount) {
let defaultWidth = frame.width / Double(columnCount)
// 0.75 looks ugly with single column
let defaultWidth = columnCount == 1 ? frame.width * 0.9 : frame.width / Double(columnCount)
let widths = [Double](repeating: defaultWidth, count: columnCount)
resetColumnWidths(widths: widths)
calculateDefaultColWidth(dataRows: dataRows, dataCols: dataCols, defaultWidth: defaultWidth, columnCount: columnCount, frame: frame)
Expand Down Expand Up @@ -71,7 +72,7 @@ class ColumnWidths {
var widths = [Double](repeating: defaultWidth, count: columnCount)
var totalWidth = 0.0
columnWidths.enumerated().forEach { (index, _) in
let averageWidth = getAverageWidth(dataRows: dataRows, dataCols: dataCols, index: index)
let averageWidth = getAverageWidth(frame, dataRows: dataRows, dataCols: dataCols, index: index)
widths[index] = averageWidth
totalWidth += averageWidth
}
Expand All @@ -84,7 +85,7 @@ class ColumnWidths {

}

fileprivate func getAverageWidth(dataRows: [DataRow], dataCols: [DataColumn], index: Int) -> Double {
fileprivate func getAverageWidth(_ frame: CGRect, dataRows: [DataRow], dataCols: [DataColumn], index: Int) -> Double {
if dataRows.count == 0 {
return DataCellView.minWidth
}
Expand All @@ -101,7 +102,7 @@ class ColumnWidths {
tempLabel.text = String(repeating: "M", count: average)
tempLabel.sizeToFit()
let newWidth = max(tempLabel.frame.width + (Double(PaddedLabel.PaddingSize) * 2.5), DataCellView.minWidth)
return newWidth
return min(newWidth, frame.width * 0.75)
}

func getTotalWidth() -> Double {
Expand Down

0 comments on commit 0c2ab33

Please sign in to comment.