From 70aa37f3f514d8dbf2ef2cdbcc3f5217f07421d3 Mon Sep 17 00:00:00 2001 From: Ryan Pandya Date: Thu, 1 Jun 2023 16:08:06 -0400 Subject: [PATCH] Added ability to update categories --- ltx_flutter/lib/appwrite/categories_api.dart | 65 +++++++++ ltx_flutter/lib/pages/categories_page.dart | 139 ++++++++++++------- 2 files changed, 151 insertions(+), 53 deletions(-) diff --git a/ltx_flutter/lib/appwrite/categories_api.dart b/ltx_flutter/lib/appwrite/categories_api.dart index 902a53c..ea17cc0 100644 --- a/ltx_flutter/lib/appwrite/categories_api.dart +++ b/ltx_flutter/lib/appwrite/categories_api.dart @@ -4,6 +4,28 @@ import 'package:ltx_flutter/constants/constants.dart'; import 'package:ltx_flutter/constants/colors.dart'; import 'package:flutter/material.dart'; +class CategoryData { + late String? color; + late num? number; + late num? parent; + late String? name; + late String? description; + + CategoryData({ + this.color, + this.number, + this.parent, + this.name, + this.description, + }) { + color = color; + number = number; + parent = parent; + name = name; + description = description; + } +} + class Category { late final Color backgroundColor; late final Color foregroundColor; @@ -11,8 +33,10 @@ class Category { late final num? parent; late final String name; late final String? description; + late final String hexColor; Category(Document doc) { + hexColor = doc.data['color']; backgroundColor = _getBackgroundColor(doc.data['color']); foregroundColor = _getForegroundColor(doc.data['color']); number = doc.data['number']; @@ -38,6 +62,16 @@ class Category { double leftPadding() { return hasParent() ? 50.0 : 20.0; } + + CategoryData data() { + return CategoryData( + description: description, + color: hexColor, + name: name, + number: number, + parent: parent, + ); + } } class CategoriesAPI extends ChangeNotifier { @@ -72,6 +106,7 @@ class CategoriesAPI extends ChangeNotifier { databaseId: CATEGORIES_DB, collectionId: COLLECTION, queries: [ + Query.limit(100), Query.orderAsc("parent"), Query.orderAsc("number"), ]); @@ -106,6 +141,36 @@ class CategoriesAPI extends ChangeNotifier { } } + Future? updateCategory({ + required String name, + required double number, + required String color, + String? description, + int? parentId, + }) async { + try { + print("Updating '${"category-${number.toString()}"}'"); + var x = await databases.updateDocument( + databaseId: CATEGORIES_DB, + collectionId: COLLECTION, + documentId: "category-${number.toString()}", + data: { + 'name': name, + 'number': number, + 'color': color, + 'parent': parentId, + 'description': description, + }); + print(x); + return x; + } catch (e) { + print(e); + throw "Didn't work"; + } finally { + notifyListeners(); + } + } + Future deleteCategory({required double number}) { return databases.deleteDocument( databaseId: CATEGORIES_DB, diff --git a/ltx_flutter/lib/pages/categories_page.dart b/ltx_flutter/lib/pages/categories_page.dart index 4e6e2ea..e9fc2db 100644 --- a/ltx_flutter/lib/pages/categories_page.dart +++ b/ltx_flutter/lib/pages/categories_page.dart @@ -1,6 +1,5 @@ import 'package:ltx_flutter/appwrite/categories_api.dart'; import 'package:flutter/material.dart'; -import 'package:appwrite/models.dart'; import 'package:provider/provider.dart'; import 'package:flutter_form_builder/flutter_form_builder.dart'; @@ -23,10 +22,10 @@ class _CategoriesPageState extends State { floatingActionButton: FloatingActionButton( onPressed: () => Navigator.push( context, - MaterialPageRoute(builder: (context) => const NewCategoryPage()), - ).then((value) { - setState(() {}); - }), + MaterialPageRoute( + builder: (context) => NewCategoryPage(category: CategoryData()), + ), + ), tooltip: "New Category", child: Icon(Icons.add), ), @@ -40,7 +39,8 @@ class _CategoriesPageState extends State { return Column( children: [ Divider( - thickness: 1, + height: 0, + thickness: 2, ), CategoryRow(category: category), ], @@ -65,52 +65,65 @@ class CategoryRow extends StatelessWidget { @override Widget build(BuildContext context) { - return Row( - children: [ - Container( - color: category.backgroundColor, - child: SizedBox.square( - dimension: 70, - child: Center( - child: Text( - style: TextStyle( - fontWeight: FontWeight.bold, - color: category.foregroundColor, - fontSize: 18, - ), - category.number.toString())), - ), - ), - Expanded( - child: Padding( - padding: EdgeInsets.only(left: category.leftPadding()), - child: Text( - style: TextStyle( - fontSize: 17, - ), - category.name, + return GestureDetector( + onLongPress: () { + Navigator.push( + context, + MaterialPageRoute( + builder: (context) => NewCategoryPage( + category: category.data(), + )), + ); + }, + child: Row( + children: [ + Container( + constraints: BoxConstraints(maxHeight: 80, maxWidth: 70), + color: category?.backgroundColor, + child: SizedBox.expand( + child: Center( + child: Text( + style: TextStyle( + fontWeight: FontWeight.bold, + color: category?.foregroundColor, + fontSize: 18, + ), + category!.number.toString())), ), ), - ), - Expanded( - child: Padding( - padding: const EdgeInsets.all(15.0), - child: Text( - style: TextStyle( - fontSize: 13, - color: Colors.white38, + Expanded( + child: Padding( + padding: EdgeInsets.only(left: category!.leftPadding()), + child: Text( + style: TextStyle( + fontSize: 17, + ), + category!.name, ), - "${category.description}", ), ), - ), - ], + Expanded( + child: Padding( + padding: const EdgeInsets.all(15.0), + child: Text( + style: TextStyle( + fontSize: 13, + color: Colors.white38, + ), + "${category?.description}", + ), + ), + ), + ], + ), ); } } class NewCategoryPage extends StatefulWidget { - const NewCategoryPage({super.key}); + const NewCategoryPage({Key? key, required this.category}) : super(key: key); + + final CategoryData category; @override State createState() => _NewCategoryPageState(); @@ -118,16 +131,22 @@ class NewCategoryPage extends StatefulWidget { class _NewCategoryPageState extends State { String _name = ""; + bool _isNew = true; final _formKey = GlobalKey(); @override void initState() { super.initState(); - _name = "New Category"; + _isNew = true; } @override Widget build(BuildContext context) { + if (widget.category.number != null) { + _isNew = false; + } + _name = _isNew ? "New Category" : "Edit Category"; + final CategoriesAPI api = context.watch(); saveCategory(form, context) { var name = form?.fields['categoryName'].value; @@ -135,20 +154,30 @@ class _NewCategoryPageState extends State { var color = form?.fields['categoryColor'].value; var description = form?.fields['categoryDescription'].value; var parent = form?.fields['categoryParent'].value; - api.addCategory( - name: name, - number: double.parse(number), - color: color, - description: description, - parentId: int.parse(parent), - ); + + if (_isNew) { + api.addCategory( + name: name, + number: double.parse(number), + color: color, + description: description, + parentId: int.parse(parent), + ); + } else { + api.updateCategory( + name: name, + number: double.parse(number), + color: color, + description: description, + parentId: int.parse(parent), + ); + } Navigator.pop(context); - api.getCategories(); } return Scaffold( appBar: AppBar( - title: const Text("New Category"), + title: Text(_name), ), body: Padding( padding: const EdgeInsets.only(left: 50, right: 50), @@ -165,6 +194,7 @@ class _NewCategoryPageState extends State { labelText: "Number", ), name: 'categoryNumber', + initialValue: widget.category.number.toString(), ), SizedBox(height: 25), FormBuilderTextField( @@ -172,15 +202,16 @@ class _NewCategoryPageState extends State { labelText: "Name", ), name: 'categoryName', + initialValue: widget.category.name, ), SizedBox(height: 25), Consumer( builder: (context, categories, child) { - print(categories.colors); return FormBuilderDropdown( decoration: InputDecoration( labelText: "Color", ), + initialValue: widget.category.color, items: [ for (var c in categories.colors) DropdownMenuItem( @@ -203,6 +234,7 @@ class _NewCategoryPageState extends State { SizedBox(height: 25), FormBuilderTextField( name: 'categoryDescription', + initialValue: widget.category.description, decoration: InputDecoration( labelText: "Description", ), @@ -211,6 +243,7 @@ class _NewCategoryPageState extends State { FormBuilderTextField( keyboardType: TextInputType.number, name: 'categoryParent', + initialValue: widget.category.parent.toString(), decoration: InputDecoration( labelText: "Parent", ),