2024-05-26 05:39:21 +00:00
|
|
|
import 'dart:async';
|
2024-06-08 13:35:50 +00:00
|
|
|
import 'dart:ui';
|
2024-05-26 05:39:21 +00:00
|
|
|
|
2024-05-25 17:21:08 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2024-06-23 11:02:41 +00:00
|
|
|
import 'package:flutter_animate/flutter_animate.dart';
|
2024-05-25 17:21:08 +00:00
|
|
|
import 'package:get/get.dart';
|
2024-06-27 16:05:43 +00:00
|
|
|
import 'package:solian/controllers/chat_events_controller.dart';
|
2024-05-25 17:21:08 +00:00
|
|
|
import 'package:solian/exts.dart';
|
2024-05-31 17:25:45 +00:00
|
|
|
import 'package:solian/models/call.dart';
|
2024-05-25 17:21:08 +00:00
|
|
|
import 'package:solian/models/channel.dart';
|
2024-06-27 16:05:43 +00:00
|
|
|
import 'package:solian/models/event.dart';
|
2024-05-26 05:39:21 +00:00
|
|
|
import 'package:solian/models/packet.dart';
|
2024-05-25 17:21:08 +00:00
|
|
|
import 'package:solian/providers/auth.dart';
|
|
|
|
import 'package:solian/providers/content/channel.dart';
|
2024-07-16 11:46:53 +00:00
|
|
|
import 'package:solian/providers/websocket.dart';
|
2024-05-26 15:13:43 +00:00
|
|
|
import 'package:solian/router.dart';
|
2024-06-08 16:09:01 +00:00
|
|
|
import 'package:solian/screens/channel/channel_detail.dart';
|
2024-05-25 17:21:08 +00:00
|
|
|
import 'package:solian/theme.dart';
|
2024-07-12 14:31:45 +00:00
|
|
|
import 'package:solian/widgets/app_bar_leading.dart';
|
2024-06-22 15:59:11 +00:00
|
|
|
import 'package:solian/widgets/app_bar_title.dart';
|
2024-08-02 09:14:23 +00:00
|
|
|
import 'package:solian/widgets/channel/channel_call_indicator.dart';
|
2024-05-31 17:25:45 +00:00
|
|
|
import 'package:solian/widgets/chat/call/chat_call_action.dart';
|
2024-06-27 16:59:11 +00:00
|
|
|
import 'package:solian/widgets/chat/chat_event.dart';
|
2024-07-06 09:12:57 +00:00
|
|
|
import 'package:solian/widgets/chat/chat_event_list.dart';
|
2024-05-26 05:39:21 +00:00
|
|
|
import 'package:solian/widgets/chat/chat_message_input.dart';
|
2024-06-06 12:23:50 +00:00
|
|
|
import 'package:solian/widgets/current_state_action.dart';
|
2024-05-25 17:21:08 +00:00
|
|
|
|
|
|
|
class ChannelChatScreen extends StatefulWidget {
|
|
|
|
final String alias;
|
|
|
|
final String realm;
|
|
|
|
|
|
|
|
const ChannelChatScreen({
|
|
|
|
super.key,
|
|
|
|
required this.alias,
|
|
|
|
this.realm = 'global',
|
|
|
|
});
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<ChannelChatScreen> createState() => _ChannelChatScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ChannelChatScreenState extends State<ChannelChatScreen> {
|
|
|
|
bool _isBusy = false;
|
2024-05-30 14:02:54 +00:00
|
|
|
int? _accountId;
|
|
|
|
|
2024-07-24 17:18:47 +00:00
|
|
|
String? _newAlias;
|
2024-05-25 17:21:08 +00:00
|
|
|
|
|
|
|
Channel? _channel;
|
2024-05-31 17:25:45 +00:00
|
|
|
Call? _ongoingCall;
|
2024-06-23 10:03:46 +00:00
|
|
|
ChannelMember? _channelProfile;
|
2024-05-26 05:39:21 +00:00
|
|
|
StreamSubscription<NetworkPackage>? _subscription;
|
2024-05-25 17:21:08 +00:00
|
|
|
|
2024-06-27 16:05:43 +00:00
|
|
|
late final ChatEventController _chatController;
|
2024-05-25 17:21:08 +00:00
|
|
|
|
2024-08-02 09:14:23 +00:00
|
|
|
_getChannel({String? alias}) async {
|
2024-05-25 17:21:08 +00:00
|
|
|
final ChannelProvider provider = Get.find();
|
|
|
|
|
|
|
|
setState(() => _isBusy = true);
|
|
|
|
|
2024-07-24 17:18:47 +00:00
|
|
|
if (alias != null) _newAlias = alias;
|
2024-05-26 15:13:43 +00:00
|
|
|
|
2024-05-25 17:21:08 +00:00
|
|
|
try {
|
2024-05-26 15:13:43 +00:00
|
|
|
final resp = await provider.getChannel(
|
2024-07-24 17:18:47 +00:00
|
|
|
_newAlias ?? widget.alias,
|
2024-05-26 15:13:43 +00:00
|
|
|
realm: widget.realm,
|
|
|
|
);
|
2024-06-08 16:09:01 +00:00
|
|
|
final respProfile = await provider.getMyChannelProfile(
|
2024-07-24 17:18:47 +00:00
|
|
|
_newAlias ?? widget.alias,
|
2024-06-08 16:09:01 +00:00
|
|
|
realm: widget.realm,
|
|
|
|
);
|
|
|
|
setState(() {
|
|
|
|
_channel = Channel.fromJson(resp.body);
|
|
|
|
_channelProfile = ChannelMember.fromJson(respProfile.body);
|
|
|
|
});
|
2024-05-25 17:21:08 +00:00
|
|
|
} catch (e) {
|
|
|
|
context.showErrorDialog(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
setState(() => _isBusy = false);
|
|
|
|
}
|
|
|
|
|
2024-08-02 09:14:23 +00:00
|
|
|
_getOngoingCall() async {
|
2024-05-31 17:25:45 +00:00
|
|
|
final ChannelProvider provider = Get.find();
|
|
|
|
|
|
|
|
setState(() => _isBusy = true);
|
|
|
|
|
|
|
|
try {
|
|
|
|
final resp = await provider.getChannelOngoingCall(
|
2024-07-24 17:18:47 +00:00
|
|
|
_newAlias ?? widget.alias,
|
2024-05-31 17:25:45 +00:00
|
|
|
realm: widget.realm,
|
|
|
|
);
|
|
|
|
if (resp != null) {
|
|
|
|
setState(() => _ongoingCall = Call.fromJson(resp.body));
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
context.showErrorDialog(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
setState(() => _isBusy = false);
|
|
|
|
}
|
|
|
|
|
2024-08-02 09:14:23 +00:00
|
|
|
void _listenMessages() {
|
2024-07-16 11:46:53 +00:00
|
|
|
final WebSocketProvider provider = Get.find();
|
2024-05-26 05:39:21 +00:00
|
|
|
_subscription = provider.stream.stream.listen((event) {
|
|
|
|
switch (event.method) {
|
2024-06-27 16:05:43 +00:00
|
|
|
case 'events.new':
|
|
|
|
final payload = Event.fromJson(event.payload!);
|
2024-07-06 09:12:57 +00:00
|
|
|
_chatController.receiveEvent(payload);
|
2024-05-26 05:39:21 +00:00
|
|
|
break;
|
2024-05-31 17:25:45 +00:00
|
|
|
case 'calls.new':
|
|
|
|
final payload = Call.fromJson(event.payload!);
|
2024-08-02 10:09:07 +00:00
|
|
|
if (payload.channel.id == _channel!.id) {
|
|
|
|
setState(() => _ongoingCall = payload);
|
|
|
|
}
|
2024-05-31 17:25:45 +00:00
|
|
|
break;
|
|
|
|
case 'calls.end':
|
2024-08-02 10:09:07 +00:00
|
|
|
final payload = Call.fromJson(event.payload!);
|
|
|
|
if (payload.channel.id == _channel!.id) {
|
|
|
|
setState(() => _ongoingCall = null);
|
|
|
|
}
|
2024-05-31 17:25:45 +00:00
|
|
|
break;
|
2024-05-26 05:39:21 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-06-27 16:05:43 +00:00
|
|
|
Event? _messageToReplying;
|
|
|
|
Event? _messageToEditing;
|
2024-05-30 15:14:29 +00:00
|
|
|
|
2024-06-27 16:05:43 +00:00
|
|
|
Widget buildHistoryBody(Event item, {bool isMerged = false}) {
|
2024-06-27 16:59:11 +00:00
|
|
|
return ChatEvent(
|
2024-06-23 10:03:46 +00:00
|
|
|
key: Key('m${item.uuid}'),
|
|
|
|
item: item,
|
|
|
|
isMerged: isMerged,
|
2024-06-27 18:49:28 +00:00
|
|
|
chatController: _chatController,
|
2024-06-23 10:03:46 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-05-25 17:21:08 +00:00
|
|
|
@override
|
|
|
|
void initState() {
|
2024-07-24 17:18:47 +00:00
|
|
|
_accountId = Get.find<AuthProvider>().userProfile.value!['id'];
|
|
|
|
|
2024-06-27 16:05:43 +00:00
|
|
|
_chatController = ChatEventController();
|
2024-06-23 10:51:49 +00:00
|
|
|
_chatController.initialize();
|
2024-06-23 05:27:21 +00:00
|
|
|
|
2024-08-02 09:14:23 +00:00
|
|
|
_getOngoingCall();
|
|
|
|
_getChannel().then((_) {
|
2024-06-27 16:05:43 +00:00
|
|
|
_chatController.getEvents(_channel!, widget.realm);
|
2024-08-02 09:14:23 +00:00
|
|
|
_listenMessages();
|
2024-06-23 10:51:49 +00:00
|
|
|
});
|
2024-06-23 05:27:21 +00:00
|
|
|
|
|
|
|
super.initState();
|
2024-05-25 17:21:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-06-01 12:18:25 +00:00
|
|
|
if (_isBusy || _channel == null) {
|
2024-06-08 16:09:01 +00:00
|
|
|
return Material(
|
|
|
|
color: Theme.of(context).colorScheme.surface,
|
|
|
|
child: const Center(
|
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
),
|
2024-05-25 17:21:08 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-05-30 14:02:54 +00:00
|
|
|
String title = _channel?.name ?? 'loading'.tr;
|
|
|
|
String? placeholder;
|
|
|
|
|
|
|
|
if (_channel?.type == 1) {
|
|
|
|
final otherside = _channel!.members!
|
|
|
|
.where((e) => e.account.externalId != _accountId)
|
|
|
|
.first;
|
|
|
|
title = otherside.account.nick;
|
|
|
|
placeholder = 'messageInputPlaceholder'.trParams(
|
|
|
|
{'channel': '@${otherside.account.name}'},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-05-25 17:21:08 +00:00
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
2024-07-12 14:37:58 +00:00
|
|
|
leading: AppBarLeadingButton.adaptive(context),
|
2024-06-22 15:59:11 +00:00
|
|
|
title: AppBarTitle(title),
|
2024-05-25 17:21:08 +00:00
|
|
|
centerTitle: false,
|
2024-06-22 15:59:11 +00:00
|
|
|
titleSpacing: SolianTheme.titleSpacing(context),
|
|
|
|
toolbarHeight: SolianTheme.toolbarHeight(context),
|
2024-05-25 17:21:08 +00:00
|
|
|
actions: [
|
2024-06-06 12:23:50 +00:00
|
|
|
const BackgroundStateWidget(),
|
2024-05-31 17:25:45 +00:00
|
|
|
Builder(builder: (context) {
|
|
|
|
if (_isBusy) return const SizedBox();
|
|
|
|
return ChatCallButton(
|
|
|
|
realm: _channel!.realm,
|
|
|
|
channel: _channel!,
|
|
|
|
ongoingCall: _ongoingCall,
|
|
|
|
);
|
|
|
|
}),
|
2024-05-25 17:21:08 +00:00
|
|
|
IconButton(
|
2024-05-26 15:13:43 +00:00
|
|
|
icon: const Icon(Icons.more_vert),
|
|
|
|
onPressed: () {
|
|
|
|
AppRouter.instance
|
|
|
|
.pushNamed(
|
|
|
|
'channelDetail',
|
|
|
|
pathParameters: {'alias': widget.alias},
|
|
|
|
queryParameters: {'realm': widget.realm},
|
2024-06-08 16:09:01 +00:00
|
|
|
extra: ChannelDetailArguments(
|
|
|
|
profile: _channelProfile!,
|
|
|
|
channel: _channel!,
|
|
|
|
),
|
2024-05-26 15:13:43 +00:00
|
|
|
)
|
|
|
|
.then((value) {
|
|
|
|
if (value == false) AppRouter.instance.pop();
|
|
|
|
if (value != null) {
|
|
|
|
final resp = Channel.fromJson(value as Map<String, dynamic>);
|
2024-08-02 09:14:23 +00:00
|
|
|
_getChannel(alias: resp.alias);
|
2024-05-26 15:13:43 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2024-05-25 17:21:08 +00:00
|
|
|
),
|
|
|
|
SizedBox(
|
|
|
|
width: SolianTheme.isLargeScreen(context) ? 8 : 16,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2024-07-06 09:12:57 +00:00
|
|
|
body: Column(
|
2024-05-25 17:21:08 +00:00
|
|
|
children: [
|
2024-05-31 17:25:45 +00:00
|
|
|
if (_ongoingCall != null)
|
2024-08-02 09:14:23 +00:00
|
|
|
ChannelCallIndicator(
|
|
|
|
channel: _channel!,
|
|
|
|
ongoingCall: _ongoingCall!,
|
2024-07-06 09:12:57 +00:00
|
|
|
),
|
|
|
|
Expanded(
|
|
|
|
child: ChatEventList(
|
|
|
|
scope: widget.realm,
|
|
|
|
channel: _channel!,
|
|
|
|
chatController: _chatController,
|
|
|
|
onEdit: (item) {
|
|
|
|
setState(() => _messageToEditing = item);
|
|
|
|
},
|
|
|
|
onReply: (item) {
|
|
|
|
setState(() => _messageToReplying = item);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Obx(() {
|
|
|
|
if (_chatController.isLoading.isTrue) {
|
|
|
|
return const LinearProgressIndicator().animate().slideY();
|
|
|
|
} else {
|
|
|
|
return const SizedBox();
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
ClipRect(
|
|
|
|
child: BackdropFilter(
|
|
|
|
filter: ImageFilter.blur(sigmaX: 50, sigmaY: 50),
|
|
|
|
child: SafeArea(
|
|
|
|
child: ChatMessageInput(
|
|
|
|
edit: _messageToEditing,
|
|
|
|
reply: _messageToReplying,
|
|
|
|
realm: widget.realm,
|
|
|
|
placeholder: placeholder,
|
|
|
|
channel: _channel!,
|
|
|
|
onSent: (Event item) {
|
|
|
|
setState(() {
|
|
|
|
_chatController.addPendingEvent(item);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
onReset: () {
|
|
|
|
setState(() {
|
|
|
|
_messageToReplying = null;
|
|
|
|
_messageToEditing = null;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
),
|
2024-06-01 12:18:25 +00:00
|
|
|
),
|
2024-05-31 17:25:45 +00:00
|
|
|
),
|
2024-07-06 09:12:57 +00:00
|
|
|
),
|
2024-05-25 17:21:08 +00:00
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2024-05-26 05:39:21 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_subscription?.cancel();
|
|
|
|
super.dispose();
|
|
|
|
}
|
2024-05-25 17:21:08 +00:00
|
|
|
}
|