Realtime updating works!
This commit is contained in:
@@ -50,7 +50,7 @@ class Category {
|
||||
}
|
||||
|
||||
Color _getForegroundColor(String colorStr) {
|
||||
return Color(int.parse("0xff$colorStr")).computeLuminance() > 0.5
|
||||
return Color(int.parse("0xff$colorStr")).computeLuminance() > 0.2
|
||||
? Colors.black
|
||||
: Colors.white;
|
||||
}
|
||||
@@ -99,7 +99,6 @@ class CategoriesAPI extends ChangeNotifier {
|
||||
if (!_ready) {
|
||||
return false;
|
||||
}
|
||||
print(_ready);
|
||||
return Category(_categories.singleWhere((e) {
|
||||
return e.data['number'] == double.parse(n.toString());
|
||||
}));
|
||||
@@ -174,7 +173,6 @@ class CategoriesAPI extends ChangeNotifier {
|
||||
'parent': parentId,
|
||||
'description': description,
|
||||
});
|
||||
print(x);
|
||||
return x;
|
||||
} catch (e) {
|
||||
print(e);
|
||||
|
||||
@@ -4,11 +4,13 @@ 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';
|
||||
import 'package:ltx_flutter/constants/constants.dart';
|
||||
|
||||
class DatabaseAPI extends ChangeNotifier {
|
||||
Client client = Client();
|
||||
late final Account account;
|
||||
late final Databases databases;
|
||||
late final Realtime realtime;
|
||||
final AuthAPI auth = AuthAPI();
|
||||
|
||||
late List<Document> _entries = [];
|
||||
@@ -26,15 +28,52 @@ class DatabaseAPI extends ChangeNotifier {
|
||||
DatabaseAPI() {
|
||||
init();
|
||||
getAll();
|
||||
subscribeRealtime();
|
||||
}
|
||||
|
||||
init() {
|
||||
client
|
||||
.setEndpoint(APPWRITE_URL)
|
||||
.setProject(APPWRITE_PROJECT_ID)
|
||||
.setSelfSigned();
|
||||
client.setEndpoint(APPWRITE_URL).setProject(APPWRITE_PROJECT_ID);
|
||||
account = Account(client);
|
||||
databases = Databases(client);
|
||||
realtime = Realtime(client);
|
||||
}
|
||||
|
||||
elDate(dynamic el) {
|
||||
return formatter.format(DateTime.parse(el.data['date']));
|
||||
}
|
||||
|
||||
sortByDate(entries) {
|
||||
return entries.sort(
|
||||
(a, b) => a.$id.compareTo(b.$id),
|
||||
);
|
||||
}
|
||||
|
||||
subscribeRealtime() {
|
||||
final subscription = realtime.subscribe(
|
||||
['databases.$APPWRITE_DATABASE_ID.collections.$COLLECTION.documents']);
|
||||
|
||||
print("Subscribed to realtime");
|
||||
subscription.stream.listen((response) {
|
||||
String dateISO =
|
||||
formatter.format(DateTime.parse(response.payload['date']));
|
||||
if (response.events.contains(
|
||||
'databases.$APPWRITE_DATABASE_ID.collections.$COLLECTION.documents.*.update')) {
|
||||
// Entry was updated
|
||||
|
||||
int entryIndex =
|
||||
_entries.indexWhere((element) => elDate(element) == dateISO);
|
||||
Document newEntry = _entries[entryIndex];
|
||||
newEntry.data['hours'] = response.payload['hours'];
|
||||
newEntry.data['mood'] = response.payload['mood'];
|
||||
newEntry.data['comments'] = response.payload['comments'];
|
||||
|
||||
_entries.removeAt(entryIndex);
|
||||
_entries.add(newEntry);
|
||||
notifyListeners();
|
||||
// _entries.add(entry);
|
||||
// sortByDate(_entries);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
int total() {
|
||||
@@ -81,9 +120,7 @@ class DatabaseAPI extends ChangeNotifier {
|
||||
);
|
||||
|
||||
_entries.add(response.first);
|
||||
_entries.sort(
|
||||
(a, b) => a.$id.compareTo(b.$id),
|
||||
);
|
||||
|
||||
notifyListeners();
|
||||
return response.first;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user