add paginator

This commit is contained in:
Sai Naw Wun
2020-10-16 10:58:31 +06:30
parent bcbcfd71ee
commit 0abe4ef73f
29 changed files with 953 additions and 703 deletions

View File

@@ -1,10 +1,11 @@
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/cargo_type.dart';
import 'package:fcs/domain/entities/shipment.dart';
import 'package:fcs/domain/vo/radio.dart';
import 'package:fcs/domain/vo/message.dart';
import 'package:fcs/helpers/paginator.dart';
import 'package:fcs/pages/main/model/base_model.dart';
import 'package:logging/logging.dart';
@@ -13,376 +14,145 @@ class ShipmentModel extends BaseModel {
StreamSubscription<QuerySnapshot> listener;
List<RadioGroup> radioGroups1 = [
RadioGroup(
text: "FCS Pickup",
index: 1,
),
RadioGroup(
text: "Courier Pickup",
index: 2,
),
RadioGroup(
text: "FCS Drop-off",
index: 3,
),
RadioGroup(
text: "Courier Drop-off",
index: 4,
),
];
List<Shipment> get shipments => _menuSelectedIndex == 1
? _shipments
: List<Shipment>.from(_delivered.values);
List<String> pickupTypes = [
List<Shipment> _shipments = [];
Paginator _delivered;
bool isLoading = false;
int _menuSelectedIndex = 1;
set menuSelectedIndex(int index) {
_menuSelectedIndex = index;
notifyListeners();
}
get menuSelectedIndex => _menuSelectedIndex;
initData(bool forCustomer) {
logout();
_menuSelectedIndex = 1;
_loadShipments(forCustomer);
_delivered = _getDeliveredExample(forCustomer);
_delivered.load();
}
@override
logout() async {
if (_delivered != null) _delivered.close();
if (listener != null) await listener.cancel();
_shipments = [];
}
Future<void> loadMore({bool isCustomer}) async {
if (menuSelectedIndex == 1)
return; // when delivered menu is not selected return
if (_delivered.ended) return;
isLoading = true;
notifyListeners();
await _delivered.load(onFinished: () {
isLoading = false;
notifyListeners();
});
}
Future<void> refresh({bool isCustomer}) async {
if (menuSelectedIndex == 1)
return; // when delivered menu is not selected return
await _delivered.refresh(onFinished: () {
notifyListeners();
});
}
Paginator _getDelivered(bool isCustomer) {
if (!isCustomer) {
if (user == null ||
!((user.hasPackages() ||
user.hasReceiving() ||
user.hasProcessing()))) throw "No privilege";
}
var pageQuery = Firestore.instance
.collection("/$packages_collection")
.where("is_delivered", isEqualTo: true)
.where("is_deleted", isEqualTo: false);
if (isCustomer) {
pageQuery = pageQuery.where("user_id", isEqualTo: user.id);
}
pageQuery = pageQuery.orderBy("current_status_date", descending: true);
var paginator = new Paginator(pageQuery, rowPerLoad: 20, toObj: (data, id) {
return Shipment.fromMap(data, id);
});
return paginator;
}
int count = 0;
Paginator _getDeliveredExample(bool onlyFcs) {
count = 1;
var pageQuery = Firestore.instance
.collection(
"/users/8OTfsbVvsUOn1SLxy1OrKk7Y_yNKkVoGalPcIlcHnAY/messages")
.orderBy("date", descending: true);
var paginator = new Paginator(pageQuery, rowPerLoad: 20, toObj: (data, id) {
var m = Message.fromMap(data, id);
return Shipment(
id: m.id,
shipmentNumber: m.message,
currentStatus: m.senderName,
pickupDate: m.date,
);
});
return paginator;
}
Future<void> _loadShipments(bool forCustomer) async {
if (user == null) return;
if (!forCustomer && !user.hasShipment()) return;
String path = "/$shipments_collection";
if (listener != null) listener.cancel();
_shipments = [];
try {
var q = Firestore.instance
.collection("$path")
.where("is_delivered", isEqualTo: false)
.where("is_deleted", isEqualTo: false);
if (forCustomer) {
q = q.where("user_id", isEqualTo: user.id);
}
listener = q.snapshots().listen((QuerySnapshot snapshot) {
_shipments.clear();
_shipments = snapshot.documents.map((documentSnapshot) {
var s = Shipment.fromMap(
documentSnapshot.data, documentSnapshot.documentID);
return s;
}).toList();
notifyListeners();
});
} catch (e) {
log.warning("Error!! $e");
}
}
List<String> shipmentTypes = [
shipment_local_pickup,
shipment_courier_pickup,
shipment_local_dropoff,
shipment_courier_dropoff
];
List<RadioGroup> get radioGroups {
List<RadioGroup> radioGroups = [
RadioGroup(
text: "Local Pickup",
index: 1,
),
RadioGroup(
text: "Courier Pickup",
index: 2,
),
RadioGroup(
text: "Local Drop-off",
index: 3,
),
RadioGroup(
text: "Courier Drop-off",
index: 4,
),
];
return radioGroups;
}
List<Shipment> get pickups {
List<Shipment> pickups = [
Shipment(
id: "S200412 - 12 Apr 2020",
userName: "Ko Kyaw Nyi",
phoneNumber: '+959111111111',
fromTime: '1PM',
toTime: '3PM',
numberOfPackage: 5,
weight: 25,
status: 'Pending',
date: DateTime(2020, 5, 1),
address: '154-19 64th Ave.\nFlushing, NY 11367',
handlingFee: 50,
isCourier: true,
radioIndex: 2,
cargoTypes: [
CargoType(name: 'General Cargo', weight: 25),
CargoType(name: 'Medicine', weight: 20),
CargoType(name: 'Dangerous Cargo', weight: 30)
]),
Shipment(
id: "S200125 - 12 May 2020",
userName: "Ko Kyaw Nyi",
phoneNumber: '+959111111111',
fromTime: '1PM',
toTime: '3PM',
numberOfPackage: 5,
weight: 25,
status: 'Confirmed',
date: DateTime(2020, 5, 6),
address: '154-19 64th Ave.\nFlushing, NY 11367',
handlingFee: 50,
cargoTypes: [
CargoType(name: 'General Cargo', weight: 25),
CargoType(name: 'Medicine', weight: 20),
CargoType(name: 'Dangerous Cargo', weight: 30)
]),
Shipment(
id: "S200441 - 13 Apr 2020",
userName: "Ko Kyaw Nyi",
phoneNumber: '+959111111111',
fromTime: '1PM',
toTime: '3PM',
numberOfPackage: 5,
weight: 25,
status: "Pickuped",
date: DateTime(2020, 5, 9),
address: '154-19 64th Ave.\nFlushing, NY 11367',
handlingFee: 50,
radioIndex: 3,
cargoTypes: [
CargoType(name: 'General Cargo', weight: 25),
CargoType(name: 'Medicine', weight: 20),
CargoType(name: 'Dangerous Cargo', weight: 30)
]),
Shipment(
id: "S200412 - 12 Apr 2020",
userName: "Ko Kyaw Nyi",
phoneNumber: '+959111111111',
fromTime: '1PM',
toTime: '3PM',
numberOfPackage: 5,
weight: 25,
status: 'Pickuped',
date: DateTime(2020, 5, 15),
address: '154-19 64th Ave.\nFlushing, NY 11367',
handlingFee: 50,
cargoTypes: [
CargoType(name: 'General Cargo', weight: 25),
CargoType(name: 'Medicine', weight: 20),
CargoType(name: 'Dangerous Cargo', weight: 30)
]),
Shipment(
id: "S200125 - 12 May 2020",
userName: "Ko Kyaw Nyi",
phoneNumber: '+959111111111',
fromTime: '1PM',
toTime: '3PM',
numberOfPackage: 5,
weight: 25,
status: 'Pickuped',
date: DateTime(2020, 5, 20),
address: '154-19 64th Ave.\nFlushing, NY 11367',
handlingFee: 50,
cargoTypes: [
CargoType(name: 'General Cargo', weight: 25),
CargoType(name: 'Medicine', weight: 20),
CargoType(name: 'Dangerous Cargo', weight: 30)
]),
Shipment(
id: "S200441 - 13 Apr 2020",
userName: "Ko Kyaw Nyi",
phoneNumber: '+959111111111',
fromTime: '1PM',
toTime: '3PM',
numberOfPackage: 5,
weight: 25,
status: "Pickuped",
date: DateTime(2020, 5, 21),
address: '154-19 64th Ave.\nFlushing, NY 11367',
handlingFee: 50,
cargoTypes: [
CargoType(name: 'General Cargo', weight: 25),
CargoType(name: 'Medicine', weight: 20),
CargoType(name: 'Dangerous Cargo', weight: 30)
]),
Shipment(
id: "S200441 - 10 Apr 2020",
userName: "Ko Kyaw Nyi",
phoneNumber: '+959111111111',
fromTime: '1PM',
toTime: '3PM',
numberOfPackage: 5,
weight: 25,
status: "Canceled",
date: DateTime(2020, 5, 25),
address: '154-19 64th Ave.\nFlushing, NY 11367',
handlingFee: 50,
cargoTypes: [
CargoType(name: 'General Cargo', weight: 25),
CargoType(name: 'Medicine', weight: 20),
CargoType(name: 'Dangerous Cargo', weight: 30)
]),
Shipment(
id: "S200441 - 6 Apr 2020",
userName: "Ko Kyaw Nyi",
phoneNumber: '+959111111111',
fromTime: '1PM',
toTime: '3PM',
numberOfPackage: 5,
weight: 25,
status: "Canceled",
date: DateTime(2020, 5, 27),
address: '154-19 64th Ave.\nFlushing, NY 11367',
handlingFee: 50,
cargoTypes: [
CargoType(name: 'General Cargo', weight: 25),
CargoType(name: 'Medicine', weight: 20),
CargoType(name: 'Dangerous Cargo', weight: 30)
]),
];
return pickups;
}
List<Shipment> pickups1 = [
Shipment(
id: "S200412 - 12 Apr 2020",
userName: "Ko Kyaw Nyi",
phoneNumber: '+959111111111',
fromTime: '1PM',
toTime: '3PM',
numberOfPackage: 5,
weight: 25,
status: 'Pending',
date: DateTime(2020, 5, 1),
address: '154-19 64th Ave.\nFlushing, NY 11367',
handlingFee: 50,
isCourier: true,
radioIndex: 2,
cargoTypes: [
CargoType(name: 'General Cargo', weight: 25),
CargoType(name: 'Medicine', weight: 20),
CargoType(name: 'Dangerous Cargo', weight: 30)
]),
Shipment(
id: "S200125 - 12 May 2020",
userName: "Ko Kyaw Nyi",
phoneNumber: '+959111111111',
fromTime: '1PM',
toTime: '3PM',
numberOfPackage: 5,
weight: 25,
status: 'Confirmed',
date: DateTime(2020, 5, 6),
address: '154-19 64th Ave.\nFlushing, NY 11367',
handlingFee: 50,
cargoTypes: [
CargoType(name: 'General Cargo', weight: 25),
CargoType(name: 'Medicine', weight: 20),
CargoType(name: 'Dangerous Cargo', weight: 30)
]),
Shipment(
id: "S200441 - 13 Apr 2020",
userName: "Ko Kyaw Nyi",
phoneNumber: '+959111111111',
fromTime: '1PM',
toTime: '3PM',
numberOfPackage: 5,
weight: 25,
status: "Pickuped",
date: DateTime(2020, 5, 9),
address: '154-19 64th Ave.\nFlushing, NY 11367',
handlingFee: 50,
radioIndex: 3,
cargoTypes: [
CargoType(name: 'General Cargo', weight: 25),
CargoType(name: 'Medicine', weight: 20),
CargoType(name: 'Dangerous Cargo', weight: 30)
]),
Shipment(
id: "S200412 - 12 Apr 2020",
userName: "Ko Kyaw Nyi",
phoneNumber: '+959111111111',
fromTime: '1PM',
toTime: '3PM',
numberOfPackage: 5,
weight: 25,
status: 'Pickuped',
date: DateTime(2020, 5, 15),
address: '154-19 64th Ave.\nFlushing, NY 11367',
handlingFee: 50,
cargoTypes: [
CargoType(name: 'General Cargo', weight: 25),
CargoType(name: 'Medicine', weight: 20),
CargoType(name: 'Dangerous Cargo', weight: 30)
]),
Shipment(
id: "S200125 - 12 May 2020",
userName: "Ko Kyaw Nyi",
phoneNumber: '+959111111111',
fromTime: '1PM',
toTime: '3PM',
numberOfPackage: 5,
weight: 25,
status: 'Pickuped',
date: DateTime(2020, 5, 20),
address: '154-19 64th Ave.\nFlushing, NY 11367',
handlingFee: 50,
cargoTypes: [
CargoType(name: 'General Cargo', weight: 25),
CargoType(name: 'Medicine', weight: 20),
CargoType(name: 'Dangerous Cargo', weight: 30)
]),
Shipment(
id: "S200441 - 13 Apr 2020",
userName: "Ko Kyaw Nyi",
phoneNumber: '+959111111111',
fromTime: '1PM',
toTime: '3PM',
numberOfPackage: 5,
weight: 25,
status: "Pickuped",
date: DateTime(2020, 5, 21),
address: '154-19 64th Ave.\nFlushing, NY 11367',
handlingFee: 50,
cargoTypes: [
CargoType(name: 'General Cargo', weight: 25),
CargoType(name: 'Medicine', weight: 20),
CargoType(name: 'Dangerous Cargo', weight: 30)
]),
Shipment(
id: "S200441 - 10 Apr 2020",
userName: "Ko Kyaw Nyi",
phoneNumber: '+959111111111',
fromTime: '1PM',
toTime: '3PM',
numberOfPackage: 5,
weight: 25,
status: "Canceled",
date: DateTime(2020, 5, 25),
address: '154-19 64th Ave.\nFlushing, NY 11367',
handlingFee: 50,
cargoTypes: [
CargoType(name: 'General Cargo', weight: 25),
CargoType(name: 'Medicine', weight: 20),
CargoType(name: 'Dangerous Cargo', weight: 30)
]),
Shipment(
id: "S200441 - 6 Apr 2020",
userName: "Ko Kyaw Nyi",
phoneNumber: '+959111111111',
fromTime: '1PM',
toTime: '3PM',
numberOfPackage: 5,
weight: 25,
status: "Canceled",
date: DateTime(2020, 5, 27),
address: '154-19 64th Ave.\nFlushing, NY 11367',
handlingFee: 50,
cargoTypes: [
CargoType(name: 'General Cargo', weight: 25),
CargoType(name: 'Medicine', weight: 20),
CargoType(name: 'Dangerous Cargo', weight: 30)
]),
];
List<Shipment> get canceled {
List<Shipment> _p = pickups.where((e) => e.status == "Canceled").toList()
..sort((e1, e2) {
return e2.date.compareTo(e1.date);
});
return _p;
}
List<Shipment> get completed {
return pickups.where((e) => e.status == "Pickuped").toList()
..sort((e1, e2) {
return e2.date.compareTo(e1.date);
});
}
List<Shipment> get upcoming {
return pickups
.where((e) =>
e.status == "Pending" ||
e.status == "Confirmed" ||
e.status == "Processed" ||
e.status == "Rescheduled")
.toList()
..sort((e1, e2) {
return e2.date.compareTo(e1.date);
});
}
void initUser(user) {
super.initUser(user);
}
@override
logout() async {
if (listener != null) await listener.cancel();
// pickups = [];
Future<void> createShipment(Shipment shipment) {
return Services.instance.shipmentService.createShipment(shipment);
}
Future<void> updateShipment(Shipment shipment) {
return Services.instance.shipmentService.updateShipment(shipment);
}
}