itemSubscribe() async {
/// 1. 등록, 2. 수정, 3. 삭제
itemDao.subscribe(pantry?.objectId, (LiveQueryEvent type, Item item) {
switch (type) {
case LiveQueryEvent.create:
case LiveQueryEvent.enter:
itemList.add(item);
break;
case LiveQueryEvent.update:
var idx = itemList.indexWhere((e) => e.objectId == item.objectId);
itemList[idx] = item;
break;
case LiveQueryEvent.leave:
case LiveQueryEvent.delete:
itemList =
itemList.where((e) => e.objectId != item.objectId).toList();
break;
default:
}
itemClassify();
});
}
subscribe(
String? pantryId,
Function(LiveQueryEvent, Item) callback,
) async {
if (_liveQuery == null) _liveQuery = LiveQuery();
unsubscribe();
var query = QueryBuilder<Item>(Item())
..whereEqualTo(Item.keyPantryId, pantryId);
_subscription = await _liveQuery?.client.subscribe(query);
subscribeOn(_subscription, callback);
}
subscribeOn(Subscription? subscription, Function callback) {
if (subscription == null) return;
subscription.on(LiveQueryEvent.create, (value) {
print('*** CREATE ***: ${DateTime.now().toString()}\n $value ');
callback(LiveQueryEvent.create, value);
});
subscription.on(LiveQueryEvent.enter, (value) {
print('*** ENTER ***: ${DateTime.now().toString()}\n $value ');
callback(LiveQueryEvent.enter, value);
});
subscription.on(LiveQueryEvent.update, (value) {
print('*** UPDATE ***: ${DateTime.now().toString()}\n $value ');
callback(LiveQueryEvent.update, value);
});
subscription.on(LiveQueryEvent.leave, (value) {
print('*** LEAVE ***: ${DateTime.now().toString()}\n $value ');
callback(LiveQueryEvent.leave, value);
});
subscription.on(LiveQueryEvent.delete, (value) {
print('*** DELETE ***: ${DateTime.now().toString()}\n $value ');
callback(LiveQueryEvent.delete, value);
});
}