From 9bc03102059890e22e2f25f3d832a0823f106e8e Mon Sep 17 00:00:00 2001 From: andrei Date: Thu, 4 Nov 2021 11:25:42 -0400 Subject: [PATCH] new list based on template --- lib/add_form.dart | 23 +++++++++++++---------- lib/db_helper.dart | 23 +++++++++++++++++++++++ 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/lib/add_form.dart b/lib/add_form.dart index fe98f84..eeaaeb7 100644 --- a/lib/add_form.dart +++ b/lib/add_form.dart @@ -84,11 +84,9 @@ class _AddFormState extends State { DropdownButtonFormField( focusNode: _TemplateFocusNode, value: _templatChoice, - onChanged: (value) { - setState() { - _templatChoice = (value != null) ? value : -1; - } - }, + onChanged: (value) => setState(() { + _templatChoice = value!; + }), decoration: const InputDecoration(labelText: "Starting Template"), items: generateDropdown(), @@ -101,11 +99,16 @@ class _AddFormState extends State { child: Icon(Icons.save_alt), onPressed: () async { if (_formKey.currentState!.validate()) { - DBHelper.dbHelper.insertList(data.List( - _listLabel, - isTemplate: type.index == 1, - )); - // TODO implement template starting point + int id = await DBHelper.dbHelper.insertList( + data.List( + _listLabel, + isTemplate: type.index == 1, + // TODO Add description to lists + ), + ); + if (_templatChoice >= 0 && _templatChoice < templates.length) { + DBHelper.dbHelper.copyList(templates[_templatChoice].id!, id); + } Navigator.pop(context); // TODO replace route with checklist page } }, diff --git a/lib/db_helper.dart b/lib/db_helper.dart index 16edee7..e53cf57 100644 --- a/lib/db_helper.dart +++ b/lib/db_helper.dart @@ -80,9 +80,32 @@ class DBHelper { Future deleteList(data.List l) async { Database db = await database; + db.delete("Item", where: 'list_id = ?', whereArgs: [l.id]); return db.delete("List", where: 'id = ?', whereArgs: [l.id]); } + Future copyList(int oldID, int newID) async { + Database db = await database; + print('$newID, $oldID'); + + var batch = db.batch(); + db.execute(""" + CREATE TEMPORARY TABLE list_copy AS + SELECT check_text, status, list_id + FROM Item + WHERE list_id = ? + """, [oldID]); + db.update("list_copy", {'list_id': newID}); + db.rawInsert(""" + INSERT INTO Item(check_text, status, list_id) + SELECT * FROM list_copy + """); + db.execute(""" + DROP TABLE list_copy + """); + batch.commit(); + } + Future insertItem(data.Check item) async { Database db = await database;