175 lines
5.3 KiB
Dart
175 lines
5.3 KiB
Dart
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/collection_page_widgets/list_item_actions_widget.dart';
|
|
import '../widgets/create_bookmark_dialog.dart';
|
|
|
|
class CollectionPage extends StatefulWidget {
|
|
const CollectionPage({super.key});
|
|
|
|
static const String routeName = '/bookmarks';
|
|
|
|
@override
|
|
State<CollectionPage> createState() => _CollectionPageState();
|
|
}
|
|
|
|
class _CollectionPageState extends State<CollectionPage> {
|
|
MapsLinkMetadata? selectedMapsLink;
|
|
int selectedBookmarkId = -1;
|
|
|
|
@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(() {}));
|
|
},
|
|
),
|
|
).whenComplete(deselectBookmark);
|
|
|
|
void onBookmarkSaved(Bookmark bookmark) {
|
|
Storage.addOrUpdateBookmark(bookmark);
|
|
setState(() {});
|
|
Provider.of<SharedLinkProvider>(
|
|
context,
|
|
listen: false,
|
|
).removeCurrentMapsLink();
|
|
}
|
|
|
|
Widget bookmarksListItemBuilder(BuildContext context, Bookmark bookmark) {
|
|
final selected = selectedBookmarkId == bookmark.id;
|
|
return ListTile(
|
|
title: Text(bookmark.name),
|
|
selected: selected,
|
|
onTap: () {
|
|
if (selected) {
|
|
onCancelSelectionPressed();
|
|
setState(() {});
|
|
} else if (selectedBookmarkId != -1 && !selected) {
|
|
selectedBookmarkId = bookmark.id;
|
|
setState(() {});
|
|
} else {
|
|
launchUrlFromString(bookmark.link).then((errorCode) {
|
|
if (context.mounted) {
|
|
return Notifying.showUrlErrorSnackbar(context, errorCode);
|
|
}
|
|
});
|
|
}
|
|
},
|
|
onLongPress: () => setState(() {
|
|
selectedBookmarkId = bookmark.id;
|
|
}),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
SharedLinkProvider provider = Provider.of<SharedLinkProvider>(context);
|
|
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 PopScope(
|
|
canPop: selectedBookmarkId == -1,
|
|
onPopInvokedWithResult: (didPop, result) {
|
|
if (didPop == false) deselectBookmark();
|
|
},
|
|
child: 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),
|
|
),
|
|
],
|
|
),
|
|
bottomNavigationBar: selectedBookmarkId > 0
|
|
? ListItemActionsWidget(
|
|
onDeletePressed: onDeleteBookmarkPressed,
|
|
onCancelPressed: onCancelSelectionPressed,
|
|
onEditPressed: () => editBookmark(
|
|
bookmarks.firstWhere(
|
|
(element) => element.id == selectedBookmarkId,
|
|
),
|
|
),
|
|
)
|
|
: null,
|
|
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),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
void onCancelSelectionPressed() => deselectBookmark();
|
|
|
|
void onDeleteBookmarkPressed() {
|
|
Storage.deleteBookmarkById(
|
|
selectedBookmarkId,
|
|
).whenComplete(() => setState(() {}));
|
|
deselectBookmark();
|
|
}
|
|
|
|
void deselectBookmark() {
|
|
selectedBookmarkId = -1;
|
|
setState(() {});
|
|
}
|
|
}
|