Solved saving on formula edit, but problem remain in initial formula list
This commit is contained in:
parent
2b3fbffd48
commit
1522a4143b
3 changed files with 107 additions and 79 deletions
|
|
@ -172,6 +172,7 @@ class _FormulaEditorState extends State<FormulaEditor> {
|
|||
);
|
||||
|
||||
return Formula(
|
||||
uuid: widget.formula.uuid,
|
||||
name: _nameController.text.trim(),
|
||||
description: _descriptionController.text.isEmpty ? null : _descriptionController.text,
|
||||
input: input,
|
||||
|
|
|
|||
|
|
@ -12,10 +12,10 @@ import 'unit_dropdown.dart';
|
|||
import 'formula_editor.dart';
|
||||
|
||||
class FormulaScreen extends StatefulWidget {
|
||||
Formula formula;
|
||||
final Formula initialformula;
|
||||
final Corpus corpus;
|
||||
|
||||
FormulaScreen({super.key, required this.formula, required this.corpus});
|
||||
FormulaScreen({super.key, required formula, required this.corpus}) : initialformula = formula;
|
||||
|
||||
@override
|
||||
State<FormulaScreen> createState() => _FormulaScreenState();
|
||||
|
|
@ -30,12 +30,15 @@ class _FormulaScreenState extends State<FormulaScreen> {
|
|||
String? _result;
|
||||
String? _selectedOutputUnit;
|
||||
bool _isDescriptionExpanded = false; // Track description expansion state
|
||||
late Formula _formula;
|
||||
|
||||
Formula get formula => _formula;
|
||||
|
||||
set formula(Formula newFormula) {
|
||||
_formula = newFormula;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
// Initialize controllers and units with listeners
|
||||
for (final input in widget.formula.input) {
|
||||
for (final input in formula.input) {
|
||||
_selectedUnits[input.name] = input.unit;
|
||||
if (input.values != null && input.values!.isNotEmpty) {
|
||||
// string/categorical variable -> use dropdown
|
||||
|
|
@ -46,7 +49,13 @@ class _FormulaScreenState extends State<FormulaScreen> {
|
|||
_inputControllers[input.name]!.addListener(_evaluateFormula);
|
||||
}
|
||||
}
|
||||
_selectedOutputUnit = widget.formula.output.unit;
|
||||
_selectedOutputUnit = formula.output.unit;
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
formula = widget.initialformula;
|
||||
}
|
||||
|
||||
@override
|
||||
|
|
@ -62,7 +71,7 @@ class _FormulaScreenState extends State<FormulaScreen> {
|
|||
void _evaluateFormula() {
|
||||
try {
|
||||
final inputValues = <String, dynamic>{};
|
||||
for (final input in widget.formula.input) {
|
||||
for (final input in formula.input) {
|
||||
// string/categorical variable
|
||||
if (input.values != null && input.values!.isNotEmpty) {
|
||||
final selected = _selectedValues[input.name];
|
||||
|
|
@ -105,10 +114,10 @@ class _FormulaScreenState extends State<FormulaScreen> {
|
|||
}
|
||||
|
||||
final evaluator = FormulaEvaluator();
|
||||
final result = evaluator.evaluate(widget.formula, inputValues);
|
||||
final result = evaluator.evaluate(formula, inputValues);
|
||||
|
||||
// Convert output to selected unit if needed
|
||||
String? unit = widget.formula.output.unit;
|
||||
String? unit = formula.output.unit;
|
||||
if (unit != null && result is Number) {
|
||||
final converted = widget.corpus.convert(result, unit, _selectedOutputUnit!);
|
||||
if (converted is num) {
|
||||
|
|
@ -126,7 +135,10 @@ class _FormulaScreenState extends State<FormulaScreen> {
|
|||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Error: ${e.toString()}'),
|
||||
backgroundColor: Theme.of(context).colorScheme.error,
|
||||
backgroundColor: Theme
|
||||
.of(context)
|
||||
.colorScheme
|
||||
.error,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
|
@ -136,7 +148,7 @@ class _FormulaScreenState extends State<FormulaScreen> {
|
|||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(widget.formula.name),
|
||||
title: Text(formula.name),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.edit),
|
||||
|
|
@ -144,14 +156,15 @@ class _FormulaScreenState extends State<FormulaScreen> {
|
|||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => FormulaEditor(
|
||||
formula: widget.formula,
|
||||
builder: (context) =>
|
||||
FormulaEditor(
|
||||
formula: formula,
|
||||
corpus: widget.corpus,
|
||||
onSave: (updatedFormula) {
|
||||
// Refresh the screen after saving
|
||||
setState(() {
|
||||
// The corpus has been updated, refresh the displayed formula
|
||||
widget.formula = updatedFormula;
|
||||
formula = updatedFormula;
|
||||
});
|
||||
},
|
||||
),
|
||||
|
|
@ -180,8 +193,8 @@ class _FormulaScreenState extends State<FormulaScreen> {
|
|||
}
|
||||
|
||||
Widget _buildDescriptionSection() {
|
||||
if (widget.formula.description == null ||
|
||||
widget.formula.description!.isEmpty) {
|
||||
if (formula.description == null ||
|
||||
formula.description!.isEmpty) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
|
||||
|
|
@ -190,7 +203,11 @@ class _FormulaScreenState extends State<FormulaScreen> {
|
|||
child: ExpansionTile(
|
||||
title: Text(
|
||||
'Description',
|
||||
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
||||
style: Theme
|
||||
.of(context)
|
||||
.textTheme
|
||||
.titleMedium
|
||||
?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
|
|
@ -206,11 +223,14 @@ class _FormulaScreenState extends State<FormulaScreen> {
|
|||
child: Container(
|
||||
padding: const EdgeInsets.all(8),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).colorScheme.surfaceVariant,
|
||||
color: Theme
|
||||
.of(context)
|
||||
.colorScheme
|
||||
.surfaceVariant,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Markdown(
|
||||
data: widget.formula.description!,
|
||||
data: formula.description!,
|
||||
shrinkWrap: true,
|
||||
builders: {
|
||||
'latex': LatexElementBuilder(),
|
||||
|
|
@ -233,12 +253,16 @@ class _FormulaScreenState extends State<FormulaScreen> {
|
|||
children: [
|
||||
Text(
|
||||
'Input Variables',
|
||||
style: Theme.of(
|
||||
style: Theme
|
||||
.of(
|
||||
context,
|
||||
).textTheme.titleMedium?.copyWith(fontWeight: FontWeight.bold),
|
||||
)
|
||||
.textTheme
|
||||
.titleMedium
|
||||
?.copyWith(fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
...widget.formula.input.map((variable) => _buildVariableRow(variable)),
|
||||
...formula.input.map((variable) => _buildVariableRow(variable)),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
|
@ -249,9 +273,13 @@ class _FormulaScreenState extends State<FormulaScreen> {
|
|||
children: [
|
||||
Text(
|
||||
'Result',
|
||||
style: Theme.of(
|
||||
style: Theme
|
||||
.of(
|
||||
context,
|
||||
).textTheme.titleMedium?.copyWith(fontWeight: FontWeight.bold),
|
||||
)
|
||||
.textTheme
|
||||
.titleMedium
|
||||
?.copyWith(fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Row(
|
||||
|
|
@ -260,7 +288,7 @@ class _FormulaScreenState extends State<FormulaScreen> {
|
|||
SizedBox(
|
||||
width: 150,
|
||||
child: Text(
|
||||
widget.formula.output.name,
|
||||
formula.output.name,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
|
|
@ -280,14 +308,13 @@ class _FormulaScreenState extends State<FormulaScreen> {
|
|||
const SizedBox(width: 8),
|
||||
UnitDropdown(
|
||||
corpus: widget.corpus,
|
||||
variable: widget.formula.output,
|
||||
variable: formula.output,
|
||||
selectedUnit: _selectedOutputUnit,
|
||||
onUnitChanged: (unit) {
|
||||
_selectedOutputUnit = unit;
|
||||
_evaluateFormula();
|
||||
print("En output unit changed to $unit: $_result");
|
||||
setState(() {
|
||||
});
|
||||
setState(() {});
|
||||
},
|
||||
),
|
||||
],
|
||||
|
|
@ -323,8 +350,7 @@ class _FormulaScreenState extends State<FormulaScreen> {
|
|||
onChanged: (v) {
|
||||
_selectedValues[variable.name] = v;
|
||||
_evaluateFormula();
|
||||
setState(() {
|
||||
});
|
||||
setState(() {});
|
||||
},
|
||||
decoration: const InputDecoration(
|
||||
border: UnderlineInputBorder(),
|
||||
|
|
|
|||
|
|
@ -76,18 +76,19 @@ class Corpus{
|
|||
|
||||
/// Updates a formula in the corpus
|
||||
void updateFormula(Formula formula) {
|
||||
if (!_allFormulas.containsKey(formula.name)) {
|
||||
throw ArgumentError("Formula not found: ${formula.name}");
|
||||
if (!_allFormulas.containsKey(formula.uuid)) {
|
||||
_allFormulas.keys.forEach( (uuid)=> print("Existing formula uuid: $uuid, name: ${_allFormulas[uuid]?.name}") );
|
||||
throw ArgumentError("Formula not found: ${formula.name} ${formula.uuid}");
|
||||
}
|
||||
|
||||
// Remove old tags
|
||||
final oldFormula = _allFormulas[formula.name]!;
|
||||
final oldFormula = _allFormulas[formula.uuid]!;
|
||||
for (final tag in oldFormula.tags) {
|
||||
_tags[tag]?.removeWhere((f) => f.name == formula.name);
|
||||
}
|
||||
|
||||
// Update the formula
|
||||
_allFormulas[formula.name] = formula;
|
||||
_allFormulas[formula.uuid] = formula;
|
||||
|
||||
// Add new tags
|
||||
for (final tag in formula.tags) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue