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

158 lines
4.4 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
2020-10-21 05:40:58 +06:30
: List<FcsShipment>.from(_shipped.values);
2020-10-15 19:40:03 +06:30
2020-10-21 05:40:58 +06:30
Paginator _shipped;
2020-10-15 19:40:03 +06:30
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-06-01 14:24:45 +06:30
}
2020-10-16 11:05:38 +06:30
initData() {
_selectedIndex = 1;
_loadFcsShipments();
2020-10-21 05:40:58 +06:30
if (_shipped != null) _shipped.close();
_shipped = _getShipped();
2020-10-21 06:24:26 +06:30
_shipped.load();
2020-10-16 11:05:38 +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-21 05:40:58 +06:30
.where("status", isEqualTo: fcs_shipment_confirmed_status)
2020-10-21 06:24:26 +06:30
.where("is_deleted", isEqualTo: false)
.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-21 05:40:58 +06:30
Paginator _getShipped() {
2020-10-15 19:40:03 +06:30
if (user == null || !user.hasFcsShipments()) return null;
var pageQuery = Firestore.instance
.collection("/$fcs_shipment_collection")
2020-10-21 05:40:58 +06:30
.where("status", isEqualTo: fcs_shipment_shipped_status)
2020-10-15 19:40:03 +06:30
.where("is_deleted", isEqualTo: false)
2020-10-21 06:24:26 +06:30
.orderBy("shipment_number", descending: true);
2020-10-15 19:40:03 +06:30
var paginator = new Paginator(pageQuery, rowPerLoad: 20, toObj: (data, id) {
return FcsShipment.fromMap(data, id);
});
return paginator;
}
Future<void> loadMore() async {
2020-10-21 05:40:58 +06:30
if (_shipped.ended && _selectedIndex == 1) return;
2020-10-15 19:40:03 +06:30
isLoading = true;
notifyListeners();
2020-10-21 05:40:58 +06:30
await _shipped.load(onFinished: () {
2020-10-15 19:40:03 +06:30
isLoading = false;
notifyListeners();
});
}
Future<void> refresh() async {
2020-10-21 05:40:58 +06:30
if (_selectedIndex == 1) return;
await _shipped.refresh(onFinished: () {
2020-10-15 19:40:03 +06:30
notifyListeners();
});
}
2020-10-19 05:13:49 +06:30
Future<List<FcsShipment>> getActiveFcsShipments() async {
List<FcsShipment> fcsShipments = [];
try {
var snaps = await Firestore.instance
.collection("/$fcs_shipment_collection")
.where("status", isEqualTo: fcs_shipment_confirmed_status)
.getDocuments(source: Source.server);
fcsShipments = snaps.documents.map((documentSnapshot) {
var fcs = FcsShipment.fromMap(
documentSnapshot.data, documentSnapshot.documentID);
return fcs;
}).toList();
} catch (e) {
log.warning("Error!! $e");
}
return fcsShipments;
}
2020-10-19 14:02:34 +06:30
Future<FcsShipment> getFcsShipment(String id) async {
try {
var snap = await Firestore.instance
.collection("/$fcs_shipment_collection")
.document(id)
.get(source: Source.server);
var fcs = FcsShipment.fromMap(snap.data, snap.documentID);
return fcs;
} catch (e) {
log.warning("Error!! $e");
}
return null;
}
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-21 05:40:58 +06:30
if (_shipped != null) _shipped.close();
2020-10-15 19:40:03 +06:30
_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
}
2020-10-19 05:13:49 +06:30
Future<void> ship(FcsShipment fcsShipment) {
return Services.instance.fcsShipmentService.ship(fcsShipment);
}
2020-06-01 14:24:45 +06:30
}