Skip to content

Commit

Permalink
Refactor ChatInputField to use SharedPreferencesService
Browse files Browse the repository at this point in the history
  • Loading branch information
hunteraraujo committed Oct 11, 2023
1 parent 8245433 commit dbec110
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
8 changes: 6 additions & 2 deletions frontend/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,12 @@ class MyApp extends StatelessWidget {
return MultiProvider(
providers: [
ChangeNotifierProvider(
create: (context) => ChatViewModel(
Provider.of<ChatService>(context, listen: false))),
create: (context) => ChatViewModel(
Provider.of<ChatService>(context, listen: false),
Provider.of<SharedPreferencesService>(context,
listen: false),
),
),
ChangeNotifierProvider(
create: (context) => TaskViewModel(
Provider.of<TaskService>(context, listen: false),
Expand Down
5 changes: 4 additions & 1 deletion frontend/lib/viewmodels/chat_viewmodel.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:auto_gpt_flutter_client/models/step.dart';
import 'package:auto_gpt_flutter_client/models/step_request_body.dart';
import 'package:auto_gpt_flutter_client/services/shared_preferences_service.dart';
import 'package:flutter/foundation.dart';
import 'package:auto_gpt_flutter_client/services/chat_service.dart';
import 'package:auto_gpt_flutter_client/models/chat.dart';
Expand All @@ -9,10 +10,12 @@ class ChatViewModel with ChangeNotifier {
final ChatService _chatService;
List<Chat> _chats = [];
String? _currentTaskId;
final SharedPreferencesService _prefsService;

bool _isWaitingForAgentResponse = false;

bool get isWaitingForAgentResponse => _isWaitingForAgentResponse;
SharedPreferencesService get prefsService => _prefsService;

bool _isContinuousMode = false;

Expand All @@ -22,7 +25,7 @@ class ChatViewModel with ChangeNotifier {
notifyListeners();
}

ChatViewModel(this._chatService);
ChatViewModel(this._chatService, this._prefsService);

/// Returns the current list of chats.
List<Chat> get chats => _chats;
Expand Down
1 change: 0 additions & 1 deletion frontend/lib/viewmodels/task_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:flutter/foundation.dart';
import 'package:collection/collection.dart';
import 'package:auto_gpt_flutter_client/services/task_service.dart';
import 'package:auto_gpt_flutter_client/models/task_request_body.dart';
import 'package:shared_preferences/shared_preferences.dart';

// TODO: How will all these functions work with test suites?
class TaskViewModel with ChangeNotifier {
Expand Down
14 changes: 10 additions & 4 deletions frontend/lib/views/chat/chat_input_field.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:auto_gpt_flutter_client/viewmodels/chat_viewmodel.dart';
import 'package:auto_gpt_flutter_client/views/chat/continuous_mode_dialog.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
Expand All @@ -7,12 +8,15 @@ class ChatInputField extends StatefulWidget {
final Function(String) onSendPressed;
final Function() onContinuousModePressed;
final bool isContinuousMode;
// TODO: Create a view model for this class and remove the ChatViewModel
final ChatViewModel viewModel;

const ChatInputField({
Key? key,
required this.onSendPressed,
required this.onContinuousModePressed,
this.isContinuousMode = false,
required this.viewModel,
}) : super(key: key);

@override
Expand Down Expand Up @@ -42,9 +46,10 @@ class _ChatInputFieldState extends State<ChatInputField> {
}

Future<void> _presentContinuousModeDialogIfNeeded() async {
final prefs = await SharedPreferences.getInstance();
final showContinuousModeDialog =
prefs.getBool('showContinuousModeDialog') ?? true;
final showContinuousModeDialog = await widget.viewModel.prefsService
.getBool('showContinuousModeDialog') ??
true;

FocusScope.of(context).requestFocus(_throwawayFocusNode);
if (showContinuousModeDialog) {
showDialog(
Expand All @@ -56,7 +61,8 @@ class _ChatInputFieldState extends State<ChatInputField> {
_executeContinuousMode();
},
onCheckboxChanged: (bool value) async {
await prefs.setBool('showContinuousModeDialog', !value);
await widget.viewModel.prefsService
.setBool('showContinuousModeDialog', !value);
},
);
},
Expand Down
1 change: 1 addition & 0 deletions frontend/lib/views/chat/chat_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ class _ChatViewState extends State<ChatView> {
!widget.viewModel.isContinuousMode;
},
isContinuousMode: widget.viewModel.isContinuousMode,
viewModel: widget.viewModel,
),
),
],
Expand Down

0 comments on commit dbec110

Please sign in to comment.