added edit bookmark capabilities

This commit is contained in:
2026-01-12 18:27:19 +01:00
parent 885e638265
commit 0309678650
4 changed files with 76 additions and 12 deletions

View File

@@ -32,4 +32,14 @@ class Bookmark {
'description': description, 'description': description,
'createdAt': createdAt, '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,
);
}
} }

View File

@@ -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 @override
void initState() { void initState() {
super.initState(); super.initState();
@@ -39,7 +52,7 @@ class _CollectionPageState extends State<CollectionPage> {
} }
void onBookmarkSaved(Bookmark bookmark) { void onBookmarkSaved(Bookmark bookmark) {
Storage.addBookmark(bookmark); Storage.addOrUpdateBookmark(bookmark);
setState(() {}); setState(() {});
context.read<SharedLinkProvider>().removeCurrentMapsLink(); context.read<SharedLinkProvider>().removeCurrentMapsLink();
} }
@@ -48,6 +61,7 @@ class _CollectionPageState extends State<CollectionPage> {
return ListTile( return ListTile(
title: Text(bookmark.name), title: Text(bookmark.name),
onTap: () => launchUrlFromString(bookmark.link), onTap: () => launchUrlFromString(bookmark.link),
onLongPress: () => editBookmark(bookmark),
); );
} }
@@ -55,6 +69,7 @@ class _CollectionPageState extends State<CollectionPage> {
Widget build(BuildContext context) { Widget build(BuildContext context) {
SharedLinkProvider provider = context.watch<SharedLinkProvider>(); SharedLinkProvider provider = context.watch<SharedLinkProvider>();
selectedMapsLink = provider.currentMapsLinkMetadata; selectedMapsLink = provider.currentMapsLinkMetadata;
if (BookmarksProvider.selectedCollectionId == null) { if (BookmarksProvider.selectedCollectionId == null) {
Navigator.of(context).pop(); Navigator.of(context).pop();
} }

View File

@@ -65,6 +65,12 @@ class Storage {
await saveAllBookmarks(bookmarks); 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 { static Future<void> deleteBookmarkById(int bookmarkId) async {
final bookmarks = loadAllBookmarks(); final bookmarks = loadAllBookmarks();
bookmarks.removeWhere((b) => b.id == bookmarkId); bookmarks.removeWhere((b) => b.id == bookmarkId);
@@ -77,10 +83,23 @@ class Storage {
await saveAllBookmarks(bookmarks); 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( static Future<void> updateBookmarkById(
int bookmarkId, { int bookmarkId, {
String? name, String? name,
String? description, String? description,
String? link,
}) async { }) async {
final bookmarks = loadAllBookmarks(); final bookmarks = loadAllBookmarks();
final index = bookmarks.indexWhere((b) => b.id == bookmarkId); final index = bookmarks.indexWhere((b) => b.id == bookmarkId);
@@ -89,6 +108,7 @@ class Storage {
if (name != null) bookmarks[index].name = name; if (name != null) bookmarks[index].name = name;
if (description != null) bookmarks[index].description = description; if (description != null) bookmarks[index].description = description;
if (link != null) bookmarks[index].link = link;
await saveAllBookmarks(bookmarks); await saveAllBookmarks(bookmarks);
} }

View File

@@ -8,11 +8,13 @@ class CreateBookmarkDialog extends StatelessWidget {
const CreateBookmarkDialog({ const CreateBookmarkDialog({
super.key, super.key,
required this.collectionId, required this.collectionId,
required this.onSavePressed, this.onSavePressed,
this.onDeletePressed,
this.selectedBookmark, this.selectedBookmark,
this.selectedMapsLink, this.selectedMapsLink,
}); });
final void Function(Bookmark bookmark) onSavePressed; final void Function(Bookmark bookmark)? onSavePressed;
final void Function()? onDeletePressed;
final int collectionId; final int collectionId;
final Bookmark? selectedBookmark; final Bookmark? selectedBookmark;
final MapsLinkMetadata? selectedMapsLink; final MapsLinkMetadata? selectedMapsLink;
@@ -27,6 +29,10 @@ class CreateBookmarkDialog extends StatelessWidget {
nameController.text = selectedMapsLink!.placeName; nameController.text = selectedMapsLink!.placeName;
linkController.text = selectedMapsLink!.url; linkController.text = selectedMapsLink!.url;
descriptionController.text = selectedMapsLink!.description; descriptionController.text = selectedMapsLink!.description;
} else if (selectedBookmark != null) {
nameController.text = selectedBookmark!.name;
linkController.text = selectedBookmark!.link;
descriptionController.text = selectedBookmark!.description;
} }
return AlertDialog( return AlertDialog(
@@ -91,7 +97,7 @@ class CreateBookmarkDialog extends StatelessWidget {
), ),
), ),
actions: [ actions: [
Column( Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween, mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [ children: [
TextButton( TextButton(
@@ -100,18 +106,31 @@ class CreateBookmarkDialog extends StatelessWidget {
), ),
FloatingActionButton( FloatingActionButton(
onPressed: () { onPressed: () {
onSavePressed( final bookmark =
selectedBookmark?.copyWith(
name: nameController.text,
link: linkController.text,
description: descriptionController.text,
) ??
Bookmark( Bookmark(
collectionId: collectionId, collectionId: collectionId,
name: nameController.text, name: nameController.text,
link: linkController.text, link: linkController.text,
description: descriptionController.text, description: descriptionController.text,
),
); );
onSavePressed?.call(bookmark);
Navigator.of(context).pop(); Navigator.of(context).pop();
}, },
child: Icon(Icons.save), child: Icon(Icons.save),
), ),
if (selectedBookmark != null)
TextButton(
onPressed: () {
onDeletePressed?.call();
Navigator.of(context).pop();
},
child: Text('Delete'),
),
], ],
), ),
], ],