calculateGroupBorders static method

Map<String, CellBorders> calculateGroupBorders(
  1. List<Map<String, dynamic>> rows,
  2. List<ColumnConfig> columns,
  3. Map<String, Map<String, dynamic>> highlightMetadata
)

Calculate which borders should be thick for each highlighted cell Returns Map<cellKey, CellBorders> where cellKey = 'rowId_columnField'

Algorithm:

  1. Build 2D grid of highlight colors from metadata (null = not highlighted)
  2. For each highlighted cell, check 4 neighbors (up, down, left, right)
  3. If neighbor has SAME color → internal border (NO border - hidden)
  4. If neighbor has DIFFERENT color or is null → group edge (thick border)

highlightMetadata - Map<cellKey, highlightData> with color, userId, etc.

Implementation

static Map<String, CellBorders> calculateGroupBorders(
  List<Map<String, dynamic>> rows,
  List<ColumnConfig> columns,
  Map<String, Map<String, dynamic>> highlightMetadata,
) {
  // Early exit - if no highlights exist, return empty map immediately
  // This eliminates 400,000 operations per frame when no cells are highlighted
  if (highlightMetadata.isEmpty) {
    return {};
  }

  final Map<String, CellBorders> result = {};

  // Build 2D grid of highlight colors from metadata
  final grid = _buildHighlightGrid(rows, columns, highlightMetadata);

  // For each highlighted cell, check neighbors to determine border visibility
  for (int row = 0; row < rows.length; row++) {
    for (int col = 0; col < columns.length; col++) {
      final cellColor = grid[row][col];
      if (cellColor == null) continue; // Not highlighted

      // Extract row ID from row data (handle various formats, including temp_row_id for new rows)
      final rowData = rows[row];
      final rowId = rowData['_id']?.toString() ??
                   rowData['row_id']?.toString() ??
                   rowData['temp_row_id']?.toString() ??
                   row.toString();

      // Build cell key using rowId and column field
      final cellKey = '${rowId}_${columns[col].field}';

      final borders = CellBorders(
        top: !_hasSameColorNeighbor(grid, row - 1, col, cellColor),
        bottom: !_hasSameColorNeighbor(grid, row + 1, col, cellColor),
        left: !_hasSameColorNeighbor(grid, row, col - 1, cellColor),
        right: !_hasSameColorNeighbor(grid, row, col + 1, cellColor),
      );
      result[cellKey] = borders;

      // REMOVED: Debug print was causing 400,000 prints per frame (20,000 cells × 20 visible rows)
    }
  }

  return result;
}