fluffychat/lib/pages/chat_permissions_settings/chat_permissions_settings.dart

102 lines
3.6 KiB
Dart
Raw Normal View History

2021-04-15 08:48:26 +02:00
import 'dart:developer';
import 'package:flutter/material.dart';
2021-10-26 18:50:34 +02:00
import 'package:adaptive_dialog/adaptive_dialog.dart';
2021-04-15 08:48:26 +02:00
import 'package:flutter_gen/gen_l10n/l10n.dart';
2021-10-26 18:50:34 +02:00
import 'package:future_loading_dialog/future_loading_dialog.dart';
import 'package:matrix/matrix.dart';
2021-05-23 13:11:55 +02:00
import 'package:vrouter/vrouter.dart';
2021-04-15 08:48:26 +02:00
2021-11-09 21:32:16 +01:00
import 'package:fluffychat/pages/chat_permissions_settings/chat_permissions_settings_view.dart';
2021-10-26 18:50:34 +02:00
import 'package:fluffychat/widgets/matrix.dart';
2021-11-09 21:32:16 +01:00
import 'package:fluffychat/widgets/permission_slider_dialog.dart';
2021-10-26 18:50:34 +02:00
2021-04-15 08:48:26 +02:00
class ChatPermissionsSettings extends StatefulWidget {
2022-01-29 12:35:03 +01:00
const ChatPermissionsSettings({Key? key}) : super(key: key);
2021-04-15 08:48:26 +02:00
@override
ChatPermissionsSettingsController createState() =>
ChatPermissionsSettingsController();
}
class ChatPermissionsSettingsController extends State<ChatPermissionsSettings> {
2022-01-29 12:35:03 +01:00
String? get roomId => VRouter.of(context).pathParameters['roomid'];
2021-04-15 08:48:26 +02:00
void editPowerLevel(BuildContext context, String key, int currentLevel,
2022-01-29 12:35:03 +01:00
{String? category}) async {
final room = Matrix.of(context).client.getRoomById(roomId!)!;
2021-04-15 08:48:26 +02:00
if (!room.canSendEvent(EventTypes.RoomPowerLevels)) {
2022-01-29 12:35:03 +01:00
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(L10n.of(context)!.noPermission)));
2021-04-15 08:48:26 +02:00
return;
}
final newLevel =
await PermissionSliderDialog(initialPermission: currentLevel)
.show(context);
if (newLevel == null) return;
final content = Map<String, dynamic>.from(
2022-01-29 12:35:03 +01:00
room.getState(EventTypes.RoomPowerLevels)!.content);
2021-04-15 08:48:26 +02:00
if (category != null) {
if (!content.containsKey(category)) {
content[category] = <String, dynamic>{};
}
content[category][key] = newLevel;
} else {
content[key] = newLevel;
}
inspect(content);
await showFutureLoadingDialog(
context: context,
future: () => room.client.setRoomStateWithKey(
room.id,
EventTypes.RoomPowerLevels,
'',
content,
),
2021-04-15 08:48:26 +02:00
);
}
Stream get onChanged => Matrix.of(context).client.onSync.stream.where(
(e) =>
2022-01-29 12:35:03 +01:00
(e.rooms?.join?.containsKey(roomId) ?? false) &&
(e.rooms!.join![roomId!]?.timeline?.events
2021-04-15 08:48:26 +02:00
?.any((s) => s.type == EventTypes.RoomPowerLevels) ??
false),
);
void updateRoomAction(Capabilities capabilities) async {
2022-01-29 12:35:03 +01:00
final room = Matrix.of(context).client.getRoomById(roomId!)!;
2021-04-15 08:48:26 +02:00
final String roomVersion =
2022-01-29 12:35:03 +01:00
room.getState(EventTypes.RoomCreate)!.content['room_version'] ?? '1';
2021-04-15 08:48:26 +02:00
final newVersion = await showConfirmationDialog<String>(
context: context,
2022-01-29 12:35:03 +01:00
title: L10n.of(context)!.replaceRoomWithNewerVersion,
actions: capabilities.mRoomVersions!.available.entries
2021-04-15 08:48:26 +02:00
.where((r) => r.key != roomVersion)
.map((version) => AlertDialogAction(
key: version.key,
label:
'${version.key} (${version.value.toString().split('.').last})'))
.toList(),
);
if (newVersion == null ||
OkCancelResult.cancel ==
await showOkCancelAlertDialog(
2021-05-23 15:02:36 +02:00
useRootNavigator: false,
2021-04-15 08:48:26 +02:00
context: context,
2022-01-29 12:35:03 +01:00
okLabel: L10n.of(context)!.yes,
cancelLabel: L10n.of(context)!.cancel,
title: L10n.of(context)!.areYouSure,
2021-04-15 08:48:26 +02:00
)) {
return;
}
await showFutureLoadingDialog(
context: context,
2022-01-29 12:35:03 +01:00
future: () => room.client.upgradeRoom(roomId!, newVersion),
2021-05-23 13:11:55 +02:00
).then((_) => VRouter.of(context).pop());
2021-04-15 08:48:26 +02:00
}
@override
2021-05-22 09:13:47 +02:00
Widget build(BuildContext context) => ChatPermissionsSettingsView(this);
2021-04-15 08:48:26 +02:00
}