From 04065061e0a17c76034968ddb89010c9874c4b13 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 15 Feb 2025 19:20:34 +0800 Subject: [PATCH] :sparkles: Leave realm --- assets/translations/en-US.json | 4 +- assets/translations/zh-CN.json | 4 +- lib/screens/realm/realm_detail.dart | 57 ++++++++++++++++++++++------- 3 files changed, 50 insertions(+), 15 deletions(-) diff --git a/assets/translations/en-US.json b/assets/translations/en-US.json index 3baf419..52e1cef 100644 --- a/assets/translations/en-US.json +++ b/assets/translations/en-US.json @@ -648,5 +648,7 @@ "realmIsPublic": "Public Realm", "realmIsPublicDescription": "The realm is public, anyone can join.", "realmIsCommunity": "Community Realm", - "realmIsCommunityDescription": "Community realm will be displayed on the discover page." + "realmIsCommunityDescription": "Community realm will be displayed on the discover page.", + "realmLeave": "Leave Realm", + "realmLeaveDescription": "Leave the current realm and delete the realm's identity." } diff --git a/assets/translations/zh-CN.json b/assets/translations/zh-CN.json index 5bb8a34..a1b02d1 100644 --- a/assets/translations/zh-CN.json +++ b/assets/translations/zh-CN.json @@ -647,5 +647,7 @@ "realmIsPublic": "公开领域", "realmIsPublicDescription": "该领域是公开的,任何人都可以加入。", "realmIsCommunity": "社区领域", - "realmIsCommunityDescription": "社区领域会显示在发现页面上。" + "realmIsCommunityDescription": "社区领域会显示在发现页面上。", + "realmLeave": "离开领域", + "realmLeaveDescription": "离开当前领域,并且删除领域中的身份。" } diff --git a/lib/screens/realm/realm_detail.dart b/lib/screens/realm/realm_detail.dart index 102618a..14a2360 100644 --- a/lib/screens/realm/realm_detail.dart +++ b/lib/screens/realm/realm_detail.dart @@ -357,6 +357,28 @@ class _RealmSettingsWidgetState extends State<_RealmSettingsWidget> { } } + Future _leaveRealm() async { + final confirm = await context.showConfirmDialog( + 'realmLeave'.tr(), + 'realmLeaveDescription'.tr(), + ); + if (!confirm) return; + if (!mounted) return; + + final sn = context.read(); + + try { + await sn.client.delete('/cgi/id/realms/${widget.realm!.alias}/members/me'); + if (!mounted) return; + Navigator.pop(context, true); + } catch (err) { + if (!mounted) return; + context.showErrorDialog(err); + } finally { + setState(() => _isBusy = false); + } + } + @override Widget build(BuildContext context) { final ua = context.read(); @@ -367,22 +389,31 @@ class _RealmSettingsWidgetState extends State<_RealmSettingsWidget> { children: [ const Gap(8), ListTile( - leading: const Icon(Symbols.edit), + leading: const Icon(Symbols.logout), trailing: const Icon(Symbols.chevron_right), - title: Text('realmEdit').tr(), - subtitle: Text('realmEditDescription').tr(), + title: Text('realmLeave').tr(), + subtitle: Text('realmLeaveDescription').tr(), contentPadding: const EdgeInsets.symmetric(horizontal: 24), - onTap: () { - GoRouter.of(context).pushNamed( - 'realmManage', - queryParameters: {'editing': widget.realm!.alias}, - ).then((value) { - if (value != null) { - widget.onUpdate(); - } - }); - }, + onTap: _isBusy ? null : () => _leaveRealm(), ), + if (isOwned) + ListTile( + leading: const Icon(Symbols.edit), + trailing: const Icon(Symbols.chevron_right), + title: Text('realmEdit').tr(), + subtitle: Text('realmEditDescription').tr(), + contentPadding: const EdgeInsets.symmetric(horizontal: 24), + onTap: () { + GoRouter.of(context).pushNamed( + 'realmManage', + queryParameters: {'editing': widget.realm!.alias}, + ).then((value) { + if (value != null) { + widget.onUpdate(); + } + }); + }, + ), if (isOwned) ListTile( leading: const Icon(Symbols.delete),