From cfa61e1a8aa1fedf88e0067422cd657943f92133 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Thu, 15 Aug 2024 22:50:00 +0800 Subject: [PATCH] :sparkles: Support named alert config --- lib/models/alert_configuration.dart | 10 ++++++++-- lib/screens/food/details.dart | 6 +++--- lib/screens/settings/alert.dart | 21 ++++++++++++++++++--- lib/translations/en_us.dart | 1 + lib/translations/zh_cn.dart | 1 + lib/widgets/alert_detect_result.dart | 4 ++-- pubspec.yaml | 2 +- 7 files changed, 34 insertions(+), 11 deletions(-) diff --git a/lib/models/alert_configuration.dart b/lib/models/alert_configuration.dart index 713e15c..b10f869 100644 --- a/lib/models/alert_configuration.dart +++ b/lib/models/alert_configuration.dart @@ -1,15 +1,20 @@ +import 'package:dietary_guard/models/food_data.dart'; + class AlertConfiguration { + String name; int nutrientId; double maxValue; double minValue; AlertConfiguration({ + required this.name, required this.nutrientId, required this.maxValue, required this.minValue, }); Map toJson() => { + 'name': name, 'nutrient_id': nutrientId, 'min_value': minValue, 'max_value': maxValue, @@ -17,6 +22,7 @@ class AlertConfiguration { factory AlertConfiguration.fromJson(Map json) => AlertConfiguration( + name: json['name'], nutrientId: json['nutrient_id'], minValue: json['min_value'], maxValue: json['max_value'], @@ -25,18 +31,18 @@ class AlertConfiguration { class AlertDetectResult { AlertConfiguration config; + FoodNutrient? nutrient; String name; String? unitName; - double? current; double? difference; bool isOutOfRange; bool isUndetected; AlertDetectResult({ required this.config, + required this.nutrient, required this.name, required this.unitName, - required this.current, required this.difference, required this.isOutOfRange, required this.isUndetected, diff --git a/lib/screens/food/details.dart b/lib/screens/food/details.dart index 67430be..f8a7c9c 100644 --- a/lib/screens/food/details.dart +++ b/lib/screens/food/details.dart @@ -33,14 +33,14 @@ class _FoodDetailsScreenState extends State { double? difference; String name = 'undetected'.tr; String? unitName; - double? current; + FoodNutrient? current; for (final nutrient in widget.item.foodNutrients) { if (item.nutrientId != nutrient.nutrientId) continue; name = nutrient.nutrientName; unitName = unitNameValues.reverse[nutrient.unitName]; if (nutrient.value != null) { + current = nutrient; final value = nutrient.value!; - current = value; if (value > item.maxValue) { difference = value - item.maxValue; isOutOfRange = true; @@ -54,9 +54,9 @@ class _FoodDetailsScreenState extends State { _alertDetectResult.add(AlertDetectResult( config: item, + nutrient: current, name: name, unitName: unitName, - current: current, difference: difference, isOutOfRange: isOutOfRange, isUndetected: isUndetected, diff --git a/lib/screens/settings/alert.dart b/lib/screens/settings/alert.dart index 0085b38..ae66f3f 100644 --- a/lib/screens/settings/alert.dart +++ b/lib/screens/settings/alert.dart @@ -16,6 +16,7 @@ class _AlertSettingsScreenState extends State { void _addAlert() { setState(() { _currentAlerts.add(AlertConfiguration( + name: 'Alert #${_currentAlerts.length}', nutrientId: 0, maxValue: 0, minValue: 0, @@ -64,16 +65,16 @@ class _AlertSettingsScreenState extends State { children: [ Expanded( child: TextFormField( - initialValue: x.nutrientId.toString(), + initialValue: x.name, decoration: InputDecoration( border: const OutlineInputBorder(), - label: Text("alertNutrientId".tr), + label: Text("alertName".tr), isDense: true, ), onTapOutside: (_) => FocusManager.instance.primaryFocus?.unfocus(), onChanged: (value) { - x.nutrientId = int.tryParse(value) ?? 0; + x.name = value; }, ), ), @@ -87,6 +88,20 @@ class _AlertSettingsScreenState extends State { ], ), const SizedBox(height: 12), + TextFormField( + initialValue: x.nutrientId.toString(), + decoration: InputDecoration( + border: const OutlineInputBorder(), + label: Text("alertNutrientId".tr), + isDense: true, + ), + onTapOutside: (_) => + FocusManager.instance.primaryFocus?.unfocus(), + onChanged: (value) { + x.nutrientId = int.tryParse(value) ?? 0; + }, + ), + const SizedBox(height: 12), Row( children: [ Expanded( diff --git a/lib/translations/en_us.dart b/lib/translations/en_us.dart index fd34f33..edffe92 100644 --- a/lib/translations/en_us.dart +++ b/lib/translations/en_us.dart @@ -17,6 +17,7 @@ const i18nEnglish = { 'alertNutrientId': 'Nutrient ID', 'alertMaxValue': 'Alert Max Value', 'alertMinValue': 'Alert Min Value', + 'alertName': 'Alert Name', 'alerts': 'Alerts', 'alertOutOfRange': '@count alert rules triggered, click the button on the right for details', diff --git a/lib/translations/zh_cn.dart b/lib/translations/zh_cn.dart index b93fcc5..d2af1ea 100644 --- a/lib/translations/zh_cn.dart +++ b/lib/translations/zh_cn.dart @@ -17,6 +17,7 @@ const i18nSimplifiedChinese = { 'alertNutrientId': '营养物质编号', 'alertMaxValue': '告警上限', 'alertMinValue': '告警下限', + 'alertName': '告警规则名', 'alerts': '告警', 'alertOutOfRange': '有 @count 项告警规则触发,点击右侧按钮了解详情', 'alertUnclear': '有 @count 项告警规则并无数据支持,点击右侧按钮了解详情', diff --git a/lib/widgets/alert_detect_result.dart b/lib/widgets/alert_detect_result.dart index fb514d6..2285138 100644 --- a/lib/widgets/alert_detect_result.dart +++ b/lib/widgets/alert_detect_result.dart @@ -60,13 +60,13 @@ class AlertDetectResultDialog extends StatelessWidget { title: Row( crossAxisAlignment: CrossAxisAlignment.center, children: [ - Text(item.name), + Text(item.config.name), const SizedBox(width: 6), Badge(label: Text('#${item.config.nutrientId}')) ], ), subtitle: Text( - '${item.current ?? 'undetected'.tr} ${(item.difference ?? 0) > 0 ? '↑' : '↓'}${item.difference?.abs().toPrecision(2) ?? '-'} ${item.unitName ?? ''}', + '${item.nutrient?.value ?? 'undetected'.tr} ${(item.difference ?? 0) > 0 ? '↑' : '↓'}${item.difference?.abs().toPrecision(2) ?? '-'} ${item.unitName ?? ''}', ), ); }, diff --git a/pubspec.yaml b/pubspec.yaml index ed14a87..ecb1a23 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # In Windows, build-name is used as the major, minor, and patch parts # of the product and file versions while build-number is used as the build suffix. -version: 1.0.0+1 +version: 1.0.0+2 environment: sdk: ^3.5.0