diff --git a/lib/pages/collection_page.dart b/lib/pages/collection_page.dart index 5273efa..32a85bb 100644 --- a/lib/pages/collection_page.dart +++ b/lib/pages/collection_page.dart @@ -45,8 +45,9 @@ class _CollectionPageState extends State { selectedBookmark: selectedBookmark, onSavePressed: onBookmarkSaved, onDeletePressed: () { - Storage.deleteBookmarkById(selectedBookmark.id); - setState(() {}); + Storage.deleteBookmarkById( + selectedBookmark.id, + ).whenComplete(() => setState(() {})); }, ), ); @@ -60,7 +61,11 @@ class _CollectionPageState extends State { Widget bookmarksListItemBuilder(BuildContext context, Bookmark bookmark) { return ListTile( title: Text(bookmark.name), - onTap: () => launchUrlFromString(bookmark.link), + onTap: () => launchUrlFromString(bookmark.link).then((errorCode) { + if (context.mounted && errorCode != UrlLaunchErrorCode.none) { + return showUrlError(context, errorCode); + } + }), onLongPress: () => editBookmark(bookmark), ); } @@ -104,4 +109,16 @@ class _CollectionPageState extends State { ), ); } + + void showUrlError(BuildContext context, UrlLaunchErrorCode errorCode) { + String errorText = ''; + if (errorCode == UrlLaunchErrorCode.couldNotLaunch) { + errorText = 'Could not launch Url'; + } else { + errorText = 'Invalid Url'; + } + ScaffoldMessenger.of( + context, + ).showSnackBar(SnackBar(content: Text(errorText))); + } } diff --git a/lib/pages/collections_list_page.dart b/lib/pages/collections_list_page.dart index e4a6782..39325f4 100644 --- a/lib/pages/collections_list_page.dart +++ b/lib/pages/collections_list_page.dart @@ -66,8 +66,9 @@ class _CollectionsListPageState extends State { selectedCollection: selectedCollection, onSavePressed: onCollectionSaved, onDeletePressed: () { - Storage.deleteCollection(selectedCollection); - setState(() {}); + Storage.deleteCollection( + selectedCollection, + ).whenComplete(() => setState(() {})); }, ), ); @@ -79,7 +80,9 @@ class _CollectionsListPageState extends State { addingNewBookmark = provider.currentMapsLinkMetadata != null; return Scaffold( appBar: AppBar( - title: addingNewBookmark ? Text('Choose Collection') : null, + title: addingNewBookmark + ? Text('Choose Collection') + : Text('Collections'), actions: [ if (addingNewBookmark) TextButton( @@ -92,11 +95,15 @@ class _CollectionsListPageState extends State { onPressed: onAddButtonPressed, child: Icon(Icons.add), ), - body: ListView.builder( - itemBuilder: (context, index) => - collectionsListItemBuilder(context, collections.elementAt(index)), - itemCount: collections.length, - ), + body: collections.isNotEmpty + ? ListView.builder( + itemBuilder: (context, index) => collectionsListItemBuilder( + context, + collections.elementAt(index), + ), + itemCount: collections.length, + ) + : Center(child: Text('Create your first Collection to get started!')), ); } } diff --git a/lib/service/url_launcher.dart b/lib/service/url_launcher.dart index 4d8e71a..dbd5ee7 100644 --- a/lib/service/url_launcher.dart +++ b/lib/service/url_launcher.dart @@ -1,8 +1,14 @@ import 'package:url_launcher/url_launcher.dart'; -Future launchUrlFromString(String url) async { - final Uri uri = Uri.parse(url); +Future launchUrlFromString(String url) async { + final Uri? uri = Uri.tryParse(url); + final isValid = + uri != null && uri.hasAbsolutePath && uri.scheme.startsWith('http'); + if (!isValid) return UrlLaunchErrorCode.invalidUrl; if (!await launchUrl(uri, mode: LaunchMode.externalApplication)) { - throw Exception('Could not launch $url'); + return UrlLaunchErrorCode.couldNotLaunch; } + return UrlLaunchErrorCode.none; } + +enum UrlLaunchErrorCode { none, couldNotLaunch, invalidUrl }