From dbec110bac0438e0dfd2f7d42a1169b31b738adb Mon Sep 17 00:00:00 2001 From: hunteraraujo Date: Tue, 10 Oct 2023 18:39:24 -0700 Subject: [PATCH] Refactor ChatInputField to use SharedPreferencesService --- frontend/lib/main.dart | 8 ++++++-- frontend/lib/viewmodels/chat_viewmodel.dart | 5 ++++- frontend/lib/viewmodels/task_viewmodel.dart | 1 - frontend/lib/views/chat/chat_input_field.dart | 14 ++++++++++---- frontend/lib/views/chat/chat_view.dart | 1 + 5 files changed, 21 insertions(+), 8 deletions(-) diff --git a/frontend/lib/main.dart b/frontend/lib/main.dart index 452aacb344f7..0b50cce4a1d7 100644 --- a/frontend/lib/main.dart +++ b/frontend/lib/main.dart @@ -99,8 +99,12 @@ class MyApp extends StatelessWidget { return MultiProvider( providers: [ ChangeNotifierProvider( - create: (context) => ChatViewModel( - Provider.of(context, listen: false))), + create: (context) => ChatViewModel( + Provider.of(context, listen: false), + Provider.of(context, + listen: false), + ), + ), ChangeNotifierProvider( create: (context) => TaskViewModel( Provider.of(context, listen: false), diff --git a/frontend/lib/viewmodels/chat_viewmodel.dart b/frontend/lib/viewmodels/chat_viewmodel.dart index 3dfec1dee5d0..2056c8743a0f 100644 --- a/frontend/lib/viewmodels/chat_viewmodel.dart +++ b/frontend/lib/viewmodels/chat_viewmodel.dart @@ -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'; @@ -9,10 +10,12 @@ class ChatViewModel with ChangeNotifier { final ChatService _chatService; List _chats = []; String? _currentTaskId; + final SharedPreferencesService _prefsService; bool _isWaitingForAgentResponse = false; bool get isWaitingForAgentResponse => _isWaitingForAgentResponse; + SharedPreferencesService get prefsService => _prefsService; bool _isContinuousMode = false; @@ -22,7 +25,7 @@ class ChatViewModel with ChangeNotifier { notifyListeners(); } - ChatViewModel(this._chatService); + ChatViewModel(this._chatService, this._prefsService); /// Returns the current list of chats. List get chats => _chats; diff --git a/frontend/lib/viewmodels/task_viewmodel.dart b/frontend/lib/viewmodels/task_viewmodel.dart index 94d8bf94d7a5..eca6554e797a 100644 --- a/frontend/lib/viewmodels/task_viewmodel.dart +++ b/frontend/lib/viewmodels/task_viewmodel.dart @@ -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 { diff --git a/frontend/lib/views/chat/chat_input_field.dart b/frontend/lib/views/chat/chat_input_field.dart index c3dc4601efc9..b46badd61833 100644 --- a/frontend/lib/views/chat/chat_input_field.dart +++ b/frontend/lib/views/chat/chat_input_field.dart @@ -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'; @@ -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 @@ -42,9 +46,10 @@ class _ChatInputFieldState extends State { } Future _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( @@ -56,7 +61,8 @@ class _ChatInputFieldState extends State { _executeContinuousMode(); }, onCheckboxChanged: (bool value) async { - await prefs.setBool('showContinuousModeDialog', !value); + await widget.viewModel.prefsService + .setBool('showContinuousModeDialog', !value); }, ); }, diff --git a/frontend/lib/views/chat/chat_view.dart b/frontend/lib/views/chat/chat_view.dart index 5f2b35c25c6d..40117d9b0297 100644 --- a/frontend/lib/views/chat/chat_view.dart +++ b/frontend/lib/views/chat/chat_view.dart @@ -152,6 +152,7 @@ class _ChatViewState extends State { !widget.viewModel.isContinuousMode; }, isContinuousMode: widget.viewModel.isContinuousMode, + viewModel: widget.viewModel, ), ), ],