fluffychat/lib/widgets/chat_settings_popup_menu.dart

202 lines
6.3 KiB
Dart
Raw Normal View History

2020-01-02 21:31:39 +00:00
import 'dart:async';
2022-02-17 11:52:58 +00:00
import 'package:flutter/cupertino.dart';
2020-01-01 18:10:13 +00:00
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
2020-01-01 18:10:13 +00:00
2021-10-26 16:50:34 +00:00
import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
2020-12-25 08:58:34 +00:00
import 'package:future_loading_dialog/future_loading_dialog.dart';
import 'package:keyboard_shortcuts/keyboard_shortcuts.dart';
2021-10-26 16:50:34 +00:00
import 'package:matrix/matrix.dart';
2021-05-23 11:11:55 +00:00
import 'package:vrouter/vrouter.dart';
2021-10-26 16:50:34 +00:00
2022-02-17 11:52:58 +00:00
import 'package:fluffychat/pages/chat/cupertino_widgets_bottom_sheet.dart';
import 'package:fluffychat/pages/chat/edit_widgets_dialog.dart';
2022-02-17 11:52:58 +00:00
import 'package:fluffychat/pages/chat/widgets_bottom_sheet.dart';
2023-01-07 09:29:34 +00:00
import 'package:fluffychat/utils/adaptive_bottom_sheet.dart';
2022-09-10 10:11:11 +00:00
import 'm2_popup_menu_button.dart';
2020-01-01 18:10:13 +00:00
import 'matrix.dart';
2020-01-02 21:31:39 +00:00
class ChatSettingsPopupMenu extends StatefulWidget {
2020-01-01 18:10:13 +00:00
final Room room;
final bool displayChatDetails;
2021-11-19 19:38:16 +00:00
const ChatSettingsPopupMenu(this.room, this.displayChatDetails, {Key? key})
2020-01-01 18:10:13 +00:00
: super(key: key);
2020-01-02 21:31:39 +00:00
@override
2022-08-14 14:59:21 +00:00
ChatSettingsPopupMenuState createState() => ChatSettingsPopupMenuState();
2020-01-02 21:31:39 +00:00
}
2022-08-14 14:59:21 +00:00
class ChatSettingsPopupMenuState extends State<ChatSettingsPopupMenu> {
2021-11-19 19:38:16 +00:00
StreamSubscription? notificationChangeSub;
2020-01-02 21:31:39 +00:00
@override
void dispose() {
notificationChangeSub?.cancel();
super.dispose();
}
2020-01-01 18:10:13 +00:00
@override
Widget build(BuildContext context) {
2020-01-02 21:31:39 +00:00
notificationChangeSub ??= Matrix.of(context)
.client
2020-06-10 08:07:01 +00:00
.onAccountData
2020-01-02 21:31:39 +00:00
.stream
2020-06-10 08:07:01 +00:00
.where((u) => u.type == 'm.push_rules')
2020-01-02 21:31:39 +00:00
.listen(
2021-11-19 19:38:16 +00:00
(u) => setState(() {}),
2020-01-02 21:31:39 +00:00
);
2021-04-14 08:37:15 +00:00
final items = <PopupMenuEntry<String>>[
PopupMenuItem<String>(
value: 'widgets',
child: Row(
children: [
const Icon(Icons.widgets_outlined),
const SizedBox(width: 12),
Text(L10n.of(context)!.matrixWidgets),
],
2022-02-17 11:52:58 +00:00
),
),
2020-01-02 21:31:39 +00:00
widget.room.pushRuleState == PushRuleState.notify
2020-01-20 12:46:39 +00:00
? PopupMenuItem<String>(
2020-05-13 13:58:59 +00:00
value: 'mute',
2021-08-29 14:52:42 +00:00
child: Row(
children: [
2021-10-14 16:09:30 +00:00
const Icon(Icons.notifications_off_outlined),
const SizedBox(width: 12),
2021-11-19 19:38:16 +00:00
Text(L10n.of(context)!.muteChat),
2021-08-29 14:52:42 +00:00
],
),
2020-01-01 18:10:13 +00:00
)
2020-01-20 12:46:39 +00:00
: PopupMenuItem<String>(
2020-05-13 13:58:59 +00:00
value: 'unmute',
2021-08-29 14:52:42 +00:00
child: Row(
children: [
2021-10-14 16:09:30 +00:00
const Icon(Icons.notifications_on_outlined),
const SizedBox(width: 12),
2021-11-19 19:38:16 +00:00
Text(L10n.of(context)!.unmuteChat),
2021-08-29 14:52:42 +00:00
],
),
2020-01-01 18:10:13 +00:00
),
2020-01-20 12:46:39 +00:00
PopupMenuItem<String>(
2020-05-13 13:58:59 +00:00
value: 'leave',
2021-08-29 14:52:42 +00:00
child: Row(
children: [
2021-10-14 16:09:30 +00:00
const Icon(Icons.delete_outlined),
const SizedBox(width: 12),
2021-11-19 19:38:16 +00:00
Text(L10n.of(context)!.leave),
2021-08-29 14:52:42 +00:00
],
),
2020-01-01 18:10:13 +00:00
),
];
2020-01-02 21:31:39 +00:00
if (widget.displayChatDetails) {
2020-01-01 18:10:13 +00:00
items.insert(
0,
2020-01-20 12:46:39 +00:00
PopupMenuItem<String>(
2020-05-13 13:58:59 +00:00
value: 'details',
2021-08-29 14:52:42 +00:00
child: Row(
children: [
2021-10-14 16:09:30 +00:00
const Icon(Icons.info_outline_rounded),
const SizedBox(width: 12),
2021-11-19 19:38:16 +00:00
Text(L10n.of(context)!.chatDetails),
2021-08-29 14:52:42 +00:00
],
),
2020-01-01 18:10:13 +00:00
),
);
2020-01-02 21:31:39 +00:00
}
return Stack(
alignment: Alignment.center,
children: [
KeyBoardShortcuts(
keysToPress: {
LogicalKeyboardKey.controlLeft,
LogicalKeyboardKey.keyI
},
helpLabel: L10n.of(context)!.chatDetails,
onKeysPressed: _showChatDetails,
2022-08-14 14:59:21 +00:00
child: Container(),
),
KeyBoardShortcuts(
keysToPress: {
LogicalKeyboardKey.controlLeft,
LogicalKeyboardKey.keyW
},
helpLabel: L10n.of(context)!.matrixWidgets,
onKeysPressed: _showWidgets,
2022-08-14 14:59:21 +00:00
child: Container(),
),
2022-09-10 10:11:11 +00:00
M2PopupMenuButton(
onSelected: (String choice) async {
switch (choice) {
case 'widgets':
if (widget.room.widgets.isNotEmpty) {
_showWidgets();
} else {
showDialog(
context: context,
builder: (context) => EditWidgetsDialog(room: widget.room),
2022-04-03 10:59:08 +00:00
useRootNavigator: false,
);
}
break;
case 'leave':
final confirmed = await showOkCancelAlertDialog(
useRootNavigator: false,
context: context,
title: L10n.of(context)!.areYouSure,
okLabel: L10n.of(context)!.ok,
cancelLabel: L10n.of(context)!.cancel,
);
if (confirmed == OkCancelResult.ok) {
final success = await showFutureLoadingDialog(
context: context, future: () => widget.room.leave());
if (success.error == null) {
VRouter.of(context).to('/rooms');
}
}
break;
case 'mute':
await showFutureLoadingDialog(
2022-02-17 11:52:58 +00:00
context: context,
future: () => widget.room
.setPushRuleState(PushRuleState.mentionsOnly));
break;
case 'unmute':
await showFutureLoadingDialog(
2022-02-17 11:52:58 +00:00
context: context,
future: () =>
widget.room.setPushRuleState(PushRuleState.notify));
break;
case 'details':
_showChatDetails();
break;
2020-02-16 10:36:18 +00:00
}
},
itemBuilder: (BuildContext context) => items,
),
],
2020-01-01 18:10:13 +00:00
);
}
void _showWidgets() => [TargetPlatform.iOS, TargetPlatform.macOS]
.contains(Theme.of(context).platform)
? showCupertinoModalPopup(
context: context,
builder: (context) => CupertinoWidgetsBottomSheet(room: widget.room),
)
2023-01-07 09:29:34 +00:00
: showAdaptiveBottomSheet(
context: context,
builder: (context) => WidgetsBottomSheet(room: widget.room),
);
void _showChatDetails() {
if (VRouter.of(context).path.endsWith('/details')) {
VRouter.of(context).toSegments(['rooms', widget.room.id]);
} else {
VRouter.of(context).toSegments(['rooms', widget.room.id, 'details']);
}
}
2020-01-01 18:10:13 +00:00
}