import 'package:Capsule/components/flash_message.dart';
|
|
import 'package:Capsule/exceptions/update_data_exception.dart';
|
|
import 'package:Capsule/utils/storage/conversations.dart';
|
|
import 'package:Capsule/utils/storage/database.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '/components/custom_title_bar.dart';
|
|
import '/models/conversations.dart';
|
|
|
|
class ConversationPermissions extends StatefulWidget {
|
|
const ConversationPermissions({
|
|
Key? key,
|
|
required this.conversation,
|
|
}) : super(key: key);
|
|
|
|
final Conversation conversation;
|
|
|
|
@override
|
|
_ConversationPermissionsState createState() => _ConversationPermissionsState();
|
|
}
|
|
|
|
class _ConversationPermissionsState extends State<ConversationPermissions> {
|
|
Map<String, Map<String, String>> perms = {
|
|
'admin_add_members': {
|
|
'title': 'Add Members',
|
|
'desc': 'Restrict adding members to admins',
|
|
},
|
|
'admin_edit_info': {
|
|
'title': 'Edit Info',
|
|
'desc': 'Restrict editing the conversation information to admins',
|
|
},
|
|
'admin_send_messages': {
|
|
'title': 'Send Messages',
|
|
'desc': 'Restrict sending messages to admins',
|
|
},
|
|
};
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: CustomTitleBar(
|
|
title: const Text(
|
|
'Permissions',
|
|
style: TextStyle(
|
|
fontSize: 28,
|
|
fontWeight: FontWeight.bold
|
|
)
|
|
),
|
|
beforeBack: () async {
|
|
final db = await getDatabaseConnection();
|
|
db.update(
|
|
'conversations',
|
|
widget.conversation.toMap(),
|
|
where: 'id = ?',
|
|
whereArgs: [widget.conversation.id],
|
|
);
|
|
|
|
updateConversation(widget.conversation)
|
|
.catchError((error) {
|
|
String message = error.toString();
|
|
if (error.runtimeType != UpdateDataException) {
|
|
message = 'An error occured, please try again later';
|
|
}
|
|
|
|
showMessage(message, context);
|
|
});
|
|
},
|
|
showBack: true,
|
|
backgroundColor: Colors.transparent,
|
|
),
|
|
body: Padding(
|
|
padding: const EdgeInsets.only(top: 30),
|
|
child: _list(),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _list() {
|
|
return ListView.builder(
|
|
itemCount: perms.length,
|
|
shrinkWrap: true,
|
|
itemBuilder: (context, i) {
|
|
String key = perms.keys.elementAt(i);
|
|
|
|
return GestureDetector(
|
|
behavior: HitTestBehavior.opaque,
|
|
onTap: () {
|
|
_setValue(key);
|
|
},
|
|
|
|
child: Padding(
|
|
padding: const EdgeInsets.only(left: 30, right: 20, top: 8, bottom: 8),
|
|
child: Row(
|
|
children: [
|
|
_getValue(key) ?
|
|
const Icon(Icons.check) :
|
|
const SizedBox(width: 24),
|
|
const SizedBox(width: 16),
|
|
Expanded(
|
|
child: Align(
|
|
alignment: Alignment.centerLeft,
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
perms[key]!['title'] ?? '',
|
|
textAlign: TextAlign.left,
|
|
style: const TextStyle(
|
|
fontSize: 20,
|
|
fontWeight: FontWeight.normal,
|
|
),
|
|
),
|
|
const SizedBox(height: 5),
|
|
Text(
|
|
perms[key]!['desc'] ?? '',
|
|
textAlign: TextAlign.left,
|
|
style: const TextStyle(
|
|
fontSize: 14,
|
|
fontWeight: FontWeight.w200,
|
|
),
|
|
),
|
|
]
|
|
)
|
|
)
|
|
)
|
|
],
|
|
)
|
|
)
|
|
);
|
|
}
|
|
);
|
|
}
|
|
|
|
bool _getValue(String key) {
|
|
switch (key) {
|
|
case 'admin_add_members': {
|
|
return widget.conversation.adminAddMembers;
|
|
}
|
|
case 'admin_edit_info': {
|
|
return widget.conversation.adminEditInfo;
|
|
}
|
|
case 'admin_send_messages': {
|
|
return widget.conversation.adminSendMessages;
|
|
}
|
|
default: {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
void _setValue(String key) {
|
|
switch (key) {
|
|
case 'admin_add_members': {
|
|
setState(() {
|
|
widget.conversation.adminAddMembers = !widget.conversation.adminAddMembers;
|
|
});
|
|
break;
|
|
}
|
|
case 'admin_edit_info': {
|
|
setState(() {
|
|
widget.conversation.adminEditInfo = !widget.conversation.adminEditInfo;
|
|
});
|
|
break;
|
|
}
|
|
case 'admin_send_messages': {
|
|
setState(() {
|
|
widget.conversation.adminSendMessages = !widget.conversation.adminSendMessages;
|
|
});
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|