2024-11-24 12:23:06 +00:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:developer';
|
|
|
|
|
|
|
|
import 'package:dio/dio.dart';
|
2024-11-16 17:16:54 +00:00
|
|
|
import 'package:easy_localization/easy_localization.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
2024-11-24 12:23:06 +00:00
|
|
|
import 'package:gap/gap.dart';
|
2024-11-23 16:22:08 +00:00
|
|
|
import 'package:go_router/go_router.dart';
|
|
|
|
import 'package:material_symbols_icons/symbols.dart';
|
2024-11-16 17:16:54 +00:00
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
import 'package:styled_widget/styled_widget.dart';
|
|
|
|
import 'package:surface/controllers/chat_message_controller.dart';
|
|
|
|
import 'package:surface/providers/channel.dart';
|
2024-11-24 12:23:06 +00:00
|
|
|
import 'package:surface/providers/chat_call.dart';
|
|
|
|
import 'package:surface/providers/sn_network.dart';
|
|
|
|
import 'package:surface/providers/websocket.dart';
|
2024-11-16 17:16:54 +00:00
|
|
|
import 'package:surface/types/chat.dart';
|
2024-11-24 12:23:06 +00:00
|
|
|
import 'package:surface/widgets/chat/call/call_prejoin.dart';
|
2024-11-16 17:16:54 +00:00
|
|
|
import 'package:surface/widgets/chat/chat_message.dart';
|
|
|
|
import 'package:surface/widgets/chat/chat_message_input.dart';
|
|
|
|
import 'package:surface/widgets/dialog.dart';
|
|
|
|
import 'package:surface/widgets/loading_indicator.dart';
|
|
|
|
import 'package:very_good_infinite_list/very_good_infinite_list.dart';
|
|
|
|
|
2024-12-08 05:45:51 +00:00
|
|
|
import '../../providers/user_directory.dart';
|
|
|
|
import '../../providers/userinfo.dart';
|
|
|
|
|
2024-11-16 17:16:54 +00:00
|
|
|
class ChatRoomScreen extends StatefulWidget {
|
|
|
|
final String scope;
|
|
|
|
final String alias;
|
2024-12-08 05:45:51 +00:00
|
|
|
|
2024-11-16 17:16:54 +00:00
|
|
|
const ChatRoomScreen({super.key, required this.scope, required this.alias});
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<ChatRoomScreen> createState() => _ChatRoomScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ChatRoomScreenState extends State<ChatRoomScreen> {
|
|
|
|
bool _isBusy = false;
|
2024-11-24 12:23:06 +00:00
|
|
|
bool _isCalling = false;
|
2024-11-16 17:16:54 +00:00
|
|
|
|
|
|
|
SnChannel? _channel;
|
2024-12-08 05:45:51 +00:00
|
|
|
SnChannelMember? _otherMember;
|
2024-11-24 12:23:06 +00:00
|
|
|
SnChatCall? _ongoingCall;
|
2024-11-16 17:16:54 +00:00
|
|
|
|
2024-11-18 14:33:03 +00:00
|
|
|
final GlobalKey<ChatMessageInputState> _inputGlobalKey = GlobalKey();
|
2024-11-16 17:16:54 +00:00
|
|
|
late final ChatMessageController _messageController;
|
|
|
|
|
2024-11-24 12:23:06 +00:00
|
|
|
StreamSubscription? _wsSubscription;
|
|
|
|
|
2024-11-16 17:16:54 +00:00
|
|
|
Future<void> _fetchChannel() async {
|
|
|
|
setState(() => _isBusy = true);
|
|
|
|
|
|
|
|
try {
|
|
|
|
final chan = context.read<ChatChannelProvider>();
|
|
|
|
_channel = await chan.getChannel('${widget.scope}:${widget.alias}');
|
2024-12-08 05:45:51 +00:00
|
|
|
|
|
|
|
if (!mounted || _channel == null) return;
|
|
|
|
final ud = context.read<UserDirectoryProvider>();
|
|
|
|
final ua = context.read<UserProvider>();
|
|
|
|
if (_channel!.type == 1) {
|
|
|
|
await ud.listAccount(
|
|
|
|
_channel!.members
|
|
|
|
?.cast<SnChannelMember?>()
|
|
|
|
.map((ele) => ele?.accountId)
|
|
|
|
.where((ele) => ele != null && ele != ua.user?.id)
|
|
|
|
.toSet() ??
|
|
|
|
{},
|
|
|
|
);
|
|
|
|
_otherMember = _channel!.members?.cast<SnChannelMember?>().firstWhere(
|
|
|
|
(ele) => ele?.accountId != ua.user?.id,
|
|
|
|
orElse: () => null,
|
|
|
|
);
|
|
|
|
}
|
2024-11-16 17:16:54 +00:00
|
|
|
} catch (err) {
|
|
|
|
if (!mounted) return;
|
|
|
|
context.showErrorDialog(err);
|
|
|
|
} finally {
|
|
|
|
setState(() => _isBusy = false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-24 12:23:06 +00:00
|
|
|
Future<void> _fetchOngoingCall() async {
|
|
|
|
setState(() => _isCalling = true);
|
|
|
|
|
|
|
|
try {
|
|
|
|
final sn = context.read<SnNetworkProvider>();
|
|
|
|
final resp = await sn.client.get(
|
|
|
|
'/cgi/im/channels/${_messageController.channel!.keyPath}/calls/ongoing',
|
|
|
|
options: Options(
|
|
|
|
validateStatus: (status) => status != null && status < 500,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
if (resp.statusCode == 200) {
|
|
|
|
_ongoingCall = SnChatCall.fromJson(resp.data);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
if (!mounted) return;
|
|
|
|
context.showErrorDialog(err);
|
|
|
|
} finally {
|
|
|
|
setState(() => _isCalling = false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _makeCall() async {
|
|
|
|
setState(() => _isCalling = true);
|
|
|
|
|
|
|
|
try {
|
|
|
|
final sn = context.read<SnNetworkProvider>();
|
|
|
|
final resp = await sn.client.post(
|
|
|
|
'/cgi/im/channels/${_messageController.channel!.keyPath}/calls',
|
|
|
|
options: Options(
|
|
|
|
sendTimeout: const Duration(seconds: 30),
|
|
|
|
receiveTimeout: const Duration(seconds: 30),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
log(jsonDecode(resp.data));
|
|
|
|
} catch (err) {
|
|
|
|
if (!mounted) return;
|
|
|
|
context.showErrorDialog(err);
|
|
|
|
} finally {
|
|
|
|
setState(() => _isCalling = false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _endCall() async {
|
|
|
|
setState(() => _isCalling = true);
|
|
|
|
|
|
|
|
try {
|
|
|
|
final sn = context.read<SnNetworkProvider>();
|
2024-11-24 12:54:01 +00:00
|
|
|
await sn.client.delete(
|
2024-11-24 12:23:06 +00:00
|
|
|
'/cgi/im/channels/${_messageController.channel!.keyPath}/calls/ongoing',
|
|
|
|
);
|
|
|
|
} catch (err) {
|
|
|
|
if (!mounted) return;
|
|
|
|
context.showErrorDialog(err);
|
|
|
|
} finally {
|
|
|
|
setState(() => _isCalling = false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _onCallJoin() async {
|
|
|
|
await showModalBottomSheet(
|
|
|
|
context: context,
|
|
|
|
builder: (context) => ChatCallPrejoinPopup(
|
|
|
|
ongoingCall: _ongoingCall!,
|
|
|
|
channel: _channel!,
|
|
|
|
onJoin: _onCallResume,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _onCallResume() {
|
|
|
|
GoRouter.of(context).pushNamed(
|
|
|
|
'chatCallRoom',
|
|
|
|
pathParameters: {
|
|
|
|
'scope': _channel!.realm!.alias,
|
|
|
|
'alias': _channel!.alias,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-11-24 16:05:49 +00:00
|
|
|
bool _checkMessageMergeable(SnChatMessage? a, SnChatMessage? b) {
|
|
|
|
if (a == null || b == null) return false;
|
|
|
|
if (a.sender.accountId != b.sender.accountId) return false;
|
|
|
|
return a.createdAt.difference(b.createdAt).inMinutes <= 3;
|
|
|
|
}
|
|
|
|
|
2024-11-16 17:16:54 +00:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
_messageController = ChatMessageController(context);
|
|
|
|
_fetchChannel().then((_) async {
|
|
|
|
await _messageController.initialize(_channel!);
|
|
|
|
await _messageController.checkUpdate();
|
2024-11-24 12:23:06 +00:00
|
|
|
await _fetchOngoingCall();
|
|
|
|
});
|
|
|
|
|
|
|
|
final ws = context.read<WebSocketProvider>();
|
|
|
|
_wsSubscription = ws.stream.stream.listen((event) {
|
|
|
|
switch (event.method) {
|
|
|
|
case 'calls.new':
|
|
|
|
final payload = SnChatCall.fromJson(event.payload!);
|
|
|
|
if (payload.channelId == _channel?.id) {
|
|
|
|
setState(() => _ongoingCall = payload);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'calls.end':
|
|
|
|
final payload = SnChatCall.fromJson(event.payload!);
|
|
|
|
if (payload.channelId == _channel?.id) {
|
|
|
|
setState(() => _ongoingCall = null);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2024-11-16 17:16:54 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
2024-11-24 12:23:06 +00:00
|
|
|
_wsSubscription?.cancel();
|
|
|
|
_messageController.dispose();
|
2024-11-16 17:16:54 +00:00
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-11-24 12:23:06 +00:00
|
|
|
final call = context.watch<ChatCallProvider>();
|
2024-12-08 05:45:51 +00:00
|
|
|
final ud = context.read<UserDirectoryProvider>();
|
2024-11-24 12:23:06 +00:00
|
|
|
|
2024-11-16 17:16:54 +00:00
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
2024-12-08 05:45:51 +00:00
|
|
|
title: Text(
|
|
|
|
_channel?.type == 1
|
|
|
|
? ud.getAccountFromCache(_otherMember?.accountId)?.nick ?? _channel!.name
|
|
|
|
: _channel?.name ?? 'loading'.tr(),
|
|
|
|
),
|
2024-11-23 16:22:08 +00:00
|
|
|
actions: [
|
|
|
|
IconButton(
|
2024-12-08 05:45:51 +00:00
|
|
|
icon: _ongoingCall == null ? const Icon(Symbols.call) : const Icon(Symbols.call_end),
|
2024-11-24 12:23:06 +00:00
|
|
|
onPressed: _isCalling
|
|
|
|
? null
|
|
|
|
: _ongoingCall == null
|
|
|
|
? _makeCall
|
|
|
|
: _endCall,
|
2024-11-23 16:22:08 +00:00
|
|
|
),
|
2024-11-24 12:23:06 +00:00
|
|
|
IconButton(
|
|
|
|
icon: const Icon(Symbols.more_vert),
|
2024-11-28 15:35:25 +00:00
|
|
|
onPressed: () {
|
|
|
|
GoRouter.of(context).pushNamed('channelDetail', pathParameters: {
|
|
|
|
'scope': widget.scope,
|
|
|
|
'alias': widget.alias,
|
|
|
|
}).then((value) {
|
2024-11-28 15:51:13 +00:00
|
|
|
if (value == false && context.mounted) {
|
|
|
|
Navigator.pop(context, true);
|
|
|
|
} else if (value != null && context.mounted) {
|
2024-11-28 15:35:25 +00:00
|
|
|
_fetchChannel();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2024-11-24 12:23:06 +00:00
|
|
|
),
|
|
|
|
const Gap(8),
|
2024-11-23 16:22:08 +00:00
|
|
|
],
|
2024-11-16 17:16:54 +00:00
|
|
|
),
|
|
|
|
body: ListenableBuilder(
|
|
|
|
listenable: _messageController,
|
|
|
|
builder: (context, _) {
|
|
|
|
return Column(
|
|
|
|
children: [
|
|
|
|
LoadingIndicator(isActive: _isBusy),
|
2024-11-24 12:23:06 +00:00
|
|
|
SingleChildScrollView(
|
|
|
|
physics: const NeverScrollableScrollPhysics(),
|
|
|
|
child: MaterialBanner(
|
|
|
|
dividerColor: Colors.transparent,
|
|
|
|
leading: const Icon(Symbols.call_received),
|
|
|
|
content: Text('callOngoingNotice').tr().padding(top: 2),
|
|
|
|
actions: [
|
|
|
|
if (call.current == null)
|
|
|
|
TextButton(
|
|
|
|
onPressed: _onCallJoin,
|
|
|
|
child: Text('callJoin').tr(),
|
|
|
|
)
|
|
|
|
else if (call.current?.channelId == _channel?.id)
|
|
|
|
TextButton(
|
|
|
|
onPressed: _onCallResume,
|
|
|
|
child: Text('callResume').tr(),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
2024-12-08 05:45:51 +00:00
|
|
|
)
|
|
|
|
.height(_ongoingCall != null ? 54 : 0, animate: true)
|
|
|
|
.animate(const Duration(milliseconds: 300), Curves.fastLinearToSlowEaseIn),
|
2024-11-16 17:16:54 +00:00
|
|
|
if (_messageController.isPending)
|
|
|
|
Expanded(
|
|
|
|
child: const CircularProgressIndicator().center(),
|
|
|
|
),
|
|
|
|
if (!_messageController.isPending)
|
|
|
|
Expanded(
|
|
|
|
child: InfiniteList(
|
2024-11-17 13:30:02 +00:00
|
|
|
reverse: true,
|
2024-11-17 14:42:09 +00:00
|
|
|
padding: const EdgeInsets.only(
|
|
|
|
left: 12,
|
|
|
|
right: 12,
|
|
|
|
top: 12,
|
2024-11-17 13:30:02 +00:00
|
|
|
),
|
2024-11-16 17:16:54 +00:00
|
|
|
hasReachedMax: _messageController.isAllLoaded,
|
|
|
|
itemCount: _messageController.messages.length,
|
|
|
|
isLoading: _messageController.isLoading,
|
|
|
|
onFetchData: () {
|
|
|
|
_messageController.loadMessages();
|
|
|
|
},
|
|
|
|
itemBuilder: (context, idx) {
|
|
|
|
final message = _messageController.messages[idx];
|
2024-11-17 14:42:09 +00:00
|
|
|
|
2024-11-24 16:05:49 +00:00
|
|
|
bool canMerge = false, canMergePrevious = false;
|
|
|
|
if (idx > 0) {
|
|
|
|
canMergePrevious = _checkMessageMergeable(
|
|
|
|
_messageController.messages[idx - 1],
|
|
|
|
_messageController.messages[idx],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (idx + 1 < _messageController.messages.length) {
|
|
|
|
canMerge = _checkMessageMergeable(
|
|
|
|
_messageController.messages[idx],
|
|
|
|
_messageController.messages[idx + 1],
|
|
|
|
);
|
|
|
|
}
|
2024-11-17 14:42:09 +00:00
|
|
|
|
2024-11-17 13:30:02 +00:00
|
|
|
return ChatMessage(
|
|
|
|
data: message,
|
2024-11-17 14:42:09 +00:00
|
|
|
isMerged: canMerge,
|
|
|
|
hasMerged: canMergePrevious,
|
2024-12-08 05:45:51 +00:00
|
|
|
isPending: _messageController.unconfirmedMessages.contains(message.uuid),
|
2024-11-18 14:52:22 +00:00
|
|
|
onReply: (value) {
|
|
|
|
_inputGlobalKey.currentState?.setReply(value);
|
2024-11-18 14:33:03 +00:00
|
|
|
},
|
2024-11-18 15:59:08 +00:00
|
|
|
onEdit: (value) {
|
|
|
|
_inputGlobalKey.currentState?.setEdit(value);
|
|
|
|
},
|
|
|
|
onDelete: (value) {
|
|
|
|
_inputGlobalKey.currentState?.deleteMessage(value);
|
|
|
|
},
|
2024-11-17 13:30:02 +00:00
|
|
|
);
|
2024-11-16 17:16:54 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
if (!_messageController.isPending)
|
|
|
|
Material(
|
|
|
|
elevation: 2,
|
2024-11-18 14:33:03 +00:00
|
|
|
child: ChatMessageInput(
|
|
|
|
key: _inputGlobalKey,
|
2024-12-08 05:45:51 +00:00
|
|
|
otherMember: _otherMember,
|
2024-11-18 14:33:03 +00:00
|
|
|
controller: _messageController,
|
|
|
|
).padding(bottom: MediaQuery.of(context).padding.bottom),
|
2024-11-16 17:16:54 +00:00
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|