cb3a09756f
The app was creating conversations with title 'New conversation'. The server only generates a title when conv_title is falsy (empty). With a non-empty title, should_gen_title is False for the first message (msg_count % 10 != 0), so auto-naming never fired. Now creates with empty title (matching web app behaviour). The list still displays 'New conversation' as a UI placeholder until the server-generated title arrives. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
129 lines
4.4 KiB
Dart
129 lines
4.4 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
|
|
import '../../core/constants.dart';
|
|
import '../../providers/chat_provider.dart';
|
|
|
|
class ConversationsTabScreen extends ConsumerWidget {
|
|
const ConversationsTabScreen({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
|
final theme = Theme.of(context);
|
|
final convsAsync = ref.watch(conversationsProvider);
|
|
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Text('Chat', style: theme.textTheme.titleLarge),
|
|
actions: [
|
|
IconButton(
|
|
icon: const Icon(Icons.add),
|
|
tooltip: 'New conversation',
|
|
onPressed: () async {
|
|
final conv = await ref
|
|
.read(conversationsProvider.notifier)
|
|
.create('');
|
|
if (context.mounted) {
|
|
context.push(Routes.chat.replaceFirst(':id', '${conv.id}'));
|
|
}
|
|
},
|
|
),
|
|
],
|
|
),
|
|
body: convsAsync.when(
|
|
loading: () => const Center(child: CircularProgressIndicator()),
|
|
error: (e, _) => Center(child: Text('Error: $e')),
|
|
data: (convs) {
|
|
if (convs.isEmpty) {
|
|
return Center(
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Icon(Icons.chat_bubble_outline,
|
|
size: 48, color: theme.colorScheme.onSurfaceVariant),
|
|
const SizedBox(height: 16),
|
|
Text('No conversations yet',
|
|
style: theme.textTheme.titleMedium),
|
|
const SizedBox(height: 8),
|
|
FilledButton.icon(
|
|
icon: const Icon(Icons.add),
|
|
label: const Text('Start a conversation'),
|
|
onPressed: () async {
|
|
final conv = await ref
|
|
.read(conversationsProvider.notifier)
|
|
.create('');
|
|
if (context.mounted) {
|
|
context.push(
|
|
Routes.chat.replaceFirst(':id', '${conv.id}'));
|
|
}
|
|
},
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
return RefreshIndicator(
|
|
onRefresh: () async => ref.invalidate(conversationsProvider),
|
|
child: ListView.builder(
|
|
itemCount: convs.length,
|
|
itemBuilder: (ctx, i) {
|
|
final c = convs[i];
|
|
return ListTile(
|
|
leading: const Icon(Icons.chat_bubble_outline),
|
|
title: Text(
|
|
c.title.isEmpty ? 'New conversation' : c.title,
|
|
maxLines: 1,
|
|
overflow: TextOverflow.ellipsis),
|
|
subtitle: Text(
|
|
_relativeTime(c.updatedAt),
|
|
style: theme.textTheme.labelSmall,
|
|
),
|
|
trailing: IconButton(
|
|
icon: const Icon(Icons.delete_outline),
|
|
onPressed: () =>
|
|
_confirmDelete(context, ref, c.id, c.title),
|
|
),
|
|
onTap: () =>
|
|
ctx.push(Routes.chat.replaceFirst(':id', '${c.id}')),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<void> _confirmDelete(
|
|
BuildContext context, WidgetRef ref, int id, String title) async {
|
|
final ok = await showDialog<bool>(
|
|
context: context,
|
|
builder: (_) => AlertDialog(
|
|
title: const Text('Delete conversation?'),
|
|
content: Text('"$title" will be permanently deleted.'),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(context, false),
|
|
child: const Text('Cancel')),
|
|
FilledButton(
|
|
onPressed: () => Navigator.pop(context, true),
|
|
child: const Text('Delete')),
|
|
],
|
|
),
|
|
);
|
|
if (ok == true) {
|
|
await ref.read(conversationsProvider.notifier).delete(id);
|
|
}
|
|
}
|
|
}
|
|
|
|
String _relativeTime(DateTime dt) {
|
|
final diff = DateTime.now().difference(dt);
|
|
if (diff.inMinutes < 1) return 'just now';
|
|
if (diff.inHours < 1) return '${diff.inMinutes}m ago';
|
|
if (diff.inDays < 1) return '${diff.inHours}h ago';
|
|
if (diff.inDays < 7) return '${diff.inDays}d ago';
|
|
return '${dt.day}/${dt.month}/${dt.year}';
|
|
}
|