import 'package:appwrite/appwrite.dart'; import 'package:appwrite/models.dart'; 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; late final num number; 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']; parent = doc.data['parent']; name = doc.data['name']; description = doc.data['description']; } Color _getBackgroundColor(String colorStr) { return Color(int.parse("0xff$colorStr")); } Color _getForegroundColor(String colorStr) { return Color(int.parse("0xff$colorStr")).computeLuminance() > 0.5 ? Colors.black : Colors.white; } bool hasParent() { return parent == number ? false : true; } 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 { Client client = Client(); late final Account account; late final Databases databases; late List _categories = []; // Constructor CategoriesAPI() { init(); getCategories(); } // Getters get total => _categories.length; get all => _categories; get isEmpty => _categories.isEmpty; get(n) => Category(_categories[n]); get colors => CategoryColor.values; init() { client.setEndpoint(APPWRITE_URL).setProject(APPWRITE_PROJECT_ID); account = Account(client); databases = Databases(client); } getCategories() async { print("Updating categories."); _categories = []; var response = await databases.listDocuments( databaseId: CATEGORIES_DB, collectionId: COLLECTION, queries: [ Query.limit(100), Query.orderAsc("parent"), Query.orderAsc("number"), ]); _categories = response.documents; notifyListeners(); } Future? addCategory({ required String name, required double number, required String color, String? description, int? parentId, }) async { try { return await databases.createDocument( databaseId: CATEGORIES_DB, collectionId: COLLECTION, documentId: "category-${number.toString()}", data: { 'name': name, 'number': number, 'color': color, 'parent': parentId, 'description': description, }); } catch (e) { print(e); throw "Didn't work"; } finally { notifyListeners(); } } 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, collectionId: COLLECTION, documentId: "category-${number.toString()}"); } }