From 29f6f28f1227947863761a1d5c06a20ddd235ecc Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Sat, 24 May 2025 01:35:27 +0200 Subject: [PATCH] Add meilisearch config settings dialogue --- lib/main.dart | 123 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 121 insertions(+), 2 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index caa48cb..ad17b4a 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -13,6 +13,7 @@ import 'dart:math'; import 'package:path/path.dart' as path; import 'package:ps_list/ps_list.dart'; import 'package:journaler/meilisearch.dart'; +import 'package:journaler/meilisearch_config.dart'; // TODO: Sound does not play when ran from a different workdir? Weird // TODO: Fix saving the same scratch over and over again @@ -67,8 +68,15 @@ Future runPrimaryInstance(File ipcFile) async { startIpcWatcher(ipcFile); // Initialize the app - // await DB.init(); - await init(); + try { + // Initialize Meilisearch first + await init(); + debugPrint('Meilisearch initialized successfully'); + } catch (e) { + debugPrint('Error initializing Meilisearch: $e'); + // Continue anyway - the app will work with default values + } + await windowManager.ensureInitialized(); WindowOptions windowOptions = const WindowOptions( @@ -1091,6 +1099,108 @@ class MainPageState extends State with WindowListener { ); } + // Show Meilisearch settings dialog + void _showMeilisearchSettings() { + final endpointController = TextEditingController(); + final apiKeyController = TextEditingController(); + bool isLoading = true; + String? errorMessage; + + // Load current values + getMeilisearchEndpoint().then((value) { + endpointController.text = value; + isLoading = false; + }).catchError((e) { + errorMessage = 'Failed to load endpoint: $e'; + isLoading = false; + }); + + getMeilisearchApiKey().then((value) { + apiKeyController.text = value; + }).catchError((e) { + errorMessage = 'Failed to load API key: $e'; + }); + + showDialog( + context: context, + builder: (BuildContext context) { + return StatefulBuilder( + builder: (context, setState) { + return AlertDialog( + title: const Text('Meilisearch Settings'), + content: Column( + mainAxisSize: MainAxisSize.min, + children: [ + if (isLoading) + const Center(child: CircularProgressIndicator()) + else if (errorMessage != null) + Text( + errorMessage!, + style: const TextStyle(color: Colors.red), + ) + else ...[ + TextField( + controller: endpointController, + decoration: const InputDecoration( + labelText: 'Endpoint URL', + hintText: 'http://localhost:7700', + border: OutlineInputBorder(), + ), + ), + const SizedBox(height: 16), + TextField( + controller: apiKeyController, + decoration: const InputDecoration( + labelText: 'API Key', + hintText: 'masterKey', + border: OutlineInputBorder(), + ), + obscureText: true, + ), + ], + ], + ), + actions: [ + TextButton( + onPressed: () { + Navigator.of(context).pop(); + }, + child: const Text('Cancel'), + ), + TextButton( + onPressed: isLoading ? null : () async { + try { + setState(() { + isLoading = true; + errorMessage = null; + }); + + await setMeilisearchEndpoint(endpointController.text); + await setMeilisearchApiKey(apiKeyController.text); + + // Try to reinitialize Meilisearch with new settings + await init(); + + if (mounted) { + Navigator.of(context).pop(); + } + } catch (e) { + setState(() { + errorMessage = 'Failed to save settings: $e'; + isLoading = false; + }); + } + }, + child: const Text('Save'), + ), + ], + ); + }, + ); + }, + ); + } + @override Widget build(BuildContext context) { // Wrap Scaffold with RawKeyboardListener as workaround for Escape key @@ -1210,6 +1320,15 @@ class MainPageState extends State with WindowListener { onPressed: _playSound, ), ), + // Meilisearch Settings Button + Padding( + padding: const EdgeInsets.symmetric(vertical: 8.0), + child: IconButton( + icon: const Icon(Icons.settings), + tooltip: 'Meilisearch Settings', + onPressed: _showMeilisearchSettings, + ), + ), const SizedBox(width: 10), ], ),