Refactor LeaderboardSubmissionDialog to use SharedPreferencesService

pull/5595/merge
hunteraraujo 2023-10-10 23:19:35 -07:00
parent dbec110bac
commit 57bcbdf45c
4 changed files with 29 additions and 15 deletions

View File

@ -116,8 +116,11 @@ class MyApp extends StatelessWidget {
create: (context) => SkillTreeViewModel()),
ChangeNotifierProvider(
create: (context) => TaskQueueViewModel(
Provider.of<BenchmarkService>(context, listen: false),
Provider.of<LeaderboardService>(context, listen: false)),
Provider.of<BenchmarkService>(context, listen: false),
Provider.of<LeaderboardService>(context, listen: false),
Provider.of<SharedPreferencesService>(context,
listen: false),
),
),
],
child: MainLayout(),

View File

@ -10,6 +10,7 @@ import 'package:auto_gpt_flutter_client/models/test_option.dart';
import 'package:auto_gpt_flutter_client/models/test_suite.dart';
import 'package:auto_gpt_flutter_client/services/benchmark_service.dart';
import 'package:auto_gpt_flutter_client/services/leaderboard_service.dart';
import 'package:auto_gpt_flutter_client/services/shared_preferences_service.dart';
import 'package:auto_gpt_flutter_client/viewmodels/chat_viewmodel.dart';
import 'package:auto_gpt_flutter_client/viewmodels/task_viewmodel.dart';
import 'package:collection/collection.dart';
@ -20,6 +21,7 @@ import 'package:auto_gpt_flutter_client/utils/stack.dart';
class TaskQueueViewModel extends ChangeNotifier {
final BenchmarkService benchmarkService;
final LeaderboardService leaderboardService;
final SharedPreferencesService prefsService;
bool isBenchmarkRunning = false;
Map<SkillTreeNode, BenchmarkTaskStatus> benchmarkStatusMap = {};
List<BenchmarkRun> currentBenchmarkRuns = [];
@ -29,7 +31,8 @@ class TaskQueueViewModel extends ChangeNotifier {
TestOption get selectedOption => _selectedOption;
List<SkillTreeNode>? get selectedNodeHierarchy => _selectedNodeHierarchy;
TaskQueueViewModel(this.benchmarkService, this.leaderboardService);
TaskQueueViewModel(
this.benchmarkService, this.leaderboardService, this.prefsService);
void updateSelectedNodeHierarchyBasedOnOption(
TestOption selectedOption,

View File

@ -1,14 +1,18 @@
import 'package:auto_gpt_flutter_client/constants/app_colors.dart';
import 'package:auto_gpt_flutter_client/utils/uri_utility.dart';
import 'package:auto_gpt_flutter_client/viewmodels/task_queue_viewmodel.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
class LeaderboardSubmissionDialog extends StatefulWidget {
final Function(String, String, String)? onSubmit;
// TODO: Create a view model for this class and remove the TaskQueueViewModel
final TaskQueueViewModel viewModel;
const LeaderboardSubmissionDialog({
Key? key,
this.onSubmit,
required this.viewModel,
}) : super(key: key);
@override
@ -26,9 +30,6 @@ class _LeaderboardSubmissionDialogState
String? _repoUrlError;
String? _commitShaError;
// TODO: Do we want this dialog to have the responsibiltiy of managing shared preferences?
SharedPreferences? _prefs;
@override
void initState() {
super.initState();
@ -36,12 +37,13 @@ class _LeaderboardSubmissionDialogState
}
Future<void> _initSharedPreferences() async {
_prefs = await SharedPreferences.getInstance();
setState(() {
_teamNameController.text = _prefs!.getString('teamName') ?? '';
_repoUrlController.text = _prefs!.getString('repoUrl') ?? '';
_commitShaController.text = _prefs!.getString('commitSha') ?? '';
});
// Using the SharedPreferencesService from the viewModel to get stored values
_teamNameController.text =
await widget.viewModel.prefsService.getString('teamName') ?? '';
_repoUrlController.text =
await widget.viewModel.prefsService.getString('repoUrl') ?? '';
_commitShaController.text =
await widget.viewModel.prefsService.getString('commitSha') ?? '';
}
void _validateAndSubmit() async {
@ -87,9 +89,13 @@ class _LeaderboardSubmissionDialogState
}
Future<void> _saveToSharedPreferences() async {
await _prefs!.setString('teamName', _teamNameController.text);
await _prefs!.setString('repoUrl', _repoUrlController.text);
await _prefs!.setString('commitSha', _commitShaController.text);
// Using the prefsService to save the values
await widget.viewModel.prefsService
.setString('teamName', _teamNameController.text);
await widget.viewModel.prefsService
.setString('repoUrl', _repoUrlController.text);
await widget.viewModel.prefsService
.setString('commitSha', _commitShaController.text);
}
@override

View File

@ -13,6 +13,7 @@ import 'package:provider/provider.dart';
class TaskQueueView extends StatelessWidget {
@override
Widget build(BuildContext context) {
// TODO: This should be injected instead
final viewModel = Provider.of<TaskQueueViewModel>(context);
// Node hierarchy
@ -135,6 +136,7 @@ class TaskQueueView extends StatelessWidget {
viewModel.submitToLeaderboard(
teamName, repoUrl, commitSha);
},
viewModel: viewModel,
),
);
},