Implemented subject suggestions in Task Creation and Edit.

Fix editing redirects
This commit is contained in:
Marc Rejohn Castillano 2026-03-03 18:39:01 +08:00
parent bfcca47353
commit b1f5d209a2
2 changed files with 152 additions and 81 deletions

View File

@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_typeahead/flutter_typeahead.dart';
import 'package:tasq/utils/app_time.dart';
import 'package:go_router/go_router.dart';
@ -23,6 +24,7 @@ import '../../widgets/tasq_adaptive_list.dart';
import '../../widgets/typing_dots.dart';
import '../../theme/app_surfaces.dart';
import '../../utils/snackbar.dart';
import '../../utils/subject_suggestions.dart';
// request metadata options used in task creation/editing dialogs
const List<String> _requestTypeOptions = [
@ -565,6 +567,14 @@ class _TasksListScreenState extends ConsumerState<TasksListScreen>
) async {
final titleController = TextEditingController();
final descriptionController = TextEditingController();
final existingSubjects = <String>[
...((ref.read(tasksProvider).valueOrNull ?? const <Task>[]).map(
(task) => task.title,
)),
...((ref.read(ticketsProvider).valueOrNull ?? const <Ticket>[]).map(
(ticket) => ticket.subject,
)),
];
String? selectedOfficeId;
String? selectedRequestType;
String? requestTypeOther;
@ -585,13 +595,31 @@ class _TasksListScreenState extends ConsumerState<TasksListScreen>
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
TextField(
TypeAheadFormField<String>(
textFieldConfiguration: TextFieldConfiguration(
controller: titleController,
decoration: const InputDecoration(
labelText: 'Task title',
),
enabled: !saving,
),
suggestionsCallback: (pattern) async {
return SubjectSuggestionEngine.suggest(
existingSubjects: existingSubjects,
query: pattern,
limit: 8,
);
},
itemBuilder: (context, suggestion) =>
ListTile(dense: true, title: Text(suggestion)),
onSuggestionSelected: (suggestion) {
titleController
..text = suggestion
..selection = TextSelection.collapsed(
offset: suggestion.length,
);
},
),
const SizedBox(height: 12),
TextField(
controller: descriptionController,
@ -710,7 +738,10 @@ class _TasksListScreenState extends ConsumerState<TasksListScreen>
onPressed: saving
? null
: () async {
final title = titleController.text.trim();
final title =
SubjectSuggestionEngine.normalizeDisplay(
titleController.text.trim(),
);
final description = descriptionController.text.trim();
final officeId = selectedOfficeId;
if (title.isEmpty || officeId == null) {

View File

@ -890,6 +890,8 @@ class _TicketDetailScreenState extends ConsumerState<TicketDetailScreen> {
WidgetRef ref,
Ticket ticket,
) async {
final screenContext = context;
final dialogShape = AppSurfaces.of(context).dialogShape;
final officesAsync = ref.watch(officesOnceProvider);
final subjectCtrl = TextEditingController(text: ticket.subject);
final descCtrl = TextEditingController(text: ticket.description);
@ -898,8 +900,11 @@ class _TicketDetailScreenState extends ConsumerState<TicketDetailScreen> {
await showDialog<void>(
context: context,
builder: (dialogContext) {
var saving = false;
return StatefulBuilder(
builder: (dialogBuilderContext, setDialogState) {
return AlertDialog(
shape: AppSurfaces.of(context).dialogShape,
shape: dialogShape,
title: const Text('Edit Ticket'),
content: SingleChildScrollView(
child: Column(
@ -907,12 +912,16 @@ class _TicketDetailScreenState extends ConsumerState<TicketDetailScreen> {
children: [
TextField(
controller: subjectCtrl,
enabled: !saving,
decoration: const InputDecoration(labelText: 'Subject'),
),
const SizedBox(height: 8),
TextField(
controller: descCtrl,
decoration: const InputDecoration(labelText: 'Description'),
enabled: !saving,
decoration: const InputDecoration(
labelText: 'Description',
),
maxLines: 4,
),
const SizedBox(height: 8),
@ -926,16 +935,23 @@ class _TicketDetailScreenState extends ConsumerState<TicketDetailScreen> {
);
return DropdownButtonFormField<String?>(
initialValue: selectedOffice,
decoration: const InputDecoration(labelText: 'Office'),
decoration: const InputDecoration(
labelText: 'Office',
),
items: [
const DropdownMenuItem(
value: null,
child: Text('Unassigned'),
),
for (final o in officesSorted)
DropdownMenuItem(value: o.id, child: Text(o.name)),
DropdownMenuItem(
value: o.id,
child: Text(o.name),
),
],
onChanged: (v) => selectedOffice = v,
onChanged: saving
? null
: (v) => setDialogState(() => selectedOffice = v),
);
},
loading: () => const SizedBox.shrink(),
@ -946,13 +962,18 @@ class _TicketDetailScreenState extends ConsumerState<TicketDetailScreen> {
),
actions: [
TextButton(
onPressed: () => Navigator.of(dialogContext).pop(),
onPressed: saving
? null
: () => Navigator.of(dialogContext).pop(),
child: const Text('Cancel'),
),
ElevatedButton(
onPressed: () async {
onPressed: saving
? null
: () async {
final subject = subjectCtrl.text.trim();
final desc = descCtrl.text.trim();
setDialogState(() => saving = true);
try {
await ref
.read(ticketsControllerProvider)
@ -962,24 +983,43 @@ class _TicketDetailScreenState extends ConsumerState<TicketDetailScreen> {
description: desc.isEmpty ? null : desc,
officeId: selectedOffice,
);
if (!mounted) return;
WidgetsBinding.instance.addPostFrameCallback((_) {
Navigator.of(context).pop();
showSuccessSnackBar(context, 'Ticket updated');
});
ref.invalidate(ticketsProvider);
ref.invalidate(ticketByIdProvider(ticket.id));
if (!dialogContext.mounted ||
!screenContext.mounted) {
return;
}
Navigator.of(dialogContext).pop();
showSuccessSnackBar(
screenContext,
'Ticket updated',
);
} catch (e) {
if (!mounted) return;
WidgetsBinding.instance.addPostFrameCallback((_) {
showErrorSnackBar(context, 'Failed to update ticket: $e');
});
if (!screenContext.mounted) return;
showErrorSnackBar(
screenContext,
'Failed to update ticket: $e',
);
} finally {
if (dialogContext.mounted) {
setDialogState(() => saving = false);
}
}
},
child: const Text('Save'),
child: saving
? const SizedBox(
width: 18,
height: 18,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Text('Save'),
),
],
);
},
);
},
);
}
Widget _timelineRow(String label, DateTime? value) {