import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import '../../core/auth/auth_bloc.dart'; import '../../core/status/status_bloc.dart'; import '../../core/chat/chat_request_bloc.dart'; import '../chat/widgets/incoming_request_sheet.dart'; class HomeScreen extends StatelessWidget { const HomeScreen({super.key}); @override Widget build(BuildContext context) { return MultiBlocListener( listeners: [ BlocListener( listener: (context, state) { if (state is StatusLoaded && state.isOnline) { context.read().add(StartListening()); } else if (state is StatusLoaded && !state.isOnline) { context.read().add(StopListening()); } }, ), BlocListener( listener: (context, state) { if (state is ChatRequestIncoming) { showModalBottomSheet( context: context, isDismissible: false, builder: (_) => BlocProvider.value( value: context.read(), child: IncomingRequestSheet(sessionId: state.sessionId), ), ); } else if (state is ChatRequestAccepted) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('Sesi baru diterima!')), ); } }, ), ], child: BlocBuilder( builder: (context, authState) { final displayName = authState is AuthAuthenticated ? authState.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().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(), ], ), ), ); }, ), ); } } class _StatusToggle extends StatelessWidget { @override Widget build(BuildContext context) { return BlocBuilder( builder: (context, state) { final isOnline = state is StatusLoaded && state.isOnline; final isLoading = state is StatusLoading; return Card( child: Padding( padding: const EdgeInsets.all(16), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, 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), ), ], ), isLoading ? const SizedBox( width: 24, height: 24, child: CircularProgressIndicator(strokeWidth: 2), ) : Switch( value: isOnline, activeColor: Colors.green, onChanged: (_) { final bloc = context.read(); if (isOnline) { bloc.add(ToggleOffline()); } else { bloc.add(ToggleOnline()); } }, ), ], ), ), ); }, ); } } class _ActiveSessionsButton extends StatelessWidget { @override Widget build(BuildContext context) { return Card( child: ListTile( leading: const Icon(Icons.chat_bubble_outline), title: const Text('Sesi Aktif'), trailing: const Icon(Icons.chevron_right), onTap: () => Navigator.of(context).pushNamed('/sessions'), ), ); } }