feat(voice): integrate voice mode into Briefing screen
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -11,6 +11,8 @@ import '../../widgets/chat_message_bubble.dart';
|
|||||||
import '../../widgets/weather_card.dart';
|
import '../../widgets/weather_card.dart';
|
||||||
import '../../widgets/news_card.dart';
|
import '../../widgets/news_card.dart';
|
||||||
import 'briefing_history_screen.dart';
|
import 'briefing_history_screen.dart';
|
||||||
|
import '../../providers/voice_provider.dart';
|
||||||
|
import '../../widgets/voice_mic_button.dart';
|
||||||
|
|
||||||
class BriefingScreen extends ConsumerStatefulWidget {
|
class BriefingScreen extends ConsumerStatefulWidget {
|
||||||
const BriefingScreen({super.key});
|
const BriefingScreen({super.key});
|
||||||
@@ -55,6 +57,7 @@ class _BriefingScreenState extends ConsumerState<BriefingScreen>
|
|||||||
WidgetsBinding.instance.removeObserver(this);
|
WidgetsBinding.instance.removeObserver(this);
|
||||||
_controller.dispose();
|
_controller.dispose();
|
||||||
_scrollController.dispose();
|
_scrollController.dispose();
|
||||||
|
ref.read(voiceProvider.notifier).exitVoiceMode();
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,6 +109,26 @@ class _BriefingScreenState extends ConsumerState<BriefingScreen>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> _toggleVoiceMode() async {
|
||||||
|
final voice = ref.read(voiceProvider);
|
||||||
|
if (voice.voiceModeActive) {
|
||||||
|
ref.read(voiceProvider.notifier).exitVoiceMode();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await ref.read(voiceProvider.notifier).enterVoiceMode(
|
||||||
|
onTranscript: (transcript) async {
|
||||||
|
await ref.read(briefingProvider.notifier).sendReply(transcript);
|
||||||
|
},
|
||||||
|
enableTts: true,
|
||||||
|
onError: (msg) {
|
||||||
|
if (mounted) {
|
||||||
|
ScaffoldMessenger.of(context)
|
||||||
|
.showSnackBar(SnackBar(content: Text(msg)));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
Future<void> _refresh() async {
|
Future<void> _refresh() async {
|
||||||
setState(() => _refreshing = true);
|
setState(() => _refreshing = true);
|
||||||
try {
|
try {
|
||||||
@@ -125,10 +148,24 @@ class _BriefingScreenState extends ConsumerState<BriefingScreen>
|
|||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final briefingAsync = ref.watch(briefingProvider);
|
final briefingAsync = ref.watch(briefingProvider);
|
||||||
final isStreaming = ref.watch(isBriefingStreamingProvider);
|
final isStreaming = ref.watch(isBriefingStreamingProvider);
|
||||||
|
final voiceState = ref.watch(voiceProvider);
|
||||||
final scheme = Theme.of(context).colorScheme;
|
final scheme = Theme.of(context).colorScheme;
|
||||||
|
|
||||||
// Scroll to bottom when messages change
|
// Scroll to bottom when messages change
|
||||||
ref.listen(briefingProvider, (_, _) => _scrollToBottom());
|
ref.listen(briefingProvider, (prev, next) => _scrollToBottom());
|
||||||
|
|
||||||
|
// Feed streaming assistant content to VoiceNotifier for TTS.
|
||||||
|
ref.listen(briefingProvider, (prev, next) {
|
||||||
|
if (!voiceState.voiceModeActive) return;
|
||||||
|
final conv = next.value;
|
||||||
|
if (conv == null || conv.messages.isEmpty) return;
|
||||||
|
final last = conv.messages.last;
|
||||||
|
if (last.role != MessageRole.assistant) return;
|
||||||
|
final isComplete = last.status != 'generating';
|
||||||
|
ref
|
||||||
|
.read(voiceProvider.notifier)
|
||||||
|
.feedContent(last.content, isComplete: isComplete);
|
||||||
|
});
|
||||||
|
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
@@ -179,7 +216,7 @@ class _BriefingScreenState extends ConsumerState<BriefingScreen>
|
|||||||
),
|
),
|
||||||
body: briefingAsync.when(
|
body: briefingAsync.when(
|
||||||
loading: () => const Center(child: CircularProgressIndicator()),
|
loading: () => const Center(child: CircularProgressIndicator()),
|
||||||
error: (_, _) => Center(
|
error: (err, stack) => Center(
|
||||||
child: Column(
|
child: Column(
|
||||||
mainAxisSize: MainAxisSize.min,
|
mainAxisSize: MainAxisSize.min,
|
||||||
children: [
|
children: [
|
||||||
@@ -248,6 +285,21 @@ class _BriefingScreenState extends ConsumerState<BriefingScreen>
|
|||||||
color: scheme.primary,
|
color: scheme.primary,
|
||||||
),
|
),
|
||||||
|
|
||||||
|
// Voice mode banner
|
||||||
|
if (voiceState.voiceModeActive)
|
||||||
|
Container(
|
||||||
|
width: double.infinity,
|
||||||
|
color: const Color(0xFFEF4444).withValues(alpha: 0.12),
|
||||||
|
padding: const EdgeInsets.symmetric(
|
||||||
|
horizontal: 16, vertical: 6),
|
||||||
|
child: const Text(
|
||||||
|
'🎤 Listening… tap mic to exit voice mode',
|
||||||
|
style: TextStyle(
|
||||||
|
fontSize: 12,
|
||||||
|
color: Color(0xFFF87171),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
// Reply bar
|
// Reply bar
|
||||||
const Divider(height: 1),
|
const Divider(height: 1),
|
||||||
SafeArea(
|
SafeArea(
|
||||||
@@ -258,22 +310,35 @@ class _BriefingScreenState extends ConsumerState<BriefingScreen>
|
|||||||
Expanded(
|
Expanded(
|
||||||
child: TextField(
|
child: TextField(
|
||||||
controller: _controller,
|
controller: _controller,
|
||||||
decoration: const InputDecoration(
|
decoration: InputDecoration(
|
||||||
hintText: 'Reply to your briefing…',
|
hintText: voiceState.voiceModeActive
|
||||||
border: OutlineInputBorder(),
|
? 'Listening…'
|
||||||
|
: 'Reply to your briefing…',
|
||||||
|
hintStyle: voiceState.voiceModeActive
|
||||||
|
? const TextStyle(fontStyle: FontStyle.italic)
|
||||||
|
: null,
|
||||||
|
border: const OutlineInputBorder(),
|
||||||
isDense: true,
|
isDense: true,
|
||||||
contentPadding: EdgeInsets.symmetric(
|
contentPadding: const EdgeInsets.symmetric(
|
||||||
horizontal: 12, vertical: 10),
|
horizontal: 12, vertical: 10),
|
||||||
),
|
),
|
||||||
minLines: 1,
|
minLines: 1,
|
||||||
maxLines: 4,
|
maxLines: 4,
|
||||||
textInputAction: TextInputAction.newline,
|
textInputAction: TextInputAction.newline,
|
||||||
enabled: !isStreaming,
|
enabled: !isStreaming && !voiceState.voiceModeActive,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
const SizedBox(width: 8),
|
const SizedBox(width: 8),
|
||||||
|
VoiceMicButton(
|
||||||
|
mode: voiceState.mode,
|
||||||
|
voiceModeActive: voiceState.voiceModeActive,
|
||||||
|
onTap: _toggleVoiceMode,
|
||||||
|
),
|
||||||
|
const SizedBox(width: 6),
|
||||||
_GradientSendButton(
|
_GradientSendButton(
|
||||||
onPressed: isStreaming ? null : _sendReply,
|
onPressed: (isStreaming || voiceState.voiceModeActive)
|
||||||
|
? null
|
||||||
|
: _sendReply,
|
||||||
isStreaming: isStreaming,
|
isStreaming: isStreaming,
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
|||||||
Reference in New Issue
Block a user