- Migrate SessionClosureBloc → SessionClosureNotifier (@riverpod) - Migrate PairingBloc → PairingNotifier (@riverpod, WebSocket + timer) - Migrate ChatBloc → ChatNotifier (@riverpod, WebSocket + message state) - Remove all flutter_bloc usage from client_app screens and main.dart - MultiBlocProvider fully removed from client_app - All screens now use ConsumerWidget/ConsumerStatefulWidget + ref Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
184 lines
5.6 KiB
Dart
184 lines
5.6 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import '../../core/auth/auth_notifier.dart';
|
|
import '../../core/api/api_client_provider.dart';
|
|
import '../../core/pairing/pairing_notifier.dart';
|
|
import '../chat/widgets/pricing_bottom_sheet.dart';
|
|
|
|
class HomeScreen extends ConsumerStatefulWidget {
|
|
const HomeScreen({super.key});
|
|
|
|
@override
|
|
ConsumerState<HomeScreen> createState() => _HomeScreenState();
|
|
}
|
|
|
|
class _HomeScreenState extends ConsumerState<HomeScreen> with WidgetsBindingObserver {
|
|
Map<String, dynamic>? _activeSession;
|
|
bool _loadingSession = true;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
WidgetsBinding.instance.addObserver(this);
|
|
_checkActiveSession();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
WidgetsBinding.instance.removeObserver(this);
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
void didChangeAppLifecycleState(AppLifecycleState state) {
|
|
if (state == AppLifecycleState.resumed) {
|
|
_checkActiveSession();
|
|
}
|
|
}
|
|
|
|
@override
|
|
void didChangeDependencies() {
|
|
super.didChangeDependencies();
|
|
_checkActiveSession();
|
|
}
|
|
|
|
Future<void> _checkActiveSession() async {
|
|
try {
|
|
final apiClient = ref.read(apiClientProvider);
|
|
final response = await apiClient.get('/api/client/chat/session/active');
|
|
final data = response['data'];
|
|
if (mounted) {
|
|
setState(() {
|
|
_activeSession = data is Map<String, dynamic> ? data : null;
|
|
_loadingSession = false;
|
|
});
|
|
}
|
|
} catch (_) {
|
|
if (mounted) setState(() => _loadingSession = false);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final authState = ref.watch(authProvider);
|
|
final authData = authState.valueOrNull;
|
|
|
|
final displayName = switch (authData) {
|
|
AuthAuthenticatedData d => d.profile['display_name'] as String? ?? '',
|
|
AuthAnonymousData d => d.displayName,
|
|
_ => '',
|
|
};
|
|
|
|
ref.listen(pairingProvider, (prev, next) {
|
|
if (next is PairingSearchingData) {
|
|
context.go('/chat/searching');
|
|
} else if (next is PairingNoBestieData) {
|
|
context.go('/chat/no-bestie');
|
|
} else if (next is PairingErrorData) {
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: Text(next.message)),
|
|
);
|
|
}
|
|
});
|
|
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Halo Bestie'),
|
|
actions: [
|
|
IconButton(
|
|
icon: const Icon(Icons.history),
|
|
onPressed: () => context.push('/chat/history'),
|
|
),
|
|
IconButton(
|
|
icon: const Icon(Icons.logout),
|
|
onPressed: () => ref.read(authProvider.notifier).logout(),
|
|
),
|
|
],
|
|
),
|
|
body: Center(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(32),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Text('Halo, $displayName!', style: const TextStyle(fontSize: 24)),
|
|
const SizedBox(height: 32),
|
|
if (_loadingSession)
|
|
const CircularProgressIndicator()
|
|
else if (_activeSession != null)
|
|
_ActiveSessionCard(
|
|
session: _activeSession!,
|
|
onTap: () {
|
|
final sessionId = _activeSession!['id'] as String;
|
|
final mitraName = _activeSession!['mitra_display_name'] as String? ?? 'Bestie';
|
|
context.push('/chat/session/$sessionId', extra: mitraName);
|
|
},
|
|
)
|
|
else ...[
|
|
const SizedBox(height: 16),
|
|
ElevatedButton(
|
|
style: ElevatedButton.styleFrom(
|
|
padding: const EdgeInsets.symmetric(horizontal: 48, vertical: 16),
|
|
),
|
|
onPressed: () => PricingBottomSheet.show(context),
|
|
child: const Text('Mulai Curhat', style: TextStyle(fontSize: 18)),
|
|
),
|
|
],
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _ActiveSessionCard extends StatelessWidget {
|
|
final Map<String, dynamic> session;
|
|
final VoidCallback onTap;
|
|
|
|
const _ActiveSessionCard({required this.session, required this.onTap});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final mitraName = session['mitra_display_name'] as String? ?? 'Bestie';
|
|
|
|
return Card(
|
|
elevation: 2,
|
|
child: InkWell(
|
|
onTap: onTap,
|
|
borderRadius: BorderRadius.circular(12),
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(20),
|
|
child: Row(
|
|
children: [
|
|
const CircleAvatar(
|
|
backgroundColor: Colors.green,
|
|
child: Icon(Icons.chat, color: Colors.white),
|
|
),
|
|
const SizedBox(width: 16),
|
|
Expanded(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
const Text(
|
|
'Sesi Aktif',
|
|
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
|
|
),
|
|
const SizedBox(height: 4),
|
|
Text(
|
|
'Sedang curhat dengan $mitraName',
|
|
style: const TextStyle(fontSize: 14, color: Colors.grey),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
const Icon(Icons.chevron_right),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|