fluffychat/lib/widgets/chat_settings_popup_menu.dart

176 lines
5.7 KiB
Dart
Raw Normal View History

2020-01-02 21:31:39 +00:00
import 'dart:async';
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:go_router/go_router.dart';
import 'package:keyboard_shortcuts/keyboard_shortcuts.dart';
2021-10-26 16:50:34 +00:00
import 'package:matrix/matrix.dart';
2020-01-01 18:10:13 +00:00
import 'matrix.dart';
2024-04-17 07:26:22 +00:00
enum ChatPopupMenuActions { details, mute, unmute, leave, search }
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;
const ChatSettingsPopupMenu(this.room, this.displayChatDetails, {super.key});
2020-01-01 18:10:13 +00:00
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
.onSync
2020-01-02 21:31:39 +00:00
.stream
.where(
(syncUpdate) =>
syncUpdate.accountData?.any(
(accountData) => accountData.type == 'm.push_rules',
) ??
false,
)
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
);
return Stack(
alignment: Alignment.center,
children: [
KeyBoardShortcuts(
keysToPress: {
LogicalKeyboardKey.controlLeft,
2023-08-18 05:24:31 +00:00
LogicalKeyboardKey.keyI,
},
helpLabel: L10n.of(context)!.chatDetails,
onKeysPressed: _showChatDetails,
child: const SizedBox.shrink(),
),
2024-04-17 07:26:22 +00:00
PopupMenuButton<ChatPopupMenuActions>(
onSelected: (choice) async {
switch (choice) {
2024-04-17 07:26:22 +00:00
case ChatPopupMenuActions.leave:
final confirmed = await showOkCancelAlertDialog(
useRootNavigator: false,
context: context,
title: L10n.of(context)!.areYouSure,
okLabel: L10n.of(context)!.ok,
cancelLabel: L10n.of(context)!.cancel,
message: L10n.of(context)!.archiveRoomDescription,
);
if (confirmed == OkCancelResult.ok) {
final success = await showFutureLoadingDialog(
context: context,
future: () => widget.room.leave(),
);
if (success.error == null) {
2023-08-07 16:40:02 +00:00
context.go('/rooms');
}
}
break;
2024-04-17 07:26:22 +00:00
case ChatPopupMenuActions.mute:
await showFutureLoadingDialog(
context: context,
future: () =>
widget.room.setPushRuleState(PushRuleState.mentionsOnly),
);
break;
2024-04-17 07:26:22 +00:00
case ChatPopupMenuActions.unmute:
await showFutureLoadingDialog(
context: context,
future: () =>
widget.room.setPushRuleState(PushRuleState.notify),
);
break;
2024-04-17 07:26:22 +00:00
case ChatPopupMenuActions.details:
_showChatDetails();
break;
2024-04-17 07:26:22 +00:00
case ChatPopupMenuActions.search:
context.go('/rooms/${widget.room.id}/search');
break;
2020-02-16 10:36:18 +00:00
}
},
2024-04-17 07:26:22 +00:00
itemBuilder: (BuildContext context) => [
if (widget.displayChatDetails)
PopupMenuItem<ChatPopupMenuActions>(
value: ChatPopupMenuActions.details,
child: Row(
children: [
const Icon(Icons.info_outline_rounded),
const SizedBox(width: 12),
Text(L10n.of(context)!.chatDetails),
],
),
),
if (widget.room.pushRuleState == PushRuleState.notify)
PopupMenuItem<ChatPopupMenuActions>(
value: ChatPopupMenuActions.mute,
child: Row(
children: [
const Icon(Icons.notifications_off_outlined),
const SizedBox(width: 12),
Text(L10n.of(context)!.muteChat),
],
),
)
else
PopupMenuItem<ChatPopupMenuActions>(
value: ChatPopupMenuActions.unmute,
child: Row(
children: [
const Icon(Icons.notifications_on_outlined),
const SizedBox(width: 12),
Text(L10n.of(context)!.unmuteChat),
],
),
),
PopupMenuItem<ChatPopupMenuActions>(
value: ChatPopupMenuActions.search,
child: Row(
children: [
const Icon(Icons.search_outlined),
const SizedBox(width: 12),
Text(L10n.of(context)!.search),
],
),
),
PopupMenuItem<ChatPopupMenuActions>(
value: ChatPopupMenuActions.leave,
child: Row(
children: [
const Icon(Icons.delete_outlined),
const SizedBox(width: 12),
Text(L10n.of(context)!.leave),
],
),
),
],
),
],
2020-01-01 18:10:13 +00:00
);
}
void _showChatDetails() {
2023-08-07 16:40:02 +00:00
if (GoRouterState.of(context).uri.path.endsWith('/details')) {
context.go('/rooms/${widget.room.id}');
} else {
context.go('/rooms/${widget.room.id}/details');
}
}
2020-01-01 18:10:13 +00:00
}