reorderWidgets method

Future<void> reorderWidgets(
  1. List<String> widgetIds
)

Reorder widget positions in the current layout

Implementation

Future<void> reorderWidgets(List<String> widgetIds) async {
  if (_currentLayoutId == null) {
    throw Exception('No current layout selected');
  }

  final layout = _configCache[_currentLayoutId]!;

  // Update order based on the new list
  for (int i = 0; i < widgetIds.length; i++) {
    final widgetId = widgetIds[i];
    final position = layout.positions.firstWhere(
      (p) => p.widgetId == widgetId,
      orElse: () => WidgetPosition(
        widgetId: widgetId,
        order: i,
      ),
    );

    position.order = i;

    // Update or add the position
    final existingIndex =
        layout.positions.indexWhere((p) => p.widgetId == widgetId);

    if (existingIndex >= 0) {
      layout.positions[existingIndex] = position;
    } else {
      layout.positions.add(position);
    }
  }

  // Sort positions by order
  layout.positions.sort((a, b) => a.order.compareTo(b.order));

  layout.lastModified = DateTime.now();

  await _saveConfig(layout);
}