From 8893291780f6c045b4b0ecfb5aa73b3579b73dda Mon Sep 17 00:00:00 2001 From: Gold872 Date: Sat, 11 Nov 2023 22:07:34 -0500 Subject: [PATCH] Improved backwards compatibility with Shuffleboard API * Check for elastic and shuffleboard widget names * Check for elastic and shuffleboard widget properties --- .../draggable_nt4_widget_container.dart | 4 ++ .../nt4_widgets/single_topic/boolean_box.dart | 46 ++++++++++++++++--- .../nt4_widgets/single_topic/graph.dart | 2 +- .../single_topic/number_slider.dart | 19 +++++--- .../single_topic/voltage_view.dart | 8 ++-- 5 files changed, 61 insertions(+), 18 deletions(-) diff --git a/lib/widgets/draggable_containers/draggable_nt4_widget_container.dart b/lib/widgets/draggable_containers/draggable_nt4_widget_container.dart index c9aef0c1..9895fae8 100644 --- a/lib/widgets/draggable_containers/draggable_nt4_widget_container.dart +++ b/lib/widgets/draggable_containers/draggable_nt4_widget_container.dart @@ -240,6 +240,7 @@ class DraggableNT4WidgetContainer extends DraggableWidgetContainer { jsonData: widgetProperties, ); case 'Text Display': + case 'Text View': return TextDisplay.fromJson( key: UniqueKey(), jsonData: widgetProperties, @@ -255,16 +256,19 @@ class DraggableNT4WidgetContainer extends DraggableWidgetContainer { jsonData: widgetProperties, ); case 'PowerDistribution': + case 'PDP': return PowerDistribution.fromJson( key: UniqueKey(), jsonData: widgetProperties, ); case 'PIDController': + case 'PID Controller': return PIDControllerWidget.fromJson( key: UniqueKey(), jsonData: widgetProperties, ); case 'DifferentialDrive': + case 'Differential Drivebase': return DifferentialDrive.fromJson( key: UniqueKey(), jsonData: widgetProperties, diff --git a/lib/widgets/nt4_widgets/single_topic/boolean_box.dart b/lib/widgets/nt4_widgets/single_topic/boolean_box.dart index ddbc0c1a..f4d0b7c4 100644 --- a/lib/widgets/nt4_widgets/single_topic/boolean_box.dart +++ b/lib/widgets/nt4_widgets/single_topic/boolean_box.dart @@ -10,8 +10,8 @@ class BooleanBox extends StatelessWidget with NT4Widget { @override String type = 'Boolean Box'; - Color trueColor; - Color falseColor; + late Color trueColor; + late Color falseColor; BooleanBox({ super.key, @@ -26,14 +26,46 @@ class BooleanBox extends StatelessWidget with NT4Widget { init(); } - BooleanBox.fromJson({super.key, required Map jsonData}) - : trueColor = - Color(tryCast(jsonData['true_color']) ?? Colors.green.value), - falseColor = - Color(tryCast(jsonData['false_color']) ?? Colors.red.value) { + BooleanBox.fromJson({super.key, required Map jsonData}) { topic = tryCast(jsonData['topic']) ?? ''; period = tryCast(jsonData['period']) ?? Globals.defaultPeriod; + int? trueColorValue = + tryCast(jsonData['true_color']) ?? tryCast(jsonData['colorWhenTrue']); + int? falseColorValue = + tryCast(jsonData['false_color']) ?? tryCast(jsonData['colorWhenFalse']); + + if (trueColorValue == null) { + String? hexString = tryCast(jsonData['colorWhenTrue']); + + if (hexString != null) { + hexString = hexString.toUpperCase().replaceAll('#', ''); + + if (hexString.length == 6) { + hexString = 'FF$hexString'; + } + + trueColorValue = int.tryParse(hexString, radix: 16); + } + } + + if (falseColorValue == null) { + String? hexString = tryCast(jsonData['colorWhenFalse']); + + if (hexString != null) { + hexString = hexString.toUpperCase().replaceAll('#', ''); + + if (hexString.length == 6) { + hexString = 'FF$hexString'; + } + + falseColorValue = int.tryParse(hexString, radix: 16); + } + } + + trueColor = Color(trueColorValue ?? Colors.green.value); + falseColor = Color(falseColorValue ?? Colors.red.value); + init(); } diff --git a/lib/widgets/nt4_widgets/single_topic/graph.dart b/lib/widgets/nt4_widgets/single_topic/graph.dart index 80f6419e..89b1be11 100644 --- a/lib/widgets/nt4_widgets/single_topic/graph.dart +++ b/lib/widgets/nt4_widgets/single_topic/graph.dart @@ -40,7 +40,7 @@ class GraphWidget extends StatelessWidget with NT4Widget { GraphWidget.fromJson({super.key, required Map jsonData}) { topic = tryCast(jsonData['topic']) ?? ''; period = tryCast(jsonData['period']) ?? Globals.defaultPeriod; - timeDisplayed = tryCast(jsonData['time_displayed']) ?? 5.0; + timeDisplayed = tryCast(jsonData['time_displayed']) ?? tryCast(jsonData['visibleTime']) ?? 5.0; minValue = tryCast(jsonData['min_value']); maxValue = tryCast(jsonData['max_value']); mainColor = Color(tryCast(jsonData['color']) ?? Colors.cyan.value); diff --git a/lib/widgets/nt4_widgets/single_topic/number_slider.dart b/lib/widgets/nt4_widgets/single_topic/number_slider.dart index e25ed550..a2eb12fd 100644 --- a/lib/widgets/nt4_widgets/single_topic/number_slider.dart +++ b/lib/widgets/nt4_widgets/single_topic/number_slider.dart @@ -12,9 +12,9 @@ class NumberSlider extends StatelessWidget with NT4Widget { @override final String type = 'Number Slider'; - double minValue; - double maxValue; - int divisions; + late double minValue; + late double maxValue; + late int divisions; double _currentValue = 0.0; double _previousValue = 0.0; @@ -33,13 +33,18 @@ class NumberSlider extends StatelessWidget with NT4Widget { init(); } - NumberSlider.fromJson({super.key, required Map jsonData}) - : minValue = tryCast(jsonData['min_value']) ?? -1.0, - maxValue = tryCast(jsonData['max_value']) ?? 1.0, - divisions = tryCast(jsonData['divisions']) ?? 5 { + NumberSlider.fromJson({super.key, required Map jsonData}) { topic = tryCast(jsonData['topic']) ?? ''; period = tryCast(jsonData['period']) ?? Globals.defaultPeriod; + minValue = + tryCast(jsonData['min_value']) ?? tryCast(jsonData['min']) ?? -1.0; + maxValue = + tryCast(jsonData['max_value']) ?? tryCast(jsonData['max']) ?? 1.0; + divisions = tryCast(jsonData['divisions']) ?? + tryCast(jsonData['numOfTickMarks']) ?? + 5; + init(); } diff --git a/lib/widgets/nt4_widgets/single_topic/voltage_view.dart b/lib/widgets/nt4_widgets/single_topic/voltage_view.dart index 2fbbc1e5..51d1eda7 100644 --- a/lib/widgets/nt4_widgets/single_topic/voltage_view.dart +++ b/lib/widgets/nt4_widgets/single_topic/voltage_view.dart @@ -40,9 +40,11 @@ class VoltageView extends StatelessWidget with NT4Widget { topic = tryCast(jsonData['topic']) ?? ''; period = tryCast(jsonData['period']) ?? Globals.defaultPeriod; - minValue = tryCast(jsonData['min_value']) ?? 4; - maxValue = tryCast(jsonData['max_value']) ?? 13.0; - divisions = tryCast(jsonData['divisions']); + minValue = tryCast(jsonData['min_value']) ?? tryCast(jsonData['min']) ?? 4; + maxValue = + tryCast(jsonData['max_value']) ?? tryCast(jsonData['max']) ?? 13.0; + divisions = + tryCast(jsonData['divisions']) ?? tryCast(jsonData['numOfTickMarks']); inverted = tryCast(jsonData['inverted']) ?? false; orientation = tryCast(jsonData['orientation']) ?? 'horizontal';