ltx-flutter/ltx_flutter/lib/appwrite/categories_api.dart

199 lines
4.6 KiB
Dart

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.2
? Colors.black
: Colors.white;
}
bool hasParent() {
return parent != number;
}
double leftPadding() {
return hasParent() ? 30.0 : 15.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<Document> _categories = [];
bool _ready = false;
// 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;
get ready => _ready;
lookUp(n) {
if (!_ready) {
return false;
}
try{
return Category(_categories.singleWhere((e) {
return e.data['number'] == double.parse(n.toString());
}));
}
catch(e){
return Category(_categories.singleWhere((e) {
return e.data['number'] == -9;
}));
}
}
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();
_ready = true;
}
Future<Document>? 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<Document>? 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,
});
return x;
} catch (e) {
print(e);
throw "Didn't work";
} finally {
notifyListeners();
}
}
Future<dynamic> deleteCategory({required double number}) {
return databases.deleteDocument(
databaseId: CATEGORIES_DB,
collectionId: COLLECTION,
documentId: "category-${number.toString()}");
}
}