feat(chat): stall watchdog, pull-to-refresh, and unfreeze on refresh
Mobile chat occasionally froze with the input disabled and nothing progressing — the underlying cause is SSE sockets dropping silently on mobile network handoffs / proxy idle timeouts. Dio never observes the close, so the send loop's `await for` hangs indefinitely with isStreaming=true. Three changes: - Wrap the SSE stream in a StreamIterator with a 45s per-event timeout as a stall watchdog. On timeout, break out and let the polling pass reconcile state from the server. - Add pull-to-refresh on the chat message list and an AppBar refresh button with inline spinner feedback. Works on both empty and populated states. - MessagesNotifier.refresh() now also clears isStreaming / streamingStatus when the server reports the latest assistant message as complete — so a stuck UI unfreezes the moment the user pulls down or taps refresh, even if the original SSE loop is still hanging. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -20,6 +20,25 @@ class _ChatScreenState extends ConsumerState<ChatScreen>
|
||||
with WidgetsBindingObserver {
|
||||
final _controller = TextEditingController();
|
||||
final _scrollController = ScrollController();
|
||||
bool _refreshing = false;
|
||||
|
||||
Future<void> _refreshMessages() async {
|
||||
if (_refreshing) return;
|
||||
setState(() => _refreshing = true);
|
||||
try {
|
||||
await ref
|
||||
.read(messagesProvider(widget.conversationId).notifier)
|
||||
.refresh();
|
||||
} catch (_) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('Could not refresh messages.')),
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
if (mounted) setState(() => _refreshing = false);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -136,6 +155,19 @@ class _ChatScreenState extends ConsumerState<ChatScreen>
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(convTitle?.isNotEmpty == true ? convTitle! : 'Chat'),
|
||||
actions: [
|
||||
IconButton(
|
||||
tooltip: 'Refresh',
|
||||
onPressed: _refreshing ? null : _refreshMessages,
|
||||
icon: _refreshing
|
||||
? const SizedBox(
|
||||
width: 18,
|
||||
height: 18,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: const Icon(Icons.refresh),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: Column(
|
||||
children: [
|
||||
@@ -147,22 +179,32 @@ class _ChatScreenState extends ConsumerState<ChatScreen>
|
||||
child: Text('Could not load messages.'),
|
||||
),
|
||||
data: (messages) {
|
||||
if (messages.isEmpty) {
|
||||
return const Center(
|
||||
child: Text('Send a message to start.'));
|
||||
}
|
||||
return ListView.builder(
|
||||
controller: _scrollController,
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 8, vertical: 12),
|
||||
itemCount: messages.length,
|
||||
itemBuilder: (context, i) => ChatMessageBubble(
|
||||
message: messages[i],
|
||||
streamingStatus: (i == messages.length - 1 &&
|
||||
messages[i].status == 'generating')
|
||||
? streamingStatus
|
||||
: '',
|
||||
),
|
||||
return RefreshIndicator(
|
||||
onRefresh: _refreshMessages,
|
||||
child: messages.isEmpty
|
||||
? ListView(
|
||||
// Needs to be scrollable for RefreshIndicator to
|
||||
// fire on empty state — plain Center won't work.
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
children: const [
|
||||
SizedBox(height: 240),
|
||||
Center(child: Text('Send a message to start.')),
|
||||
],
|
||||
)
|
||||
: ListView.builder(
|
||||
controller: _scrollController,
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 8, vertical: 12),
|
||||
itemCount: messages.length,
|
||||
itemBuilder: (context, i) => ChatMessageBubble(
|
||||
message: messages[i],
|
||||
streamingStatus: (i == messages.length - 1 &&
|
||||
messages[i].status == 'generating')
|
||||
? streamingStatus
|
||||
: '',
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user