2024-05-29 16:05:39 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:get/get.dart';
|
|
|
|
import 'package:solian/exts.dart';
|
|
|
|
import 'package:solian/models/realm.dart';
|
|
|
|
import 'package:solian/providers/auth.dart';
|
|
|
|
|
2024-05-30 15:14:29 +00:00
|
|
|
class RealmDeletionDialog extends StatefulWidget {
|
2024-05-29 16:05:39 +00:00
|
|
|
final Realm realm;
|
|
|
|
final bool isOwned;
|
|
|
|
|
2024-05-30 15:14:29 +00:00
|
|
|
const RealmDeletionDialog({
|
2024-05-29 16:05:39 +00:00
|
|
|
super.key,
|
|
|
|
required this.realm,
|
|
|
|
required this.isOwned,
|
|
|
|
});
|
|
|
|
|
|
|
|
@override
|
2024-05-30 15:14:29 +00:00
|
|
|
State<RealmDeletionDialog> createState() => _RealmDeletionDialogState();
|
2024-05-29 16:05:39 +00:00
|
|
|
}
|
|
|
|
|
2024-05-30 15:14:29 +00:00
|
|
|
class _RealmDeletionDialogState extends State<RealmDeletionDialog> {
|
2024-05-29 16:05:39 +00:00
|
|
|
bool _isBusy = false;
|
|
|
|
|
2024-06-06 12:49:18 +00:00
|
|
|
Future<void> deleteRealm() async {
|
2024-05-29 16:05:39 +00:00
|
|
|
final AuthProvider auth = Get.find();
|
|
|
|
if (!await auth.isAuthorized) return;
|
|
|
|
|
|
|
|
setState(() => _isBusy = true);
|
|
|
|
|
2024-07-16 11:46:53 +00:00
|
|
|
final client = auth.configureClient('auth');
|
2024-05-29 16:05:39 +00:00
|
|
|
|
2024-07-16 11:46:53 +00:00
|
|
|
final resp = await client.delete('/realms/${widget.realm.id}');
|
2024-05-29 16:05:39 +00:00
|
|
|
if (resp.statusCode != 200) {
|
|
|
|
context.showErrorDialog(resp.bodyString);
|
|
|
|
} else if (Navigator.canPop(context)) {
|
|
|
|
Navigator.pop(context, 'OK');
|
|
|
|
}
|
|
|
|
|
|
|
|
setState(() => _isBusy = false);
|
|
|
|
}
|
|
|
|
|
2024-06-06 12:49:18 +00:00
|
|
|
Future<void> leaveRealm() async {
|
2024-05-29 16:05:39 +00:00
|
|
|
final AuthProvider auth = Get.find();
|
|
|
|
if (!await auth.isAuthorized) return;
|
|
|
|
|
|
|
|
setState(() => _isBusy = true);
|
|
|
|
|
2024-07-16 11:46:53 +00:00
|
|
|
final client = auth.configureClient('auth');
|
2024-05-29 16:05:39 +00:00
|
|
|
|
|
|
|
final resp =
|
2024-07-16 11:46:53 +00:00
|
|
|
await client.delete('/realms/${widget.realm.id}/members/me');
|
2024-05-29 16:05:39 +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
|
|
|
|
? 'realmDeletionConfirm'.tr
|
|
|
|
: 'channelLeaveConfirm'.tr),
|
2024-05-29 16:05:39 +00:00
|
|
|
content: Text(
|
2024-06-08 16:09:01 +00:00
|
|
|
widget.isOwned
|
|
|
|
? 'realmDeletionConfirmCaption'
|
|
|
|
.trParams({'realm': '#${widget.realm.alias}'})
|
|
|
|
: 'realmLeaveConfirmCaption'
|
|
|
|
.trParams({'realm': '#${widget.realm.alias}'}),
|
2024-05-29 16:05:39 +00:00
|
|
|
),
|
|
|
|
actions: <Widget>[
|
|
|
|
TextButton(
|
|
|
|
onPressed: _isBusy ? null : () => Navigator.pop(context),
|
|
|
|
child: Text('cancel'.tr),
|
|
|
|
),
|
|
|
|
TextButton(
|
|
|
|
onPressed: _isBusy
|
|
|
|
? null
|
|
|
|
: widget.isOwned
|
2024-06-06 12:49:18 +00:00
|
|
|
? deleteRealm
|
|
|
|
: leaveRealm,
|
2024-05-29 16:05:39 +00:00
|
|
|
child: Text('confirm'.tr),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|