actions-test/lib/screens/room/permissions.dart

121 lines
3.3 KiB
Dart
Raw Normal View History

import 'dart:math';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:outbag_app/backend/permissions.dart';
import 'package:outbag_app/backend/request.dart';
import 'package:outbag_app/backend/room.dart';
import 'package:outbag_app/backend/user.dart';
import 'package:outbag_app/tools/fetch_wrapper.dart';
import 'package:provider/provider.dart';
class EditRoomPermissionSetPage extends StatefulWidget {
final String server;
final String tag;
const EditRoomPermissionSetPage(this.server, this.tag, {super.key});
@override
State<StatefulWidget> createState() => _EditRoomPermissionSetPageState();
}
class _EditRoomPermissionSetPageState extends State<EditRoomPermissionSetPage> {
List<int> items = RoomPermission.asList;
int permissions = 0;
void fetchInfo() {
final router = GoRouter.of(context);
final sm = ScaffoldMessenger.of(context);
final user = context.read<User>();
doNetworkRequest(
sm,
req: () => postWithCreadentials(
path: 'getRoomInfo',
credentials: user,
target: user.server,
body: {'room': widget.tag, 'server': widget.server}),
onAnyErr: () {
// user should not be here
// close screen
router.pushReplacementNamed('home');
return false;
},
onOK: (body) async {
final info = RoomInfo.fromJSON(body['data']);
setState(() {
permissions = info.permissions;
});
return true;
},
);
}
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) => fetchInfo());
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Default permissions'),
leading: IconButton(
onPressed: () {
// go back
Navigator.of(context).pop();
},
icon: const Icon(Icons.arrow_back),
tooltip: "Go back",
),
),
body: ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
final item = items[index];
final int col = pow(2, index + 1) as int;
return SwitchListTile(
title: Text(RoomPermission.name(item)),
subtitle: Text(RoomPermission.describe(item)),
onChanged: (state) {
setState(() {
permissions += (state ? 1 : -1) * col;
});
},
value: permissions & col != 0);
},
),
floatingActionButton: FloatingActionButton.extended(
icon: const Icon(Icons.edit),
tooltip: "Update default permission set",
label: const Text('Edit'),
onPressed: () {
final router = GoRouter.of(context);
final sm = ScaffoldMessenger.of(context);
final user = context.read<User>();
// 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();
});
},
),
);
}
}