116 lines
2.9 KiB
Dart
116 lines
2.9 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 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;
|
|
|
|
Category(Document doc) {
|
|
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;
|
|
}
|
|
}
|
|
|
|
class CategoriesAPI extends ChangeNotifier {
|
|
Client client = Client();
|
|
late final Account account;
|
|
late final Databases databases;
|
|
late List<Document> _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.orderAsc("parent"),
|
|
Query.orderAsc("number"),
|
|
]);
|
|
_categories = response.documents;
|
|
notifyListeners();
|
|
}
|
|
|
|
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<dynamic> deleteCategory({required double number}) {
|
|
return databases.deleteDocument(
|
|
databaseId: CATEGORIES_DB,
|
|
collectionId: COLLECTION,
|
|
documentId: "category-${number.toString()}");
|
|
}
|
|
}
|