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

199 lines
5.7 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;
const ChatMemberScreen({super.key, required this.channel});
@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-01 09:37:34 +00:00
var uri = getRequestUri(
'messaging', '/api/channels/global/${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
}
}
Future<void> kickMember(ChannelMember item) async {
setState(() => _isSubmitting = true);
final auth = context.read<AuthProvider>();
if (!await auth.isAuthorized()) {
setState(() => _isSubmitting = false);
return;
}
2024-05-01 09:37:34 +00:00
var uri = getRequestUri(
'messaging', '/api/channels/global/${widget.channel.alias}/kick');
2024-04-26 12:49:21 +00:00
var res = await auth.client!.post(
uri,
headers: {
'Content-Type': 'application/json',
},
body: jsonEncode({
'account_name': item.account.name,
}),
);
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-04-26 13:38:31 +00:00
Future<void> inviteMember(String username) async {
setState(() => _isSubmitting = true);
final auth = context.read<AuthProvider>();
if (!await auth.isAuthorized()) {
setState(() => _isSubmitting = false);
return;
}
2024-05-01 09:37:34 +00:00
var uri = getRequestUri(
'messaging', '/api/channels/global/${widget.channel.alias}/invite');
2024-04-26 13:38:31 +00:00
var res = await auth.client!.post(
uri,
headers: {
'Content-Type': 'application/json',
},
body: jsonEncode({
'account_name': username,
}),
);
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);
}
void promptInviteMember() async {
final input = await showModalBottomSheet(
context: context,
builder: (context) {
return const FriendPicker();
},
);
if (input == null) return;
await inviteMember((input as Account).name);
}
2024-04-26 12:49:21 +00:00
bool getKickable(ChannelMember item) {
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),
onPressed: () => promptInviteMember(),
),
],
2024-04-26 12:49:21 +00:00
child: RefreshIndicator(
onRefresh: () => fetchMemberships(),
child: CustomScrollView(
slivers: [
SliverToBoxAdapter(
2024-05-01 09:37:34 +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-01 09:37:34 +00:00
direction: getKickable(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-01 09:37:34 +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: (_) {
kickMember(element);
},
);
},
)
],
),
),
);
}
}