import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import '../l10n/app_localizations.dart'; import '../model/bookmark.dart'; import '../model/maps_link_metadata.dart'; import '../service/bookmarks_provider.dart'; import '../service/notifying.dart'; import '../service/shared_link_provider.dart'; import '../service/storage.dart'; import '../service/url_launcher.dart'; import '../widgets/create_bookmark_dialog.dart'; class CollectionPage extends StatefulWidget { const CollectionPage({super.key}); static const String routeName = '/bookmarks'; @override State createState() => _CollectionPageState(); } class _CollectionPageState extends State { MapsLinkMetadata? selectedMapsLink; @override void initState() { super.initState(); WidgetsBinding.instance.addPostFrameCallback((_) { if (selectedMapsLink != null) onAddButtonPressed(); }); } void onAddButtonPressed() => showDialog( context: context, builder: (context) => CreateBookmarkDialog( collectionId: BookmarksProvider.selectedCollectionId!, onSavePressed: onBookmarkSaved, selectedMapsLink: selectedMapsLink, ), ); void editBookmark(Bookmark selectedBookmark) => showDialog( context: context, builder: (context) => CreateBookmarkDialog( collectionId: BookmarksProvider.selectedCollectionId!, selectedBookmark: selectedBookmark, onSavePressed: onBookmarkSaved, onDeletePressed: () { Storage.deleteBookmarkById( selectedBookmark.id, ).whenComplete(() => setState(() {})); }, ), ); void onBookmarkSaved(Bookmark bookmark) { Storage.addOrUpdateBookmark(bookmark); setState(() {}); context.read().removeCurrentMapsLink(); } Widget bookmarksListItemBuilder(BuildContext context, Bookmark bookmark) { return ListTile( title: Text(bookmark.name), onTap: () => launchUrlFromString(bookmark.link).then((errorCode) { if (context.mounted) { return Notifying.showUrlErrorSnackbar(context, errorCode); } }), onLongPress: () => editBookmark(bookmark), ); } @override Widget build(BuildContext context) { SharedLinkProvider provider = context.watch(); selectedMapsLink = provider.currentMapsLinkMetadata; if (BookmarksProvider.selectedCollectionId == null) { Navigator.of(context).pop(); } final bookmarks = Storage.loadBookmarksForCollection( BookmarksProvider.selectedCollectionId!, ); final collection = Storage.loadCollections().firstWhere( (c) => c.id == BookmarksProvider.selectedCollectionId, ); return Scaffold( appBar: AppBar( title: selectedMapsLink != null ? Text( AppLocalizations.of(context)!.addToCollection(collection.name), ) : Text(collection.name), actions: [ if (selectedMapsLink != null) TextButton( onPressed: () => provider.removeCurrentMapsLink(), child: Text(AppLocalizations.of(context)!.cancel), ), ], ), body: Center( child: SizedBox( width: MediaQuery.of(context).size.width * 0.9, child: ListView.separated( itemBuilder: (context, index) => bookmarksListItemBuilder(context, bookmarks.elementAt(index)), itemCount: bookmarks.length, separatorBuilder: (context, index) => SizedBox(height: 10), ), ), ), floatingActionButton: FloatingActionButton( onPressed: onAddButtonPressed, child: Icon(selectedMapsLink != null ? Icons.save : Icons.add), ), ); } }