diff --git a/lib/model/bookmark.dart b/lib/model/bookmark.dart index f10910a..598adb8 100644 --- a/lib/model/bookmark.dart +++ b/lib/model/bookmark.dart @@ -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, + ); + } } diff --git a/lib/pages/collection_page.dart b/lib/pages/collection_page.dart index b8d3b98..ef151ed 100644 --- a/lib/pages/collection_page.dart +++ b/lib/pages/collection_page.dart @@ -30,6 +30,19 @@ class _CollectionPageState extends State { ), ); + 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 { } void onBookmarkSaved(Bookmark bookmark) { - Storage.addBookmark(bookmark); + Storage.addOrUpdateBookmark(bookmark); setState(() {}); context.read().removeCurrentMapsLink(); } @@ -48,6 +61,7 @@ class _CollectionPageState extends State { return ListTile( title: Text(bookmark.name), onTap: () => launchUrlFromString(bookmark.link), + onLongPress: () => editBookmark(bookmark), ); } @@ -55,6 +69,7 @@ class _CollectionPageState extends State { Widget build(BuildContext context) { SharedLinkProvider provider = context.watch(); selectedMapsLink = provider.currentMapsLinkMetadata; + if (BookmarksProvider.selectedCollectionId == null) { Navigator.of(context).pop(); } diff --git a/lib/service/storage.dart b/lib/service/storage.dart index bc4aac4..fe476dc 100644 --- a/lib/service/storage.dart +++ b/lib/service/storage.dart @@ -65,6 +65,12 @@ class Storage { await saveAllBookmarks(bookmarks); } + static Future deleteBookmark(Bookmark bookmark) async { + final bookmarks = loadAllBookmarks(); + bookmarks.remove(bookmark); + await saveAllBookmarks(bookmarks); + } + static Future deleteBookmarkById(int bookmarkId) async { final bookmarks = loadAllBookmarks(); bookmarks.removeWhere((b) => b.id == bookmarkId); @@ -77,10 +83,23 @@ class Storage { await saveAllBookmarks(bookmarks); } + static Future 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 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); } diff --git a/lib/widgets/create_bookmark_dialog.dart b/lib/widgets/create_bookmark_dialog.dart index 2eb461a..bed058f 100644 --- a/lib/widgets/create_bookmark_dialog.dart +++ b/lib/widgets/create_bookmark_dialog.dart @@ -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'), + ), ], ), ],