From b73362c1015f5fa2b1820cbe7bf9b91c0436f7ec Mon Sep 17 00:00:00 2001 From: Jakob Meier Date: Sat, 1 Apr 2023 12:26:58 +0200 Subject: [PATCH] Replaced math based approach with bitwise operations --- lib/screens/room/about/permissions.dart | 56 +++++++++++++------------ 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/lib/screens/room/about/permissions.dart b/lib/screens/room/about/permissions.dart index d3c6878..94558a6 100644 --- a/lib/screens/room/about/permissions.dart +++ b/lib/screens/room/about/permissions.dart @@ -31,10 +31,10 @@ class _EditRoomPermissionSetPageState extends State { doNetworkRequest( sm, req: () => postWithCreadentials( - path: 'getRoomInfo', - credentials: user, - target: user.server, - body: {'room': widget.tag, 'server': widget.server}), + path: 'getRoomInfo', + credentials: user, + target: user.server, + body: {'room': widget.tag, 'server': widget.server}), onAnyErr: () { // user should not be here // close screen @@ -44,7 +44,7 @@ class _EditRoomPermissionSetPageState extends State { onOK: (body) async { final info = RoomInfo.fromJSON(body['data']); setState(() { - permissions = info.permissions; + permissions = info.permissions; }); return true; }, @@ -69,17 +69,21 @@ class _EditRoomPermissionSetPageState extends State { itemBuilder: (context, index) { final item = items[index]; - final int col = pow(2, index + 1) as int; + final int col = pow(2, index) as int; return SwitchListTile( - title: Text(RoomPermission.name(item, context)), - subtitle: Text(RoomPermission.describe(item, context)), - onChanged: (state) { - setState(() { - permissions += (state ? 1 : -1) * col; - }); - }, - value: permissions & col != 0); + title: Text(RoomPermission.name(item, context)), + subtitle: Text(RoomPermission.describe(item, context)), + onChanged: (state) { + setState(() { + if (state) { + permissions |= col; + } else { + permissions &= ~col; + } + }); + }, + value: permissions & col != 0); }, ), floatingActionButton: FloatingActionButton.extended( @@ -93,18 +97,18 @@ class _EditRoomPermissionSetPageState extends State { // update permissions doNetworkRequest(sm, - req: () => postWithCreadentials( - path: 'setRoomRight', - credentials: user, - target: user.server, - body: { - 'room': widget.tag, - 'server': widget.server, - 'rights': permissions - }), - onOK: (_) { - router.pop(); - }); + req: () => postWithCreadentials( + path: 'setRoomRight', + credentials: user, + target: user.server, + body: { + 'room': widget.tag, + 'server': widget.server, + 'rights': permissions + }), + onOK: (_) { + router.pop(); + }); }, ), );