diff --git a/android/src/main/java/com/qliktrialreactnativestraighttable/ColumnWidths.java b/android/src/main/java/com/qliktrialreactnativestraighttable/ColumnWidths.java index 6fc5c06a..45eca59c 100644 --- a/android/src/main/java/com/qliktrialreactnativestraighttable/ColumnWidths.java +++ b/android/src/main/java/com/qliktrialreactnativestraighttable/ColumnWidths.java @@ -43,15 +43,15 @@ public void loadWidths(int frameWidth, List dataColumns, List rows) { + private void loadDefaultWidths(float frameWidth, List 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; } @@ -59,30 +59,30 @@ private void loadDefaultWidths(int frameWidth, List rows) { } 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 rows, boolean shouldAddWidth) { + public int resizeColumnByAverage(DataColumn column, List rows, boolean shouldAddWidth, float frameWidth) { int runningTotal = 0; Paint paint = new Paint(); for(DataRow row : rows) { @@ -96,7 +96,7 @@ public int resizeColumnByAverage(DataColumn column, List 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"); @@ -106,6 +106,7 @@ public int resizeColumnByAverage(DataColumn column, List 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); } diff --git a/android/src/main/java/com/qliktrialreactnativestraighttable/CustomLinearLayoutManger.java b/android/src/main/java/com/qliktrialreactnativestraighttable/CustomLinearLayoutManger.java index fe5cc741..20b35545 100644 --- a/android/src/main/java/com/qliktrialreactnativestraighttable/CustomLinearLayoutManger.java +++ b/android/src/main/java/com/qliktrialreactnativestraighttable/CustomLinearLayoutManger.java @@ -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; } diff --git a/android/src/main/java/com/qliktrialreactnativestraighttable/CustomRecyclerView.java b/android/src/main/java/com/qliktrialreactnativestraighttable/CustomRecyclerView.java index 9c6441a5..73119c37 100644 --- a/android/src/main/java/com/qliktrialreactnativestraighttable/CustomRecyclerView.java +++ b/android/src/main/java/com/qliktrialreactnativestraighttable/CustomRecyclerView.java @@ -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 @@ -196,7 +196,9 @@ public void run() { tableView.rootLayout.requestLayout(); } requestLayout(); - dataProvider.notifyDataSetChanged(); + if(recursive) { + dataProvider.notifyDataSetChanged(); + } } }); diff --git a/android/src/main/java/com/qliktrialreactnativestraighttable/DataProvider.java b/android/src/main/java/com/qliktrialreactnativestraighttable/DataProvider.java index cc20d0eb..4d686311 100644 --- a/android/src/main/java/com/qliktrialreactnativestraighttable/DataProvider.java +++ b/android/src/main/java/com/qliktrialreactnativestraighttable/DataProvider.java @@ -230,7 +230,7 @@ public void setDataColumns(List 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()); } diff --git a/android/src/main/java/com/qliktrialreactnativestraighttable/TableView.java b/android/src/main/java/com/qliktrialreactnativestraighttable/TableView.java index cdbb3cd9..bc761714 100644 --- a/android/src/main/java/com/qliktrialreactnativestraighttable/TableView.java +++ b/android/src/main/java/com/qliktrialreactnativestraighttable/TableView.java @@ -297,6 +297,7 @@ void createRecyclerView() { void invalidateLayout() { tableViewFactory.invalidateLayout(); + updateHeaderViewLineCount(); requestLayout(); } diff --git a/android/src/main/java/com/qliktrialreactnativestraighttable/TableViewFactory.java b/android/src/main/java/com/qliktrialreactnativestraighttable/TableViewFactory.java index 7de0d7cd..8a3d18c4 100644 --- a/android/src/main/java/com/qliktrialreactnativestraighttable/TableViewFactory.java +++ b/android/src/main/java/com/qliktrialreactnativestraighttable/TableViewFactory.java @@ -82,6 +82,7 @@ public void createAll() { updateFirstColumnHeaderHeight(); updateTotalsViewHeight(); + tempListenForLayoutChanges(); dataProvider.notifyDataSetChanged(); @@ -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; @@ -488,4 +504,15 @@ public void run() { }); } } + + private void testTextWrap() { + if(firstColumnRecyclerView != null) { + firstColumnRecyclerView.testTextWrap(true); + } + if(coupledRecyclerView != null) { + coupledRecyclerView.testTextWrap(true); + } + } } + + diff --git a/ios/ColumnWidths.swift b/ios/ColumnWidths.swift index 549e914d..b53dd7d1 100644 --- a/ios/ColumnWidths.swift +++ b/ios/ColumnWidths.swift @@ -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) @@ -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 } @@ -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 } @@ -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 {