Allow edit title and description after url validated
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -73,7 +73,7 @@ class BookmarkFormModalState extends ConsumerState<BookmarkFormModal> {
|
||||
],
|
||||
),
|
||||
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<BookmarkFormModal> {
|
||||
),
|
||||
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),
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user