Files
Auto-GPT/frontend/lib/views/task/task_view.dart
hunteraraujo 8923e79b29 Refactor TaskView to Support Combined Data and Test Suite Detail View
This commit introduces substantial improvements to the TaskView class to accommodate both tasks and test suites in a unified view. It also integrates the TestSuiteDetailView to display test suite details when a test suite is selected.

Key Enhancements:

1. Modified the `initState` method to call `fetchAndCombineData()` from TaskViewModel, thereby populating the combined data source.
2. Replaced the ListView that was rendering tasks with a ListView that can render both tasks and test suites.
3. Introduced conditional rendering for TestSuiteDetailView when a test suite is selected.
4. Updated onTap actions to select and deselect tasks and test suites appropriately.
5. Moved to using a Stack layout to allow overlay of TestSuiteDetailView on top of the existing layout.

This refactor enhances the TaskView's capabilities to manage and display both tasks and test suites, offering a more integrated user experience.
2023-09-18 15:08:22 -07:00

140 lines
5.6 KiB
Dart

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';
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/views/task/new_task_button.dart';
import 'package:auto_gpt_flutter_client/views/task/task_list_tile.dart';
import 'package:provider/provider.dart';
class TaskView extends StatefulWidget {
final TaskViewModel viewModel;
const TaskView({Key? key, required this.viewModel}) : super(key: key);
@override
_TaskViewState createState() => _TaskViewState();
}
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;
});
}
@override
Widget build(BuildContext context) {
// Combine tasks and test suites into a single list
final items = widget.viewModel.combinedDataSource;
return Scaffold(
backgroundColor: Colors.white,
body: Stack(
children: [
Column(
children: [
// Title and New Task button
Padding(
padding: const EdgeInsets.all(8.0),
child: NewTaskButton(
onPressed: () async {
// Update the current task ID and chats in ChatViewModel
final chatViewModel =
Provider.of<ChatViewModel>(context, listen: false);
chatViewModel.clearCurrentTaskAndChats();
widget.viewModel.deselectTask();
print(
'New Task button pressed, cleared current task ID and chats');
},
)),
// Task List
Expanded(
child: ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
final item = items[index];
if (item is Task) {
return TaskListTile(
task: item,
onTap: () {
// Select the task in TaskViewModel
widget.viewModel.selectTask(item.id);
// Update the current task ID in ChatViewModel
// TODO: Do we want to have a reference to chat view model in this class?
final chatViewModel = Provider.of<ChatViewModel>(
context,
listen: false);
chatViewModel.setCurrentTaskId(item.id);
print('Task ${item.title} tapped');
},
onDelete: () {
// Delete the task in TaskViewModel
widget.viewModel.deleteTask(item.id);
// TODO: Do we want to have a reference to chat view model in this class?
final chatViewModel = Provider.of<ChatViewModel>(
context,
listen: false);
if (chatViewModel.currentTaskId == item.id) {
chatViewModel.clearCurrentTaskAndChats();
}
print('Task ${item.title} delete button tapped');
},
selected: item.id == widget.viewModel.selectedTask?.id,
);
} else if (item is TestSuite) {
return TestSuiteListTile(
testSuite: item,
onTap: () {
// Navigate to the new view for this test suite
widget.viewModel.deselectTask();
widget.viewModel.selectTestSuite(item);
// TODO: Do we want to have a reference to chat view model in this class?
Provider.of<ChatViewModel>(context, listen: false)
.clearCurrentTaskAndChats();
},
);
} else {
return const SizedBox
.shrink(); // return an empty widget if type is unknown
}
},
),
),
const SizedBox(height: 16),
ApiBaseUrlField(controller: _baseUrlController),
const SizedBox(height: 16),
],
),
if (widget.viewModel.selectedTestSuite != null)
Positioned(
top: 0,
left: 0,
right: 0,
bottom: 0,
child: TestSuiteDetailView(
testSuite: widget.viewModel.selectedTestSuite!,
viewModel: widget.viewModel,
),
),
],
),
);
}
}