Phase 3.1: Complete mitra_app Riverpod migration (all blocs, fix auth bug)
- Migrate AuthBloc → MitraAuthNotifier (fixes stuck-loading bug: now returns MitraAuthInitialData when currentUser is null) - Migrate StatusBloc → OnlineStatusNotifier (heartbeat timer + lifecycle) - Migrate ExtensionBloc → MitraExtensionNotifier (accept/reject + goodbye) - Migrate ChatRequestBloc → ChatRequestNotifier (WebSocket incoming requests) - Migrate MitraChatBloc → MitraChatNotifier (WebSocket chat + messages) - Update router to use Riverpod auth state for redirects - Remove all flutter_bloc usage from mitra_app screens and main.dart - MultiBlocProvider fully removed from mitra_app Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,22 +1,22 @@
|
||||
import 'dart:async';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import '../../../core/chat/mitra_chat_bloc.dart';
|
||||
import '../../../core/chat/extension_bloc.dart';
|
||||
import '../../../core/chat/mitra_chat_notifier.dart';
|
||||
import '../../../core/chat/extension_notifier.dart';
|
||||
import '../../../core/constants.dart';
|
||||
|
||||
class MitraChatScreen extends StatefulWidget {
|
||||
class MitraChatScreen extends ConsumerStatefulWidget {
|
||||
final String sessionId;
|
||||
final String customerName;
|
||||
|
||||
const MitraChatScreen({super.key, required this.sessionId, required this.customerName});
|
||||
|
||||
@override
|
||||
State<MitraChatScreen> createState() => _MitraChatScreenState();
|
||||
ConsumerState<MitraChatScreen> createState() => _MitraChatScreenState();
|
||||
}
|
||||
|
||||
class _MitraChatScreenState extends State<MitraChatScreen> {
|
||||
class _MitraChatScreenState extends ConsumerState<MitraChatScreen> {
|
||||
final _messageController = TextEditingController();
|
||||
final _scrollController = ScrollController();
|
||||
Timer? _typingThrottle;
|
||||
@@ -24,12 +24,12 @@ class _MitraChatScreenState extends State<MitraChatScreen> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
context.read<MitraChatBloc>().add(ConnectChat(widget.sessionId));
|
||||
ref.read(mitraChatProvider.notifier).connect(widget.sessionId);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
context.read<MitraChatBloc>().add(DisconnectChat());
|
||||
ref.read(mitraChatProvider.notifier).disconnect();
|
||||
_messageController.dispose();
|
||||
_scrollController.dispose();
|
||||
_typingThrottle?.cancel();
|
||||
@@ -50,100 +50,89 @@ class _MitraChatScreenState extends State<MitraChatScreen> {
|
||||
|
||||
void _onTextChanged(String text) {
|
||||
if (_typingThrottle?.isActive ?? false) return;
|
||||
context.read<MitraChatBloc>().add(SendTyping());
|
||||
ref.read(mitraChatProvider.notifier).sendTyping();
|
||||
_typingThrottle = Timer(const Duration(seconds: 2), () {});
|
||||
}
|
||||
|
||||
void _sendMessage() {
|
||||
final text = _messageController.text.trim();
|
||||
if (text.isEmpty) return;
|
||||
context.read<MitraChatBloc>().add(SendMessage(text));
|
||||
ref.read(mitraChatProvider.notifier).sendMessage(text);
|
||||
_messageController.clear();
|
||||
_scrollToBottom();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MultiBlocListener(
|
||||
listeners: [
|
||||
BlocListener<MitraChatBloc, MitraChatState>(
|
||||
listener: (context, state) {
|
||||
if (state is ChatConnected) {
|
||||
_scrollToBottom();
|
||||
final unread = state.messages
|
||||
.where((m) => m.senderType == UserType.customer && m.status != MessageStatus.read)
|
||||
.map((m) => m.id)
|
||||
.toList();
|
||||
if (unread.isNotEmpty) {
|
||||
context.read<MitraChatBloc>().add(MarkMessagesRead(unread));
|
||||
}
|
||||
if (state.sessionClosing) {
|
||||
// Trigger goodbye view
|
||||
}
|
||||
}
|
||||
},
|
||||
),
|
||||
BlocListener<ExtensionBloc, ExtensionState>(
|
||||
listener: (context, state) {
|
||||
if (state is ExtensionComplete) {
|
||||
context.go('/home');
|
||||
}
|
||||
},
|
||||
),
|
||||
],
|
||||
child: Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(widget.customerName),
|
||||
actions: [
|
||||
BlocBuilder<MitraChatBloc, MitraChatState>(
|
||||
builder: (context, state) {
|
||||
if (state is ChatConnected && state.remainingSeconds != null) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(right: 16),
|
||||
child: Center(
|
||||
child: Text(
|
||||
'${state.remainingSeconds}s',
|
||||
style: TextStyle(
|
||||
color: state.remainingSeconds! < 30 ? Colors.red : null,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
return const SizedBox.shrink();
|
||||
},
|
||||
final chatState = ref.watch(mitraChatProvider);
|
||||
final extState = ref.watch(mitraExtensionProvider);
|
||||
|
||||
// Listen for extension complete → navigate home
|
||||
ref.listen(mitraExtensionProvider, (prev, next) {
|
||||
if (next is ExtensionCompleteData) {
|
||||
context.go('/home');
|
||||
}
|
||||
});
|
||||
|
||||
// Listen for chat state changes
|
||||
ref.listen(mitraChatProvider, (prev, next) {
|
||||
if (next is MitraChatConnectedData) {
|
||||
_scrollToBottom();
|
||||
final unread = next.messages
|
||||
.where((m) => m.senderType == UserType.customer && m.status != MessageStatus.read)
|
||||
.map((m) => m.id)
|
||||
.toList();
|
||||
if (unread.isNotEmpty) {
|
||||
ref.read(mitraChatProvider.notifier).markRead(unread);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(widget.customerName),
|
||||
actions: [
|
||||
if (chatState is MitraChatConnectedData && chatState.remainingSeconds != null)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(right: 16),
|
||||
child: Center(
|
||||
child: Text(
|
||||
'${chatState.remainingSeconds}s',
|
||||
style: TextStyle(
|
||||
color: chatState.remainingSeconds! < 30 ? Colors.red : null,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: BlocBuilder<MitraChatBloc, MitraChatState>(
|
||||
builder: (context, state) {
|
||||
if (state is ChatConnecting) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
if (state is ChatError) {
|
||||
return Center(child: Text(state.message));
|
||||
}
|
||||
if (state is ChatConnected) {
|
||||
return _buildChatBody(context, state);
|
||||
}
|
||||
return const SizedBox.shrink();
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
body: _buildBody(chatState, extState),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildChatBody(BuildContext context, ChatConnected state) {
|
||||
Widget _buildBody(MitraChatData chatState, ExtensionData extState) {
|
||||
if (chatState is MitraChatConnectingData) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
if (chatState is MitraChatErrorData) {
|
||||
return Center(child: Text(chatState.message));
|
||||
}
|
||||
if (chatState is MitraChatConnectedData) {
|
||||
return _buildChatBody(chatState, extState);
|
||||
}
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
|
||||
Widget _buildChatBody(MitraChatConnectedData state, ExtensionData extState) {
|
||||
// Extension request from customer
|
||||
if (state.extensionRequest != null) {
|
||||
return _buildExtensionView(context, state.extensionRequest!);
|
||||
return _buildExtensionView(state.extensionRequest!, extState);
|
||||
}
|
||||
|
||||
// Goodbye view
|
||||
final extState = context.watch<ExtensionBloc>().state;
|
||||
if (state.sessionClosing || extState is ExtensionShowGoodbye || extState is ExtensionSubmitting) {
|
||||
return _buildGoodbyeView(context, extState);
|
||||
if (state.sessionClosing || extState is ExtensionShowGoodbyeData || extState is ExtensionSubmittingData) {
|
||||
return _buildGoodbyeView(extState);
|
||||
}
|
||||
|
||||
return Column(
|
||||
@@ -173,7 +162,7 @@ class _MitraChatScreenState extends State<MitraChatScreen> {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildMessageBubble(ChatMessage msg, bool isMe) {
|
||||
Widget _buildMessageBubble(MitraChatMessage msg, bool isMe) {
|
||||
return Align(
|
||||
alignment: isMe ? Alignment.centerRight : Alignment.centerLeft,
|
||||
child: Container(
|
||||
@@ -253,62 +242,57 @@ class _MitraChatScreenState extends State<MitraChatScreen> {
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildExtensionView(BuildContext context, Map<String, dynamic> request) {
|
||||
Widget _buildExtensionView(Map<String, dynamic> request, ExtensionData extState) {
|
||||
final duration = request['duration_minutes'] as int?;
|
||||
final extensionId = request['extension_id'] as String?;
|
||||
final isResponding = extState is ExtensionRespondingData;
|
||||
|
||||
return BlocBuilder<ExtensionBloc, ExtensionState>(
|
||||
builder: (context, extState) {
|
||||
final isResponding = extState is ExtensionResponding;
|
||||
|
||||
return Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(32),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Icon(Icons.timer, size: 64, color: Colors.orange),
|
||||
const SizedBox(height: 16),
|
||||
const Text('Permintaan Perpanjangan', style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold)),
|
||||
const SizedBox(height: 8),
|
||||
Text('Customer ingin perpanjang $duration menit', textAlign: TextAlign.center),
|
||||
const SizedBox(height: 24),
|
||||
if (isResponding)
|
||||
const CircularProgressIndicator()
|
||||
else
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
ElevatedButton(
|
||||
style: ElevatedButton.styleFrom(backgroundColor: Colors.green),
|
||||
onPressed: extensionId == null ? null : () => context.read<ExtensionBloc>().add(RespondToExtension(
|
||||
sessionId: widget.sessionId,
|
||||
extensionId: extensionId,
|
||||
accepted: true,
|
||||
)),
|
||||
child: const Text('Terima', style: TextStyle(color: Colors.white)),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
ElevatedButton(
|
||||
style: ElevatedButton.styleFrom(backgroundColor: Colors.red),
|
||||
onPressed: extensionId == null ? null : () => context.read<ExtensionBloc>().add(RespondToExtension(
|
||||
sessionId: widget.sessionId,
|
||||
extensionId: extensionId,
|
||||
accepted: false,
|
||||
)),
|
||||
child: const Text('Tolak', style: TextStyle(color: Colors.white)),
|
||||
),
|
||||
],
|
||||
return Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(32),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Icon(Icons.timer, size: 64, color: Colors.orange),
|
||||
const SizedBox(height: 16),
|
||||
const Text('Permintaan Perpanjangan', style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold)),
|
||||
const SizedBox(height: 8),
|
||||
Text('Customer ingin perpanjang $duration menit', textAlign: TextAlign.center),
|
||||
const SizedBox(height: 24),
|
||||
if (isResponding)
|
||||
const CircularProgressIndicator()
|
||||
else
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
ElevatedButton(
|
||||
style: ElevatedButton.styleFrom(backgroundColor: Colors.green),
|
||||
onPressed: extensionId == null ? null : () => ref.read(mitraExtensionProvider.notifier).respond(
|
||||
widget.sessionId,
|
||||
extensionId: extensionId,
|
||||
accepted: true,
|
||||
),
|
||||
child: const Text('Terima', style: TextStyle(color: Colors.white)),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
const SizedBox(width: 16),
|
||||
ElevatedButton(
|
||||
style: ElevatedButton.styleFrom(backgroundColor: Colors.red),
|
||||
onPressed: extensionId == null ? null : () => ref.read(mitraExtensionProvider.notifier).respond(
|
||||
widget.sessionId,
|
||||
extensionId: extensionId,
|
||||
accepted: false,
|
||||
),
|
||||
child: const Text('Tolak', style: TextStyle(color: Colors.white)),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildGoodbyeView(BuildContext context, ExtensionState extState) {
|
||||
Widget _buildGoodbyeView(ExtensionData extState) {
|
||||
final controller = TextEditingController();
|
||||
return SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(32),
|
||||
@@ -331,17 +315,17 @@ class _MitraChatScreenState extends State<MitraChatScreen> {
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
ElevatedButton(
|
||||
onPressed: extState is ExtensionSubmitting
|
||||
onPressed: extState is ExtensionSubmittingData
|
||||
? null
|
||||
: () {
|
||||
final text = controller.text.trim();
|
||||
if (text.isNotEmpty) {
|
||||
context.read<ExtensionBloc>().add(
|
||||
SubmitGoodbye(sessionId: widget.sessionId, message: text),
|
||||
ref.read(mitraExtensionProvider.notifier).submitGoodbye(
|
||||
widget.sessionId, text,
|
||||
);
|
||||
}
|
||||
},
|
||||
child: extState is ExtensionSubmitting
|
||||
child: extState is ExtensionSubmittingData
|
||||
? const SizedBox(width: 20, height: 20, child: CircularProgressIndicator(strokeWidth: 2))
|
||||
: const Text('Kirim & Selesai'),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user