Files
fcs/lib/pages/fcs_shipment/model/fcs_shipment_model.dart
2020-10-12 03:34:05 +06:30

64 lines
1.7 KiB
Dart

import 'dart:async';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:fcs/data/services/services.dart';
import 'package:fcs/domain/constants.dart';
import 'package:fcs/domain/entities/fcs_shipment.dart';
import 'package:fcs/pages/main/model/base_model.dart';
import 'package:logging/logging.dart';
class FcsShipmentModel extends BaseModel {
final log = Logger('FcsShipmentModel');
StreamSubscription<QuerySnapshot> listener;
List<FcsShipment> fcsShipments = [];
@override
void privilegeChanged() {
super.privilegeChanged();
_loadFcsShipments();
}
Future<void> _loadFcsShipments() async {
if (user == null || !user.hasFcsShipments()) return;
String path = "/$fcs_shipment_collection/";
if (listener != null) listener.cancel();
fcsShipments = [];
try {
listener = Firestore.instance
.collection("$path")
.orderBy("shipment_number", descending: true)
.snapshots()
.listen((QuerySnapshot snapshot) {
fcsShipments.clear();
fcsShipments = snapshot.documents.map((documentSnapshot) {
var s = FcsShipment.fromMap(
documentSnapshot.data, documentSnapshot.documentID);
return s;
}).toList();
notifyListeners();
});
} catch (e) {
log.warning("Error!! $e");
}
}
void initUser(user) {
super.initUser(user);
}
@override
logout() async {
if (listener != null) await listener.cancel();
fcsShipments = [];
}
Future<void> create(FcsShipment fcsShipment) {
return Services.instance.fcsShipmentService.createFcsShipment(fcsShipment);
}
Future<void> update(FcsShipment fcsShipment) {
return Services.instance.fcsShipmentService.updateFcsShipment(fcsShipment);
}
}