evaluateAndApplyRules method

Future<bool> evaluateAndApplyRules()

Evaluate rules and apply the results to the current layout

Implementation

Future<bool> evaluateAndApplyRules() async {
  if (_layoutStore.currentLayout == null) {
    return false;
  }

  // Get all interaction events
  final events = await _tracker.getAllEvents();

  // Count interactions by widget
  final counts = <String, int>{};
  for (final event in events) {
    counts[event.widgetId] = (counts[event.widgetId] ?? 0) + 1;
  }

  // Skip if we don't have enough data
  if (counts.isEmpty) {
    return false;
  }

  // Apply rules to get the recommended widget order
  List<String>? recommendedOrder;
  for (final rule in _rules) {
    if (recommendedOrder == null) {
      recommendedOrder = rule.apply(counts);
    } else {
      // Apply subsequent rules only to the result of previous rules
      final filteredCounts = <String, int>{};
      for (final id in recommendedOrder) {
        filteredCounts[id] = counts[id] ?? 0;
      }
      recommendedOrder = rule.apply(filteredCounts);
    }
  }

  if (recommendedOrder == null || recommendedOrder.isEmpty) {
    return false;
  }

  // Apply the new order to the current layout
  await _layoutStore.reorderWidgets(recommendedOrder);

  return true;
}