new list based on template

This commit is contained in:
andrei 2021-11-04 11:25:42 -04:00
parent 02fa1f75b3
commit 9bc0310205
2 changed files with 36 additions and 10 deletions

View File

@ -84,11 +84,9 @@ class _AddFormState extends State<AddForm> {
DropdownButtonFormField<int>( DropdownButtonFormField<int>(
focusNode: _TemplateFocusNode, focusNode: _TemplateFocusNode,
value: _templatChoice, value: _templatChoice,
onChanged: (value) { onChanged: (value) => setState(() {
setState() { _templatChoice = value!;
_templatChoice = (value != null) ? value : -1; }),
}
},
decoration: decoration:
const InputDecoration(labelText: "Starting Template"), const InputDecoration(labelText: "Starting Template"),
items: generateDropdown(), items: generateDropdown(),
@ -101,11 +99,16 @@ class _AddFormState extends State<AddForm> {
child: Icon(Icons.save_alt), child: Icon(Icons.save_alt),
onPressed: () async { onPressed: () async {
if (_formKey.currentState!.validate()) { if (_formKey.currentState!.validate()) {
DBHelper.dbHelper.insertList(data.List( int id = await DBHelper.dbHelper.insertList(
_listLabel, data.List(
isTemplate: type.index == 1, _listLabel,
)); isTemplate: type.index == 1,
// TODO implement template starting point // 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 Navigator.pop(context); // TODO replace route with checklist page
} }
}, },

View File

@ -80,9 +80,32 @@ class DBHelper {
Future<int> deleteList(data.List l) async { Future<int> deleteList(data.List l) async {
Database db = await database; Database db = await database;
db.delete("Item", where: 'list_id = ?', whereArgs: [l.id]);
return db.delete("List", where: 'id = ?', whereArgs: [l.id]); return db.delete("List", where: 'id = ?', whereArgs: [l.id]);
} }
Future<void> 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<int> insertItem(data.Check item) async { Future<int> insertItem(data.Check item) async {
Database db = await database; Database db = await database;