Fixed problems when saving an updated formula (duplicates)

This commit is contained in:
Álvaro González 2026-03-09 10:40:02 +01:00
parent 1522a4143b
commit d3b228cbdb
5 changed files with 25 additions and 21 deletions

View file

@ -15,7 +15,7 @@ import 'unit_dropdown.dart';
class FormulaEditor extends StatefulWidget {
final Formula formula;
final Corpus corpus;
final Function(Formula)? onSave; // Callback when formula is saved
final Function(Formula)? onSave;
const FormulaEditor({
super.key,

View file

@ -51,15 +51,18 @@ class _FormulaListState extends State<FormulaList> {
}).toList();
}
String _formulaAndDependenciesToStringLiteral(Formula formula) {
// Get the formula and its dependencies
String _formulaAndDependenciesToExportStringLiteral(Formula formula) {
final dependencies = widget.corpus.withDependencies(formula);
return SetUtils.prettyPrint(dependencies.map((f) => f.toMap()).toList());
final dependenciesAsMap = dependencies.map((f) => f.toMap()).toList();
for( final f in dependenciesAsMap ){
f.remove("uuid");
}
return SetUtils.prettyPrint(dependenciesAsMap);
}
void _shareFormula(Formula formula) async {
try {
final exportString = _formulaAndDependenciesToStringLiteral(formula);
final exportString = _formulaAndDependenciesToExportStringLiteral(formula);
// Share the string
await share_plus.SharePlus.instance.share(
@ -92,7 +95,7 @@ class _FormulaListState extends State<FormulaList> {
void _copyFormula(Formula formula) async {
try {
final exportString = _formulaAndDependenciesToStringLiteral(formula);
final exportString = _formulaAndDependenciesToExportStringLiteral(formula);
// Copy to clipboard
await Clipboard.setData(ClipboardData(text: exportString));
@ -204,6 +207,11 @@ class _FormulaListState extends State<FormulaList> {
builder: (context) => FormulaScreen(
formula: formula,
corpus: widget.corpus,
onSave: (formula){
setState(() {
// Refresh the list when returning from the formula screen
});
},
),
),
);

View file

@ -12,10 +12,11 @@ import 'unit_dropdown.dart';
import 'formula_editor.dart';
class FormulaScreen extends StatefulWidget {
final Formula initialformula;
final Formula initialFormula;
final Corpus corpus;
final Function(Formula)? onSave; // Callback when formula is saved
FormulaScreen({super.key, required formula, required this.corpus}) : initialformula = formula;
FormulaScreen({super.key, required formula, required this.corpus, this.onSave}) : initialFormula = formula;
@override
State<FormulaScreen> createState() => _FormulaScreenState();
@ -55,7 +56,7 @@ class _FormulaScreenState extends State<FormulaScreen> {
@override
void initState() {
super.initState();
formula = widget.initialformula;
formula = widget.initialFormula;
}
@override
@ -120,11 +121,7 @@ class _FormulaScreenState extends State<FormulaScreen> {
String? unit = formula.output.unit;
if (unit != null && result is Number) {
final converted = widget.corpus.convert(result, unit, _selectedOutputUnit!);
if (converted is num) {
_result = converted.toStringAsFixed(2);
} else {
_result = converted.toString();
}
_result = converted.toStringAsFixed(2);
} else {
_result = result?.toString();
}
@ -161,9 +158,8 @@ class _FormulaScreenState extends State<FormulaScreen> {
formula: formula,
corpus: widget.corpus,
onSave: (updatedFormula) {
// Refresh the screen after saving
widget.onSave?.call(updatedFormula);
setState(() {
// The corpus has been updated, refresh the displayed formula
formula = updatedFormula;
});
},

View file

@ -46,7 +46,7 @@ extension CorpusDatabaseExtension on FormulasDatabase {
final parsed = SetUtils.parseCorpusElements('[${element.elementText}]');
if (parsed.isNotEmpty && parsed.first is models.Formula) {
final existingFormula = parsed.first as models.Formula;
if (existingFormula.name == formula.name) {
if (existingFormula.uuid == formula.uuid) {
// Update this element
await updateFormulaElement(
element.id,
@ -70,7 +70,7 @@ extension CorpusDatabaseExtension on FormulasDatabase {
}
// Method to delete a formula from the database by name
Future<bool> deleteFormula(String formulaName) async {
Future<bool> deleteFormula(String uuid) async {
final elements = await getAllFormulaElements();
for (final element in elements) {
@ -78,7 +78,7 @@ extension CorpusDatabaseExtension on FormulasDatabase {
final parsed = SetUtils.parseCorpusElements('[${element.elementText}]');
if (parsed.isNotEmpty && parsed.first is models.Formula) {
final existingFormula = parsed.first as models.Formula;
if (existingFormula.name == formulaName) {
if (existingFormula.uuid == uuid) {
await deleteFormulaElement(element.id);
return true;
}

View file

@ -311,8 +311,8 @@ class Formula extends FormulaElement {
@override
Map<String, dynamic> toMap() {
// UUID NOT INCLUDED ON PURPOSE
return {
'uuid': uuid,
'name': name,
if (description != null) 'description': description,
'input': input.map((v) => v.toMap()).toList(growable: false),
@ -323,7 +323,7 @@ class Formula extends FormulaElement {
}
Formula({
String? uuid = null,
String? uuid,
required this.name,
this.description,
required this.input,