Categories page works!
This commit is contained in:
@@ -1,30 +1,63 @@
|
||||
import 'package:appwrite/appwrite.dart';
|
||||
import 'package:appwrite/models.dart';
|
||||
import 'package:ltx_flutter/constants/constants.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:ltx_flutter/appwrite/appwrite.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 final String userId;
|
||||
final AuthAPI auth = AuthAPI();
|
||||
late List<Document> _categories = [];
|
||||
|
||||
// Constructor
|
||||
CategoriesAPI() {
|
||||
init();
|
||||
getCategories();
|
||||
}
|
||||
|
||||
// loadUser() async {
|
||||
// try {
|
||||
// user = await account.get();
|
||||
// notifyListeners();
|
||||
// } catch (e) {
|
||||
// print(e);
|
||||
// notifyListeners();
|
||||
// }
|
||||
// }
|
||||
// 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);
|
||||
@@ -32,34 +65,45 @@ class CategoriesAPI extends ChangeNotifier {
|
||||
databases = Databases(client);
|
||||
}
|
||||
|
||||
Future<DocumentList> getCategories() {
|
||||
print("Getting categories");
|
||||
return databases.listDocuments(
|
||||
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({
|
||||
Future<Document>? addCategory({
|
||||
required String name,
|
||||
required double number,
|
||||
required String color,
|
||||
String? description,
|
||||
int? parentId,
|
||||
}) {
|
||||
return databases.createDocument(
|
||||
databaseId: CATEGORIES_DB,
|
||||
collectionId: COLLECTION,
|
||||
documentId: "category-${number.toString()}",
|
||||
data: {
|
||||
'name': name,
|
||||
'number': number,
|
||||
'color': color,
|
||||
'parent': parentId,
|
||||
'description': description,
|
||||
});
|
||||
}) 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}) {
|
||||
|
||||
@@ -1,19 +1,28 @@
|
||||
import 'package:appwrite/appwrite.dart';
|
||||
import 'package:appwrite/models.dart';
|
||||
import 'package:ltx_flutter/appwrite/appwrite.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:ltx_flutter/appwrite/auth_api.dart';
|
||||
import 'package:ltx_flutter/constants/constants.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
class DatabaseAPI {
|
||||
class DatabaseAPI extends ChangeNotifier {
|
||||
Client client = Client();
|
||||
late final Account account;
|
||||
late final Databases databases;
|
||||
final AuthAPI auth = AuthAPI();
|
||||
|
||||
late List<Document> _entries = [];
|
||||
|
||||
// Getter methods
|
||||
List<Document> get entries => _entries;
|
||||
int? get total => _entries.length;
|
||||
|
||||
final DateFormat formatter = DateFormat('yyyy-MM-dd');
|
||||
|
||||
// Constructor
|
||||
DatabaseAPI() {
|
||||
init();
|
||||
getEntries();
|
||||
}
|
||||
|
||||
init() {
|
||||
@@ -25,7 +34,7 @@ class DatabaseAPI {
|
||||
databases = Databases(client);
|
||||
}
|
||||
|
||||
Future<DocumentList> getEntries({int limit = 100, String dateISO = ""}) {
|
||||
getEntries({int limit = 100, String dateISO = ""}) async {
|
||||
if (dateISO == "") {
|
||||
dateISO = DateTime.now().toIso8601String();
|
||||
}
|
||||
@@ -37,7 +46,7 @@ class DatabaseAPI {
|
||||
|
||||
print("Getting $limit entries starting from $offset");
|
||||
|
||||
return databases.listDocuments(
|
||||
var response = await databases.listDocuments(
|
||||
databaseId: APPWRITE_DATABASE_ID,
|
||||
collectionId: COLLECTION,
|
||||
queries: [
|
||||
@@ -45,6 +54,8 @@ class DatabaseAPI {
|
||||
Query.offset(offset),
|
||||
Query.limit(limit),
|
||||
]);
|
||||
_entries = response.documents;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<Document> addEntry(
|
||||
|
||||
Reference in New Issue
Block a user