1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-26 06:49:22 +00:00

Avoid duplicate shortcut creation

This commit is contained in:
Pierre HUBERT 2020-04-29 13:49:16 +02:00
parent 25f72bd11c
commit c0e2516f39
2 changed files with 19 additions and 2 deletions

View File

@ -6,6 +6,10 @@ import 'package:comunic/models/custom_emoji.dart';
/// @author Pierre HUBERT /// @author Pierre HUBERT
class CustomEmojiesList extends AbstractList<CustomEmoji> { class CustomEmojiesList extends AbstractList<CustomEmoji> {
/// Check if an emoji, identified by its shortcut, is present in this list
bool hasShortcut(String shortcut) =>
firstWhere((f) => f.shortcut == shortcut, orElse: () => null) != null;
/// Serialize this list /// Serialize this list
List<Map<String, dynamic>> toSerializableList() => List<Map<String, dynamic>> toSerializableList() =>
map((f) => f.toMap()).toList(); map((f) => f.toMap()).toList();

View File

@ -2,6 +2,7 @@ import 'dart:io';
import 'package:comunic/helpers/settings_helper.dart'; import 'package:comunic/helpers/settings_helper.dart';
import 'package:comunic/helpers/users_helper.dart'; import 'package:comunic/helpers/users_helper.dart';
import 'package:comunic/lists/custom_emojies_list.dart';
import 'package:comunic/models/new_emoji.dart'; import 'package:comunic/models/new_emoji.dart';
import 'package:comunic/models/user.dart'; import 'package:comunic/models/user.dart';
import 'package:comunic/ui/widgets/async_screen_widget.dart'; import 'package:comunic/ui/widgets/async_screen_widget.dart';
@ -90,7 +91,9 @@ class _CustomEmojiesAccountBodyState extends State<_CustomEmojiesAccountBody> {
try { try {
final newEmoji = await showDialog<NewEmoji>( final newEmoji = await showDialog<NewEmoji>(
context: context, context: context,
builder: (c) => _NewCustomEmojiDialog(), builder: (c) => _NewCustomEmojiDialog(
currentList: _user.customEmojies,
),
); );
if (newEmoji == null) return; if (newEmoji == null) return;
@ -107,6 +110,14 @@ class _CustomEmojiesAccountBodyState extends State<_CustomEmojiesAccountBody> {
/// Dialog used to upload new custom emojies /// Dialog used to upload new custom emojies
class _NewCustomEmojiDialog extends StatefulWidget { class _NewCustomEmojiDialog extends StatefulWidget {
final CustomEmojiesList currentList;
const _NewCustomEmojiDialog({
Key key,
@required this.currentList,
}) : assert(currentList != null),
super(key: key);
@override @override
_NewCustomEmojiDialogState createState() => _NewCustomEmojiDialogState(); _NewCustomEmojiDialogState createState() => _NewCustomEmojiDialogState();
} }
@ -120,7 +131,9 @@ class _NewCustomEmojiDialogState extends State<_NewCustomEmojiDialog> {
String get _shortcut => _controller.text; String get _shortcut => _controller.text;
bool get _shortcutValid => bool get _shortcutValid =>
_shortcut.isNotEmpty && validateShortcut(_shortcut); _shortcut.isNotEmpty &&
validateShortcut(_shortcut) &&
!widget.currentList.hasShortcut(_shortcut);
bool get _valid => _hasImage && _shortcutValid; bool get _valid => _hasImage && _shortcutValid;