From 666fa20e7b3c5033ba42c3bd012def15af5d41f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Gonz=C3=A1lez?= Date: Mon, 8 Sep 2025 20:27:42 +0200 Subject: [PATCH] feat: switch to async unit loading with temperature corpus Co-authored-by: aider (openrouter/deepseek/deepseek-r1:free) --- lib/main.dart | 52 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 8fd1cab..d93a078 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -2,29 +2,41 @@ import 'package:d4rt_formulas/ai/FormulaWidget.dart'; import 'package:d4rt_formulas/formula_models.dart'; import 'package:flutter/material.dart'; +import 'package:resource/resource.dart'; +import 'dart:convert'; + void main() { - //runApp(const MyApp()); - Formula formula = sampleFormula(); - runApp( MaterialApp( home: FormulaWidget(formula: formula)) ); + runApp(MaterialApp( + home: FutureBuilder( + future: createTestCorpus(), + builder: (context, snapshot) { + if (snapshot.connectionState == ConnectionState.done) { + if (snapshot.hasError) { + return Center(child: Text('Error loading units: ${snapshot.error}')); + } + return UnitList(corpus: snapshot.data!); + } + return const Center(child: CircularProgressIndicator()); + }, + ), + )); } -Formula sampleFormula(){ - final literal = """ - { - "name": "Newton's second law", - "input": [ - { "name": 'm', "magnitude": 'mass'}, - { "name": 'a', "magnitude": 'acceleration'} - ], - "output": { "name": 'F', "magnitude": 'force'}, - "d4rtCode": ''' - F = a * m; - ''' - } - """; - - final formula = Formula.fromStringLiteral(literal); - return formula; +Future createTestCorpus() async { + final corpus = UnitCorpus(); + final resources = ["lib/units/temperature.d4rt.units"]; + + for (final resourcePath in resources) { + try { + final resource = Resource(resourcePath); + final literal = await resource.readAsString(encoding: utf8); + final units = UnitSpec.fromArrayStringLiteral(literal); + corpus.loadUnits(units); + } catch (e) { + print('Error loading $resourcePath: $e'); + } + } + return corpus; } class MyApp extends StatelessWidget {