diff --git a/lib/pages/dashboard_page.dart b/lib/pages/dashboard_page.dart index e450783..e599436 100644 --- a/lib/pages/dashboard_page.dart +++ b/lib/pages/dashboard_page.dart @@ -51,6 +51,8 @@ class _DashboardPageState extends State { itemBuilder: (context, index) => _listBuilder(context, index, snapshot.data!), ); + } else if (snapshot.hasError) { + return Text(snapshot.error.toString()); } else { return const CircularProgressIndicator(); } @@ -59,13 +61,13 @@ class _DashboardPageState extends State { Widget? _listBuilder(BuildContext context, int index, List list) { Checklist cl = list.elementAt(index); return ListTile( - title: Text(cl.title), + title: Text(cl.title == '' ? 'Unnamed ${cl.id}' : cl.title), subtitle: Text(cl.description), ); } void _onAddTapped() { - DbHelper.addChecklist().then((id) { + DbHelper.addOrUpdateChecklist(null).then((id) { checklistProvider.updateSelectedChecklist(id, silent: true); }); Navigator.of(context).pushNamed(DetailChecklistPage.routeName); diff --git a/lib/services/dbhelper.dart b/lib/services/dbhelper.dart index e71df8d..1d2e5f3 100644 --- a/lib/services/dbhelper.dart +++ b/lib/services/dbhelper.dart @@ -6,6 +6,9 @@ import '../models/listitem.dart'; class DbHelper { static late final SupabaseClient _client; + static const checklistsTableName = 'checklists'; + static const itemsTableName = 'items'; + static const checkedItemsTableName = 'checkedItems'; static Future init() async { await Supabase.initialize( @@ -28,15 +31,45 @@ class DbHelper { } static Future> get fetchChecklist async { - //TODO replace example data - await Future.delayed(const Duration(seconds: 2)); - return ed.checklists; + List checklists = []; + final res = await _client + .from(checklistsTableName) + .select>>(); + for (final element in res) { + Checklist cl = Checklist( + element['id'], + element['ownerId'], + element['title'], + element['description'], + DateTime.parse(element['createdTime']), + [], + ); + checklists.add(cl); + } + return checklists; } /// returns id of newly created checklist - static Future addChecklist() async { - //TODO Add checklist - return 0; + static Future addOrUpdateChecklist(Checklist? checklist) async { + final ownerId = _client.auth.currentSession!.user.id; + + Map upsertMap = { + 'ownerId': ownerId, + }; + if (checklist != null) { + List> entries = [ + MapEntry('id', checklist.id), + MapEntry('title', checklist.title), + MapEntry('description', checklist.description), + ]; + upsertMap.addEntries(entries); + } + + final res = await _client + .from('checklists') + .upsert(upsertMap) + .select>>('id'); + return res.last['id'] as int; } static Future getChecklistById(int id) async {