added workflow to add bookmark from share intent
This commit is contained in:
@@ -1,7 +1,10 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
import '../model/bookmark.dart';
|
import '../model/bookmark.dart';
|
||||||
|
import '../model/maps_link_metadata.dart';
|
||||||
import '../service/bookmarks_provider.dart';
|
import '../service/bookmarks_provider.dart';
|
||||||
|
import '../service/shared_link_provider.dart';
|
||||||
import '../service/storage.dart';
|
import '../service/storage.dart';
|
||||||
import '../service/url_launcher.dart';
|
import '../service/url_launcher.dart';
|
||||||
import '../widgets/create_bookmark_dialog.dart';
|
import '../widgets/create_bookmark_dialog.dart';
|
||||||
@@ -16,17 +19,29 @@ class CollectionPage extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _CollectionPageState extends State<CollectionPage> {
|
class _CollectionPageState extends State<CollectionPage> {
|
||||||
|
MapsLinkMetadata? selectedMapsLink;
|
||||||
|
|
||||||
void onAddButtonPressed() => showDialog(
|
void onAddButtonPressed() => showDialog(
|
||||||
context: context,
|
context: context,
|
||||||
builder: (context) => CreateBookmarkDialog(
|
builder: (context) => CreateBookmarkDialog(
|
||||||
collectionId: BookmarksProvider.selectedCollectionId!,
|
collectionId: BookmarksProvider.selectedCollectionId!,
|
||||||
onSavePressed: onBookmarkSaved,
|
onSavePressed: onBookmarkSaved,
|
||||||
|
selectedMapsLink: selectedMapsLink,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||||
|
if (selectedMapsLink != null) onAddButtonPressed();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
void onBookmarkSaved(Bookmark bookmark) {
|
void onBookmarkSaved(Bookmark bookmark) {
|
||||||
Storage.addBookmark(bookmark);
|
Storage.addBookmark(bookmark);
|
||||||
setState(() {});
|
setState(() {});
|
||||||
|
context.read<SharedLinkProvider>().removeCurrentMapsLink();
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget bookmarkListBuilder(BuildContext context, Bookmark bookmark) {
|
Widget bookmarkListBuilder(BuildContext context, Bookmark bookmark) {
|
||||||
@@ -38,6 +53,8 @@ class _CollectionPageState extends State<CollectionPage> {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
|
SharedLinkProvider provider = context.watch<SharedLinkProvider>();
|
||||||
|
selectedMapsLink = provider.currentMapsLinkMetadata;
|
||||||
if (BookmarksProvider.selectedCollectionId == null) {
|
if (BookmarksProvider.selectedCollectionId == null) {
|
||||||
Navigator.of(context).pop();
|
Navigator.of(context).pop();
|
||||||
}
|
}
|
||||||
@@ -49,7 +66,18 @@ class _CollectionPageState extends State<CollectionPage> {
|
|||||||
);
|
);
|
||||||
|
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(title: Text(collection.name)),
|
appBar: AppBar(
|
||||||
|
title: selectedMapsLink != null
|
||||||
|
? Text('Add to ${collection.name}')
|
||||||
|
: Text(collection.name),
|
||||||
|
actions: [
|
||||||
|
if (selectedMapsLink != null)
|
||||||
|
TextButton(
|
||||||
|
onPressed: () => provider.removeCurrentMapsLink(),
|
||||||
|
child: Text('Cancel'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
body: ListView.builder(
|
body: ListView.builder(
|
||||||
itemBuilder: (context, index) =>
|
itemBuilder: (context, index) =>
|
||||||
bookmarkListBuilder(context, bookmarks.elementAt(index)),
|
bookmarkListBuilder(context, bookmarks.elementAt(index)),
|
||||||
@@ -57,7 +85,7 @@ class _CollectionPageState extends State<CollectionPage> {
|
|||||||
),
|
),
|
||||||
floatingActionButton: FloatingActionButton(
|
floatingActionButton: FloatingActionButton(
|
||||||
onPressed: onAddButtonPressed,
|
onPressed: onAddButtonPressed,
|
||||||
child: Icon(Icons.add),
|
child: Icon(selectedMapsLink != null ? Icons.save : Icons.add),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,12 +18,23 @@ class CollectionsListPage extends StatefulWidget {
|
|||||||
|
|
||||||
class _CollectionsListPageState extends State<CollectionsListPage> {
|
class _CollectionsListPageState extends State<CollectionsListPage> {
|
||||||
final collections = Storage.loadCollections();
|
final collections = Storage.loadCollections();
|
||||||
|
bool addingNewBookmark = false;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final provider = context.watch<SharedLinkProvider>();
|
final provider = context.watch<SharedLinkProvider>();
|
||||||
|
addingNewBookmark = provider.currentMapsLinkMetadata != null;
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(),
|
appBar: AppBar(
|
||||||
|
title: addingNewBookmark ? Text('Choose Collection') : null,
|
||||||
|
actions: [
|
||||||
|
if (addingNewBookmark)
|
||||||
|
TextButton(
|
||||||
|
onPressed: () => provider.removeCurrentMapsLink(),
|
||||||
|
child: Text('Cancel'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
floatingActionButton: FloatingActionButton(
|
floatingActionButton: FloatingActionButton(
|
||||||
onPressed: onAddButtonPressed,
|
onPressed: onAddButtonPressed,
|
||||||
child: Icon(Icons.add),
|
child: Icon(Icons.add),
|
||||||
@@ -32,9 +43,9 @@ class _CollectionsListPageState extends State<CollectionsListPage> {
|
|||||||
itemBuilder: itemBuilder,
|
itemBuilder: itemBuilder,
|
||||||
itemCount: collections.length,
|
itemCount: collections.length,
|
||||||
),
|
),
|
||||||
bottomSheet: provider.currentMapsLinkMetadata == null
|
// bottomSheet: provider.currentMapsLinkMetadata == null
|
||||||
? null
|
// ? null
|
||||||
: BottomSheet(onClosing: () {}, builder: bottomSheetBuilder),
|
// : BottomSheet(onClosing: () {}, builder: bottomSheetBuilder),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,29 +62,34 @@ class _CollectionsListPageState extends State<CollectionsListPage> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void onAddButtonPressed() => showDialog(
|
void onAddButtonPressed() =>
|
||||||
|
showDialog(
|
||||||
context: context,
|
context: context,
|
||||||
builder: (context) =>
|
builder: (context) =>
|
||||||
CreateBookmarkCollectionDialog(onSavePressed: onCollectionSaved),
|
CreateBookmarkCollectionDialog(onSavePressed: onCollectionSaved),
|
||||||
);
|
).whenComplete(() {
|
||||||
|
if (addingNewBookmark) navigateToCollection(collections.last.id);
|
||||||
|
});
|
||||||
|
|
||||||
void onCollectionSaved(String name) {
|
void onCollectionSaved(String name) {
|
||||||
collections.add(Collection(name: name));
|
collections.add(Collection(name: name));
|
||||||
setState(() {});
|
|
||||||
Storage.saveCollections(collections);
|
Storage.saveCollections(collections);
|
||||||
|
setState(() {});
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget itemBuilder(BuildContext context, int index) {
|
Widget itemBuilder(BuildContext context, int index) {
|
||||||
final collection = collections.elementAt(index);
|
final collection = collections.elementAt(index);
|
||||||
return ListTile(
|
return ListTile(
|
||||||
title: Text(collection.name),
|
title: Text(collection.name),
|
||||||
onTap: () {
|
onTap: () => navigateToCollection(collection.id),
|
||||||
BookmarksProvider.selectedCollectionId = collection.id;
|
|
||||||
Navigator.pushNamed(context, CollectionPage.routeName);
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void navigateToCollection(int collectionId) {
|
||||||
|
BookmarksProvider.selectedCollectionId = collectionId;
|
||||||
|
Navigator.pushNamed(context, CollectionPage.routeName);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void didChangeDependencies() {
|
void didChangeDependencies() {
|
||||||
super.didChangeDependencies();
|
super.didChangeDependencies();
|
||||||
|
|||||||
@@ -6,13 +6,18 @@ import '../model/maps_link_metadata.dart';
|
|||||||
class SharedLinkProvider extends ChangeNotifier {
|
class SharedLinkProvider extends ChangeNotifier {
|
||||||
MapsLinkMetadata? _currentMapsLinkMetadata;
|
MapsLinkMetadata? _currentMapsLinkMetadata;
|
||||||
|
|
||||||
void setCurrentMapsLink(String mapsLink) async {
|
void setCurrentMapsLink(String mapsLink, {bool silent = false}) async {
|
||||||
final metadata = await MetadataFetch.extract(mapsLink);
|
final metadata = await MetadataFetch.extract(mapsLink);
|
||||||
_currentMapsLinkMetadata = MapsLinkMetadata(
|
_currentMapsLinkMetadata = MapsLinkMetadata(
|
||||||
url: mapsLink,
|
url: mapsLink,
|
||||||
placeName: metadata?.title ?? '',
|
placeName: metadata?.title ?? '',
|
||||||
);
|
);
|
||||||
notifyListeners();
|
if (!silent) notifyListeners();
|
||||||
|
}
|
||||||
|
|
||||||
|
void removeCurrentMapsLink({bool silent = false}) {
|
||||||
|
_currentMapsLinkMetadata = null;
|
||||||
|
if (!silent) notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
MapsLinkMetadata? get currentMapsLinkMetadata => _currentMapsLinkMetadata;
|
MapsLinkMetadata? get currentMapsLinkMetadata => _currentMapsLinkMetadata;
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
|
|||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
|
|
||||||
import '../model/bookmark.dart';
|
import '../model/bookmark.dart';
|
||||||
|
import '../model/maps_link_metadata.dart';
|
||||||
|
|
||||||
class CreateBookmarkDialog extends StatelessWidget {
|
class CreateBookmarkDialog extends StatelessWidget {
|
||||||
const CreateBookmarkDialog({
|
const CreateBookmarkDialog({
|
||||||
@@ -9,10 +10,12 @@ class CreateBookmarkDialog extends StatelessWidget {
|
|||||||
required this.collectionId,
|
required this.collectionId,
|
||||||
required this.onSavePressed,
|
required this.onSavePressed,
|
||||||
this.selectedBookmark,
|
this.selectedBookmark,
|
||||||
|
this.selectedMapsLink,
|
||||||
});
|
});
|
||||||
final void Function(Bookmark bookmark) onSavePressed;
|
final void Function(Bookmark bookmark) onSavePressed;
|
||||||
final int collectionId;
|
final int collectionId;
|
||||||
final Bookmark? selectedBookmark;
|
final Bookmark? selectedBookmark;
|
||||||
|
final MapsLinkMetadata? selectedMapsLink;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
@@ -20,6 +23,12 @@ class CreateBookmarkDialog extends StatelessWidget {
|
|||||||
final linkController = TextEditingController();
|
final linkController = TextEditingController();
|
||||||
final descriptionController = TextEditingController();
|
final descriptionController = TextEditingController();
|
||||||
|
|
||||||
|
if (selectedMapsLink != null) {
|
||||||
|
nameController.text = selectedMapsLink!.placeName;
|
||||||
|
linkController.text = selectedMapsLink!.url;
|
||||||
|
descriptionController.text = selectedMapsLink!.description;
|
||||||
|
}
|
||||||
|
|
||||||
return AlertDialog(
|
return AlertDialog(
|
||||||
title: Text('Create Bookmark'),
|
title: Text('Create Bookmark'),
|
||||||
content: SingleChildScrollView(
|
content: SingleChildScrollView(
|
||||||
@@ -82,6 +91,13 @@ class CreateBookmarkDialog extends StatelessWidget {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
actions: [
|
actions: [
|
||||||
|
Column(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||||
|
children: [
|
||||||
|
TextButton(
|
||||||
|
onPressed: () => Navigator.of(context).pop(),
|
||||||
|
child: Text('Cancel'),
|
||||||
|
),
|
||||||
FloatingActionButton(
|
FloatingActionButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
onSavePressed(
|
onSavePressed(
|
||||||
@@ -97,6 +113,8 @@ class CreateBookmarkDialog extends StatelessWidget {
|
|||||||
child: Icon(Icons.save),
|
child: Icon(Icons.save),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
),
|
||||||
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user