2024-04-26 17:36:54 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
2024-04-19 16:01:48 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2024-04-26 17:36:54 +00:00
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
import 'package:solian/models/call.dart';
|
2024-04-19 16:01:48 +00:00
|
|
|
import 'package:solian/models/channel.dart';
|
2024-04-26 17:36:54 +00:00
|
|
|
import 'package:solian/providers/auth.dart';
|
2024-05-02 13:35:33 +00:00
|
|
|
import 'package:solian/providers/chat.dart';
|
2024-04-19 16:01:48 +00:00
|
|
|
import 'package:solian/router.dart';
|
2024-04-26 17:36:54 +00:00
|
|
|
import 'package:solian/utils/service_url.dart';
|
2024-04-29 12:22:06 +00:00
|
|
|
import 'package:solian/widgets/exts.dart';
|
2024-04-19 16:01:48 +00:00
|
|
|
|
2024-04-26 17:36:54 +00:00
|
|
|
class ChannelCallAction extends StatefulWidget {
|
|
|
|
final Call? call;
|
2024-04-19 16:01:48 +00:00
|
|
|
final Channel channel;
|
|
|
|
final Function onUpdate;
|
|
|
|
|
2024-05-02 13:35:33 +00:00
|
|
|
const ChannelCallAction({super.key, this.call, required this.channel, required this.onUpdate});
|
2024-04-26 17:36:54 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
State<ChannelCallAction> createState() => _ChannelCallActionState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ChannelCallActionState extends State<ChannelCallAction> {
|
|
|
|
bool _isSubmitting = false;
|
|
|
|
|
|
|
|
Future<void> makeCall() async {
|
|
|
|
setState(() => _isSubmitting = true);
|
|
|
|
|
|
|
|
final auth = context.read<AuthProvider>();
|
|
|
|
if (!await auth.isAuthorized()) {
|
|
|
|
setState(() => _isSubmitting = false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-05-02 13:35:33 +00:00
|
|
|
var uri = getRequestUri('messaging', '/api/channels/${widget.channel.alias}/calls');
|
2024-04-26 17:36:54 +00:00
|
|
|
|
|
|
|
var res = await auth.client!.post(uri);
|
|
|
|
if (res.statusCode != 200) {
|
|
|
|
var message = utf8.decode(res.bodyBytes);
|
2024-04-29 12:22:06 +00:00
|
|
|
context.showErrorDialog(message);
|
2024-04-26 17:36:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setState(() => _isSubmitting = false);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> endsCall() async {
|
|
|
|
setState(() => _isSubmitting = true);
|
|
|
|
|
2024-05-02 13:35:33 +00:00
|
|
|
final chat = context.read<ChatProvider>();
|
2024-04-26 17:36:54 +00:00
|
|
|
final auth = context.read<AuthProvider>();
|
|
|
|
if (!await auth.isAuthorized()) {
|
|
|
|
setState(() => _isSubmitting = false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-05-02 13:35:33 +00:00
|
|
|
var uri = getRequestUri('messaging', '/api/channels/${widget.channel.alias}/calls/ongoing');
|
2024-04-26 17:36:54 +00:00
|
|
|
|
|
|
|
var res = await auth.client!.delete(uri);
|
|
|
|
if (res.statusCode != 200) {
|
|
|
|
var message = utf8.decode(res.bodyBytes);
|
2024-04-29 12:22:06 +00:00
|
|
|
context.showErrorDialog(message);
|
2024-05-02 13:35:33 +00:00
|
|
|
} else {
|
|
|
|
if (chat.currentCall != null && chat.currentCall?.info.channelId == widget.channel.id) {
|
|
|
|
chat.currentCall!.deactivate();
|
|
|
|
chat.currentCall!.dispose();
|
|
|
|
}
|
2024-04-26 17:36:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setState(() => _isSubmitting = false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return IconButton(
|
|
|
|
onPressed: _isSubmitting
|
|
|
|
? null
|
|
|
|
: () {
|
|
|
|
if (widget.call == null) {
|
|
|
|
makeCall();
|
|
|
|
} else {
|
|
|
|
endsCall();
|
|
|
|
}
|
|
|
|
},
|
2024-05-02 13:35:33 +00:00
|
|
|
icon: widget.call == null ? const Icon(Icons.call) : const Icon(Icons.call_end),
|
2024-04-26 17:36:54 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ChannelManageAction extends StatelessWidget {
|
|
|
|
final Channel channel;
|
|
|
|
final Function onUpdate;
|
2024-04-19 16:01:48 +00:00
|
|
|
|
2024-05-02 13:35:33 +00:00
|
|
|
const ChannelManageAction({super.key, required this.channel, required this.onUpdate});
|
2024-04-19 16:01:48 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-04-25 13:33:53 +00:00
|
|
|
return IconButton(
|
2024-04-26 15:25:56 +00:00
|
|
|
onPressed: () async {
|
|
|
|
final result = await router.pushNamed(
|
2024-04-25 13:33:53 +00:00
|
|
|
'chat.channel.manage',
|
|
|
|
extra: channel,
|
|
|
|
pathParameters: {'channel': channel.alias},
|
|
|
|
);
|
2024-04-26 17:36:54 +00:00
|
|
|
switch (result) {
|
2024-04-26 15:25:56 +00:00
|
|
|
case 'disposed':
|
2024-04-26 17:36:54 +00:00
|
|
|
if (router.canPop()) router.pop('refresh');
|
2024-04-26 15:25:56 +00:00
|
|
|
case 'refresh':
|
|
|
|
onUpdate();
|
|
|
|
}
|
2024-04-25 13:33:53 +00:00
|
|
|
},
|
2024-04-26 17:36:54 +00:00
|
|
|
icon: const Icon(Icons.settings),
|
2024-04-19 16:01:48 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|