delete checklists

This commit is contained in:
marcoabat
2023-08-06 23:55:18 +02:00
parent af8b08c120
commit 69e94d323a
2 changed files with 61 additions and 27 deletions

View File

@@ -7,6 +7,7 @@ import 'package:provider/provider.dart';
class DashboardPage extends StatefulWidget { class DashboardPage extends StatefulWidget {
const DashboardPage({super.key}); const DashboardPage({super.key});
static const routeName = '/dashboard'; static const routeName = '/dashboard';
@override @override
@@ -15,11 +16,79 @@ class DashboardPage extends StatefulWidget {
class _DashboardPageState extends State<DashboardPage> { class _DashboardPageState extends State<DashboardPage> {
final Future<List<Checklist>> checklistFuture = DbHelper.fetchChecklist; final Future<List<Checklist>> checklistFuture = DbHelper.fetchChecklist;
late List<Checklist> checklists;
late ChecklistProvider checklistProvider; late ChecklistProvider checklistProvider;
@override int? _selectedChecklistIndex;
void initState() {
super.initState(); 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<List<Checklist>> snapshot) {
if (snapshot.hasData) {
checklists = snapshot.data!;
return ListView.builder(
itemCount: snapshot.data!.length,
itemBuilder: (context, index) =>
_listBuilder(context, index, snapshot.data!),
);
} else if (snapshot.hasError) {
return Text(snapshot.error.toString());
} else {
return const CircularProgressIndicator();
}
}
Widget? _listBuilder(BuildContext context, int index, List<Checklist> list) {
Checklist cl = list.elementAt(index);
return ListTile(
title: Text(cl.title == '' ? 'Unnamed ${cl.id}' : cl.title),
subtitle: Text(cl.description),
onTap: () => _onListEntryTapped(cl, index),
onLongPress: () => _onLongPress(index),
selected: _selectedChecklistIndex == index,
);
}
void _onAddTapped() {
DbHelper.addOrUpdateChecklist(null).then((id) {
checklistProvider.updateSelectedChecklist(id, silent: true);
}).whenComplete(
() => 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 @override
@@ -39,46 +108,6 @@ class _DashboardPageState extends State<DashboardPage> {
future: checklistFuture, future: checklistFuture,
builder: _futureBuilder, builder: _futureBuilder,
), ),
floatingActionButton: FloatingActionButton( floatingActionButton: _fabBuilder);
onPressed: _onAddTapped,
child: const Icon(Icons.add),
),
);
}
Widget _futureBuilder(
BuildContext context, AsyncSnapshot<List<Checklist>> snapshot) {
if (snapshot.hasData) {
return ListView.builder(
itemCount: snapshot.data!.length,
itemBuilder: (context, index) =>
_listBuilder(context, index, snapshot.data!),
);
} else if (snapshot.hasError) {
return Text(snapshot.error.toString());
} else {
return const CircularProgressIndicator();
}
}
Widget? _listBuilder(BuildContext context, int index, List<Checklist> list) {
Checklist cl = list.elementAt(index);
return ListTile(
title: Text(cl.title == '' ? 'Unnamed ${cl.id}' : cl.title),
subtitle: Text(cl.description),
onTap: () => _onListEntryTapped(cl),
);
}
void _onAddTapped() {
DbHelper.addOrUpdateChecklist(null).then((id) {
checklistProvider.updateSelectedChecklist(id, silent: true);
}).whenComplete(
() => Navigator.of(context).pushNamed(DetailChecklistPage.routeName));
}
void _onListEntryTapped(Checklist cl) {
checklistProvider.updateSelectedChecklist(cl.id, silent: true);
Navigator.of(context).pushNamed(DetailChecklistPage.routeName);
} }
} }

View File

@@ -148,6 +148,11 @@ class DbHelper {
await _client.from(itemsTableName).delete().in_('id', ids); await _client.from(itemsTableName).delete().in_('id', ids);
} }
static Future<void> deleteChecklistByid(int id) async {
await _client.from(itemsTableName).delete().eq('checklist_id', id);
await _client.from(checklistsTableName).delete().eq('id', id);
}
static Stream<AuthState> get authChangeEventStream => static Stream<AuthState> get authChangeEventStream =>
_client.auth.onAuthStateChange; _client.auth.onAuthStateChange;
} }