fluffychat/lib/components/user_bottom_sheet.dart

288 lines
8.8 KiB
Dart
Raw Normal View History

2020-10-28 07:23:50 +01:00
import 'dart:math';
2020-11-14 10:08:13 +01:00
import 'package:adaptive_dialog/adaptive_dialog.dart';
2020-10-28 07:23:50 +01:00
import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/components/adaptive_page_layout.dart';
import 'package:fluffychat/utils/app_route.dart';
import 'package:fluffychat/utils/fluffy_share.dart';
import 'package:fluffychat/views/chat.dart';
import 'package:flutter/material.dart';
import 'content_banner.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
import '../utils/presence_extension.dart';
import 'dialogs/simple_dialogs.dart';
import 'matrix.dart';
2020-10-31 10:39:38 +01:00
import '../views/key_verification.dart';
import '../utils/app_route.dart';
2020-10-28 07:23:50 +01:00
class UserBottomSheet extends StatelessWidget {
final User user;
final Function onMention;
const UserBottomSheet({Key key, @required this.user, this.onMention})
: super(key: key);
void participantAction(BuildContext context, String action) async {
2020-11-14 10:08:13 +01:00
final Function _askConfirmation = () async =>
(await showOkCancelAlertDialog(
context: context, title: L10n.of(context).areYouSure) ==
OkCancelResult.ok);
2020-10-28 07:23:50 +01:00
switch (action) {
case 'mention':
Navigator.of(context).pop();
onMention();
break;
case 'ban':
2020-11-14 10:08:13 +01:00
if (await _askConfirmation()) {
2020-10-28 07:23:50 +01:00
await SimpleDialogs(context).tryRequestWithLoadingDialog(user.ban());
Navigator.of(context).pop();
2020-10-28 07:23:50 +01:00
}
break;
case 'unban':
2020-11-14 10:08:13 +01:00
if (await _askConfirmation()) {
2020-10-28 07:23:50 +01:00
await SimpleDialogs(context)
.tryRequestWithLoadingDialog(user.unban());
Navigator.of(context).pop();
2020-10-28 07:23:50 +01:00
}
break;
case 'kick':
2020-11-14 10:08:13 +01:00
if (await _askConfirmation()) {
2020-10-28 07:23:50 +01:00
await SimpleDialogs(context).tryRequestWithLoadingDialog(user.kick());
Navigator.of(context).pop();
2020-10-28 07:23:50 +01:00
}
break;
case 'admin':
2020-11-14 10:08:13 +01:00
if (await _askConfirmation()) {
2020-10-28 07:23:50 +01:00
await SimpleDialogs(context)
.tryRequestWithLoadingDialog(user.setPower(100));
Navigator.of(context).pop();
2020-10-28 07:23:50 +01:00
}
break;
case 'moderator':
2020-11-14 10:08:13 +01:00
if (await _askConfirmation()) {
2020-10-28 07:23:50 +01:00
await SimpleDialogs(context)
.tryRequestWithLoadingDialog(user.setPower(50));
Navigator.of(context).pop();
2020-10-28 07:23:50 +01:00
}
break;
case 'user':
2020-11-14 10:08:13 +01:00
if (await _askConfirmation()) {
2020-10-28 07:23:50 +01:00
await SimpleDialogs(context)
.tryRequestWithLoadingDialog(user.setPower(0));
Navigator.of(context).pop();
2020-10-28 07:23:50 +01:00
}
break;
case 'message':
final roomId = await user.startDirectChat();
await Navigator.of(context).pushAndRemoveUntil(
AppRoute.defaultRoute(
context,
ChatView(roomId),
),
(Route r) => r.isFirst);
break;
}
}
2020-10-31 10:39:38 +01:00
void _verifyAction(BuildContext context) async {
final client = Matrix.of(context).client;
final req = await client.userDeviceKeys[user.id].startVerification();
await Navigator.of(context).push(
AppRoute.defaultRoute(
context,
KeyVerificationView(request: req),
),
);
}
2020-10-28 07:23:50 +01:00
@override
Widget build(BuildContext context) {
2020-10-31 10:39:38 +01:00
final client = Matrix.of(context).client;
final presence = client.presences[user.id];
final verificationStatus = client.userDeviceKeys[user.id]?.verified;
2020-10-28 07:23:50 +01:00
var items = <PopupMenuEntry<String>>[];
if (onMention != null) {
items.add(
PopupMenuItem(
child: _TextWithIcon(
L10n.of(context).mention,
Icons.alternate_email_outlined,
),
value: 'mention'),
2020-10-28 07:23:50 +01:00
);
}
if (user.id != Matrix.of(context).client.userID &&
!user.room.isDirectChat) {
2020-10-28 07:23:50 +01:00
items.add(
PopupMenuItem(
child: _TextWithIcon(
L10n.of(context).sendAMessage,
Icons.send,
),
value: 'message'),
2020-10-28 07:23:50 +01:00
);
}
if (user.canChangePowerLevel &&
user.room.ownPowerLevel == 100 &&
user.powerLevel != 100) {
items.add(
PopupMenuItem(
child: _TextWithIcon(
L10n.of(context).makeAnAdmin,
Icons.arrow_upward,
),
value: 'admin'),
2020-10-28 07:23:50 +01:00
);
}
if (user.canChangePowerLevel &&
user.room.ownPowerLevel >= 50 &&
user.powerLevel != 50) {
items.add(
PopupMenuItem(
child: _TextWithIcon(
L10n.of(context).makeAModerator,
Icons.arrow_upward_outlined,
),
value: 'moderator'),
2020-10-28 07:23:50 +01:00
);
}
if (user.canChangePowerLevel && user.powerLevel != 0) {
items.add(
PopupMenuItem(
child: _TextWithIcon(
L10n.of(context).revokeAllPermissions,
Icons.arrow_downward_outlined,
),
value: 'user'),
2020-10-28 07:23:50 +01:00
);
}
if (user.canKick) {
items.add(
PopupMenuItem(
child: _TextWithIcon(
L10n.of(context).kickFromChat,
Icons.exit_to_app_outlined,
),
value: 'kick'),
2020-10-28 07:23:50 +01:00
);
}
if (user.canBan && user.membership != Membership.ban) {
items.add(
PopupMenuItem(
child: _TextWithIcon(
L10n.of(context).banFromChat,
Icons.warning_sharp,
),
value: 'ban'),
2020-10-28 07:23:50 +01:00
);
} else if (user.room.canBan &&
user.powerLevel < user.room.ownPowerLevel &&
user.membership == Membership.ban) {
2020-10-28 07:23:50 +01:00
items.add(
PopupMenuItem(
child: _TextWithIcon(
L10n.of(context).removeExile, Icons.warning_outlined),
value: 'unban'),
2020-10-28 07:23:50 +01:00
);
}
return Center(
child: Container(
width: min(MediaQuery.of(context).size.width,
AdaptivePageLayout.defaultMinWidth * 1.5),
child: SafeArea(
child: Material(
elevation: 4,
child: Scaffold(
extendBodyBehindAppBar: true,
appBar: AppBar(
elevation: 0,
backgroundColor:
Theme.of(context).scaffoldBackgroundColor.withOpacity(0.5),
leading: IconButton(
icon: Icon(Icons.arrow_downward_outlined),
onPressed: Navigator.of(context).pop,
),
title: Text(user.calcDisplayname()),
actions: [
2020-10-31 10:39:38 +01:00
if (verificationStatus != null)
2020-11-22 11:46:31 +01:00
IconButton(
icon: Icon(
2020-10-31 10:39:38 +01:00
Icons.lock,
color: {
UserVerifiedStatus.unknownDevice: Colors.red,
UserVerifiedStatus.verified: Colors.green,
}[verificationStatus] ??
Colors.orange,
),
2020-11-22 11:46:31 +01:00
onPressed: () =>
2020-10-31 10:39:38 +01:00
verificationStatus == UserVerifiedStatus.unknown
? _verifyAction(context)
: null,
),
2020-10-28 07:23:50 +01:00
if (user.id != Matrix.of(context).client.userID)
PopupMenuButton(
itemBuilder: (_) => items,
onSelected: (action) =>
participantAction(context, action),
),
],
),
2020-10-28 09:59:18 +01:00
body: Column(
2020-10-28 07:23:50 +01:00
children: [
2020-10-28 09:59:18 +01:00
Expanded(
child: ContentBanner(
user.avatarUrl,
defaultIcon: Icons.person_outline,
),
2020-10-28 07:23:50 +01:00
),
ListTile(
title: Text(L10n.of(context).username),
subtitle: Text(user.id),
trailing: Icon(Icons.share),
onTap: () => FluffyShare.share(user.id, context),
),
if (presence != null)
ListTile(
title: Text(presence.getLocalizedStatusMessage(context)),
subtitle:
Text(presence.getLocalizedLastActiveAgo(context)),
trailing: Icon(Icons.circle,
2020-10-28 18:13:04 +01:00
color: presence.presence.currentlyActive ?? false
2020-10-28 07:23:50 +01:00
? Colors.green
: Colors.grey),
),
],
),
),
),
),
),
);
}
}
class _TextWithIcon extends StatelessWidget {
final String text;
final IconData iconData;
const _TextWithIcon(
this.text,
this.iconData, {
Key key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(iconData),
SizedBox(width: 16),
Text(text),
],
);
}
}