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.

174 lines
4.9 KiB

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