FurryChat/lib/components/list_items/participant_list_item.dart

125 lines
4 KiB
Dart
Raw Normal View History

2020-01-01 18:10:13 +00:00
import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/utils/app_route.dart';
import 'package:fluffychat/views/chat.dart';
import 'package:flutter/material.dart';
import '../avatar.dart';
import '../matrix.dart';
class ParticipantListItem extends StatelessWidget {
final User user;
const ParticipantListItem(this.user);
participantAction(BuildContext context, String action) async {
final MatrixState matrix = Matrix.of(context);
switch (action) {
case "ban":
2020-01-02 21:31:39 +00:00
await matrix.tryRequestWithLoadingDialog(user.ban());
2020-01-01 18:10:13 +00:00
break;
case "unban":
2020-01-02 21:31:39 +00:00
await matrix.tryRequestWithLoadingDialog(user.unban());
2020-01-01 18:10:13 +00:00
break;
case "kick":
2020-01-02 21:31:39 +00:00
await matrix.tryRequestWithLoadingDialog(user.kick());
2020-01-01 18:10:13 +00:00
break;
case "admin":
2020-01-02 21:31:39 +00:00
await matrix.tryRequestWithLoadingDialog(user.setPower(100));
2020-01-01 18:10:13 +00:00
break;
case "user":
2020-01-02 21:31:39 +00:00
await matrix.tryRequestWithLoadingDialog(user.setPower(100));
2020-01-01 18:10:13 +00:00
break;
case "message":
final String roomId = await user.startDirectChat();
2020-01-02 21:31:39 +00:00
await Navigator.of(context).pushAndRemoveUntil(
2020-01-01 18:10:13 +00:00
AppRoute.defaultRoute(
context,
Chat(roomId),
),
(Route r) => r.isFirst);
break;
}
}
@override
Widget build(BuildContext context) {
const Map<Membership, String> membershipBatch = {
Membership.join: "",
Membership.ban: "Banned",
Membership.invite: "Invited",
Membership.leave: "Left",
};
final String permissionBatch = user.powerLevel == 100
? "Admin"
: user.powerLevel >= 50 ? "Moderator" : "";
List<PopupMenuEntry<String>> items = <PopupMenuEntry<String>>[];
if (user.canChangePowerLevel &&
user.room.ownPowerLevel == 100 &&
2020-01-02 21:31:39 +00:00
user.powerLevel != 100) {
2020-01-01 18:10:13 +00:00
items.add(
PopupMenuItem(child: Text("Make an admin"), value: "admin"),
);
2020-01-02 21:31:39 +00:00
}
if (user.canChangePowerLevel && user.powerLevel != 0) {
2020-01-01 18:10:13 +00:00
items.add(
PopupMenuItem(child: Text("Revoke all permissions"), value: "user"),
);
2020-01-02 21:31:39 +00:00
}
if (user.canKick) {
2020-01-01 18:10:13 +00:00
items.add(
PopupMenuItem(child: Text("Kick from group"), value: "kick"),
);
2020-01-02 21:31:39 +00:00
}
if (user.canBan && user.membership != Membership.ban) {
2020-01-01 18:10:13 +00:00
items.add(
PopupMenuItem(child: Text("Ban from group"), value: "ban"),
);
2020-01-02 21:31:39 +00:00
} else if (user.canBan && user.membership == Membership.ban) {
2020-01-01 18:10:13 +00:00
items.add(
PopupMenuItem(child: Text("Remove exile"), value: "unban"),
);
2020-01-02 21:31:39 +00:00
}
if (user.id != Matrix.of(context).client.userID) {
2020-01-01 18:10:13 +00:00
items.add(
PopupMenuItem(child: Text("Send a message"), value: "message"),
);
2020-01-02 21:31:39 +00:00
}
2020-01-01 18:10:13 +00:00
return PopupMenuButton(
onSelected: (action) => participantAction(context, action),
itemBuilder: (c) => items,
child: ListTile(
title: Row(
children: <Widget>[
Text(user.calcDisplayname()),
permissionBatch.isEmpty
? Container()
: Container(
padding: EdgeInsets.all(4),
margin: EdgeInsets.symmetric(horizontal: 8),
decoration: BoxDecoration(
2020-01-03 10:57:00 +00:00
color: Theme.of(context).secondaryHeaderColor,
2020-01-01 18:10:13 +00:00
borderRadius: BorderRadius.circular(8),
),
child: Center(child: Text(permissionBatch)),
),
membershipBatch[user.membership].isEmpty
? Container()
: Container(
padding: EdgeInsets.all(4),
margin: EdgeInsets.symmetric(horizontal: 8),
decoration: BoxDecoration(
2020-01-03 10:57:00 +00:00
color: Theme.of(context).secondaryHeaderColor,
2020-01-01 18:10:13 +00:00
borderRadius: BorderRadius.circular(8),
),
child:
Center(child: Text(membershipBatch[user.membership])),
),
],
),
subtitle: Text(user.id),
leading: Avatar(user.avatarUrl),
),
);
}
}