103 lines
3.0 KiB
Dart
103 lines
3.0 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
|
|
import '../model/collection.dart';
|
|
import '../service/bookmarks_provider.dart';
|
|
import '../service/shared_link_provider.dart';
|
|
import '../service/storage.dart';
|
|
import '../widgets/create_bookmark_collection_dialog.dart';
|
|
import 'collection_page.dart';
|
|
|
|
class CollectionsListPage extends StatefulWidget {
|
|
const CollectionsListPage({super.key});
|
|
|
|
static const String routeName = '/collections';
|
|
|
|
@override
|
|
State<CollectionsListPage> createState() => _CollectionsListPageState();
|
|
}
|
|
|
|
class _CollectionsListPageState extends State<CollectionsListPage> {
|
|
bool addingNewBookmark = false;
|
|
|
|
Widget bottomSheetBuilder(BuildContext context) {
|
|
final titleTextFieldController = TextEditingController(
|
|
text: context
|
|
.read<SharedLinkProvider>()
|
|
.currentMapsLinkMetadata!
|
|
.placeName,
|
|
);
|
|
return SizedBox(
|
|
height: 200,
|
|
child: TextField(controller: titleTextFieldController),
|
|
);
|
|
}
|
|
|
|
void onAddButtonPressed() => showDialog(
|
|
context: context,
|
|
builder: (context) =>
|
|
CreateBookmarkCollectionDialog(onSavePressed: onCollectionSaved),
|
|
);
|
|
|
|
void onCollectionSaved(Collection collection) {
|
|
Storage.addOrUpdateCollection(collection);
|
|
setState(() {});
|
|
}
|
|
|
|
Widget collectionsListItemBuilder(
|
|
BuildContext context,
|
|
Collection collection,
|
|
) {
|
|
return ListTile(
|
|
title: Text(collection.name),
|
|
onTap: () => navigateToCollection(collection.id),
|
|
onLongPress: () => onEditCollection(collection),
|
|
);
|
|
}
|
|
|
|
void navigateToCollection(int collectionId) {
|
|
BookmarksProvider.selectedCollectionId = collectionId;
|
|
Navigator.pushNamed(context, CollectionPage.routeName);
|
|
}
|
|
|
|
void onEditCollection(Collection selectedCollection) => showDialog(
|
|
context: context,
|
|
builder: (context) => CreateBookmarkCollectionDialog(
|
|
selectedCollection: selectedCollection,
|
|
onSavePressed: onCollectionSaved,
|
|
onDeletePressed: () {
|
|
Storage.deleteCollection(selectedCollection);
|
|
setState(() {});
|
|
},
|
|
),
|
|
);
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final collections = Storage.loadCollections();
|
|
final provider = context.watch<SharedLinkProvider>();
|
|
addingNewBookmark = provider.currentMapsLinkMetadata != null;
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: addingNewBookmark ? Text('Choose Collection') : null,
|
|
actions: [
|
|
if (addingNewBookmark)
|
|
TextButton(
|
|
onPressed: () => provider.removeCurrentMapsLink(),
|
|
child: Text('Cancel'),
|
|
),
|
|
],
|
|
),
|
|
floatingActionButton: FloatingActionButton(
|
|
onPressed: onAddButtonPressed,
|
|
child: Icon(Icons.add),
|
|
),
|
|
body: ListView.builder(
|
|
itemBuilder: (context, index) =>
|
|
collectionsListItemBuilder(context, collections.elementAt(index)),
|
|
itemCount: collections.length,
|
|
),
|
|
);
|
|
}
|
|
}
|