Got a first draft of TodayPage working!
This commit is contained in:
@@ -79,6 +79,7 @@ class CategoriesAPI extends ChangeNotifier {
|
||||
late final Account account;
|
||||
late final Databases databases;
|
||||
late List<Document> _categories = [];
|
||||
bool _ready = false;
|
||||
|
||||
// Constructor
|
||||
CategoriesAPI() {
|
||||
@@ -92,6 +93,17 @@ class CategoriesAPI extends ChangeNotifier {
|
||||
get isEmpty => _categories.isEmpty;
|
||||
get(n) => Category(_categories[n]);
|
||||
get colors => CategoryColor.values;
|
||||
get ready => _ready;
|
||||
|
||||
lookUp(n) {
|
||||
if (!_ready) {
|
||||
return false;
|
||||
}
|
||||
print(_ready);
|
||||
return Category(_categories.singleWhere((e) {
|
||||
return e.data['number'] == double.parse(n.toString());
|
||||
}));
|
||||
}
|
||||
|
||||
init() {
|
||||
client.setEndpoint(APPWRITE_URL).setProject(APPWRITE_PROJECT_ID);
|
||||
@@ -112,6 +124,7 @@ class CategoriesAPI extends ChangeNotifier {
|
||||
]);
|
||||
_categories = response.documents;
|
||||
notifyListeners();
|
||||
_ready = true;
|
||||
}
|
||||
|
||||
Future<Document>? addCategory({
|
||||
|
||||
@@ -12,17 +12,20 @@ class DatabaseAPI extends ChangeNotifier {
|
||||
final AuthAPI auth = AuthAPI();
|
||||
|
||||
late List<Document> _entries = [];
|
||||
bool _ready = false;
|
||||
|
||||
// Getter methods
|
||||
List<Document> get entries => _entries;
|
||||
int? get total => _entries.length;
|
||||
int get length => _entries.length;
|
||||
bool get ready => _ready;
|
||||
double get progress => length / total();
|
||||
|
||||
final DateFormat formatter = DateFormat('yyyy-MM-dd');
|
||||
|
||||
// Constructor
|
||||
DatabaseAPI() {
|
||||
init();
|
||||
getEntries();
|
||||
getAll();
|
||||
}
|
||||
|
||||
init() {
|
||||
@@ -34,28 +37,67 @@ class DatabaseAPI extends ChangeNotifier {
|
||||
databases = Databases(client);
|
||||
}
|
||||
|
||||
getEntries({int limit = 100, String dateISO = ""}) async {
|
||||
if (dateISO == "") {
|
||||
dateISO = DateTime.now().toIso8601String();
|
||||
}
|
||||
int total() {
|
||||
String dateISO = DateTime.now().toIso8601String();
|
||||
|
||||
var referenceDate = DateTime.parse("2023-01-01");
|
||||
|
||||
final date = DateTime.parse(dateISO);
|
||||
final offset = date.difference(referenceDate).inDays;
|
||||
return date.difference(referenceDate).inDays;
|
||||
}
|
||||
|
||||
print("Getting $limit entries starting from $offset");
|
||||
getAll() async {
|
||||
String paginationQuery = Query.offset(0);
|
||||
int max = total();
|
||||
|
||||
while (_entries.length < max) {
|
||||
int remainder = max - _entries.length;
|
||||
int limit = remainder > 100 ? 100 : remainder;
|
||||
|
||||
if (_entries.isNotEmpty) {
|
||||
String lastDate = _entries.last.$id;
|
||||
paginationQuery = Query.cursorAfter(lastDate);
|
||||
}
|
||||
|
||||
List<Document> newEntries = await getEntries(
|
||||
limit: limit,
|
||||
paginationQuery: paginationQuery,
|
||||
);
|
||||
|
||||
_entries.addAll(newEntries);
|
||||
notifyListeners();
|
||||
}
|
||||
_ready = true;
|
||||
}
|
||||
|
||||
getOne({required String date}) async {
|
||||
int offset =
|
||||
DateTime.parse(date).difference(DateTime.parse("2023-01-01")).inDays +
|
||||
1;
|
||||
|
||||
List<Document> response = await getEntries(
|
||||
limit: 1,
|
||||
paginationQuery: Query.offset(offset),
|
||||
);
|
||||
|
||||
_entries.add(response.first);
|
||||
_entries.sort(
|
||||
(a, b) => a.$id.compareTo(b.$id),
|
||||
);
|
||||
notifyListeners();
|
||||
return response.first;
|
||||
}
|
||||
|
||||
getEntries({int limit = 100, paginationQuery}) async {
|
||||
var response = await databases.listDocuments(
|
||||
databaseId: APPWRITE_DATABASE_ID,
|
||||
collectionId: COLLECTION,
|
||||
queries: [
|
||||
Query.orderAsc("date"),
|
||||
Query.offset(offset),
|
||||
paginationQuery,
|
||||
Query.limit(limit),
|
||||
]);
|
||||
_entries = response.documents;
|
||||
notifyListeners();
|
||||
return response.documents;
|
||||
}
|
||||
|
||||
Future<Document> addEntry(
|
||||
|
||||
Reference in New Issue
Block a user