Skip to content

Commit

Permalink
Refactor Settings and Task Views for API Base URL Management
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
hunteraraujo committed Sep 25, 2023
1 parent ffa76c3 commit da8b9d5
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 70 deletions.
12 changes: 4 additions & 8 deletions frontend/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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))),
Expand Down
30 changes: 0 additions & 30 deletions frontend/lib/viewmodels/api_settings_viewmodel.dart

This file was deleted.

13 changes: 9 additions & 4 deletions frontend/lib/viewmodels/settings_viewmodel.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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;
Expand All @@ -19,16 +22,17 @@ 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
Future<void> _loadPreferences() async {
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();
}
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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,
Expand All @@ -53,7 +53,7 @@ class ApiBaseUrlField extends StatelessWidget {
),
ElevatedButton(
onPressed: () {
apiSettingsViewModel.updateBaseURL(controller.text);
settingsViewModel.updateBaseURL(controller.text);
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.white,
Expand Down
12 changes: 2 additions & 10 deletions frontend/lib/views/settings/settings_view.dart
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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'),
Expand Down
9 changes: 0 additions & 9 deletions frontend/lib/views/task/task_view.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -21,17 +19,13 @@ class TaskView extends StatefulWidget {
}

class _TaskViewState extends State<TaskView> {
final TextEditingController _baseUrlController = TextEditingController();

@override
void initState() {
super.initState();

// Schedule the fetchTasks call for after the initial build
WidgetsBinding.instance.addPostFrameCallback((_) {
widget.viewModel.fetchAndCombineData();
_baseUrlController.text =
Provider.of<ApiSettingsViewModel>(context, listen: false).baseURL;
});
}

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit da8b9d5

Please sign in to comment.