Encrypted messaging app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
4.6 KiB

  1. import 'package:flutter/material.dart';
  2. import '/components/custom_title_bar.dart';
  3. import '/components/flash_message.dart';
  4. import '/database/models/conversations.dart';
  5. import '/exceptions/update_data_exception.dart';
  6. import '/services/conversations_service.dart';
  7. class ConversationPermissions extends StatefulWidget {
  8. const ConversationPermissions({
  9. Key? key,
  10. required this.conversation,
  11. }) : super(key: key);
  12. final Conversation conversation;
  13. @override
  14. _ConversationPermissionsState createState() => _ConversationPermissionsState();
  15. }
  16. class _ConversationPermissionsState extends State<ConversationPermissions> {
  17. Map<String, Map<String, String>> perms = {
  18. 'admin_add_members': {
  19. 'title': 'Add Members',
  20. 'desc': 'Restrict adding members to admins',
  21. },
  22. 'admin_edit_info': {
  23. 'title': 'Edit Info',
  24. 'desc': 'Restrict editing the conversation information to admins',
  25. },
  26. 'admin_send_messages': {
  27. 'title': 'Send Messages',
  28. 'desc': 'Restrict sending messages to admins',
  29. },
  30. };
  31. @override
  32. Widget build(BuildContext context) {
  33. return Scaffold(
  34. appBar: CustomTitleBar(
  35. title: const Text(
  36. 'Permissions',
  37. style: TextStyle(
  38. fontSize: 28,
  39. fontWeight: FontWeight.bold
  40. )
  41. ),
  42. beforeBack: () async {
  43. ConversationsService.updateConversation(widget.conversation)
  44. .catchError((error) {
  45. String message = error.toString();
  46. if (error.runtimeType != UpdateDataException) {
  47. message = 'An error occured, please try again later';
  48. }
  49. showMessage(message, context);
  50. });
  51. },
  52. showBack: true,
  53. backgroundColor: Colors.transparent,
  54. ),
  55. body: Padding(
  56. padding: const EdgeInsets.only(top: 30),
  57. child: _list(),
  58. ),
  59. );
  60. }
  61. Widget _list() {
  62. return ListView.builder(
  63. itemCount: perms.length,
  64. shrinkWrap: true,
  65. itemBuilder: (context, i) {
  66. String key = perms.keys.elementAt(i);
  67. return GestureDetector(
  68. behavior: HitTestBehavior.opaque,
  69. onTap: () {
  70. _setValue(key);
  71. },
  72. child: Padding(
  73. padding: const EdgeInsets.only(left: 30, right: 20, top: 8, bottom: 8),
  74. child: Row(
  75. children: [
  76. _getValue(key) ?
  77. const Icon(Icons.check) :
  78. const SizedBox(width: 24),
  79. const SizedBox(width: 16),
  80. Expanded(
  81. child: Align(
  82. alignment: Alignment.centerLeft,
  83. child: Column(
  84. crossAxisAlignment: CrossAxisAlignment.start,
  85. children: [
  86. Text(
  87. perms[key]!['title'] ?? '',
  88. textAlign: TextAlign.left,
  89. style: const TextStyle(
  90. fontSize: 20,
  91. fontWeight: FontWeight.normal,
  92. ),
  93. ),
  94. const SizedBox(height: 5),
  95. Text(
  96. perms[key]!['desc'] ?? '',
  97. textAlign: TextAlign.left,
  98. style: const TextStyle(
  99. fontSize: 14,
  100. fontWeight: FontWeight.w200,
  101. ),
  102. ),
  103. ]
  104. )
  105. )
  106. )
  107. ],
  108. )
  109. )
  110. );
  111. }
  112. );
  113. }
  114. bool _getValue(String key) {
  115. switch (key) {
  116. case 'admin_add_members': {
  117. return widget.conversation.adminAddMembers;
  118. }
  119. case 'admin_edit_info': {
  120. return widget.conversation.adminEditInfo;
  121. }
  122. case 'admin_send_messages': {
  123. return widget.conversation.adminSendMessages;
  124. }
  125. default: {
  126. return false;
  127. }
  128. }
  129. }
  130. void _setValue(String key) {
  131. switch (key) {
  132. case 'admin_add_members': {
  133. setState(() {
  134. widget.conversation.adminAddMembers = !widget.conversation.adminAddMembers;
  135. });
  136. break;
  137. }
  138. case 'admin_edit_info': {
  139. setState(() {
  140. widget.conversation.adminEditInfo = !widget.conversation.adminEditInfo;
  141. });
  142. break;
  143. }
  144. case 'admin_send_messages': {
  145. setState(() {
  146. widget.conversation.adminSendMessages = !widget.conversation.adminSendMessages;
  147. });
  148. break;
  149. }
  150. }
  151. }
  152. }