From 8dacc0f4d4ac5b83a6889d292db802069b92e8d5 Mon Sep 17 00:00:00 2001 From: Juan Gilsanz Polo Date: Mon, 24 Jun 2024 18:56:12 +0200 Subject: [PATCH] Allow edit title and description after url validated --- .../bookmarks/model/bookmark_form.model.dart | 5 ++-- .../provider/bookmark_form.provider.dart | 23 +++++++----------- .../bookmarks/ui/bookmark_form_modal.dart | 24 +++++++++---------- 3 files changed, 21 insertions(+), 31 deletions(-) diff --git a/lib/screens/bookmarks/model/bookmark_form.model.dart b/lib/screens/bookmarks/model/bookmark_form.model.dart index eac959e..7d0fcb9 100644 --- a/lib/screens/bookmarks/model/bookmark_form.model.dart +++ b/lib/screens/bookmarks/model/bookmark_form.model.dart @@ -1,12 +1,11 @@ import 'package:flutter/material.dart'; import 'package:linkdy/constants/enums.dart'; -import 'package:linkdy/models/data/check_bookmark.dart'; class BookmarkFormModel { final TextEditingController urlController; String? urlError; - CheckBookmark? checkBookmark; + bool? bookmarkValid; LoadStatus? checkBookmarkLoadStatus; final TextEditingController titleController; final TextEditingController descriptionController; @@ -21,7 +20,7 @@ class BookmarkFormModel { BookmarkFormModel({ required this.urlController, this.urlError, - this.checkBookmark, + this.bookmarkValid, this.checkBookmarkLoadStatus, required this.titleController, required this.descriptionController, diff --git a/lib/screens/bookmarks/provider/bookmark_form.provider.dart b/lib/screens/bookmarks/provider/bookmark_form.provider.dart index 4ea5592..06dc62f 100644 --- a/lib/screens/bookmarks/provider/bookmark_form.provider.dart +++ b/lib/screens/bookmarks/provider/bookmark_form.provider.dart @@ -48,14 +48,7 @@ class BookmarkForm extends _$BookmarkForm { void initializeProvider(Bookmark bookmark) { state.editBookmarkId = bookmark.id; - state.checkBookmark = CheckBookmark( - bookmark: bookmark, - metadata: Metadata( - url: bookmark.url, - description: bookmark.websiteDescription, - title: bookmark.websiteTitle, - ), - ); + state.bookmarkValid = true; state.checkBookmarkLoadStatus = LoadStatus.loaded; state.urlController.text = bookmark.url ?? ''; state.titleController.text = bookmark.title ?? ''; @@ -79,7 +72,7 @@ class BookmarkForm extends _$BookmarkForm { } void validateUrl(String value) { - state.checkBookmark = null; + state.bookmarkValid = null; state.checkBookmarkLoadStatus = null; if (Regexps.urlWithoutProtocol.hasMatch(value)) { state.urlError = null; @@ -93,13 +86,15 @@ class BookmarkForm extends _$BookmarkForm { void checkUrlDetails({required bool updateState}) async { if (state.urlError == null && state.urlController.text != "") { state.checkBookmarkLoadStatus = LoadStatus.loading; - state.checkBookmark = null; + state.bookmarkValid = null; if (updateState == true) { ref.notifyListeners(); } final result = await ref.read(checkBookmarkProvider(state.urlController.text).future); if (result.successful == true) { - state.checkBookmark = result.content; + state.titleController.text = result.content?.metadata?.title ?? ""; + state.descriptionController.text = result.content?.metadata?.description ?? ""; + state.bookmarkValid = true; state.checkBookmarkLoadStatus = LoadStatus.loaded; } else { state.checkBookmarkLoadStatus = LoadStatus.error; @@ -121,10 +116,8 @@ class BookmarkForm extends _$BookmarkForm { void saveBookmark() async { final newBookmark = SetBookmarkData( url: state.urlController.text, - title: state.titleController.text != "" ? state.titleController.text : state.checkBookmark?.metadata?.title ?? '', - description: state.descriptionController.text != "" - ? state.descriptionController.text - : state.checkBookmark?.metadata?.description ?? '', + title: state.titleController.text, + description: state.descriptionController.text, isArchived: false, unread: state.markAsUnread, shared: state.share, diff --git a/lib/screens/bookmarks/ui/bookmark_form_modal.dart b/lib/screens/bookmarks/ui/bookmark_form_modal.dart index cfe5a2c..fa2f277 100644 --- a/lib/screens/bookmarks/ui/bookmark_form_modal.dart +++ b/lib/screens/bookmarks/ui/bookmark_form_modal.dart @@ -73,7 +73,7 @@ class BookmarkFormModalState extends ConsumerState { ], ), IconButton( - onPressed: ref.watch(bookmarkFormProvider).checkBookmark != null + onPressed: ref.watch(bookmarkFormProvider).bookmarkValid == true ? () => ref.read(bookmarkFormProvider.notifier).saveBookmark() : null, icon: const Icon(Icons.save_rounded), @@ -116,7 +116,7 @@ class BookmarkFormModalState extends ConsumerState { ), actions: [ IconButton( - onPressed: ref.watch(bookmarkFormProvider).checkBookmark != null + onPressed: ref.watch(bookmarkFormProvider).bookmarkValid == true ? () => ref.read(bookmarkFormProvider.notifier).saveBookmark() : null, icon: const Icon(Icons.save_rounded), @@ -163,7 +163,7 @@ class _ModalContent extends ConsumerWidget { final tags = ref.watch(getTagsProvider); - final enabledFields = provider.checkBookmark != null; + final enabledFields = provider.bookmarkValid == true; return Column( crossAxisAlignment: CrossAxisAlignment.start, @@ -208,14 +208,14 @@ class _ModalContent extends ConsumerWidget { : null, style: ButtonStyle( foregroundColor: provider.checkBookmarkLoadStatus == LoadStatus.loaded - ? const MaterialStatePropertyAll(Colors.green) + ? const WidgetStatePropertyAll(Colors.green) : provider.checkBookmarkLoadStatus == LoadStatus.error - ? const MaterialStatePropertyAll(Colors.red) + ? const WidgetStatePropertyAll(Colors.red) : null, backgroundColor: provider.checkBookmarkLoadStatus == LoadStatus.loaded - ? MaterialStatePropertyAll(Colors.green.withOpacity(0.15)) + ? WidgetStatePropertyAll(Colors.green.withOpacity(0.15)) : provider.checkBookmarkLoadStatus == LoadStatus.error - ? MaterialStatePropertyAll(Colors.red.withOpacity(0.15)) + ? WidgetStatePropertyAll(Colors.red.withOpacity(0.15)) : null, ), child: Row( @@ -267,9 +267,8 @@ class _ModalContent extends ConsumerWidget { ), ), labelText: t.bookmarks.addBookmark.title, - hintText: provider.checkBookmark?.metadata?.title, floatingLabelBehavior: - provider.checkBookmark != null ? FloatingLabelBehavior.always : FloatingLabelBehavior.auto, + provider.bookmarkValid == true ? FloatingLabelBehavior.always : FloatingLabelBehavior.auto, helperText: t.bookmarks.addBookmark.leaveEmptyUseWebsiteTitle, enabled: enabledFields, ), @@ -288,9 +287,8 @@ class _ModalContent extends ConsumerWidget { ), ), labelText: t.bookmarks.addBookmark.description, - hintText: provider.checkBookmark?.metadata?.description, floatingLabelBehavior: - provider.checkBookmark != null ? FloatingLabelBehavior.always : FloatingLabelBehavior.auto, + provider.bookmarkValid == true ? FloatingLabelBehavior.always : FloatingLabelBehavior.auto, helperText: t.bookmarks.addBookmark.leaveEmptyUseWebsiteDescription, enabled: enabledFields, ), @@ -423,7 +421,7 @@ class _ModalContent extends ConsumerWidget { title: Text(t.bookmarks.addBookmark.markAsUnread), subtitle: Text(t.bookmarks.addBookmark.markAsUnreadDescription), value: provider.markAsUnread, - onChanged: provider.checkBookmark != null + onChanged: provider.bookmarkValid == true ? (v) => ref.read(bookmarkFormProvider.notifier).updateMarkAsUnread(v) : null, ), @@ -433,7 +431,7 @@ class _ModalContent extends ConsumerWidget { subtitle: Text(t.bookmarks.addBookmark.shareDescription), value: provider.share, onChanged: - provider.checkBookmark != null ? (v) => ref.read(bookmarkFormProvider.notifier).updateShare(v) : null, + provider.bookmarkValid == true ? (v) => ref.read(bookmarkFormProvider.notifier).updateShare(v) : null, ), const SizedBox(height: 16), ],