diff --git a/lib/pages/dashboard_page.dart b/lib/pages/dashboard_page.dart index 0578b5a..a1ee652 100644 --- a/lib/pages/dashboard_page.dart +++ b/lib/pages/dashboard_page.dart @@ -7,6 +7,7 @@ import 'package:provider/provider.dart'; class DashboardPage extends StatefulWidget { const DashboardPage({super.key}); + static const routeName = '/dashboard'; @override @@ -15,40 +16,29 @@ class DashboardPage extends StatefulWidget { class _DashboardPageState extends State { final Future> checklistFuture = DbHelper.fetchChecklist; + late List checklists; late ChecklistProvider checklistProvider; - @override - void initState() { - super.initState(); - } + int? _selectedChecklistIndex; - @override - Widget build(BuildContext context) { - checklistProvider = Provider.of(context, listen: true); - return Scaffold( - appBar: AppBar( - title: const Text('Brießenchecker9000'), - actions: [ - IconButton( - onPressed: () => DbHelper.logout(), - icon: const Icon(Icons.logout), - ) - ], - ), - body: FutureBuilder( - future: checklistFuture, - builder: _futureBuilder, - ), - floatingActionButton: FloatingActionButton( + FloatingActionButton get _fabBuilder { + if (_selectedChecklistIndex == null) { + return FloatingActionButton( onPressed: _onAddTapped, child: const Icon(Icons.add), - ), + ); + } + return FloatingActionButton( + onPressed: _onDeleteTapped, + backgroundColor: Theme.of(context).colorScheme.error, + child: Icon(Icons.delete, color: Theme.of(context).colorScheme.onError), ); } Widget _futureBuilder( BuildContext context, AsyncSnapshot> snapshot) { if (snapshot.hasData) { + checklists = snapshot.data!; return ListView.builder( itemCount: snapshot.data!.length, itemBuilder: (context, index) => @@ -66,7 +56,9 @@ class _DashboardPageState extends State { return ListTile( title: Text(cl.title == '' ? 'Unnamed ${cl.id}' : cl.title), subtitle: Text(cl.description), - onTap: () => _onListEntryTapped(cl), + onTap: () => _onListEntryTapped(cl, index), + onLongPress: () => _onLongPress(index), + selected: _selectedChecklistIndex == index, ); } @@ -77,8 +69,45 @@ class _DashboardPageState extends State { () => Navigator.of(context).pushNamed(DetailChecklistPage.routeName)); } - void _onListEntryTapped(Checklist cl) { - checklistProvider.updateSelectedChecklist(cl.id, silent: true); - Navigator.of(context).pushNamed(DetailChecklistPage.routeName); + void _onListEntryTapped(Checklist cl, int index) { + if (_selectedChecklistIndex == index) { + setState(() => _selectedChecklistIndex = null); + } else if (_selectedChecklistIndex != null) { + setState(() => _selectedChecklistIndex = index); + } else { + checklistProvider.updateSelectedChecklist(cl.id, silent: true); + Navigator.of(context).pushNamed(DetailChecklistPage.routeName); + } + } + + void _onLongPress(int index) { + setState(() { + _selectedChecklistIndex = index; + }); + } + + void _onDeleteTapped() { + DbHelper.deleteChecklistByid( + checklists.elementAt(_selectedChecklistIndex!).id); + } + + @override + Widget build(BuildContext context) { + checklistProvider = Provider.of(context, listen: true); + return Scaffold( + appBar: AppBar( + title: const Text('Brießenchecker9000'), + actions: [ + IconButton( + onPressed: () => DbHelper.logout(), + icon: const Icon(Icons.logout), + ) + ], + ), + body: FutureBuilder( + future: checklistFuture, + builder: _futureBuilder, + ), + floatingActionButton: _fabBuilder); } } diff --git a/lib/services/dbhelper.dart b/lib/services/dbhelper.dart index 2d85c79..28eba0c 100644 --- a/lib/services/dbhelper.dart +++ b/lib/services/dbhelper.dart @@ -148,6 +148,11 @@ class DbHelper { await _client.from(itemsTableName).delete().in_('id', ids); } + static Future deleteChecklistByid(int id) async { + await _client.from(itemsTableName).delete().eq('checklist_id', id); + await _client.from(checklistsTableName).delete().eq('id', id); + } + static Stream get authChangeEventStream => _client.auth.onAuthStateChange; }