Solian/lib/widgets/channel/channel_deletion.dart

95 lines
2.5 KiB
Dart
Raw Normal View History

2024-05-26 15:13:43 +00:00
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:solian/exts.dart';
import 'package:solian/models/channel.dart';
import 'package:solian/providers/auth.dart';
class ChannelDeletionDialog extends StatefulWidget {
2024-05-26 15:13:43 +00:00
final Channel channel;
final String realm;
final bool isOwned;
const ChannelDeletionDialog({
2024-05-26 15:13:43 +00:00
super.key,
required this.channel,
required this.realm,
required this.isOwned,
});
@override
State<ChannelDeletionDialog> createState() => _ChannelDeletionDialogState();
2024-05-26 15:13:43 +00:00
}
class _ChannelDeletionDialogState extends State<ChannelDeletionDialog> {
2024-05-26 15:13:43 +00:00
bool _isBusy = false;
Future<void> deleteChannel() async {
final AuthProvider auth = Get.find();
2024-07-24 17:18:47 +00:00
if (auth.isAuthorized.isFalse) return;
2024-05-26 15:13:43 +00:00
setState(() => _isBusy = true);
2024-09-16 03:57:16 +00:00
final client = await auth.configureClient('messaging');
2024-05-26 15:13:43 +00:00
2024-09-16 03:57:16 +00:00
final resp =
await client.delete('/channels/${widget.realm}/${widget.channel.id}');
2024-05-26 15:13:43 +00:00
if (resp.statusCode != 200) {
context.showErrorDialog(resp.bodyString);
} else if (Navigator.canPop(context)) {
Navigator.pop(context, 'OK');
}
setState(() => _isBusy = false);
}
Future<void> leaveChannel() async {
final AuthProvider auth = Get.find();
2024-07-24 17:18:47 +00:00
if (auth.isAuthorized.isFalse) return;
2024-05-26 15:13:43 +00:00
setState(() => _isBusy = true);
2024-09-16 03:57:16 +00:00
final client = await auth.configureClient('messaging');
2024-05-26 15:13:43 +00:00
final resp = await client.delete(
'/channels/${widget.realm}/${widget.channel.alias}/members/me',
2024-05-26 15:13:43 +00:00
);
if (resp.statusCode != 200) {
context.showErrorDialog(resp.bodyString);
} else if (Navigator.canPop(context)) {
Navigator.pop(context, 'OK');
}
setState(() => _isBusy = false);
}
@override
Widget build(BuildContext context) {
return AlertDialog(
2024-06-08 16:09:01 +00:00
title: Text(widget.isOwned
? 'channelDeletionConfirm'.tr
: 'channelLeaveConfirm'.tr),
2024-05-26 15:13:43 +00:00
content: Text(
2024-09-16 03:57:16 +00:00
widget.isOwned
? 'channelDeletionConfirmCaption'
.trParams({'channel': '#${widget.channel.alias}'})
: 'channelLeaveConfirmCaption'
.trParams({'channel': '#${widget.channel.alias}'}),
2024-05-26 15:13:43 +00:00
),
actions: <Widget>[
TextButton(
onPressed: _isBusy ? null : () => Navigator.pop(context),
child: Text('cancel'.tr),
),
TextButton(
onPressed: _isBusy
? null
: widget.isOwned
? deleteChannel
: leaveChannel,
child: Text('confirm'.tr),
),
],
);
}
}