62 lines
2.0 KiB
Dart
62 lines
2.0 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import 'package:rasadyar_core/core.dart';
|
|
|
|
class ConditionEvaluator {
|
|
static bool check(String? condition, RxMap<String, dynamic>? state) {
|
|
if (condition == null || condition.isEmpty) return true;
|
|
if (state == null) {
|
|
return condition.contains('activeStepperIndex == 0');
|
|
}
|
|
|
|
try {
|
|
if (condition.contains(' == ')) {
|
|
final parts = condition.split(' == ');
|
|
if (parts.length != 2) return true;
|
|
|
|
final key = parts[0].trim();
|
|
var expectedValue = parts[1].trim();
|
|
|
|
// Remove quotes
|
|
if ((expectedValue.startsWith("'") && expectedValue.endsWith("'")) ||
|
|
(expectedValue.startsWith('"') && expectedValue.endsWith('"'))) {
|
|
expectedValue = expectedValue.substring(1, expectedValue.length - 1);
|
|
}
|
|
|
|
final actualValue = state[key];
|
|
|
|
if (actualValue == null) {
|
|
if (key == 'activeStepperIndex' && expectedValue == '0') return true;
|
|
return false;
|
|
}
|
|
|
|
// Handle numeric comparison if possible
|
|
final expectedInt = int.tryParse(expectedValue);
|
|
if (expectedInt != null) {
|
|
if (actualValue is int) return actualValue == expectedInt;
|
|
if (actualValue is String) return int.tryParse(actualValue) == expectedInt;
|
|
}
|
|
|
|
return actualValue.toString() == expectedValue;
|
|
}
|
|
return true;
|
|
} catch (e) {
|
|
debugPrint('Error parsing condition: $e');
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// Extracts step index without using expensive RegExp in a loop if possible,
|
|
/// or at least centralizes the logic.
|
|
static int? extractStepIndex(String? condition) {
|
|
if (condition == null || !condition.contains('activeStepperIndex')) return null;
|
|
|
|
try {
|
|
final parts = condition.split(' == ');
|
|
if (parts.length == 2 && parts[0].trim() == 'activeStepperIndex') {
|
|
return int.tryParse(parts[1].trim());
|
|
}
|
|
} catch (_) {}
|
|
return null;
|
|
}
|
|
}
|