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';
|
|
|
|
import 'package:solian/services.dart';
|
|
|
|
import 'package:solian/widgets/account/account_avatar.dart';
|
2024-06-27 07:57:02 +00:00
|
|
|
import 'package:solian/widgets/account/account_profile_popup.dart';
|
2024-07-23 17:17:41 +00:00
|
|
|
import 'package:solian/widgets/account/relative_select.dart';
|
2024-10-14 13:13:57 +00:00
|
|
|
import 'package:solian/widgets/loading_indicator.dart';
|
2024-05-29 16:05:39 +00:00
|
|
|
|
|
|
|
class RealmMemberListPopup extends StatefulWidget {
|
|
|
|
final Realm realm;
|
|
|
|
|
|
|
|
const RealmMemberListPopup({
|
|
|
|
super.key,
|
|
|
|
required this.realm,
|
|
|
|
});
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<RealmMemberListPopup> createState() => _RealmMemberListPopupState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _RealmMemberListPopupState extends State<RealmMemberListPopup> {
|
|
|
|
bool _isBusy = true;
|
|
|
|
int? _accountId;
|
|
|
|
|
|
|
|
List<RealmMember> _members = List.empty();
|
|
|
|
|
|
|
|
void getProfile() async {
|
|
|
|
final AuthProvider auth = Get.find();
|
2024-07-24 17:18:47 +00:00
|
|
|
if (auth.isAuthorized.isFalse) return;
|
2024-05-29 16:05:39 +00:00
|
|
|
|
2024-07-24 17:18:47 +00:00
|
|
|
setState(() => _accountId = auth.userProfile.value!['id']);
|
2024-05-29 16:05:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void getMembers() async {
|
|
|
|
setState(() => _isBusy = true);
|
|
|
|
|
2024-09-16 03:57:16 +00:00
|
|
|
final client = await ServiceFinder.configureClient('auth');
|
2024-05-29 16:05:39 +00:00
|
|
|
|
2024-07-16 11:46:53 +00:00
|
|
|
final resp = await client.get('/realms/${widget.realm.alias}/members');
|
2024-05-29 16:05:39 +00:00
|
|
|
if (resp.statusCode == 200) {
|
|
|
|
setState(() {
|
|
|
|
_members = resp.body
|
|
|
|
.map((x) => RealmMember.fromJson(x))
|
|
|
|
.toList()
|
|
|
|
.cast<RealmMember>();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
context.showErrorDialog(resp.bodyString);
|
|
|
|
}
|
|
|
|
|
|
|
|
setState(() => _isBusy = false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void promptAddMember() async {
|
|
|
|
final input = await showModalBottomSheet(
|
|
|
|
context: context,
|
|
|
|
builder: (context) {
|
2024-07-23 17:17:41 +00:00
|
|
|
return RelativeSelector(title: 'channelMembersAdd'.tr);
|
2024-05-29 16:05:39 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
if (input == null) return;
|
|
|
|
|
|
|
|
addMember(input.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void addMember(String username) async {
|
|
|
|
final AuthProvider auth = Get.find();
|
2024-07-24 17:18:47 +00:00
|
|
|
if (auth.isAuthorized.isFalse) return;
|
2024-05-29 16:05:39 +00:00
|
|
|
|
|
|
|
setState(() => _isBusy = true);
|
|
|
|
|
2024-09-16 03:57:16 +00:00
|
|
|
final client = await auth.configureClient('auth');
|
2024-05-29 16:05:39 +00:00
|
|
|
|
|
|
|
final resp = await client.post(
|
2024-07-16 11:46:53 +00:00
|
|
|
'/realms/${widget.realm.alias}/members',
|
2024-05-29 16:05:39 +00:00
|
|
|
{'target': username},
|
|
|
|
);
|
|
|
|
if (resp.statusCode == 200) {
|
|
|
|
getMembers();
|
|
|
|
} else {
|
|
|
|
context.showErrorDialog(resp.bodyString);
|
|
|
|
}
|
|
|
|
|
|
|
|
setState(() => _isBusy = false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void removeMember(RealmMember item) async {
|
|
|
|
final AuthProvider auth = Get.find();
|
2024-07-24 17:18:47 +00:00
|
|
|
if (auth.isAuthorized.isFalse) return;
|
2024-05-29 16:05:39 +00:00
|
|
|
|
|
|
|
setState(() => _isBusy = true);
|
|
|
|
|
2024-09-16 03:57:16 +00:00
|
|
|
final client = await auth.configureClient('auth');
|
2024-05-29 16:05:39 +00:00
|
|
|
|
|
|
|
final resp = await client.request(
|
2024-07-16 11:46:53 +00:00
|
|
|
'/realms/${widget.realm.alias}/members',
|
2024-05-29 16:05:39 +00:00
|
|
|
'delete',
|
|
|
|
body: {'target': item.account.name},
|
|
|
|
);
|
|
|
|
if (resp.statusCode == 200) {
|
|
|
|
getMembers();
|
|
|
|
} else {
|
|
|
|
context.showErrorDialog(resp.bodyString);
|
|
|
|
}
|
|
|
|
|
|
|
|
setState(() => _isBusy = false);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
|
|
|
|
getProfile();
|
|
|
|
getMembers();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return SizedBox(
|
|
|
|
height: MediaQuery.of(context).size.height * 0.85,
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
Text(
|
|
|
|
'realmMembers'.tr,
|
|
|
|
style: Theme.of(context).textTheme.headlineSmall,
|
|
|
|
).paddingOnly(left: 24, right: 24, top: 32, bottom: 16),
|
2024-10-14 13:13:57 +00:00
|
|
|
LoadingIndicator(isActive: _isBusy),
|
2024-05-29 16:05:39 +00:00
|
|
|
ListTile(
|
|
|
|
tileColor: Theme.of(context).colorScheme.surfaceContainerHigh,
|
|
|
|
contentPadding: const EdgeInsets.symmetric(horizontal: 20),
|
|
|
|
leading: const Icon(Icons.person_add),
|
|
|
|
title: Text('realmMembersAdd'.tr),
|
|
|
|
subtitle: Text(
|
|
|
|
'realmMembersAddHint'
|
|
|
|
.trParams({'realm': '#${widget.realm.alias}'}),
|
|
|
|
),
|
|
|
|
onTap: () => promptAddMember(),
|
|
|
|
),
|
|
|
|
Expanded(
|
|
|
|
child: ListView.builder(
|
|
|
|
itemCount: _members.length,
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
var element = _members[index];
|
|
|
|
return ListTile(
|
|
|
|
title: Text(element.account.nick),
|
|
|
|
subtitle: Text(element.account.name),
|
2024-06-27 07:57:02 +00:00
|
|
|
leading: GestureDetector(
|
2024-10-11 16:41:03 +00:00
|
|
|
child:
|
|
|
|
AttachedCircleAvatar(content: element.account.avatar),
|
2024-06-27 07:57:02 +00:00
|
|
|
onTap: () {
|
|
|
|
showModalBottomSheet(
|
|
|
|
useRootNavigator: true,
|
|
|
|
isScrollControlled: true,
|
|
|
|
backgroundColor: Theme.of(context).colorScheme.surface,
|
|
|
|
context: context,
|
|
|
|
builder: (context) => AccountProfilePopup(
|
2024-08-03 04:29:13 +00:00
|
|
|
name: element.account.name,
|
2024-06-27 07:57:02 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
2024-05-29 16:05:39 +00:00
|
|
|
trailing: Row(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
|
|
|
const IconButton(
|
|
|
|
color: Colors.teal,
|
|
|
|
icon: Icon(Icons.admin_panel_settings),
|
|
|
|
onPressed: null,
|
|
|
|
),
|
|
|
|
IconButton(
|
|
|
|
color: Colors.red,
|
|
|
|
icon: const Icon(Icons.remove_circle),
|
2024-09-11 15:40:23 +00:00
|
|
|
onPressed: element.account.id == _accountId
|
2024-05-29 16:05:39 +00:00
|
|
|
? null
|
|
|
|
: () => removeMember(element),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|