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-03-19 17:52:33 +00:00
|
|
|
import 'ai/import_from_text_screen.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-04-02 18:31:21 +00:00
|
|
|
var corpusFuture = loadCorpusFromDatabaseOrAssets();
|
|
|
|
|
|
|
|
|
|
runApp( MyApp(corpusFuture));
|
2026-02-07 16:01:37 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-21 12:54:56 +00:00
|
|
|
final GlobalKey<_CorpusLoaderState> corpusLoaderKey = GlobalKey<_CorpusLoaderState>();
|
|
|
|
|
|
2026-02-07 16:01:37 +00:00
|
|
|
class MyApp extends StatelessWidget {
|
2026-04-02 18:31:21 +00:00
|
|
|
final Future<Corpus> corpusFuture;
|
|
|
|
|
MyApp(this.corpusFuture, {Key? key}) : super(key: key);
|
2026-03-21 12:54:56 +00:00
|
|
|
|
2026-02-07 16:01:37 +00:00
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
return MaterialApp(
|
2026-04-02 18:31:21 +00:00
|
|
|
home: CorpusLoader(corpusFuture),
|
2026-02-13 07:53:04 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class CorpusLoader extends StatefulWidget {
|
2026-04-02 18:31:21 +00:00
|
|
|
final Future<Corpus> corpusFuture;
|
|
|
|
|
CorpusLoader(this.corpusFuture, {Key? key}) : super(key: corpusLoaderKey);
|
2026-03-21 12:54:56 +00:00
|
|
|
|
2026-02-13 07:53:04 +00:00
|
|
|
@override
|
2026-03-21 12:54:56 +00:00
|
|
|
State<CorpusLoader> createState() => _CorpusLoaderState();
|
2026-02-13 07:53:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _CorpusLoaderState extends State<CorpusLoader> {
|
|
|
|
|
late Future<Corpus> _corpusFuture;
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
2026-04-02 18:31:21 +00:00
|
|
|
_corpusFuture = widget.corpusFuture;
|
2026-02-13 07:53:04 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-15 10:31:53 +00:00
|
|
|
void _handleImport() {
|
2026-03-17 15:04:11 +00:00
|
|
|
_corpusFuture.then((corpus) {
|
|
|
|
|
Navigator.push(
|
|
|
|
|
context,
|
|
|
|
|
MaterialPageRoute(
|
2026-04-02 19:01:22 +00:00
|
|
|
builder: (context) =>
|
|
|
|
|
ImportFromTextScreen(
|
|
|
|
|
corpus: corpus,
|
|
|
|
|
),
|
2026-03-15 10:31:53 +00:00
|
|
|
),
|
2026-03-17 15:04:11 +00:00
|
|
|
).then((result) {
|
2026-04-02 19:01:22 +00:00
|
|
|
setState(() {
|
|
|
|
|
// Refresh the list when returning from import
|
|
|
|
|
});
|
2026-03-17 15:04:11 +00:00
|
|
|
});
|
2026-03-15 10:31:53 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-13 07:53:04 +00:00
|
|
|
@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!;
|
2026-04-06 14:43:56 +00:00
|
|
|
_registerCorpusInstance(corpus);
|
2026-02-26 19:03:19 +00:00
|
|
|
|
2026-02-13 07:53:04 +00:00
|
|
|
return Scaffold(
|
2026-03-15 10:31:53 +00:00
|
|
|
appBar: AppBar(
|
|
|
|
|
title: const Text('Formulas'),
|
|
|
|
|
actions: [
|
|
|
|
|
IconButton(
|
2026-03-19 11:05:10 +00:00
|
|
|
icon: const Icon(Icons.library_add),
|
2026-03-15 10:31:53 +00:00
|
|
|
tooltip: 'Import formulas',
|
|
|
|
|
onPressed: _handleImport,
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
2026-02-13 07:53:04 +00:00
|
|
|
body: FormulaList(
|
|
|
|
|
corpus: snapshot.data!,
|
2026-03-15 10:31:53 +00:00
|
|
|
onImport: _handleImport,
|
2026-02-13 07:53:04 +00:00
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return const Center(child: CircularProgressIndicator());
|
|
|
|
|
},
|
2026-02-07 16:01:37 +00:00
|
|
|
);
|
|
|
|
|
}
|
2026-04-06 14:43:56 +00:00
|
|
|
|
|
|
|
|
void _registerCorpusInstance(Corpus corpus) {
|
|
|
|
|
var existingCorpus = GetIt.instance.isRegistered<Corpus>() ? GetIt.instance.get<Corpus>() : null;
|
|
|
|
|
if (existingCorpus == null ) {
|
|
|
|
|
print( "Registering corpus in GetIt for the first time." );
|
|
|
|
|
GetIt.instance.registerSingleton<Corpus>(corpus);
|
|
|
|
|
}
|
|
|
|
|
else if( existingCorpus == corpus ){
|
|
|
|
|
print( "The corpus was already registered and is the same instance, no need to re-register." );
|
|
|
|
|
}
|
|
|
|
|
else if( existingCorpus != corpus ){
|
|
|
|
|
throw Exception( "The corpus was already registered but is a different instance. This should not happen." );
|
|
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
|