Allow edit title and description after url validated

This commit is contained in:
Juan Gilsanz Polo
2024-06-24 18:56:12 +02:00
parent 91593b04fe
commit 8dacc0f4d4
3 changed files with 21 additions and 31 deletions

View File

@@ -1,12 +1,11 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:linkdy/constants/enums.dart'; import 'package:linkdy/constants/enums.dart';
import 'package:linkdy/models/data/check_bookmark.dart';
class BookmarkFormModel { class BookmarkFormModel {
final TextEditingController urlController; final TextEditingController urlController;
String? urlError; String? urlError;
CheckBookmark? checkBookmark; bool? bookmarkValid;
LoadStatus? checkBookmarkLoadStatus; LoadStatus? checkBookmarkLoadStatus;
final TextEditingController titleController; final TextEditingController titleController;
final TextEditingController descriptionController; final TextEditingController descriptionController;
@@ -21,7 +20,7 @@ class BookmarkFormModel {
BookmarkFormModel({ BookmarkFormModel({
required this.urlController, required this.urlController,
this.urlError, this.urlError,
this.checkBookmark, this.bookmarkValid,
this.checkBookmarkLoadStatus, this.checkBookmarkLoadStatus,
required this.titleController, required this.titleController,
required this.descriptionController, required this.descriptionController,

View File

@@ -48,14 +48,7 @@ class BookmarkForm extends _$BookmarkForm {
void initializeProvider(Bookmark bookmark) { void initializeProvider(Bookmark bookmark) {
state.editBookmarkId = bookmark.id; state.editBookmarkId = bookmark.id;
state.checkBookmark = CheckBookmark( state.bookmarkValid = true;
bookmark: bookmark,
metadata: Metadata(
url: bookmark.url,
description: bookmark.websiteDescription,
title: bookmark.websiteTitle,
),
);
state.checkBookmarkLoadStatus = LoadStatus.loaded; state.checkBookmarkLoadStatus = LoadStatus.loaded;
state.urlController.text = bookmark.url ?? ''; state.urlController.text = bookmark.url ?? '';
state.titleController.text = bookmark.title ?? ''; state.titleController.text = bookmark.title ?? '';
@@ -79,7 +72,7 @@ class BookmarkForm extends _$BookmarkForm {
} }
void validateUrl(String value) { void validateUrl(String value) {
state.checkBookmark = null; state.bookmarkValid = null;
state.checkBookmarkLoadStatus = null; state.checkBookmarkLoadStatus = null;
if (Regexps.urlWithoutProtocol.hasMatch(value)) { if (Regexps.urlWithoutProtocol.hasMatch(value)) {
state.urlError = null; state.urlError = null;
@@ -93,13 +86,15 @@ class BookmarkForm extends _$BookmarkForm {
void checkUrlDetails({required bool updateState}) async { void checkUrlDetails({required bool updateState}) async {
if (state.urlError == null && state.urlController.text != "") { if (state.urlError == null && state.urlController.text != "") {
state.checkBookmarkLoadStatus = LoadStatus.loading; state.checkBookmarkLoadStatus = LoadStatus.loading;
state.checkBookmark = null; state.bookmarkValid = null;
if (updateState == true) { if (updateState == true) {
ref.notifyListeners(); ref.notifyListeners();
} }
final result = await ref.read(checkBookmarkProvider(state.urlController.text).future); final result = await ref.read(checkBookmarkProvider(state.urlController.text).future);
if (result.successful == true) { 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; state.checkBookmarkLoadStatus = LoadStatus.loaded;
} else { } else {
state.checkBookmarkLoadStatus = LoadStatus.error; state.checkBookmarkLoadStatus = LoadStatus.error;
@@ -121,10 +116,8 @@ class BookmarkForm extends _$BookmarkForm {
void saveBookmark() async { void saveBookmark() async {
final newBookmark = SetBookmarkData( final newBookmark = SetBookmarkData(
url: state.urlController.text, url: state.urlController.text,
title: state.titleController.text != "" ? state.titleController.text : state.checkBookmark?.metadata?.title ?? '', title: state.titleController.text,
description: state.descriptionController.text != "" description: state.descriptionController.text,
? state.descriptionController.text
: state.checkBookmark?.metadata?.description ?? '',
isArchived: false, isArchived: false,
unread: state.markAsUnread, unread: state.markAsUnread,
shared: state.share, shared: state.share,

View File

@@ -73,7 +73,7 @@ class BookmarkFormModalState extends ConsumerState<BookmarkFormModal> {
], ],
), ),
IconButton( IconButton(
onPressed: ref.watch(bookmarkFormProvider).checkBookmark != null onPressed: ref.watch(bookmarkFormProvider).bookmarkValid == true
? () => ref.read(bookmarkFormProvider.notifier).saveBookmark() ? () => ref.read(bookmarkFormProvider.notifier).saveBookmark()
: null, : null,
icon: const Icon(Icons.save_rounded), icon: const Icon(Icons.save_rounded),
@@ -116,7 +116,7 @@ class BookmarkFormModalState extends ConsumerState<BookmarkFormModal> {
), ),
actions: [ actions: [
IconButton( IconButton(
onPressed: ref.watch(bookmarkFormProvider).checkBookmark != null onPressed: ref.watch(bookmarkFormProvider).bookmarkValid == true
? () => ref.read(bookmarkFormProvider.notifier).saveBookmark() ? () => ref.read(bookmarkFormProvider.notifier).saveBookmark()
: null, : null,
icon: const Icon(Icons.save_rounded), icon: const Icon(Icons.save_rounded),
@@ -163,7 +163,7 @@ class _ModalContent extends ConsumerWidget {
final tags = ref.watch(getTagsProvider); final tags = ref.watch(getTagsProvider);
final enabledFields = provider.checkBookmark != null; final enabledFields = provider.bookmarkValid == true;
return Column( return Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
@@ -208,14 +208,14 @@ class _ModalContent extends ConsumerWidget {
: null, : null,
style: ButtonStyle( style: ButtonStyle(
foregroundColor: provider.checkBookmarkLoadStatus == LoadStatus.loaded foregroundColor: provider.checkBookmarkLoadStatus == LoadStatus.loaded
? const MaterialStatePropertyAll(Colors.green) ? const WidgetStatePropertyAll(Colors.green)
: provider.checkBookmarkLoadStatus == LoadStatus.error : provider.checkBookmarkLoadStatus == LoadStatus.error
? const MaterialStatePropertyAll(Colors.red) ? const WidgetStatePropertyAll(Colors.red)
: null, : null,
backgroundColor: provider.checkBookmarkLoadStatus == LoadStatus.loaded backgroundColor: provider.checkBookmarkLoadStatus == LoadStatus.loaded
? MaterialStatePropertyAll(Colors.green.withOpacity(0.15)) ? WidgetStatePropertyAll(Colors.green.withOpacity(0.15))
: provider.checkBookmarkLoadStatus == LoadStatus.error : provider.checkBookmarkLoadStatus == LoadStatus.error
? MaterialStatePropertyAll(Colors.red.withOpacity(0.15)) ? WidgetStatePropertyAll(Colors.red.withOpacity(0.15))
: null, : null,
), ),
child: Row( child: Row(
@@ -267,9 +267,8 @@ class _ModalContent extends ConsumerWidget {
), ),
), ),
labelText: t.bookmarks.addBookmark.title, labelText: t.bookmarks.addBookmark.title,
hintText: provider.checkBookmark?.metadata?.title,
floatingLabelBehavior: floatingLabelBehavior:
provider.checkBookmark != null ? FloatingLabelBehavior.always : FloatingLabelBehavior.auto, provider.bookmarkValid == true ? FloatingLabelBehavior.always : FloatingLabelBehavior.auto,
helperText: t.bookmarks.addBookmark.leaveEmptyUseWebsiteTitle, helperText: t.bookmarks.addBookmark.leaveEmptyUseWebsiteTitle,
enabled: enabledFields, enabled: enabledFields,
), ),
@@ -288,9 +287,8 @@ class _ModalContent extends ConsumerWidget {
), ),
), ),
labelText: t.bookmarks.addBookmark.description, labelText: t.bookmarks.addBookmark.description,
hintText: provider.checkBookmark?.metadata?.description,
floatingLabelBehavior: floatingLabelBehavior:
provider.checkBookmark != null ? FloatingLabelBehavior.always : FloatingLabelBehavior.auto, provider.bookmarkValid == true ? FloatingLabelBehavior.always : FloatingLabelBehavior.auto,
helperText: t.bookmarks.addBookmark.leaveEmptyUseWebsiteDescription, helperText: t.bookmarks.addBookmark.leaveEmptyUseWebsiteDescription,
enabled: enabledFields, enabled: enabledFields,
), ),
@@ -423,7 +421,7 @@ class _ModalContent extends ConsumerWidget {
title: Text(t.bookmarks.addBookmark.markAsUnread), title: Text(t.bookmarks.addBookmark.markAsUnread),
subtitle: Text(t.bookmarks.addBookmark.markAsUnreadDescription), subtitle: Text(t.bookmarks.addBookmark.markAsUnreadDescription),
value: provider.markAsUnread, value: provider.markAsUnread,
onChanged: provider.checkBookmark != null onChanged: provider.bookmarkValid == true
? (v) => ref.read(bookmarkFormProvider.notifier).updateMarkAsUnread(v) ? (v) => ref.read(bookmarkFormProvider.notifier).updateMarkAsUnread(v)
: null, : null,
), ),
@@ -433,7 +431,7 @@ class _ModalContent extends ConsumerWidget {
subtitle: Text(t.bookmarks.addBookmark.shareDescription), subtitle: Text(t.bookmarks.addBookmark.shareDescription),
value: provider.share, value: provider.share,
onChanged: 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), const SizedBox(height: 16),
], ],