mirror of
https://github.com/aljazceru/Auto-GPT.git
synced 2025-12-17 22:14:28 +01:00
Refactor Settings and Task Views for API Base URL Management
- Deprecated `ApiSettingsViewModel` in favor of enhancing `SettingsViewModel`. - Moved the API Base URL field from `TaskView` to `SettingsView` to centralize configuration. - Integrated `RestApiUtility` dependency into `SettingsViewModel` to ensure consistent URL management across the app.
This commit is contained in:
@@ -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<RestApiUtility, ApiSettingsViewModel>(
|
||||
create: (context) => ApiSettingsViewModel(
|
||||
ChangeNotifierProxyProvider<RestApiUtility, SettingsViewModel>(
|
||||
create: (context) => SettingsViewModel(
|
||||
Provider.of<RestApiUtility>(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<ChatService>(context, listen: false))),
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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.
|
||||
|
||||
@@ -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<ApiSettingsViewModel>(
|
||||
builder: (context, apiSettingsViewModel, child) {
|
||||
return Consumer<SettingsViewModel>(
|
||||
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,
|
||||
@@ -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'),
|
||||
|
||||
@@ -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<TaskView> {
|
||||
final TextEditingController _baseUrlController = TextEditingController();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
@@ -30,8 +26,6 @@ class _TaskViewState extends State<TaskView> {
|
||||
// Schedule the fetchTasks call for after the initial build
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
widget.viewModel.fetchAndCombineData();
|
||||
_baseUrlController.text =
|
||||
Provider.of<ApiSettingsViewModel>(context, listen: false).baseURL;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -116,9 +110,6 @@ class _TaskViewState extends State<TaskView> {
|
||||
},
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
ApiBaseUrlField(controller: _baseUrlController),
|
||||
const SizedBox(height: 16),
|
||||
],
|
||||
),
|
||||
if (widget.viewModel.selectedTestSuite != null)
|
||||
|
||||
Reference in New Issue
Block a user