From 340b37d6e6cecc9085fecf88b3eb191893f72676 Mon Sep 17 00:00:00 2001 From: Juan Gilsanz Polo Date: Wed, 3 Apr 2024 16:18:02 +0200 Subject: [PATCH] Validate URL after import --- .../bookmarks/provider/bookmark_form.provider.dart | 13 +++++++++---- lib/screens/bookmarks/ui/bookmark_form_modal.dart | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/screens/bookmarks/provider/bookmark_form.provider.dart b/lib/screens/bookmarks/provider/bookmark_form.provider.dart index 34fccb8..3c110f9 100644 --- a/lib/screens/bookmarks/provider/bookmark_form.provider.dart +++ b/lib/screens/bookmarks/provider/bookmark_form.provider.dart @@ -66,9 +66,12 @@ class BookmarkForm extends _$BookmarkForm { } void initializeProviderUrl(String url) { - state.urlController.text = url; - if (Regexps.urlWithoutProtocol.hasMatch(url)) { + final decoded = Uri.decodeComponent(url); + var value = decoded.contains(":~:text=") ? decoded.split(":~:text=")[1] : null; + state.urlController.text = value ?? decoded; + if (Regexps.urlWithoutProtocol.hasMatch(value ?? decoded)) { state.urlError = null; + checkUrlDetails(updateState: false); } else { state.urlError = t.bookmarks.addBookmark.invalidUrl; } @@ -86,11 +89,13 @@ class BookmarkForm extends _$BookmarkForm { } } - void checkUrlDetails() async { + void checkUrlDetails({required bool updateState}) async { if (state.urlError == null && state.urlController.text != "") { state.checkBookmarkLoadStatus = LoadStatus.loading; state.checkBookmark = null; - ref.notifyListeners(); + if (updateState == true) { + ref.notifyListeners(); + } final result = await ref.read(checkBookmarkProvider(state.urlController.text).future); if (result.successful == true) { state.checkBookmark = result.content; diff --git a/lib/screens/bookmarks/ui/bookmark_form_modal.dart b/lib/screens/bookmarks/ui/bookmark_form_modal.dart index 0c63b35..ad099b1 100644 --- a/lib/screens/bookmarks/ui/bookmark_form_modal.dart +++ b/lib/screens/bookmarks/ui/bookmark_form_modal.dart @@ -197,7 +197,7 @@ class _ModalContent extends ConsumerWidget { onPressed: provider.checkBookmarkLoadStatus == null && provider.urlError == null && provider.urlController.text != "" - ? () => ref.read(bookmarkFormProvider.notifier).checkUrlDetails() + ? () => ref.read(bookmarkFormProvider.notifier).checkUrlDetails(updateState: true) : null, style: ButtonStyle( foregroundColor: provider.checkBookmarkLoadStatus == LoadStatus.loaded