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;
|
2024-05-05 15:01:08 +00:00
|
|
|
final String realm;
|
2024-04-26 15:25:56 +00:00
|
|
|
final bool isOwned;
|
|
|
|
|
2024-05-05 15:01:08 +00:00
|
|
|
const ChannelDeletion({
|
|
|
|
super.key,
|
|
|
|
required this.channel,
|
|
|
|
required this.realm,
|
|
|
|
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(
|
2024-05-05 15:01:08 +00:00
|
|
|
getRequestUri('messaging', '/api/channels/${widget.realm}/${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;
|
|
|
|
}
|
|
|
|
|
2024-05-05 15:01:08 +00:00
|
|
|
var res = await auth.client!.delete(
|
|
|
|
getRequestUri('messaging', '/api/channels/${widget.realm}/${widget.channel.alias}/me'),
|
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),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|