2026-02-24 19:28:01 +00:00
|
|
|
import 'package:d4rt_formulas/d4rt_formulas.dart';
|
2025-08-27 09:10:22 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2026-02-26 19:03:19 +00:00
|
|
|
import 'package:get_it/get_it.dart';
|
2026-02-07 16:01:37 +00:00
|
|
|
import 'database/database_service.dart';
|
2026-02-13 07:53:04 +00:00
|
|
|
import 'service_locator.dart';
|
2025-09-08 18:27:42 +00:00
|
|
|
|
2025-09-21 19:35:20 +00:00
|
|
|
import 'ai/formula_list.dart';
|
2025-09-10 15:17:28 +00:00
|
|
|
import 'corpus.dart';
|
2025-09-20 14:46:21 +00:00
|
|
|
import 'defaults/default_corpus.dart';
|
2026-02-13 07:53:04 +00:00
|
|
|
import 'formula_models.dart' as models;
|
2025-09-10 15:17:28 +00:00
|
|
|
|
2026-02-07 16:01:37 +00:00
|
|
|
void main() async {
|
2026-01-21 07:49:56 +00:00
|
|
|
WidgetsFlutterBinding.ensureInitialized();
|
2026-02-13 07:53:04 +00:00
|
|
|
|
2026-02-07 16:01:37 +00:00
|
|
|
// Setup service locator and initialize the database
|
|
|
|
|
setupLocator();
|
2026-02-13 07:53:04 +00:00
|
|
|
|
2026-02-07 16:01:37 +00:00
|
|
|
runApp(const MyApp());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class MyApp extends StatelessWidget {
|
|
|
|
|
const MyApp({Key? key}) : super(key: key);
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
return MaterialApp(
|
2026-02-13 07:53:04 +00:00
|
|
|
home: CorpusLoader(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class CorpusLoader extends StatefulWidget {
|
|
|
|
|
@override
|
|
|
|
|
_CorpusLoaderState createState() => _CorpusLoaderState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _CorpusLoaderState extends State<CorpusLoader> {
|
|
|
|
|
late Future<Corpus> _corpusFuture;
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
|
|
|
|
_corpusFuture = loadCorpusFromDatabaseOrAssets();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
return FutureBuilder<Corpus>(
|
|
|
|
|
future: _corpusFuture,
|
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
|
if (snapshot.connectionState == ConnectionState.done) {
|
|
|
|
|
if (snapshot.hasError) {
|
|
|
|
|
return Center(child: Text('Error loading corpus: ${snapshot.error}'));
|
2025-09-08 18:27:42 +00:00
|
|
|
}
|
2026-02-26 19:03:19 +00:00
|
|
|
|
|
|
|
|
var corpus = snapshot.data!;
|
|
|
|
|
GetIt.instance.registerSingleton<Corpus>(corpus);
|
|
|
|
|
|
2026-02-13 07:53:04 +00:00
|
|
|
// If the corpus is empty (user chose not to load default), we could handle that here
|
|
|
|
|
// For now, just display the formula list
|
|
|
|
|
return Scaffold(
|
|
|
|
|
appBar: AppBar(title: const Text('Formulas')),
|
|
|
|
|
body: FormulaList(
|
2026-02-26 19:03:19 +00:00
|
|
|
corpus: corpus,
|
2026-02-13 07:53:04 +00:00
|
|
|
formulas: snapshot.data!.getFormulas(),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return const Center(child: CircularProgressIndicator());
|
|
|
|
|
},
|
2026-02-07 16:01:37 +00:00
|
|
|
);
|
|
|
|
|
}
|
2025-09-05 16:53:06 +00:00
|
|
|
}
|
|
|
|
|
|
2026-02-13 07:53:04 +00:00
|
|
|
/// Attempts to load corpus from database first, falls back to default corpus if database is empty
|
|
|
|
|
Future<Corpus> loadCorpusFromDatabaseOrAssets() async {
|
|
|
|
|
final database = getDatabase();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// Try to load from database first
|
|
|
|
|
final dbElements = await database.loadCorpusElements();
|
|
|
|
|
|
|
|
|
|
if (dbElements.isEmpty) {
|
|
|
|
|
// Database is empty, load default corpus and save to database
|
|
|
|
|
final defaultCorpus = await createDefaultCorpus();
|
|
|
|
|
|
|
|
|
|
// Convert corpus to elements and save to database
|
|
|
|
|
final elements = <models.FormulaElement>[];
|
|
|
|
|
elements.addAll(defaultCorpus.allUnits().cast<models.FormulaElement>());
|
|
|
|
|
elements.addAll(defaultCorpus.getFormulas().cast<models.FormulaElement>());
|
|
|
|
|
|
|
|
|
|
await database.saveCorpusElements(elements);
|
|
|
|
|
|
|
|
|
|
return defaultCorpus;
|
|
|
|
|
} else {
|
|
|
|
|
// Load corpus from database elements
|
|
|
|
|
return await Corpus.fromDatabaseElements(dbElements);
|
|
|
|
|
}
|
2026-02-24 19:28:01 +00:00
|
|
|
} catch (e, st) {
|
2026-02-13 07:53:04 +00:00
|
|
|
// If there's an error loading from database, fall back to default corpus
|
2026-02-24 19:28:01 +00:00
|
|
|
errorHandler.notify(e,st);
|
2026-02-13 07:53:04 +00:00
|
|
|
return await createDefaultCorpus();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Shows a dialog to ask user if they want to use the default corpus
|
|
|
|
|
Future<bool> showUseDefaultCorpusDialog(BuildContext context) async {
|
|
|
|
|
return await showDialog<bool>(
|
|
|
|
|
context: context,
|
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
|
return AlertDialog(
|
|
|
|
|
title: const Text('Empty Database'),
|
|
|
|
|
content: const Text('The database is empty. Would you like to load the default corpus?'),
|
|
|
|
|
actions: <Widget>[
|
|
|
|
|
TextButton(
|
|
|
|
|
onPressed: () => Navigator.of(context).pop(false), // Don't use default corpus
|
|
|
|
|
child: const Text('No'),
|
|
|
|
|
),
|
|
|
|
|
TextButton(
|
|
|
|
|
onPressed: () => Navigator.of(context).pop(true), // Use default corpus
|
|
|
|
|
child: const Text('Yes'),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
) ?? false; // Default to false if dialog is dismissed
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-27 09:10:22 +00:00
|
|
|
|