added edit bookmark capabilities
This commit is contained in:
@@ -32,4 +32,14 @@ class Bookmark {
|
||||
'description': description,
|
||||
'createdAt': createdAt,
|
||||
};
|
||||
|
||||
Bookmark copyWith({String? link, String? name, String? description}) {
|
||||
return Bookmark(
|
||||
collectionId: collectionId,
|
||||
name: name ?? this.name,
|
||||
link: link ?? this.link,
|
||||
description: description ?? this.description,
|
||||
createdAt: createdAt,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,6 +30,19 @@ class _CollectionPageState extends State<CollectionPage> {
|
||||
),
|
||||
);
|
||||
|
||||
void editBookmark(Bookmark selectedBookmark) => showDialog(
|
||||
context: context,
|
||||
builder: (context) => CreateBookmarkDialog(
|
||||
collectionId: BookmarksProvider.selectedCollectionId!,
|
||||
selectedBookmark: selectedBookmark,
|
||||
onSavePressed: onBookmarkSaved,
|
||||
onDeletePressed: () {
|
||||
Storage.deleteBookmarkById(selectedBookmark.id);
|
||||
setState(() {});
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
@@ -39,7 +52,7 @@ class _CollectionPageState extends State<CollectionPage> {
|
||||
}
|
||||
|
||||
void onBookmarkSaved(Bookmark bookmark) {
|
||||
Storage.addBookmark(bookmark);
|
||||
Storage.addOrUpdateBookmark(bookmark);
|
||||
setState(() {});
|
||||
context.read<SharedLinkProvider>().removeCurrentMapsLink();
|
||||
}
|
||||
@@ -48,6 +61,7 @@ class _CollectionPageState extends State<CollectionPage> {
|
||||
return ListTile(
|
||||
title: Text(bookmark.name),
|
||||
onTap: () => launchUrlFromString(bookmark.link),
|
||||
onLongPress: () => editBookmark(bookmark),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -55,6 +69,7 @@ class _CollectionPageState extends State<CollectionPage> {
|
||||
Widget build(BuildContext context) {
|
||||
SharedLinkProvider provider = context.watch<SharedLinkProvider>();
|
||||
selectedMapsLink = provider.currentMapsLinkMetadata;
|
||||
|
||||
if (BookmarksProvider.selectedCollectionId == null) {
|
||||
Navigator.of(context).pop();
|
||||
}
|
||||
|
||||
@@ -65,6 +65,12 @@ class Storage {
|
||||
await saveAllBookmarks(bookmarks);
|
||||
}
|
||||
|
||||
static Future<void> deleteBookmark(Bookmark bookmark) async {
|
||||
final bookmarks = loadAllBookmarks();
|
||||
bookmarks.remove(bookmark);
|
||||
await saveAllBookmarks(bookmarks);
|
||||
}
|
||||
|
||||
static Future<void> deleteBookmarkById(int bookmarkId) async {
|
||||
final bookmarks = loadAllBookmarks();
|
||||
bookmarks.removeWhere((b) => b.id == bookmarkId);
|
||||
@@ -77,10 +83,23 @@ class Storage {
|
||||
await saveAllBookmarks(bookmarks);
|
||||
}
|
||||
|
||||
static Future<void> addOrUpdateBookmark(Bookmark bookmark) async {
|
||||
final bookmarks = loadAllBookmarks();
|
||||
final index = bookmarks.indexWhere((b) => b.id == bookmark.id);
|
||||
|
||||
if (index == -1) {
|
||||
bookmarks.add(bookmark);
|
||||
} else if (index >= 0) {
|
||||
bookmarks[index] = bookmark;
|
||||
}
|
||||
await saveAllBookmarks(bookmarks);
|
||||
}
|
||||
|
||||
static Future<void> updateBookmarkById(
|
||||
int bookmarkId, {
|
||||
String? name,
|
||||
String? description,
|
||||
String? link,
|
||||
}) async {
|
||||
final bookmarks = loadAllBookmarks();
|
||||
final index = bookmarks.indexWhere((b) => b.id == bookmarkId);
|
||||
@@ -89,6 +108,7 @@ class Storage {
|
||||
|
||||
if (name != null) bookmarks[index].name = name;
|
||||
if (description != null) bookmarks[index].description = description;
|
||||
if (link != null) bookmarks[index].link = link;
|
||||
|
||||
await saveAllBookmarks(bookmarks);
|
||||
}
|
||||
|
||||
@@ -8,11 +8,13 @@ class CreateBookmarkDialog extends StatelessWidget {
|
||||
const CreateBookmarkDialog({
|
||||
super.key,
|
||||
required this.collectionId,
|
||||
required this.onSavePressed,
|
||||
this.onSavePressed,
|
||||
this.onDeletePressed,
|
||||
this.selectedBookmark,
|
||||
this.selectedMapsLink,
|
||||
});
|
||||
final void Function(Bookmark bookmark) onSavePressed;
|
||||
final void Function(Bookmark bookmark)? onSavePressed;
|
||||
final void Function()? onDeletePressed;
|
||||
final int collectionId;
|
||||
final Bookmark? selectedBookmark;
|
||||
final MapsLinkMetadata? selectedMapsLink;
|
||||
@@ -27,6 +29,10 @@ class CreateBookmarkDialog extends StatelessWidget {
|
||||
nameController.text = selectedMapsLink!.placeName;
|
||||
linkController.text = selectedMapsLink!.url;
|
||||
descriptionController.text = selectedMapsLink!.description;
|
||||
} else if (selectedBookmark != null) {
|
||||
nameController.text = selectedBookmark!.name;
|
||||
linkController.text = selectedBookmark!.link;
|
||||
descriptionController.text = selectedBookmark!.description;
|
||||
}
|
||||
|
||||
return AlertDialog(
|
||||
@@ -91,7 +97,7 @@ class CreateBookmarkDialog extends StatelessWidget {
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
Column(
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
TextButton(
|
||||
@@ -100,18 +106,31 @@ class CreateBookmarkDialog extends StatelessWidget {
|
||||
),
|
||||
FloatingActionButton(
|
||||
onPressed: () {
|
||||
onSavePressed(
|
||||
Bookmark(
|
||||
collectionId: collectionId,
|
||||
name: nameController.text,
|
||||
link: linkController.text,
|
||||
description: descriptionController.text,
|
||||
),
|
||||
);
|
||||
final bookmark =
|
||||
selectedBookmark?.copyWith(
|
||||
name: nameController.text,
|
||||
link: linkController.text,
|
||||
description: descriptionController.text,
|
||||
) ??
|
||||
Bookmark(
|
||||
collectionId: collectionId,
|
||||
name: nameController.text,
|
||||
link: linkController.text,
|
||||
description: descriptionController.text,
|
||||
);
|
||||
onSavePressed?.call(bookmark);
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
child: Icon(Icons.save),
|
||||
),
|
||||
if (selectedBookmark != null)
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
onDeletePressed?.call();
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
child: Text('Delete'),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user