11import 'package:flutter/material.dart' ;
2+ import 'package:future_loading_dialog/future_loading_dialog.dart' ;
23import 'package:matrix/matrix.dart' ;
34import 'package:watch_it/watch_it.dart' ;
45import 'package:yaru/yaru.dart' ;
56
67import '../../common/chat_model.dart' ;
78import '../../common/view/build_context_x.dart' ;
8- import '../../common/view/confirm.dart' ;
99import '../../common/view/snackbars.dart' ;
1010import '../../common/view/ui_constants.dart' ;
1111import '../../l10n/l10n.dart' ;
@@ -23,6 +23,16 @@ class ChatPermissionsSettingsView extends StatelessWidget with WatchItMixin {
2323 final l10n = context.l10n;
2424 final chatModel = di <ChatModel >();
2525
26+ final canChangePowerLevel =
27+ watchStream (
28+ (ChatModel m) => m
29+ .getJoinedRoomUpdate (room.id)
30+ .map ((_) => room.canChangePowerLevel),
31+ initialValue: room.canChangePowerLevel,
32+ preserveState: false ,
33+ ).data ??
34+ room.canChangePowerLevel;
35+
2636 final powerLevelsContent =
2737 watchStream (
2838 (ChatModel m) => m.getPermissionsStream (room),
@@ -63,20 +73,21 @@ class ChatPermissionsSettingsView extends StatelessWidget with WatchItMixin {
6373 children: [
6474 for (final entry in powerLevels.entries)
6575 _ChatRoomPermissionTile (
76+ canEdit: canChangePowerLevel,
6677 permissionKey: entry.key,
6778 permission: entry.value,
68- onChanged: (level) => chatModel.editPowerLevel (
69- room: room,
70- key: entry.key,
71- newLevel: level,
72- onFail: () =>
73- showSnackBar (context, content: Text (l10n.noPermission)),
74- onCustomPermissionsChosen: () => _showPermissionChooser (
75- context,
76- currentLevel: entry.value,
79+ onChanged: (level) => showFutureLoadingDialog (
80+ context: context,
81+ onError: (e) {
82+ showSnackBar (context, content: Text (e.toString ()));
83+ return e;
84+ },
85+ future: () => chatModel.editPowerLevel (
86+ room: room,
87+ key: entry.key,
88+ newLevel: level,
7789 ),
7890 ),
79- canEdit: room.canChangePowerLevel,
8091 ),
8192 ],
8293 ),
@@ -93,19 +104,22 @@ class ChatPermissionsSettingsView extends StatelessWidget with WatchItMixin {
93104 return Padding (
94105 padding: const EdgeInsets .only (bottom: kMediumPadding),
95106 child: _ChatRoomPermissionTile (
107+ canEdit: canChangePowerLevel,
96108 permissionKey: key,
97109 permission: value,
98110 category: 'notifications' ,
99- canEdit: room.canChangePowerLevel,
100- onChanged: (level) => chatModel.editPowerLevel (
101- room: room,
102- key: key,
103- onFail: () =>
104- showSnackBar (context, content: Text (l10n.noPermission)),
105- onCustomPermissionsChosen: () =>
106- _showPermissionChooser (context, currentLevel: value),
107- newLevel: level,
108- category: 'notifications' ,
111+ onChanged: (level) => showFutureLoadingDialog (
112+ context: context,
113+ onError: (e) {
114+ showSnackBar (context, content: Text (e.toString ()));
115+ return e;
116+ },
117+ future: () => chatModel.editPowerLevel (
118+ room: room,
119+ key: key,
120+ newLevel: level,
121+ category: 'notifications' ,
122+ ),
109123 ),
110124 ),
111125 );
@@ -118,21 +132,23 @@ class ChatPermissionsSettingsView extends StatelessWidget with WatchItMixin {
118132 children: [
119133 for (final entry in eventsPowerLevels.entries)
120134 _ChatRoomPermissionTile (
135+ canEdit: canChangePowerLevel,
121136 permissionKey: entry.key,
122137 category: 'events' ,
123138 permission: entry.value ?? 0 ,
124- canEdit: room.canChangePowerLevel,
125- onChanged: (level) => di <ChatModel >().editPowerLevel (
126- room: room,
127- onFail: () =>
128- showSnackBar (context, content: Text (l10n.noPermission)),
129- key: entry.key,
130- onCustomPermissionsChosen: () => _showPermissionChooser (
131- context,
132- currentLevel: entry.value ?? 0 ,
139+
140+ onChanged: (level) => showFutureLoadingDialog (
141+ context: context,
142+ onError: (e) {
143+ showSnackBar (context, content: Text (e.toString ()));
144+ return e;
145+ },
146+ future: () => di <ChatModel >().editPowerLevel (
147+ room: room,
148+ key: entry.key,
149+ newLevel: level,
150+ category: 'events' ,
133151 ),
134- newLevel: level,
135- category: 'events' ,
136152 ),
137153 ),
138154 ],
@@ -141,35 +157,9 @@ class ChatPermissionsSettingsView extends StatelessWidget with WatchItMixin {
141157 ],
142158 );
143159 }
144-
145- Future <int ?> _showPermissionChooser (
146- BuildContext context, {
147- int currentLevel = 0 ,
148- }) async {
149- final l10n = context.l10n;
150- final customLevel = await showConfirmDialogWithInput (
151- context: context,
152- title: l10n.setPermissionsLevel,
153- initialText: currentLevel.toString (),
154- keyboardType: TextInputType .number,
155- autocorrect: false ,
156- validator: (text) {
157- if (text.isEmpty) {
158- return l10n.pleaseEnterANumber;
159- }
160- final level = int .tryParse (text);
161- if (level == null ) {
162- return l10n.pleaseEnterANumber;
163- }
164- return null ;
165- },
166- );
167- if (customLevel == null ) return null ;
168- return int .tryParse (customLevel);
169- }
170160}
171161
172- class _ChatRoomPermissionTile extends StatelessWidget {
162+ class _ChatRoomPermissionTile extends StatelessWidget with WatchItMixin {
173163 final String permissionKey;
174164 final int permission;
175165 final String ? category;
@@ -238,6 +228,9 @@ class _ChatRoomPermissionTile extends StatelessWidget {
238228 final l10n = context.l10n;
239229 final theme = context.theme;
240230 final colorScheme = theme.colorScheme;
231+ final style = canEdit
232+ ? theme.textTheme.bodyMedium
233+ : theme.textTheme.bodyMedium? .copyWith (color: theme.disabledColor);
241234
242235 final color = permission >= 100
243236 ? colorScheme.warning
@@ -251,40 +244,48 @@ class _ChatRoomPermissionTile extends StatelessWidget {
251244 style: theme.textTheme.titleSmall,
252245 ),
253246 trailing: Material (
254- color: color.withAlpha (32 ),
247+ color: canEdit ? color.withAlpha (32 ) : null ,
255248 borderRadius: BorderRadius .circular (kYaruContainerRadius),
256249 child: DropdownButton <int >(
257250 isDense: true ,
258- style: TextStyle (color : colorScheme.onSurface) ,
251+ style: style ,
259252 icon: const SizedBox .shrink (),
260253 padding: const EdgeInsets .symmetric (
261254 horizontal: kMediumPadding,
262255 vertical: kSmallPadding,
263256 ),
264257 borderRadius: BorderRadius .circular (kYaruContainerRadius),
265258 underline: const SizedBox .shrink (),
266- onChanged: canEdit ? onChanged : null ,
259+ onChanged: onChanged,
267260 value: permission,
268261 items: [
269262 DropdownMenuItem (
270263 value: permission < 50 ? permission : 0 ,
271- child: Text (l10n.userLevel (permission < 50 ? permission : 0 )),
264+ child: Text (
265+ l10n.userLevel (permission < 50 ? permission : 0 ),
266+ style: style,
267+ ),
272268 ),
273269 DropdownMenuItem (
274270 value: permission < 100 && permission >= 50 ? permission : 50 ,
275271 child: Text (
276272 l10n.moderatorLevel (
277273 permission < 100 && permission >= 50 ? permission : 50 ,
278274 ),
275+ style: style,
279276 ),
280277 ),
281278 DropdownMenuItem (
282279 value: permission >= 100 ? permission : 100 ,
283280 child: Text (
284281 l10n.adminLevel (permission >= 100 ? permission : 100 ),
282+ style: style,
285283 ),
286284 ),
287- DropdownMenuItem (value: null , child: Text (l10n.custom)),
285+ DropdownMenuItem (
286+ value: null ,
287+ child: Text (l10n.custom, style: style),
288+ ),
288289 ],
289290 ),
290291 ),
0 commit comments