Solian/lib/screens/channel/channel_chat.dart

377 lines
12 KiB
Dart
Raw Permalink Normal View History

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';
import 'package:get/get.dart';
2024-10-06 09:31:44 +00:00
import 'package:shared_preferences/shared_preferences.dart';
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';
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/call.dart';
2024-05-25 17:21:08 +00:00
import 'package:solian/providers/content/channel.dart';
import 'package:solian/providers/websocket.dart';
2024-05-26 15:13:43 +00:00
import 'package:solian/router.dart';
import 'package:solian/screens/channel/call/call.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';
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-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-08-23 14:43:04 +00:00
import 'package:solian/widgets/chat/chat_typing_indicator.dart';
2024-06-06 12:23:50 +00:00
import 'package:solian/widgets/current_state_action.dart';
2024-10-06 11:41:44 +00:00
import 'package:solian/widgets/root_container.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>
with WidgetsBindingObserver, TickerProviderStateMixin {
DateTime? _isOutOfSyncSince;
2024-05-25 17:21:08 +00:00
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
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-23 15:16:41 +00:00
final List<ChannelMember> _typingUsers = List.empty(growable: true);
final Map<int, Timer> _typingInactiveTimer = {};
2024-08-23 14:43:04 +00:00
2024-08-02 09:14:23 +00:00
void _listenMessages() {
2024-08-23 14:43:04 +00:00
final WebSocketProvider ws = Get.find();
_subscription = ws.stream.stream.listen((event) {
2024-05-26 05:39:21 +00:00
switch (event.method) {
case 'events.new':
final payload = Event.fromJson(event.payload!);
2024-08-23 14:43:04 +00:00
final typingIdx =
_typingUsers.indexWhere((x) => x.id == payload.senderId);
if (typingIdx != -1) _typingUsers.removeAt(typingIdx);
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!);
if (payload.channel.id == _channel!.id) {
setState(() => _ongoingCall = payload);
}
2024-05-31 17:25:45 +00:00
break;
case 'calls.end':
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-08-23 14:43:04 +00:00
case 'status.typing':
if (event.payload?['channel_id'] != _channel!.id) break;
final member = ChannelMember.fromJson(event.payload!['member']);
2024-08-23 15:16:41 +00:00
if (member.id == _channelProfile!.id) break;
2024-08-23 14:43:04 +00:00
if (!_typingUsers.any((x) => x.id == member.id)) {
setState(() {
_typingUsers.add(member);
});
}
_typingInactiveTimer[member.id]?.cancel();
_typingInactiveTimer[member.id] = Timer(
const Duration(seconds: 3),
() {
setState(() {
_typingUsers.removeWhere((x) => x.id == member.id);
_typingInactiveTimer.remove(member.id);
});
},
);
2024-05-26 05:39:21 +00:00
}
});
}
void _keepUpdateWithServer() {
_getOngoingCall();
2024-09-15 02:55:27 +00:00
_chatController.getInitialEvents(_channel!, widget.realm);
setState(() => _isOutOfSyncSince = null);
}
Event? _messageToReplying;
Event? _messageToEditing;
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.resumed:
if (_isOutOfSyncSince == null) break;
if (DateTime.now().difference(_isOutOfSyncSince!).inSeconds < 30) break;
_keepUpdateWithServer();
break;
case AppLifecycleState.paused:
if (mounted) {
setState(() => _isOutOfSyncSince = DateTime.now());
}
break;
default:
break;
}
2024-06-23 10:03:46 +00:00
}
2024-10-06 09:31:44 +00:00
late SharedPreferences _prefs;
2024-05-25 17:21:08 +00:00
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
2024-07-24 17:18:47 +00:00
_accountId = Get.find<AuthProvider>().userProfile.value!['id'];
_chatController = ChatEventController();
_chatController.initialize();
2024-10-06 09:31:44 +00:00
SharedPreferences.getInstance().then((inst) {
_prefs = inst;
_getOngoingCall();
_getChannel().then((_) {
_chatController.getInitialEvents(_channel!, widget.realm);
_listenMessages();
});
});
2024-05-25 17:21:08 +00:00
}
@override
Widget build(BuildContext context) {
2024-05-30 14:02:54 +00:00
String title = _channel?.name ?? 'loading'.tr;
String? placeholder;
final otherside =
2024-10-04 19:38:30 +00:00
_channel?.members!.where((e) => e.account.id != _accountId).firstOrNull;
if (_channel?.type == 1 && otherside != null) {
2024-05-30 14:02:54 +00:00
title = otherside.account.nick;
placeholder = 'messageInputPlaceholder'.trParams(
{'channel': '@${otherside.account.name}'},
);
}
2024-10-06 17:47:34 +00:00
return ResponsiveRootContainer(
2024-10-06 11:41:44 +00:00
child: Scaffold(
appBar: AppBar(
leading: AppBarLeadingButton.adaptive(context),
title: AppBarTitle(title),
centerTitle: false,
titleSpacing: AppTheme.titleSpacing(context),
toolbarHeight: AppTheme.toolbarHeight(context),
actions: [
const BackgroundStateWidget(),
Builder(builder: (context) {
if (_isBusy || _channel == null) return const SizedBox.shrink();
return ChatCallButton(
realm: _channel!.realm,
channel: _channel!,
ongoingCall: _ongoingCall,
);
}),
IconButton(
icon: const Icon(Icons.more_vert),
onPressed: () {
if (_channel == null) return;
AppRouter.instance
.pushNamed(
'channelDetail',
pathParameters: {'alias': widget.alias},
queryParameters: {'realm': widget.realm},
extra: ChannelDetailArguments(
profile: _channelProfile!,
channel: _channel!,
),
)
.then((value) {
if (value == false) AppRouter.instance.pop();
if (value != null) {
final resp =
Channel.fromJson(value as Map<String, dynamic>);
_getChannel(alias: resp.alias);
}
});
},
),
SizedBox(
width: AppTheme.isLargeScreen(context) ? 8 : 16,
),
],
),
body: Builder(builder: (context) {
if (_isBusy || _channel == null) {
return const Center(
child: CircularProgressIndicator(),
2024-05-31 17:25:45 +00:00
);
2024-10-06 11:41:44 +00:00
}
2024-10-06 11:41:44 +00:00
return Row(
children: [
Expanded(
child: Column(
children: [
if (_ongoingCall != null)
ChannelCallIndicator(
channel: _channel!,
ongoingCall: _ongoingCall!,
onJoin: () {
if (!AppTheme.isUltraLargeScreen(context)) {
final ChatCallProvider call = Get.find();
call.gotoScreen(context);
}
},
),
Expanded(
child: ChatEventList(
noAnimated:
_prefs.getBool('non_animated_message_list') ??
false,
scope: widget.realm,
channel: _channel!,
chatController: _chatController,
onEdit: (item) {
setState(() => _messageToEditing = item);
},
onReply: (item) {
setState(() => _messageToReplying = item);
},
),
),
2024-10-06 11:41:44 +00:00
ClipRect(
child: BackdropFilter(
filter: ImageFilter.blur(sigmaX: 50, sigmaY: 50),
child: SafeArea(
child: Column(
children: [
ChatTypingIndicator(users: _typingUsers),
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-10-06 11:41:44 +00:00
],
),
),
2024-10-06 11:41:44 +00:00
Obx(() {
final ChatCallProvider call = Get.find();
if (call.isMounted.value &&
AppTheme.isUltraLargeScreen(context)) {
return const Expanded(
child: Row(children: [
VerticalDivider(width: 0.3, thickness: 0.3),
Expanded(
child: CallScreen(
hideAppBar: true,
isExpandable: true,
),
),
2024-10-06 11:41:44 +00:00
]),
);
}
return const SizedBox.shrink();
}),
],
);
}),
),
2024-05-25 17:21:08 +00:00
);
}
2024-05-26 05:39:21 +00:00
@override
void dispose() {
2024-08-23 14:43:04 +00:00
for (var timer in _typingInactiveTimer.values) {
timer.cancel();
}
2024-05-26 05:39:21 +00:00
_subscription?.cancel();
WidgetsBinding.instance.removeObserver(this);
2024-05-26 05:39:21 +00:00
super.dispose();
}
2024-05-25 17:21:08 +00:00
}