diff --git a/frontend/lib/main.dart b/frontend/lib/main.dart index 07d267a3f19b..3062d2971e1a 100644 --- a/frontend/lib/main.dart +++ b/frontend/lib/main.dart @@ -10,7 +10,6 @@ import 'package:firebase_auth/firebase_auth.dart'; import 'package:auto_gpt_flutter_client/viewmodels/task_viewmodel.dart'; import 'package:auto_gpt_flutter_client/viewmodels/chat_viewmodel.dart'; import 'package:auto_gpt_flutter_client/viewmodels/skill_tree_viewmodel.dart'; -import 'package:auto_gpt_flutter_client/viewmodels/api_settings_viewmodel.dart'; import 'package:auto_gpt_flutter_client/services/chat_service.dart'; import 'package:auto_gpt_flutter_client/services/task_service.dart'; @@ -55,11 +54,11 @@ void main() async { update: (context, restApiUtility, leaderboardService) => LeaderboardService(restApiUtility), ), - ChangeNotifierProxyProvider( - create: (context) => ApiSettingsViewModel( + ChangeNotifierProxyProvider( + create: (context) => SettingsViewModel( Provider.of(context, listen: false)), - update: (context, restApiUtility, apiSettingsViewModel) => - ApiSettingsViewModel(restApiUtility), + update: (context, restApiUtility, settingsViewModel) => + SettingsViewModel(restApiUtility), ), ], child: MyApp(), @@ -87,9 +86,6 @@ class MyApp extends StatelessWidget { if (snapshot.hasData && snapshot.data != null) { return MultiProvider( providers: [ - ChangeNotifierProvider( - create: (context) => SettingsViewModel(), - ), ChangeNotifierProvider( create: (context) => ChatViewModel( Provider.of(context, listen: false))), diff --git a/frontend/lib/viewmodels/api_settings_viewmodel.dart b/frontend/lib/viewmodels/api_settings_viewmodel.dart deleted file mode 100644 index 7f1acf6f9e4b..000000000000 --- a/frontend/lib/viewmodels/api_settings_viewmodel.dart +++ /dev/null @@ -1,30 +0,0 @@ -import 'package:auto_gpt_flutter_client/utils/rest_api_utility.dart'; -import 'package:flutter/material.dart'; -import 'package:shared_preferences/shared_preferences.dart'; - -class ApiSettingsViewModel with ChangeNotifier { - String _baseURL = "http://127.0.0.1:8000/ap/v1"; - SharedPreferences? _prefs; - final RestApiUtility _restApiUtility; - - ApiSettingsViewModel(this._restApiUtility) { - _loadBaseURL(); - } - - String get baseURL => _baseURL; - - void _loadBaseURL() async { - _prefs = await SharedPreferences.getInstance(); - _baseURL = _prefs?.getString('baseURL') ?? _baseURL; - _restApiUtility.updateBaseURL(_baseURL); - notifyListeners(); - } - - void updateBaseURL(String newURL) async { - _baseURL = newURL; - _prefs ??= await SharedPreferences.getInstance(); - _prefs?.setString('baseURL', newURL); - _restApiUtility.updateBaseURL(newURL); - notifyListeners(); - } -} diff --git a/frontend/lib/viewmodels/settings_viewmodel.dart b/frontend/lib/viewmodels/settings_viewmodel.dart index 27df8bdb5d65..3e6306d8e328 100644 --- a/frontend/lib/viewmodels/settings_viewmodel.dart +++ b/frontend/lib/viewmodels/settings_viewmodel.dart @@ -1,4 +1,5 @@ import 'package:auto_gpt_flutter_client/services/auth_service.dart'; +import 'package:auto_gpt_flutter_client/utils/rest_api_utility.dart'; import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; @@ -11,6 +12,8 @@ class SettingsViewModel extends ChangeNotifier { String _baseURL = ''; // State for Base URL int _continuousModeSteps = 1; // State for Continuous Mode Steps + final RestApiUtility _restApiUtility; + // Getters to access the private state variables bool get isDarkModeEnabled => _isDarkModeEnabled; bool get isDeveloperModeEnabled => _isDeveloperModeEnabled; @@ -19,8 +22,8 @@ class SettingsViewModel extends ChangeNotifier { final AuthService _authService = AuthService(); - SettingsViewModel() { - _loadPreferences(); // Load stored preferences when the view model is created + SettingsViewModel(this._restApiUtility) { + _loadPreferences(); } // Method to load stored preferences @@ -28,7 +31,8 @@ class SettingsViewModel extends ChangeNotifier { final prefs = await SharedPreferences.getInstance(); _isDarkModeEnabled = prefs.getBool('isDarkModeEnabled') ?? false; _isDeveloperModeEnabled = prefs.getBool('isDeveloperModeEnabled') ?? false; - _baseURL = prefs.getString('baseURL') ?? ''; + _baseURL = prefs.getString('baseURL') ?? 'http://127.0.0.1:8000/ap/v1'; + _restApiUtility.updateBaseURL(_baseURL); _continuousModeSteps = prefs.getInt('continuousModeSteps') ?? 10; notifyListeners(); } @@ -47,11 +51,12 @@ class SettingsViewModel extends ChangeNotifier { _saveBoolPreference('isDeveloperModeEnabled', value); } - /// Updates the state of Base URL and notifies listeners. + /// Updates the state of Base URL, notifies listeners, and updates the RestApiUtility baseURL. void updateBaseURL(String value) { _baseURL = value; notifyListeners(); _saveStringPreference('baseURL', value); + _restApiUtility.updateBaseURL(value); } /// Increments the number of Continuous Mode Steps and notifies listeners. diff --git a/frontend/lib/views/task/api_base_url_field.dart b/frontend/lib/views/settings/api_base_url_field.dart similarity index 80% rename from frontend/lib/views/task/api_base_url_field.dart rename to frontend/lib/views/settings/api_base_url_field.dart index 5bd080164f5d..caea7fb14778 100644 --- a/frontend/lib/views/task/api_base_url_field.dart +++ b/frontend/lib/views/settings/api_base_url_field.dart @@ -1,16 +1,16 @@ +import 'package:auto_gpt_flutter_client/viewmodels/settings_viewmodel.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; -import 'package:auto_gpt_flutter_client/viewmodels/api_settings_viewmodel.dart'; class ApiBaseUrlField extends StatelessWidget { - final TextEditingController controller; - - const ApiBaseUrlField({required this.controller}); + final TextEditingController controller = TextEditingController(); @override Widget build(BuildContext context) { - return Consumer( - builder: (context, apiSettingsViewModel, child) { + return Consumer( + builder: (context, settingsViewModel, child) { + // TODO: This view shouldn't know about the settings view model. It should use a delegate + controller.text = settingsViewModel.baseURL; return Padding( padding: const EdgeInsets.symmetric(horizontal: 16), child: Column( @@ -39,8 +39,8 @@ class ApiBaseUrlField extends StatelessWidget { children: [ ElevatedButton( onPressed: () { - controller.text = 'http://127.0.0.1:8000/api/v1'; - apiSettingsViewModel.updateBaseURL(controller.text); + controller.text = 'http://127.0.0.1:8000/ap/v1'; + settingsViewModel.updateBaseURL(controller.text); }, style: ElevatedButton.styleFrom( backgroundColor: Colors.white, @@ -53,7 +53,7 @@ class ApiBaseUrlField extends StatelessWidget { ), ElevatedButton( onPressed: () { - apiSettingsViewModel.updateBaseURL(controller.text); + settingsViewModel.updateBaseURL(controller.text); }, style: ElevatedButton.styleFrom( backgroundColor: Colors.white, diff --git a/frontend/lib/views/settings/settings_view.dart b/frontend/lib/views/settings/settings_view.dart index 758761b95f77..eccf2f60a08f 100644 --- a/frontend/lib/views/settings/settings_view.dart +++ b/frontend/lib/views/settings/settings_view.dart @@ -1,4 +1,5 @@ import 'package:auto_gpt_flutter_client/viewmodels/settings_viewmodel.dart'; +import 'package:auto_gpt_flutter_client/views/settings/api_base_url_field.dart'; import 'package:flutter/material.dart'; /// [SettingsView] displays a list of settings that the user can configure. @@ -32,16 +33,7 @@ class SettingsView extends StatelessWidget { onChanged: viewModel.toggleDeveloperMode, ), // Base URL Configuration - ListTile( - title: const Text('Base URL'), - subtitle: TextFormField( - initialValue: viewModel.baseURL, - onChanged: viewModel.updateBaseURL, - decoration: const InputDecoration( - hintText: 'Enter Base URL', - ), - ), - ), + ApiBaseUrlField(), // Continuous Mode Steps Configuration ListTile( title: const Text('Continuous Mode Steps'), diff --git a/frontend/lib/views/task/task_view.dart b/frontend/lib/views/task/task_view.dart index a1d1aa628910..78b8e91c95f2 100644 --- a/frontend/lib/views/task/task_view.dart +++ b/frontend/lib/views/task/task_view.dart @@ -1,7 +1,5 @@ import 'package:auto_gpt_flutter_client/models/task.dart'; import 'package:auto_gpt_flutter_client/models/test_suite.dart'; -import 'package:auto_gpt_flutter_client/viewmodels/api_settings_viewmodel.dart'; -import 'package:auto_gpt_flutter_client/views/task/api_base_url_field.dart'; import 'package:auto_gpt_flutter_client/views/task/test_suite_detail_view.dart'; import 'package:auto_gpt_flutter_client/views/task/test_suite_list_tile.dart'; import 'package:flutter/material.dart'; @@ -21,8 +19,6 @@ class TaskView extends StatefulWidget { } class _TaskViewState extends State { - final TextEditingController _baseUrlController = TextEditingController(); - @override void initState() { super.initState(); @@ -30,8 +26,6 @@ class _TaskViewState extends State { // Schedule the fetchTasks call for after the initial build WidgetsBinding.instance.addPostFrameCallback((_) { widget.viewModel.fetchAndCombineData(); - _baseUrlController.text = - Provider.of(context, listen: false).baseURL; }); } @@ -116,9 +110,6 @@ class _TaskViewState extends State { }, ), ), - const SizedBox(height: 16), - ApiBaseUrlField(controller: _baseUrlController), - const SizedBox(height: 16), ], ), if (widget.viewModel.selectedTestSuite != null)