Solian/lib/widgets/chat/channel_action.dart

112 lines
3.0 KiB
Dart
Raw Normal View History

2024-04-26 17:36:54 +00:00
import 'dart:convert';
import 'package:flutter/material.dart';
2024-04-26 17:36:54 +00:00
import 'package:provider/provider.dart';
import 'package:solian/models/call.dart';
import 'package:solian/models/channel.dart';
2024-04-26 17:36:54 +00:00
import 'package:solian/providers/auth.dart';
import 'package:solian/router.dart';
2024-04-26 17:36:54 +00:00
import 'package:solian/utils/service_url.dart';
2024-04-26 17:36:54 +00:00
class ChannelCallAction extends StatefulWidget {
final Call? call;
final Channel channel;
final Function onUpdate;
2024-04-26 17:36:54 +00:00
const ChannelCallAction({super.key, this.call, required this.channel, required this.onUpdate});
@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;
}
var uri = getRequestUri('messaging', '/api/channels/${widget.channel.alias}/calls');
var res = await auth.client!.post(uri);
if (res.statusCode != 200) {
var message = utf8.decode(res.bodyBytes);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text("Something went wrong... $message")),
);
}
setState(() => _isSubmitting = false);
}
Future<void> endsCall() async {
setState(() => _isSubmitting = true);
final auth = context.read<AuthProvider>();
if (!await auth.isAuthorized()) {
setState(() => _isSubmitting = false);
return;
}
var uri = getRequestUri('messaging', '/api/channels/${widget.channel.alias}/calls/ongoing');
var res = await auth.client!.delete(uri);
if (res.statusCode != 200) {
var message = utf8.decode(res.bodyBytes);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text("Something went wrong... $message")),
);
}
setState(() => _isSubmitting = false);
}
@override
Widget build(BuildContext context) {
return IconButton(
onPressed: _isSubmitting
? null
: () {
if (widget.call == null) {
makeCall();
} else {
endsCall();
}
},
icon: widget.call == null ? const Icon(Icons.call) : const Icon(Icons.call_end),
);
}
}
class ChannelManageAction extends StatelessWidget {
final Channel channel;
final Function onUpdate;
2024-04-26 17:36:54 +00:00
const ChannelManageAction({super.key, required this.channel, required this.onUpdate});
@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),
);
}
}