FurryChat/lib/components/chat_settings_popup_menu.dart

96 lines
2.8 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:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/utils/app_route.dart';
import 'package:fluffychat/views/chat_details.dart';
import 'package:fluffychat/views/chat_list.dart';
import 'package:flutter/material.dart';
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;
const ChatSettingsPopupMenu(this.room, this.displayChatDetails, {Key key})
: super(key: key);
2020-01-02 21:31:39 +00:00
@override
_ChatSettingsPopupMenuState createState() => _ChatSettingsPopupMenuState();
}
class _ChatSettingsPopupMenuState extends State<ChatSettingsPopupMenu> {
StreamSubscription notificationChangeSub;
@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
.onUserEvent
.stream
.where((u) => u.type == 'account_data' && u.eventType == "m.push_rules")
.listen(
(u) => setState(() => null),
);
2020-01-01 18:10:13 +00:00
List<PopupMenuEntry<String>> items = <PopupMenuEntry<String>>[
2020-01-02 21:31:39 +00:00
widget.room.pushRuleState == PushRuleState.notify
2020-01-01 18:10:13 +00:00
? const PopupMenuItem<String>(
value: "mute",
child: Text('Mute chat'),
)
: const PopupMenuItem<String>(
value: "unmute",
child: Text('Unmute chat'),
),
const PopupMenuItem<String>(
value: "leave",
child: Text('Leave'),
),
];
2020-01-02 21:31:39 +00:00
if (widget.displayChatDetails) {
2020-01-01 18:10:13 +00:00
items.insert(
0,
const PopupMenuItem<String>(
value: "details",
child: Text('Chat details'),
),
);
2020-01-02 21:31:39 +00:00
}
2020-01-01 18:10:13 +00:00
return PopupMenuButton(
onSelected: (String choice) async {
switch (choice) {
case "leave":
2020-01-02 21:31:39 +00:00
await Matrix.of(context)
.tryRequestWithLoadingDialog(widget.room.leave());
await Navigator.of(context).pushAndRemoveUntil(
2020-01-01 18:10:13 +00:00
AppRoute.defaultRoute(context, ChatListView()),
(Route r) => false);
break;
case "mute":
await Matrix.of(context).tryRequestWithLoadingDialog(
2020-01-02 21:31:39 +00:00
widget.room.setPushRuleState(PushRuleState.mentions_only));
2020-01-01 18:10:13 +00:00
break;
case "unmute":
await Matrix.of(context).tryRequestWithLoadingDialog(
2020-01-02 21:31:39 +00:00
widget.room.setPushRuleState(PushRuleState.notify));
2020-01-01 18:10:13 +00:00
break;
case "details":
2020-01-02 21:31:39 +00:00
await Navigator.of(context).push(
2020-01-01 18:10:13 +00:00
AppRoute.defaultRoute(
context,
2020-01-02 21:31:39 +00:00
ChatDetails(widget.room),
2020-01-01 18:10:13 +00:00
),
);
break;
}
},
itemBuilder: (BuildContext context) => items,
);
}
}