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

117 lines
3.4 KiB
Dart
Raw Normal View History

import 'dart:math';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
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: Text(AppLocalizations.of(context)!.roomDefaultPermissions),
),
body: ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
final item = items[index];
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(() {
if (state) {
permissions |= col;
} else {
permissions &= ~col;
}
});
},
value: permissions & col != 0);
},
),
floatingActionButton: FloatingActionButton.extended(
icon: const Icon(Icons.edit),
tooltip: AppLocalizations.of(context)!.updateRoomPermissionsHint,
label: Text(AppLocalizations.of(context)!.updateRoomPermissions),
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();
});
},
),
);
}
}