save formula, not tested
This commit is contained in:
parent
77bea838f7
commit
9c8afe739d
5 changed files with 121 additions and 9 deletions
|
|
@ -5,6 +5,8 @@ import 'package:flutter_markdown_plus/flutter_markdown_plus.dart';
|
|||
import 'package:markdown/markdown.dart' as markdown;
|
||||
import '../formula_models.dart';
|
||||
import '../corpus.dart';
|
||||
import '../database/database_service.dart';
|
||||
import '../service_locator.dart';
|
||||
import 'formula_screen.dart';
|
||||
import 'unit_dropdown.dart';
|
||||
|
||||
|
|
@ -13,11 +15,13 @@ import 'unit_dropdown.dart';
|
|||
class FormulaEditor extends StatefulWidget {
|
||||
final Formula formula;
|
||||
final Corpus corpus;
|
||||
final Function(Formula)? onSave; // Callback when formula is saved
|
||||
|
||||
const FormulaEditor({
|
||||
super.key,
|
||||
required this.formula,
|
||||
required this.corpus,
|
||||
this.onSave,
|
||||
});
|
||||
|
||||
@override
|
||||
|
|
@ -181,7 +185,7 @@ class _FormulaEditorState extends State<FormulaEditor> {
|
|||
}
|
||||
}
|
||||
|
||||
void _saveFormula() {
|
||||
Future<void> _saveFormula() async {
|
||||
if (!_validateFormula()) {
|
||||
return;
|
||||
}
|
||||
|
|
@ -189,14 +193,34 @@ class _FormulaEditorState extends State<FormulaEditor> {
|
|||
final formula = _buildFormula();
|
||||
if (formula == null) return;
|
||||
|
||||
// For now, just show a success message
|
||||
// In a real implementation, this would save to database
|
||||
try {
|
||||
final database = getDatabase();
|
||||
|
||||
// Update corpus in memory
|
||||
widget.corpus.updateFormula(formula);
|
||||
|
||||
// Update database
|
||||
final updated = await database.updateFormula(formula);
|
||||
|
||||
if (!updated) {
|
||||
// If formula wasn't found (e.g., name changed), add it as new
|
||||
await database.addFormula(formula);
|
||||
}
|
||||
|
||||
// Call the onSave callback if provided
|
||||
widget.onSave?.call(formula);
|
||||
|
||||
// Show success message
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Formula "${formula.name}" saved successfully!'),
|
||||
backgroundColor: Theme.of(context).colorScheme.primary,
|
||||
),
|
||||
);
|
||||
} catch (e, stack) {
|
||||
print('Error saving formula: $e\n$stack');
|
||||
_showErrorDialog('Error saving formula: $e');
|
||||
}
|
||||
}
|
||||
|
||||
void _showErrorDialog(String message) {
|
||||
|
|
|
|||
|
|
@ -80,6 +80,12 @@ class _FormulaListState extends State<FormulaList> {
|
|||
builder: (context) => FormulaEditor(
|
||||
formula: formula,
|
||||
corpus: widget.corpus,
|
||||
onSave: (updatedFormula) {
|
||||
// Refresh the formula list after saving
|
||||
setState(() {
|
||||
// The corpus has been updated, so we just need to rebuild
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -184,6 +184,12 @@ class _FormulaScreenState extends State<FormulaScreen> {
|
|||
builder: (context) => FormulaEditor(
|
||||
formula: widget.formula,
|
||||
corpus: widget.corpus,
|
||||
onSave: (updatedFormula) {
|
||||
// Refresh the screen after saving
|
||||
setState(() {
|
||||
// The corpus has been updated, refresh the displayed formula
|
||||
});
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -63,6 +63,27 @@ class Corpus{
|
|||
return _allFormulas.get(name);
|
||||
}
|
||||
|
||||
/// Updates a formula in the corpus
|
||||
void updateFormula(Formula formula) {
|
||||
if (!_allFormulas.containsKey(formula.name)) {
|
||||
throw ArgumentError("Formula not found: ${formula.name}");
|
||||
}
|
||||
|
||||
// Remove old tags
|
||||
final oldFormula = _allFormulas[formula.name]!;
|
||||
for (final tag in oldFormula.tags) {
|
||||
_tags[tag]?.removeWhere((f) => f.name == formula.name);
|
||||
}
|
||||
|
||||
// Update the formula
|
||||
_allFormulas[formula.name] = formula;
|
||||
|
||||
// Add new tags
|
||||
for (final tag in formula.tags) {
|
||||
_tags[tag]?.add(formula);
|
||||
}
|
||||
}
|
||||
|
||||
final Multimap<String, String> _baseToUnits = Multimap.create();
|
||||
final Map<String, UnitSpec> _allUnits = {};
|
||||
|
||||
|
|
|
|||
|
|
@ -35,4 +35,59 @@ extension CorpusDatabaseExtension on FormulasDatabase {
|
|||
await insertFormulaElement(element.toStringLiteral());
|
||||
}
|
||||
}
|
||||
|
||||
// Method to update a formula in the database by name
|
||||
Future<bool> updateFormula(models.Formula formula) async {
|
||||
final elements = await getAllFormulaElements();
|
||||
|
||||
for (final element in elements) {
|
||||
try {
|
||||
final parsed = models.parseCorpusElements('[${element.elementText}]');
|
||||
if (parsed.isNotEmpty && parsed.first is models.Formula) {
|
||||
final existingFormula = parsed.first as models.Formula;
|
||||
if (existingFormula.name == formula.name) {
|
||||
// Update this element
|
||||
await updateFormulaElement(
|
||||
element.id,
|
||||
formula.toStringLiteral()
|
||||
);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
print('Error parsing database element during update: $e');
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return false; // Formula not found
|
||||
}
|
||||
|
||||
// Method to add a new formula to the database
|
||||
Future<void> addFormula(models.Formula formula) async {
|
||||
await insertFormulaElement(formula.toStringLiteral());
|
||||
}
|
||||
|
||||
// Method to delete a formula from the database by name
|
||||
Future<bool> deleteFormula(String formulaName) async {
|
||||
final elements = await getAllFormulaElements();
|
||||
|
||||
for (final element in elements) {
|
||||
try {
|
||||
final parsed = models.parseCorpusElements('[${element.elementText}]');
|
||||
if (parsed.isNotEmpty && parsed.first is models.Formula) {
|
||||
final existingFormula = parsed.first as models.Formula;
|
||||
if (existingFormula.name == formulaName) {
|
||||
await deleteFormulaElement(element.id);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
print('Error parsing database element during delete: $e');
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return false; // Formula not found
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue