Files
fcs/lib/pages/fcs_shipment/model/fcs_shipment_model.dart

112 lines
3.1 KiB
Dart
Raw Normal View History

2020-10-08 03:32:52 +06:30
import 'dart:async';
import 'package:cloud_firestore/cloud_firestore.dart';
2020-10-07 14:42:07 +06:30
import 'package:fcs/data/services/services.dart';
2020-10-08 03:32:52 +06:30
import 'package:fcs/domain/constants.dart';
2020-10-07 14:42:07 +06:30
import 'package:fcs/domain/entities/fcs_shipment.dart';
2020-10-15 19:40:03 +06:30
import 'package:fcs/helpers/paginator.dart';
2020-10-07 02:33:06 +06:30
import 'package:fcs/pages/main/model/base_model.dart';
2020-10-08 03:32:52 +06:30
import 'package:logging/logging.dart';
2020-06-01 14:24:45 +06:30
2020-10-07 02:33:06 +06:30
class FcsShipmentModel extends BaseModel {
2020-10-08 03:32:52 +06:30
final log = Logger('FcsShipmentModel');
2020-06-01 14:24:45 +06:30
2020-10-08 03:32:52 +06:30
StreamSubscription<QuerySnapshot> listener;
2020-10-15 19:40:03 +06:30
List<FcsShipment> _fcsShipments = [];
List<FcsShipment> get fcsShipments => _selectedIndex == 1
? _fcsShipments
: List<FcsShipment>.from(_delivered.values);
Paginator _delivered;
bool isLoading = false;
int _selectedIndex = 1;
set selectedIndex(int index) {
_selectedIndex = index;
notifyListeners();
}
get selectedIndex => _selectedIndex;
2020-06-01 14:24:45 +06:30
2020-10-08 03:32:52 +06:30
@override
void privilegeChanged() {
super.privilegeChanged();
_loadFcsShipments();
2020-10-15 19:40:03 +06:30
if (_delivered != null) _delivered.close();
_delivered = _getDelivered();
2020-06-01 14:24:45 +06:30
}
2020-10-08 03:32:52 +06:30
Future<void> _loadFcsShipments() async {
2020-10-12 03:34:05 +06:30
if (user == null || !user.hasFcsShipments()) return;
2020-10-08 03:32:52 +06:30
String path = "/$fcs_shipment_collection/";
if (listener != null) listener.cancel();
2020-10-15 19:40:03 +06:30
_fcsShipments = [];
2020-10-08 03:32:52 +06:30
try {
listener = Firestore.instance
.collection("$path")
2020-10-08 11:38:05 +06:30
.orderBy("shipment_number", descending: true)
2020-10-08 03:32:52 +06:30
.snapshots()
.listen((QuerySnapshot snapshot) {
2020-10-15 19:40:03 +06:30
_fcsShipments.clear();
_fcsShipments = snapshot.documents.map((documentSnapshot) {
2020-10-08 03:32:52 +06:30
var s = FcsShipment.fromMap(
documentSnapshot.data, documentSnapshot.documentID);
return s;
}).toList();
notifyListeners();
});
} catch (e) {
log.warning("Error!! $e");
}
2020-06-01 14:24:45 +06:30
}
2020-10-15 19:40:03 +06:30
Paginator _getDelivered() {
if (user == null || !user.hasFcsShipments()) return null;
var pageQuery = Firestore.instance
.collection("/$fcs_shipment_collection")
.where("is_delivered", isEqualTo: true)
.where("is_deleted", isEqualTo: false)
.orderBy("current_status_date", descending: true);
var paginator = new Paginator(pageQuery, rowPerLoad: 20, toObj: (data, id) {
return FcsShipment.fromMap(data, id);
});
return paginator;
}
Future<void> loadMore() async {
if (_delivered.ended) return;
isLoading = true;
notifyListeners();
await _delivered.load(onFinished: () {
isLoading = false;
notifyListeners();
});
}
Future<void> refresh() async {
await _delivered.refresh(onFinished: () {
notifyListeners();
});
}
2020-06-01 14:24:45 +06:30
void initUser(user) {
super.initUser(user);
}
@override
logout() async {
2020-10-12 03:34:05 +06:30
if (listener != null) await listener.cancel();
2020-10-15 19:40:03 +06:30
if (_delivered != null) _delivered.close();
_fcsShipments = [];
2020-10-08 03:32:52 +06:30
}
Future<void> create(FcsShipment fcsShipment) {
return Services.instance.fcsShipmentService.createFcsShipment(fcsShipment);
}
Future<void> update(FcsShipment fcsShipment) {
return Services.instance.fcsShipmentService.updateFcsShipment(fcsShipment);
2020-06-01 14:24:45 +06:30
}
}