Solian/lib/widgets/chat/channel_deletion.dart

99 lines
2.8 KiB
Dart
Raw Normal View History

2024-04-26 15:25:56 +00:00
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:solian/models/channel.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:solian/providers/auth.dart';
import 'package:solian/utils/service_url.dart';
2024-04-29 12:22:06 +00:00
import 'package:solian/widgets/exts.dart';
2024-04-26 15:25:56 +00:00
class ChannelDeletion extends StatefulWidget {
final Channel channel;
final bool isOwned;
2024-05-01 09:37:34 +00:00
const ChannelDeletion(
{super.key, required this.channel, required this.isOwned});
2024-04-26 15:25:56 +00:00
@override
State<ChannelDeletion> createState() => _ChannelDeletionState();
}
class _ChannelDeletionState extends State<ChannelDeletion> {
bool _isSubmitting = false;
Future<void> deleteChannel() async {
setState(() => _isSubmitting = true);
final auth = context.read<AuthProvider>();
if (!await auth.isAuthorized()) {
setState(() => _isSubmitting = false);
return;
}
var res = await auth.client!.delete(
getRequestUri('messaging', '/api/channels/global/${widget.channel.id}'),
2024-04-26 15:25:56 +00:00
);
if (res.statusCode != 200) {
var message = utf8.decode(res.bodyBytes);
2024-04-29 12:22:06 +00:00
context.showErrorDialog(message);
2024-04-26 15:25:56 +00:00
} else if (Navigator.canPop(context)) {
Navigator.pop(context, true);
}
setState(() => _isSubmitting = false);
}
Future<void> leaveChannel() async {
setState(() => _isSubmitting = true);
final auth = context.read<AuthProvider>();
if (!await auth.isAuthorized()) {
setState(() => _isSubmitting = false);
return;
}
var res = await auth.client!.post(
getRequestUri('messaging', '/api/channels/global/${widget.channel.alias}/leave'),
2024-04-26 15:25:56 +00:00
);
if (res.statusCode != 200) {
var message = utf8.decode(res.bodyBytes);
2024-04-29 12:22:06 +00:00
context.showErrorDialog(message);
2024-04-26 15:25:56 +00:00
} else if (Navigator.canPop(context)) {
Navigator.pop(context, true);
}
setState(() => _isSubmitting = false);
}
@override
Widget build(BuildContext context) {
final content = widget.isOwned
? AppLocalizations.of(context)!.chatChannelDeleteConfirm
: AppLocalizations.of(context)!.chatChannelLeaveConfirm;
return AlertDialog(
title: Text(AppLocalizations.of(context)!.confirmation),
content: Text(content),
actions: <Widget>[
TextButton(
onPressed: _isSubmitting ? null : () => Navigator.pop(context),
child: Text(AppLocalizations.of(context)!.confirmCancel),
),
TextButton(
onPressed: _isSubmitting
? null
: () {
if (widget.isOwned) {
deleteChannel();
} else {
leaveChannel();
}
},
child: Text(AppLocalizations.of(context)!.confirmOkay),
),
],
);
}
}