- Control center: add mitra ping config UI (require ping toggle + interval)
- Mitra app StatusNotifier: honor require_ping and ping_interval_seconds
from API; skip heartbeat when ping not required
- Both apps: update notification services for FCM deep-linking
- mitra_app: handle chat_request (open_accept), session_closing
- client_app: handle session_closing, paired
- Unread badge providers:
- mitra_app: UnreadSessions provider (polls active-with-unread, badge
on active sessions button)
- client_app: UnreadCount provider (polls active-with-unread, badge
on _ActiveSessionCard)
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
197 lines
6.0 KiB
Dart
197 lines
6.0 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/status/status_notifier.dart';
|
|
import '../../core/chat/chat_request_notifier.dart';
|
|
import '../../core/chat/unread_notifier.dart';
|
|
import '../chat/widgets/incoming_request_sheet.dart';
|
|
|
|
class HomeScreen extends ConsumerStatefulWidget {
|
|
const HomeScreen({super.key});
|
|
|
|
@override
|
|
ConsumerState<HomeScreen> createState() => _HomeScreenState();
|
|
}
|
|
|
|
class _HomeScreenState extends ConsumerState<HomeScreen> with WidgetsBindingObserver {
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
WidgetsBinding.instance.addObserver(this);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
WidgetsBinding.instance.removeObserver(this);
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
void didChangeAppLifecycleState(AppLifecycleState state) {
|
|
if (state == AppLifecycleState.resumed) {
|
|
final chatState = ref.read(chatRequestProvider);
|
|
if (chatState is ChatRequestIncomingData) {
|
|
_showIncomingRequest(chatState.sessionId);
|
|
}
|
|
}
|
|
}
|
|
|
|
void _showIncomingRequest(String sessionId) {
|
|
showModalBottomSheet(
|
|
context: context,
|
|
isDismissible: false,
|
|
builder: (_) => IncomingRequestSheet(sessionId: sessionId),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final authState = ref.watch(mitraAuthProvider);
|
|
final authData = authState.valueOrNull;
|
|
final displayName = authData is MitraAuthAuthenticatedData
|
|
? authData.profile['display_name'] as String
|
|
: '';
|
|
|
|
// 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 ConsumerWidget {
|
|
const _StatusToggle();
|
|
|
|
@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: [
|
|
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 notifier = ref.read(onlineStatusProvider.notifier);
|
|
if (isOnline) {
|
|
notifier.toggleOffline();
|
|
} else {
|
|
notifier.toggleOnline();
|
|
}
|
|
},
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _ActiveSessionsButton extends ConsumerWidget {
|
|
const _ActiveSessionsButton();
|
|
|
|
@override
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
|
final unreadCounts = ref.watch(unreadSessionsProvider);
|
|
final totalUnread = unreadCounts.values.fold(0, (a, b) => a + b);
|
|
|
|
return Column(
|
|
children: [
|
|
Card(
|
|
child: ListTile(
|
|
leading: Badge(
|
|
isLabelVisible: totalUnread > 0,
|
|
label: Text('$totalUnread'),
|
|
child: const Icon(Icons.chat_bubble_outline),
|
|
),
|
|
title: const Text('Sesi Aktif'),
|
|
trailing: const Icon(Icons.chevron_right),
|
|
onTap: () => context.push('/sessions'),
|
|
),
|
|
),
|
|
Card(
|
|
child: ListTile(
|
|
leading: const Icon(Icons.history),
|
|
title: const Text('Riwayat Chat'),
|
|
trailing: const Icon(Icons.chevron_right),
|
|
onTap: () => context.push('/chat/history'),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|