Solian/lib/screens/chat/channel/channel_member.dart

192 lines
5.6 KiB
Dart
Raw Normal View History

2024-04-26 12:49:21 +00:00
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter_animate/flutter_animate.dart';
import 'package:provider/provider.dart';
2024-04-26 13:38:31 +00:00
import 'package:solian/models/account.dart';
2024-04-26 12:49:21 +00:00
import 'package:solian/models/channel.dart';
import 'package:solian/providers/auth.dart';
import 'package:solian/utils/service_url.dart';
import 'package:solian/widgets/account/account_avatar.dart';
2024-04-26 13:38:31 +00:00
import 'package:solian/widgets/account/friend_picker.dart';
2024-04-29 12:22:06 +00:00
import 'package:solian/widgets/exts.dart';
import 'package:solian/widgets/scaffold.dart';
2024-04-26 12:49:21 +00:00
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
class ChatMemberScreen extends StatefulWidget {
final Channel channel;
2024-05-05 15:01:08 +00:00
final String realm;
2024-04-26 12:49:21 +00:00
2024-05-05 15:01:08 +00:00
const ChatMemberScreen({super.key, required this.channel, this.realm = 'global'});
2024-04-26 12:49:21 +00:00
@override
State<ChatMemberScreen> createState() => _ChatMemberScreenState();
}
class _ChatMemberScreenState extends State<ChatMemberScreen> {
bool _isSubmitting = false;
List<ChannelMember> _members = List.empty();
int _selfId = 0;
Future<void> fetchMemberships() async {
final auth = context.read<AuthProvider>();
final prof = await auth.getProfiles();
if (!await auth.isAuthorized()) return;
_selfId = prof['id'];
2024-05-05 15:01:08 +00:00
var uri = getRequestUri('messaging', '/api/channels/${widget.realm}/${widget.channel.alias}/members');
2024-04-26 12:49:21 +00:00
var res = await auth.client!.get(uri);
if (res.statusCode == 200) {
final result = jsonDecode(utf8.decode(res.bodyBytes)) as List<dynamic>;
setState(() {
_members = result.map((x) => ChannelMember.fromJson(x)).toList();
});
} else {
var message = utf8.decode(res.bodyBytes);
2024-04-29 12:22:06 +00:00
context.showErrorDialog(message);
2024-04-26 12:49:21 +00:00
}
}
2024-05-05 15:01:08 +00:00
Future<void> removeMember(ChannelMember item) async {
2024-04-26 12:49:21 +00:00
setState(() => _isSubmitting = true);
final auth = context.read<AuthProvider>();
if (!await auth.isAuthorized()) {
setState(() => _isSubmitting = false);
return;
}
2024-05-05 15:51:46 +00:00
var uri = getRequestUri('messaging', '/api/channels/${widget.realm}/${widget.channel.alias}/members');
2024-04-26 12:49:21 +00:00
2024-05-05 15:01:08 +00:00
var res = await auth.client!.delete(
2024-04-26 12:49:21 +00:00
uri,
headers: {
'Content-Type': 'application/json',
},
body: jsonEncode({
2024-05-05 15:51:46 +00:00
'target': item.account.name,
2024-04-26 12:49:21 +00:00
}),
);
if (res.statusCode == 200) {
await fetchMemberships();
} else {
var message = utf8.decode(res.bodyBytes);
2024-04-29 12:22:06 +00:00
context.showErrorDialog(message);
2024-04-26 12:49:21 +00:00
}
setState(() => _isSubmitting = false);
}
2024-05-05 15:01:08 +00:00
Future<void> addMember(String username) async {
2024-04-26 13:38:31 +00:00
setState(() => _isSubmitting = true);
final auth = context.read<AuthProvider>();
if (!await auth.isAuthorized()) {
setState(() => _isSubmitting = false);
return;
}
2024-05-05 15:51:46 +00:00
var uri = getRequestUri('messaging', '/api/channels/${widget.realm}/${widget.channel.alias}/members');
2024-04-26 13:38:31 +00:00
var res = await auth.client!.post(
uri,
headers: {
'Content-Type': 'application/json',
},
body: jsonEncode({
2024-05-05 15:51:46 +00:00
'target': username,
2024-04-26 13:38:31 +00:00
}),
);
if (res.statusCode == 200) {
await fetchMemberships();
} else {
var message = utf8.decode(res.bodyBytes);
2024-04-29 12:22:06 +00:00
context.showErrorDialog(message);
2024-04-26 13:38:31 +00:00
}
setState(() => _isSubmitting = false);
}
2024-05-05 15:51:46 +00:00
void promptAddMember() async {
2024-04-26 13:38:31 +00:00
final input = await showModalBottomSheet(
context: context,
builder: (context) {
return const FriendPicker();
},
);
if (input == null) return;
2024-05-05 15:01:08 +00:00
await addMember((input as Account).name);
2024-04-26 13:38:31 +00:00
}
2024-05-05 15:01:08 +00:00
bool getRemovable(ChannelMember item) {
2024-04-26 12:49:21 +00:00
if (_selfId != widget.channel.account.externalId) return false;
if (item.accountId == widget.channel.accountId) return false;
if (item.account.externalId == _selfId) return false;
return true;
}
@override
void initState() {
super.initState();
Future.delayed(Duration.zero, () => fetchMemberships());
}
@override
Widget build(BuildContext context) {
return IndentScaffold(
2024-04-26 12:49:21 +00:00
title: AppLocalizations.of(context)!.chatMember,
noSafeArea: true,
hideDrawer: true,
2024-04-26 13:38:31 +00:00
appBarActions: [
IconButton(
icon: const Icon(Icons.add),
2024-05-05 15:51:46 +00:00
onPressed: () => promptAddMember(),
2024-04-26 13:38:31 +00:00
),
],
2024-04-26 12:49:21 +00:00
child: RefreshIndicator(
onRefresh: () => fetchMemberships(),
child: CustomScrollView(
slivers: [
SliverToBoxAdapter(
2024-05-05 15:01:08 +00:00
child: _isSubmitting ? const LinearProgressIndicator().animate().scaleX() : Container(),
2024-04-26 12:49:21 +00:00
),
SliverList.builder(
itemCount: _members.length,
itemBuilder: (context, index) {
final element = _members[index];
final randomId = DateTime.now().microsecondsSinceEpoch >> 10;
return Dismissible(
key: Key(randomId.toString()),
2024-05-05 15:01:08 +00:00
direction: getRemovable(element) ? DismissDirection.startToEnd : DismissDirection.none,
2024-04-26 12:49:21 +00:00
background: Container(
color: Colors.red,
padding: const EdgeInsets.symmetric(horizontal: 20),
alignment: Alignment.centerLeft,
child: const Icon(Icons.remove, color: Colors.white),
),
child: ListTile(
2024-05-05 15:01:08 +00:00
leading: AccountAvatar(source: element.account.avatar, direct: true),
2024-04-26 12:49:21 +00:00
title: Text(element.account.nick),
subtitle: Text(element.account.name),
),
onDismissed: (_) {
2024-05-05 15:01:08 +00:00
removeMember(element);
2024-04-26 12:49:21 +00:00
},
);
},
)
],
),
),
);
}
}