fluffychat/lib/pages/chat_permissions_settings.dart

102 lines
3.5 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-10-26 18:50:34 +02:00
import 'package:fluffychat/pages/permission_slider_dialog.dart';
import 'package:fluffychat/pages/views/chat_permissions_settings_view.dart';
import 'package:fluffychat/widgets/matrix.dart';
2021-04-15 08:48:26 +02:00
class ChatPermissionsSettings extends StatefulWidget {
2021-05-23 13:11:55 +02: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> {
2021-05-23 13:11:55 +02: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,
{String category}) async {
2021-05-23 13:11:55 +02:00
final room = Matrix.of(context).client.getRoomById(roomId);
2021-04-15 08:48:26 +02:00
if (!room.canSendEvent(EventTypes.RoomPowerLevels)) {
2021-05-23 13:11:55 +02:00
ScaffoldMessenger.of(context)
2021-04-15 08:48:26 +02:00
.showSnackBar(SnackBar(content: Text(L10n.of(context).noPermission)));
return;
}
final newLevel =
await PermissionSliderDialog(initialPermission: currentLevel)
.show(context);
if (newLevel == null) return;
final content = Map<String, dynamic>.from(
room.getState(EventTypes.RoomPowerLevels).content);
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) =>
2021-05-23 13:11:55 +02: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 {
2021-05-23 13:11:55 +02:00
final room = Matrix.of(context).client.getRoomById(roomId);
2021-04-15 08:48:26 +02:00
final String roomVersion =
room.getState(EventTypes.RoomCreate).content['room_version'] ?? '1';
final newVersion = await showConfirmationDialog<String>(
context: context,
title: L10n.of(context).replaceRoomWithNewerVersion,
actions: capabilities.mRoomVersions.available.entries
.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,
okLabel: L10n.of(context).yes,
cancelLabel: L10n.of(context).cancel,
title: L10n.of(context).areYouSure,
)) {
return;
}
await showFutureLoadingDialog(
context: context,
2021-05-23 13:11:55 +02:00
future: () => room.client.upgradeRoom(roomId, newVersion),
).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
}