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,19 +1,19 @@
|
||||
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/auth/auth_bloc.dart';
|
||||
import '../../core/status/status_bloc.dart';
|
||||
import '../../core/chat/chat_request_bloc.dart';
|
||||
import '../../core/auth/auth_notifier.dart';
|
||||
import '../../core/status/status_notifier.dart';
|
||||
import '../../core/chat/chat_request_notifier.dart';
|
||||
import '../chat/widgets/incoming_request_sheet.dart';
|
||||
|
||||
class HomeScreen extends StatefulWidget {
|
||||
class HomeScreen extends ConsumerStatefulWidget {
|
||||
const HomeScreen({super.key});
|
||||
|
||||
@override
|
||||
State<HomeScreen> createState() => _HomeScreenState();
|
||||
ConsumerState<HomeScreen> createState() => _HomeScreenState();
|
||||
}
|
||||
|
||||
class _HomeScreenState extends State<HomeScreen> with WidgetsBindingObserver {
|
||||
class _HomeScreenState extends ConsumerState<HomeScreen> with WidgetsBindingObserver {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
@@ -29,9 +29,8 @@ class _HomeScreenState extends State<HomeScreen> with WidgetsBindingObserver {
|
||||
@override
|
||||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||||
if (state == AppLifecycleState.resumed) {
|
||||
// Check if there's a pending request that was missed while backgrounded
|
||||
final chatState = context.read<ChatRequestBloc>().state;
|
||||
if (chatState is ChatRequestIncoming) {
|
||||
final chatState = ref.read(chatRequestProvider);
|
||||
if (chatState is ChatRequestIncomingData) {
|
||||
_showIncomingRequest(chatState.sessionId);
|
||||
}
|
||||
}
|
||||
@@ -41,136 +40,128 @@ class _HomeScreenState extends State<HomeScreen> with WidgetsBindingObserver {
|
||||
showModalBottomSheet(
|
||||
context: context,
|
||||
isDismissible: false,
|
||||
builder: (_) => BlocProvider.value(
|
||||
value: context.read<ChatRequestBloc>(),
|
||||
child: IncomingRequestSheet(sessionId: sessionId),
|
||||
),
|
||||
builder: (_) => IncomingRequestSheet(sessionId: sessionId),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MultiBlocListener(
|
||||
listeners: [
|
||||
BlocListener<StatusBloc, StatusState>(
|
||||
listener: (context, state) {
|
||||
if (state is StatusLoaded && state.isOnline) {
|
||||
context.read<ChatRequestBloc>().add(StartListening());
|
||||
} else if (state is StatusLoaded && !state.isOnline) {
|
||||
context.read<ChatRequestBloc>().add(StopListening());
|
||||
}
|
||||
},
|
||||
),
|
||||
BlocListener<ChatRequestBloc, ChatRequestState>(
|
||||
listener: (context, state) {
|
||||
if (state is ChatRequestIncoming) {
|
||||
_showIncomingRequest(state.sessionId);
|
||||
} else if (state is ChatRequestAccepted) {
|
||||
final session = state.session;
|
||||
final sessionId = session['session_id'] as String? ?? session['id'] as String;
|
||||
context.push('/chat/session/$sessionId', extra: {
|
||||
'customerName': session['customer_display_name'] as String? ?? 'Customer',
|
||||
});
|
||||
}
|
||||
},
|
||||
),
|
||||
],
|
||||
child: BlocBuilder<AuthBloc, AuthState>(
|
||||
builder: (context, authState) {
|
||||
final displayName = authState is AuthAuthenticated
|
||||
? authState.profile['display_name'] as String
|
||||
: '';
|
||||
final authState = ref.watch(mitraAuthProvider);
|
||||
final authData = authState.valueOrNull;
|
||||
final displayName = authData is MitraAuthAuthenticatedData
|
||||
? authData.profile['display_name'] as String
|
||||
: '';
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Halo Bestie Mitra'),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.logout),
|
||||
onPressed: () => context.read<AuthBloc>().add(LogoutRequested()),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
children: [
|
||||
Text('Halo, $displayName!', style: const TextStyle(fontSize: 24)),
|
||||
const SizedBox(height: 32),
|
||||
_StatusToggle(),
|
||||
const SizedBox(height: 16),
|
||||
_ActiveSessionsButton(),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
// Listen for status changes to start/stop chat request listening
|
||||
ref.listen(onlineStatusProvider, (prev, next) {
|
||||
if (next is StatusLoadedData && next.isOnline) {
|
||||
ref.read(chatRequestProvider.notifier).startListening();
|
||||
} else if (next is StatusLoadedData && !next.isOnline) {
|
||||
ref.read(chatRequestProvider.notifier).stopListening();
|
||||
}
|
||||
});
|
||||
|
||||
// Listen for incoming chat requests
|
||||
ref.listen(chatRequestProvider, (prev, next) {
|
||||
if (next is ChatRequestIncomingData) {
|
||||
_showIncomingRequest(next.sessionId);
|
||||
} else if (next is ChatRequestAcceptedData) {
|
||||
final session = next.session;
|
||||
final sessionId = session['session_id'] as String? ?? session['id'] as String;
|
||||
context.push('/chat/session/$sessionId', extra: {
|
||||
'customerName': session['customer_display_name'] as String? ?? 'Customer',
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Halo Bestie Mitra'),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.logout),
|
||||
onPressed: () => ref.read(mitraAuthProvider.notifier).logout(),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
children: [
|
||||
Text('Halo, $displayName!', style: const TextStyle(fontSize: 24)),
|
||||
const SizedBox(height: 32),
|
||||
const _StatusToggle(),
|
||||
const SizedBox(height: 16),
|
||||
const _ActiveSessionsButton(),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _StatusToggle extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocBuilder<StatusBloc, StatusState>(
|
||||
builder: (context, state) {
|
||||
final isOnline = state is StatusLoaded && state.isOnline;
|
||||
final isLoading = state is StatusLoading;
|
||||
class _StatusToggle extends ConsumerWidget {
|
||||
const _StatusToggle();
|
||||
|
||||
return Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final statusState = ref.watch(onlineStatusProvider);
|
||||
final isOnline = statusState is StatusLoadedData && statusState.isOnline;
|
||||
final isLoading = statusState is StatusLoadingData;
|
||||
|
||||
return Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
isOnline ? 'Online' : 'Offline',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: isOnline ? Colors.green : Colors.grey,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
isOnline
|
||||
? 'Kamu siap menerima chat'
|
||||
: 'Aktifkan untuk menerima chat',
|
||||
style: const TextStyle(fontSize: 14, color: Colors.grey),
|
||||
),
|
||||
],
|
||||
Text(
|
||||
isOnline ? 'Online' : 'Offline',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: isOnline ? Colors.green : Colors.grey,
|
||||
),
|
||||
),
|
||||
Text(
|
||||
isOnline
|
||||
? 'Kamu siap menerima chat'
|
||||
: 'Aktifkan untuk menerima chat',
|
||||
style: const TextStyle(fontSize: 14, color: Colors.grey),
|
||||
),
|
||||
isLoading
|
||||
? const SizedBox(
|
||||
width: 24,
|
||||
height: 24,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: Switch(
|
||||
value: isOnline,
|
||||
activeColor: Colors.green,
|
||||
onChanged: (_) {
|
||||
final bloc = context.read<StatusBloc>();
|
||||
if (isOnline) {
|
||||
bloc.add(ToggleOffline());
|
||||
} else {
|
||||
bloc.add(ToggleOnline());
|
||||
}
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
isLoading
|
||||
? const SizedBox(
|
||||
width: 24,
|
||||
height: 24,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: Switch(
|
||||
value: isOnline,
|
||||
activeColor: Colors.green,
|
||||
onChanged: (_) {
|
||||
final notifier = ref.read(onlineStatusProvider.notifier);
|
||||
if (isOnline) {
|
||||
notifier.toggleOffline();
|
||||
} else {
|
||||
notifier.toggleOnline();
|
||||
}
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _ActiveSessionsButton extends StatelessWidget {
|
||||
const _ActiveSessionsButton();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
|
||||
Reference in New Issue
Block a user