automaticallyImplyLeading was set to false, hiding the back arrow. iOS has no physical back button so this is needed for navigation. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
374 lines
12 KiB
Dart
374 lines
12 KiB
Dart
import 'dart:async';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import '../../../core/chat/chat_notifier.dart';
|
|
import '../../../core/chat/session_closure_notifier.dart';
|
|
import '../../../core/constants.dart';
|
|
import '../widgets/pricing_bottom_sheet.dart';
|
|
|
|
class ChatScreen extends ConsumerStatefulWidget {
|
|
final String sessionId;
|
|
final String mitraName;
|
|
|
|
const ChatScreen({super.key, required this.sessionId, required this.mitraName});
|
|
|
|
@override
|
|
ConsumerState<ChatScreen> createState() => _ChatScreenState();
|
|
}
|
|
|
|
class _ChatScreenState extends ConsumerState<ChatScreen> {
|
|
final _messageController = TextEditingController();
|
|
final _scrollController = ScrollController();
|
|
Timer? _typingThrottle;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
Future.microtask(() {
|
|
ref.read(chatProvider.notifier).connect(widget.sessionId);
|
|
});
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
final notifier = ref.read(chatProvider.notifier);
|
|
_messageController.dispose();
|
|
_scrollController.dispose();
|
|
_typingThrottle?.cancel();
|
|
super.dispose();
|
|
Future.microtask(() => notifier.disconnect());
|
|
}
|
|
|
|
void _scrollToBottom() {
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (_scrollController.hasClients) {
|
|
_scrollController.animateTo(
|
|
_scrollController.position.maxScrollExtent,
|
|
duration: const Duration(milliseconds: 200),
|
|
curve: Curves.easeOut,
|
|
);
|
|
}
|
|
});
|
|
}
|
|
|
|
void _onTextChanged(String text) {
|
|
if (_typingThrottle?.isActive ?? false) return;
|
|
ref.read(chatProvider.notifier).sendTyping();
|
|
_typingThrottle = Timer(const Duration(seconds: 2), () {});
|
|
}
|
|
|
|
void _sendMessage() {
|
|
final text = _messageController.text.trim();
|
|
if (text.isEmpty) return;
|
|
ref.read(chatProvider.notifier).sendMessage(text);
|
|
_messageController.clear();
|
|
_scrollToBottom();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final chatState = ref.watch(chatProvider);
|
|
final closureState = ref.watch(sessionClosureProvider);
|
|
|
|
// Listen for closure complete to navigate home
|
|
ref.listen(sessionClosureProvider, (prev, next) {
|
|
if (next is ClosureCompleteData) {
|
|
context.go('/home');
|
|
}
|
|
});
|
|
|
|
// Listen for chat state changes to manage closure state
|
|
ref.listen(chatProvider, (prev, next) {
|
|
if (next is ChatConnectedData) {
|
|
if (next.sessionClosing && !next.sessionExpired) {
|
|
final closure = ref.read(sessionClosureProvider);
|
|
if (closure is ClosureInitialData) {
|
|
ref.read(sessionClosureProvider.notifier).declineExtension();
|
|
}
|
|
}
|
|
if (!next.sessionPaused && !next.sessionExpired && !next.sessionClosing) {
|
|
final closure = ref.read(sessionClosureProvider);
|
|
if (closure is! ClosureInitialData) {
|
|
ref.read(sessionClosureProvider.notifier).reset();
|
|
}
|
|
}
|
|
_scrollToBottom();
|
|
final unread = next.messages
|
|
.where((m) => m.senderType == UserType.mitra && m.status != MessageStatus.read)
|
|
.map((m) => m.id)
|
|
.toList();
|
|
if (unread.isNotEmpty) {
|
|
ref.read(chatProvider.notifier).markRead(unread);
|
|
}
|
|
}
|
|
});
|
|
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Text(widget.mitraName),
|
|
automaticallyImplyLeading: true,
|
|
actions: [
|
|
if (chatState is ChatConnectedData && 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: _buildBody(chatState, closureState),
|
|
);
|
|
}
|
|
|
|
Widget _buildBody(ChatData chatState, SessionClosureData closureState) {
|
|
if (chatState is ChatConnectingData) {
|
|
return const Center(child: CircularProgressIndicator());
|
|
}
|
|
if (chatState is ChatErrorData) {
|
|
return Center(child: Text(chatState.message));
|
|
}
|
|
if (chatState is ChatConnectedData) {
|
|
return _buildChatBody(chatState, closureState);
|
|
}
|
|
return const SizedBox.shrink();
|
|
}
|
|
|
|
Widget _buildChatBody(ChatConnectedData state, SessionClosureData closureState) {
|
|
if (closureState is ClosureShowGoodbyeData || closureState is ClosureSubmittingData) {
|
|
return _buildGoodbyeView(closureState);
|
|
}
|
|
|
|
if (state.sessionExpired) {
|
|
return _buildExpiredView();
|
|
}
|
|
|
|
if (state.sessionPaused) {
|
|
return _buildPausedView();
|
|
}
|
|
|
|
return Column(
|
|
children: [
|
|
Expanded(
|
|
child: ListView.builder(
|
|
controller: _scrollController,
|
|
padding: const EdgeInsets.all(16),
|
|
itemCount: state.messages.length,
|
|
itemBuilder: (context, index) {
|
|
final msg = state.messages[index];
|
|
final isMe = msg.senderType == UserType.customer;
|
|
return _buildMessageBubble(msg, isMe);
|
|
},
|
|
),
|
|
),
|
|
if (state.isOtherTyping)
|
|
const Padding(
|
|
padding: EdgeInsets.symmetric(horizontal: 16, vertical: 4),
|
|
child: Align(
|
|
alignment: Alignment.centerLeft,
|
|
child: Text('Bestie sedang mengetik...', style: TextStyle(color: Colors.grey, fontSize: 12)),
|
|
),
|
|
),
|
|
_buildInputBar(),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildMessageBubble(ChatMessage msg, bool isMe) {
|
|
return Align(
|
|
alignment: isMe ? Alignment.centerRight : Alignment.centerLeft,
|
|
child: Container(
|
|
margin: const EdgeInsets.symmetric(vertical: 4),
|
|
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 10),
|
|
constraints: BoxConstraints(maxWidth: MediaQuery.of(context).size.width * 0.75),
|
|
decoration: BoxDecoration(
|
|
color: isMe ? Colors.blue.shade100 : Colors.grey.shade200,
|
|
borderRadius: BorderRadius.circular(16),
|
|
),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.end,
|
|
children: [
|
|
Text(msg.content, style: const TextStyle(fontSize: 15)),
|
|
const SizedBox(height: 4),
|
|
Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Text(
|
|
'${msg.createdAt.hour.toString().padLeft(2, '0')}:${msg.createdAt.minute.toString().padLeft(2, '0')}',
|
|
style: const TextStyle(fontSize: 10, color: Colors.grey),
|
|
),
|
|
if (isMe) ...[
|
|
const SizedBox(width: 4),
|
|
_buildStatusIcon(msg.status),
|
|
],
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildStatusIcon(String status) {
|
|
switch (status) {
|
|
case 'sending':
|
|
return const Icon(Icons.access_time, size: 14, color: Colors.grey);
|
|
case MessageStatus.sent:
|
|
return const Icon(Icons.check, size: 14, color: Colors.grey);
|
|
case MessageStatus.delivered:
|
|
return const Icon(Icons.done_all, size: 14, color: Colors.grey);
|
|
case MessageStatus.read:
|
|
return const Icon(Icons.done_all, size: 14, color: Colors.blue);
|
|
default:
|
|
return const SizedBox.shrink();
|
|
}
|
|
}
|
|
|
|
Widget _buildInputBar() {
|
|
return SafeArea(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(8),
|
|
child: Row(
|
|
children: [
|
|
Expanded(
|
|
child: TextField(
|
|
controller: _messageController,
|
|
onChanged: _onTextChanged,
|
|
textInputAction: TextInputAction.send,
|
|
onSubmitted: (_) => _sendMessage(),
|
|
decoration: InputDecoration(
|
|
hintText: 'Ketik pesan...',
|
|
border: OutlineInputBorder(borderRadius: BorderRadius.circular(24)),
|
|
contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
|
|
),
|
|
),
|
|
),
|
|
const SizedBox(width: 8),
|
|
IconButton(
|
|
icon: const Icon(Icons.send, color: Colors.blue),
|
|
onPressed: _sendMessage,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildExpiredView() {
|
|
return Center(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(32),
|
|
child: TweenAnimationBuilder<int>(
|
|
tween: IntTween(begin: 300, end: 0),
|
|
duration: const Duration(seconds: 300),
|
|
builder: (context, remaining, _) {
|
|
if (remaining <= 0) {
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
ref.read(sessionClosureProvider.notifier).declineExtension();
|
|
});
|
|
}
|
|
final minutes = remaining ~/ 60;
|
|
final seconds = remaining % 60;
|
|
return Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
const Icon(Icons.timer_off, size: 64, color: Colors.orange),
|
|
const SizedBox(height: 16),
|
|
const Text('Waktu sesi habis', style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold)),
|
|
const SizedBox(height: 8),
|
|
const Text('Apakah kamu ingin memperpanjang sesi?', textAlign: TextAlign.center),
|
|
const SizedBox(height: 12),
|
|
Text(
|
|
'$minutes:${seconds.toString().padLeft(2, '0')}',
|
|
style: TextStyle(
|
|
fontSize: 32,
|
|
fontWeight: FontWeight.bold,
|
|
color: remaining < 60 ? Colors.red : Colors.orange,
|
|
),
|
|
),
|
|
const SizedBox(height: 24),
|
|
ElevatedButton(
|
|
onPressed: () => PricingBottomSheet.showForExtension(context, sessionId: widget.sessionId),
|
|
child: const Text('Perpanjang Sesi'),
|
|
),
|
|
const SizedBox(height: 12),
|
|
TextButton(
|
|
onPressed: () => ref.read(sessionClosureProvider.notifier).declineExtension(),
|
|
child: const Text('Tidak, akhiri sesi'),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildGoodbyeView(SessionClosureData closureState) {
|
|
final controller = TextEditingController();
|
|
return SingleChildScrollView(
|
|
padding: const EdgeInsets.all(32),
|
|
child: Column(
|
|
children: [
|
|
const SizedBox(height: 48),
|
|
const Icon(Icons.waving_hand, size: 64, color: Colors.amber),
|
|
const SizedBox(height: 16),
|
|
const Text('Pesan Penutup', style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold)),
|
|
const SizedBox(height: 8),
|
|
const Text('Tuliskan pesan terakhirmu untuk Bestie', textAlign: TextAlign.center),
|
|
const SizedBox(height: 24),
|
|
TextField(
|
|
controller: controller,
|
|
maxLines: 3,
|
|
decoration: InputDecoration(
|
|
hintText: 'Terima kasih, Bestie...',
|
|
border: OutlineInputBorder(borderRadius: BorderRadius.circular(12)),
|
|
),
|
|
),
|
|
const SizedBox(height: 16),
|
|
ElevatedButton(
|
|
onPressed: closureState is ClosureSubmittingData
|
|
? null
|
|
: () {
|
|
final text = controller.text.trim();
|
|
if (text.isNotEmpty) {
|
|
ref.read(sessionClosureProvider.notifier).submitGoodbye(
|
|
widget.sessionId, text,
|
|
);
|
|
}
|
|
},
|
|
child: closureState is ClosureSubmittingData
|
|
? const SizedBox(width: 20, height: 20, child: CircularProgressIndicator(strokeWidth: 2))
|
|
: const Text('Kirim & Selesai'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildPausedView() {
|
|
return const Center(
|
|
child: Padding(
|
|
padding: EdgeInsets.all(32),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
CircularProgressIndicator(),
|
|
SizedBox(height: 24),
|
|
Text('Menunggu konfirmasi Bestie...', style: TextStyle(fontSize: 18)),
|
|
SizedBox(height: 8),
|
|
Text('Chat dijeda sementara', style: TextStyle(color: Colors.grey)),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|