76 lines
2.0 KiB
Dart
76 lines
2.0 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
import 'package:fcs/domain/entities/fcs_shipment.dart';
|
|
import 'package:logging/logging.dart';
|
|
|
|
import '../../../domain/constants.dart';
|
|
import '../../main/model/base_model.dart';
|
|
|
|
class ShipmentSelectionModel extends BaseModel {
|
|
final log = Logger("ShipmentSelectionModel");
|
|
|
|
List<FcsShipment> _shipments = [];
|
|
|
|
List<FcsShipment> get getShipments {
|
|
var list = new List<FcsShipment>.from(_shipments);
|
|
return list
|
|
..insert(0, FcsShipment(id: all, shipmentNumber: "All shipments"));
|
|
}
|
|
|
|
bool isLoading = false;
|
|
DocumentSnapshot? _lastDocument;
|
|
bool ended = false;
|
|
|
|
Future<void> refresh() async {
|
|
_shipments.clear();
|
|
_lastDocument = null;
|
|
ended = false;
|
|
await loadMoreData();
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<void> loadMoreData() async {
|
|
int rowPerPage = 20;
|
|
|
|
try {
|
|
isLoading = true;
|
|
String path = "/$fcs_shipment_collection";
|
|
Query query = FirebaseFirestore.instance
|
|
.collection(path)
|
|
.where("status", whereIn: [
|
|
fcs_shipment_processing_status,
|
|
fcs_shipment_shipped_status,
|
|
fcs_shipment_arrived_status,
|
|
fcs_shipment_invoiced_status
|
|
])
|
|
.where("is_deleted", isEqualTo: false)
|
|
.orderBy("update_time", descending: true);
|
|
|
|
if (_lastDocument != null) {
|
|
query = query.startAfterDocument(_lastDocument!);
|
|
}
|
|
|
|
QuerySnapshot querySnap = await query.limit(rowPerPage).get();
|
|
|
|
if (querySnap.docs.isEmpty) return;
|
|
_lastDocument = querySnap.docs[querySnap.docs.length - 1];
|
|
|
|
List<FcsShipment> list = querySnap.docs.map((documentSnapshot) {
|
|
var p = FcsShipment.fromMap(
|
|
documentSnapshot.data() as Map<String, dynamic>,
|
|
documentSnapshot.id);
|
|
return p;
|
|
}).toList();
|
|
|
|
_shipments.addAll(list);
|
|
if (list.length < rowPerPage) ended = true;
|
|
notifyListeners();
|
|
} catch (e) {
|
|
log.warning("error:$e");
|
|
} finally {
|
|
isLoading = false;
|
|
}
|
|
}
|
|
}
|