From c0e2516f3963be5746bb5cb3180ef9232e77b3f3 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Wed, 29 Apr 2020 13:49:16 +0200 Subject: [PATCH] Avoid duplicate shortcut creation --- lib/lists/custom_emojies_list.dart | 4 ++++ .../custom_emojies_account_settings.dart | 17 +++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/lists/custom_emojies_list.dart b/lib/lists/custom_emojies_list.dart index d967008..043ec56 100644 --- a/lib/lists/custom_emojies_list.dart +++ b/lib/lists/custom_emojies_list.dart @@ -6,6 +6,10 @@ import 'package:comunic/models/custom_emoji.dart'; /// @author Pierre HUBERT class CustomEmojiesList extends AbstractList { + /// 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 List> toSerializableList() => map((f) => f.toMap()).toList(); diff --git a/lib/ui/routes/account_settings/custom_emojies_account_settings.dart b/lib/ui/routes/account_settings/custom_emojies_account_settings.dart index b34985c..953a67f 100644 --- a/lib/ui/routes/account_settings/custom_emojies_account_settings.dart +++ b/lib/ui/routes/account_settings/custom_emojies_account_settings.dart @@ -2,6 +2,7 @@ import 'dart:io'; import 'package:comunic/helpers/settings_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/user.dart'; import 'package:comunic/ui/widgets/async_screen_widget.dart'; @@ -90,7 +91,9 @@ class _CustomEmojiesAccountBodyState extends State<_CustomEmojiesAccountBody> { try { final newEmoji = await showDialog( context: context, - builder: (c) => _NewCustomEmojiDialog(), + builder: (c) => _NewCustomEmojiDialog( + currentList: _user.customEmojies, + ), ); if (newEmoji == null) return; @@ -107,6 +110,14 @@ class _CustomEmojiesAccountBodyState extends State<_CustomEmojiesAccountBody> { /// Dialog used to upload new custom emojies class _NewCustomEmojiDialog extends StatefulWidget { + final CustomEmojiesList currentList; + + const _NewCustomEmojiDialog({ + Key key, + @required this.currentList, + }) : assert(currentList != null), + super(key: key); + @override _NewCustomEmojiDialogState createState() => _NewCustomEmojiDialogState(); } @@ -120,7 +131,9 @@ class _NewCustomEmojiDialogState extends State<_NewCustomEmojiDialog> { String get _shortcut => _controller.text; bool get _shortcutValid => - _shortcut.isNotEmpty && validateShortcut(_shortcut); + _shortcut.isNotEmpty && + validateShortcut(_shortcut) && + !widget.currentList.hasShortcut(_shortcut); bool get _valid => _hasImage && _shortcutValid;