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 { class FormulaEditor extends StatefulWidget {
final Formula formula; final Formula formula;
final Corpus corpus; final Corpus corpus;
final Function(Formula)? onSave; // Callback when formula is saved final Function(Formula)? onSave;
const FormulaEditor({ const FormulaEditor({
super.key, super.key,

View file

@ -51,15 +51,18 @@ class _FormulaListState extends State<FormulaList> {
}).toList(); }).toList();
} }
String _formulaAndDependenciesToStringLiteral(Formula formula) { String _formulaAndDependenciesToExportStringLiteral(Formula formula) {
// Get the formula and its dependencies
final dependencies = widget.corpus.withDependencies(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 { void _shareFormula(Formula formula) async {
try { try {
final exportString = _formulaAndDependenciesToStringLiteral(formula); final exportString = _formulaAndDependenciesToExportStringLiteral(formula);
// Share the string // Share the string
await share_plus.SharePlus.instance.share( await share_plus.SharePlus.instance.share(
@ -92,7 +95,7 @@ class _FormulaListState extends State<FormulaList> {
void _copyFormula(Formula formula) async { void _copyFormula(Formula formula) async {
try { try {
final exportString = _formulaAndDependenciesToStringLiteral(formula); final exportString = _formulaAndDependenciesToExportStringLiteral(formula);
// Copy to clipboard // Copy to clipboard
await Clipboard.setData(ClipboardData(text: exportString)); await Clipboard.setData(ClipboardData(text: exportString));
@ -204,6 +207,11 @@ class _FormulaListState extends State<FormulaList> {
builder: (context) => FormulaScreen( builder: (context) => FormulaScreen(
formula: formula, formula: formula,
corpus: widget.corpus, 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'; import 'formula_editor.dart';
class FormulaScreen extends StatefulWidget { class FormulaScreen extends StatefulWidget {
final Formula initialformula; final Formula initialFormula;
final Corpus corpus; 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 @override
State<FormulaScreen> createState() => _FormulaScreenState(); State<FormulaScreen> createState() => _FormulaScreenState();
@ -55,7 +56,7 @@ class _FormulaScreenState extends State<FormulaScreen> {
@override @override
void initState() { void initState() {
super.initState(); super.initState();
formula = widget.initialformula; formula = widget.initialFormula;
} }
@override @override
@ -120,11 +121,7 @@ class _FormulaScreenState extends State<FormulaScreen> {
String? unit = formula.output.unit; String? unit = formula.output.unit;
if (unit != null && result is Number) { if (unit != null && result is Number) {
final converted = widget.corpus.convert(result, unit, _selectedOutputUnit!); final converted = widget.corpus.convert(result, unit, _selectedOutputUnit!);
if (converted is num) {
_result = converted.toStringAsFixed(2); _result = converted.toStringAsFixed(2);
} else {
_result = converted.toString();
}
} else { } else {
_result = result?.toString(); _result = result?.toString();
} }
@ -161,9 +158,8 @@ class _FormulaScreenState extends State<FormulaScreen> {
formula: formula, formula: formula,
corpus: widget.corpus, corpus: widget.corpus,
onSave: (updatedFormula) { onSave: (updatedFormula) {
// Refresh the screen after saving widget.onSave?.call(updatedFormula);
setState(() { setState(() {
// The corpus has been updated, refresh the displayed formula
formula = updatedFormula; formula = updatedFormula;
}); });
}, },

View file

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

View file

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