Skip to content

Commit

Permalink
fix(bookmark_collections): 🐛 prevent from overwriting an already exis…
Browse files Browse the repository at this point in the history
…ting collection (#58)
  • Loading branch information
albertms10 committed Feb 8, 2024
1 parent b2dd02d commit 504abb6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/localization/app_ca.arb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"cancel": "Canceŀla",
"ok": "D’acord",
"emptyCollectionValidation": "El nom de la coŀlecció no pot ser buit",
"collectionNameAlreadyInUseValidation": "El nom de la coŀlecció ja està en ús",
"newCollection": "Nova coŀlecció",
"addToACollection": "Afegeix a una coŀlecció",
"changeCollections": "Canvia les coŀleccions",
Expand Down
6 changes: 5 additions & 1 deletion lib/src/model/bookmark_collections.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ final class BookmarkCollections with ChangeNotifier {
Set<BookmarkCollection> get sortedCollections =>
SplayTreeSet.of(_collections.values);

void addCollection(String name) {
bool addCollection(String name) {
notifyListeners();

if (_collections.containsKey(name)) return false;

_collections.addAll({name: BookmarkCollection(name, {})});

return true;
}

/// Returns whether [id] is bookmarked.
Expand Down
12 changes: 8 additions & 4 deletions lib/src/pages/collections_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ class _AddCollectionDialogState extends State<_AddCollectionDialog> {

@override
Widget build(BuildContext context) {
final collections = Provider.of<BookmarkCollections>(context).collections;

final appLocalizations = AppLocalizations.of(context);

return Form(
Expand All @@ -96,9 +98,12 @@ class _AddCollectionDialogState extends State<_AddCollectionDialog> {
autofocus: true,
decoration: const InputDecoration(errorMaxLines: 2),
validator: (value) {
if (value == null || value.isEmpty) {
if (value == null || value.trim().isEmpty) {
return appLocalizations.emptyCollectionValidation;
}
if (collections.containsKey(value.trim())) {
return appLocalizations.collectionNameAlreadyInUseValidation;
}

return null;
},
Expand All @@ -110,9 +115,8 @@ class _AddCollectionDialogState extends State<_AddCollectionDialog> {
),
TextButton(
onPressed: () {
if (_formKey.currentState!.validate()) {
Navigator.of(context).pop<String>(_controller.text);
}
if (!_formKey.currentState!.validate()) return;
Navigator.of(context).pop<String>(_controller.text.trim());
},
child: Text(appLocalizations.ok),
),
Expand Down

0 comments on commit 504abb6

Please sign in to comment.