439 lines
13 KiB
Dart
439 lines
13 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/carton.dart';
|
|
import 'package:fcs/domain/entities/cargo_type.dart';
|
|
import 'package:fcs/domain/entities/package.dart';
|
|
import 'package:fcs/domain/vo/message.dart';
|
|
import 'package:fcs/domain/vo/shipment_status.dart';
|
|
import 'package:fcs/domain/vo/delivery_address.dart';
|
|
import 'package:fcs/helpers/paginator.dart';
|
|
import 'package:fcs/pages/main/model/base_model.dart';
|
|
import 'package:logging/logging.dart';
|
|
|
|
class CartonModel extends BaseModel {
|
|
List<Carton> _boxes = [];
|
|
final log = Logger('CartonModel');
|
|
List<Carton> get boxes =>
|
|
_selectedIndex == 1 ? _boxes : List<Carton>.from(_delivered.values);
|
|
|
|
Paginator _delivered;
|
|
int _selectedIndex = 1;
|
|
bool isLoading = false;
|
|
|
|
StreamSubscription<QuerySnapshot> listener;
|
|
static List<ShipmentStatus> statusHistory = [
|
|
ShipmentStatus(status: "Packed", date: DateTime(2020, 6, 1), done: true),
|
|
ShipmentStatus(status: "Shipped", date: DateTime(2020, 6, 5), done: false),
|
|
ShipmentStatus(
|
|
status: "Delivered", date: DateTime(2020, 6, 15), done: false)
|
|
];
|
|
static List<Package> packages = [
|
|
// PackageModel.packages[0],
|
|
// PackageModel.packages[1],
|
|
// PackageModel.packages[2]
|
|
];
|
|
|
|
List<Carton> boxeList = [
|
|
Carton(
|
|
shipmentNumber: "A202",
|
|
receiverNumber: "3",
|
|
receiverName: "Ko Myo Min",
|
|
boxNumber: "1",
|
|
rate: 7,
|
|
packageType: "General",
|
|
weight: 75,
|
|
status: "Packed",
|
|
receiverAddress: '1 Bo Yar Nyunt St.\nDagon Tsp, Yangon',
|
|
cargoDesc: "Clothes",
|
|
desc: "Desc 1",
|
|
cartonType: carton_from_packages,
|
|
arrivedDate: DateTime(2020, 6, 1),
|
|
width: 10,
|
|
height: 10,
|
|
length: 10,
|
|
shipmentWeight: 6,
|
|
packages: packages,
|
|
shipmentHistory: statusHistory,
|
|
deliveryAddress: DeliveryAddress(
|
|
fullName: 'U Nyi Nyi',
|
|
addressLine1: '154-19 64th Ave.',
|
|
addressLine2: 'Flushing',
|
|
city: 'NY',
|
|
state: 'NY',
|
|
phoneNumber: '+1 (292)215-2247'),
|
|
cargoTypes: [
|
|
CargoType(name: 'General', weight: 25),
|
|
CargoType(name: 'Medicine', weight: 20),
|
|
CargoType(name: 'Dangerous', weight: 30)
|
|
]),
|
|
Carton(
|
|
shipmentNumber: "A203",
|
|
receiverNumber: "3",
|
|
receiverName: "Ko Myo Min",
|
|
boxNumber: "2",
|
|
rate: 7,
|
|
packageType: "General",
|
|
weight: 75,
|
|
status: "Packed",
|
|
cargoDesc: "Clothes",
|
|
desc: "Desc 2",
|
|
arrivedDate: DateTime(2020, 6, 1),
|
|
width: 10,
|
|
height: 20,
|
|
length: 30,
|
|
shipmentWeight: 36,
|
|
shipmentHistory: statusHistory,
|
|
packages: packages,
|
|
receiverAddress: '1 Bo Yar Nyunt St.\nDagon Tsp, Yangon',
|
|
cartonType: carton_from_shipments,
|
|
deliveryAddress: DeliveryAddress(
|
|
fullName: 'Mg Myo',
|
|
addressLine1: '153-154 5th Thitsar.',
|
|
addressLine2: 'South Okkalapa Township',
|
|
city: 'Yangon',
|
|
state: 'Myanmar',
|
|
phoneNumber: '+09 95724 8750'),
|
|
cargoTypes: [
|
|
CargoType(name: 'General', weight: 25),
|
|
CargoType(name: 'Medicine', weight: 20),
|
|
CargoType(name: 'Dangerous', weight: 30)
|
|
]),
|
|
Carton(
|
|
shipmentNumber: "A204",
|
|
receiverNumber: "3",
|
|
receiverName: "Ko Myo Min",
|
|
boxNumber: "3",
|
|
rate: 7,
|
|
packageType: "General",
|
|
weight: 75,
|
|
cargoDesc: "Shoes",
|
|
status: "Packed",
|
|
desc: "Desc 3",
|
|
cartonType: carton_mix_box,
|
|
arrivedDate: DateTime(2020, 6, 1),
|
|
width: 10,
|
|
height: 10,
|
|
length: 10,
|
|
shipmentWeight: 6,
|
|
shipmentHistory: statusHistory,
|
|
packages: packages,
|
|
receiverAddress: '1 Bo Yar Nyunt St.\nDagon Tsp, Yangon',
|
|
deliveryAddress: DeliveryAddress(
|
|
fullName: 'Mg Myo',
|
|
addressLine1: '153-154 5th Thitsar.',
|
|
addressLine2: 'South Okkalapa Township',
|
|
city: 'Yangon',
|
|
state: 'Myanmar',
|
|
phoneNumber: '+09 95724 8750'),
|
|
cargoTypes: [
|
|
CargoType(name: 'General', weight: 25),
|
|
CargoType(name: 'Medicine', weight: 20),
|
|
CargoType(name: 'Dangerous', weight: 30)
|
|
]),
|
|
Carton(
|
|
shipmentNumber: "A202",
|
|
receiverNumber: "2",
|
|
receiverName: "Ma Aye",
|
|
boxNumber: "1",
|
|
rate: 8,
|
|
packageType: "Medicine",
|
|
weight: 75,
|
|
status: "Packed",
|
|
cargoDesc: "Dietary supplement",
|
|
desc: "Desc 4",
|
|
arrivedDate: DateTime(2020, 6, 1),
|
|
width: 10,
|
|
height: 10,
|
|
length: 10,
|
|
shipmentWeight: 6,
|
|
shipmentHistory: statusHistory,
|
|
packages: packages,
|
|
receiverAddress: '2 Shwe Taung Kyar St, Bahan Tsp, Yangon',
|
|
deliveryAddress: DeliveryAddress(
|
|
fullName: 'U Nyi Nyi',
|
|
addressLine1: '154-19 64th Ave.',
|
|
addressLine2: 'Flushing',
|
|
city: 'NY',
|
|
state: 'NY',
|
|
phoneNumber: '+1 (292)215-2247'),
|
|
cargoTypes: [
|
|
CargoType(name: 'General', weight: 25),
|
|
CargoType(name: 'Medicine', weight: 20),
|
|
CargoType(name: 'Dangerous', weight: 30)
|
|
]),
|
|
Carton(
|
|
shipmentNumber: "A202",
|
|
receiverNumber: "2",
|
|
receiverName: "Ma Aye",
|
|
boxNumber: "3",
|
|
rate: 7,
|
|
packageType: "General",
|
|
cargoDesc: "Handbags",
|
|
weight: 75,
|
|
status: "Arrived",
|
|
arrivedDate: DateTime(2020, 6, 1),
|
|
width: 10,
|
|
height: 10,
|
|
length: 10,
|
|
shipmentWeight: 6,
|
|
shipmentHistory: statusHistory,
|
|
packages: packages,
|
|
receiverAddress: '2 Shwe Taung Kyar St, Bahan Tsp, Yangon',
|
|
deliveryAddress: DeliveryAddress(
|
|
fullName: 'U Nyi Nyi',
|
|
addressLine1: '154-19 64th Ave.',
|
|
addressLine2: 'Flushing',
|
|
city: 'NY',
|
|
state: 'NY',
|
|
phoneNumber: '+1 (292)215-2247'),
|
|
cargoTypes: [
|
|
CargoType(name: 'General', weight: 25),
|
|
CargoType(name: 'Medicine', weight: 20),
|
|
CargoType(name: 'Dangerous', weight: 30)
|
|
]),
|
|
Carton(
|
|
shipmentNumber: "A202",
|
|
receiverNumber: "2",
|
|
receiverName: "Ma Aye",
|
|
boxNumber: "2",
|
|
rate: 7,
|
|
packageType: "General",
|
|
cargoDesc: "Handbags",
|
|
weight: 75,
|
|
status: "Shipped",
|
|
arrivedDate: DateTime(2020, 6, 1),
|
|
width: 10,
|
|
height: 10,
|
|
length: 10,
|
|
shipmentWeight: 6,
|
|
shipmentHistory: statusHistory,
|
|
packages: packages,
|
|
receiverAddress: '2 Shwe Taung Kyar St, Bahan Tsp, Yangon',
|
|
deliveryAddress: DeliveryAddress(
|
|
fullName: 'U Nyi Nyi',
|
|
addressLine1: '154-19 64th Ave.',
|
|
addressLine2: 'Flushing',
|
|
city: 'NY',
|
|
state: 'NY',
|
|
phoneNumber: '+1 (292)215-2247'),
|
|
cargoTypes: [
|
|
CargoType(name: 'General', weight: 25),
|
|
CargoType(name: 'Medicine', weight: 20),
|
|
CargoType(name: 'Dangerous', weight: 30)
|
|
]),
|
|
Carton(
|
|
shipmentNumber: "A201",
|
|
receiverNumber: "1",
|
|
receiverName: "Ko Wai",
|
|
boxNumber: "1",
|
|
rate: 9,
|
|
packageType: "Dangerous",
|
|
cargoDesc: "Phones and Scooters",
|
|
weight: 75,
|
|
status: "Delivered",
|
|
arrivedDate: DateTime(2020, 5, 21),
|
|
width: 10,
|
|
height: 10,
|
|
length: 10,
|
|
shipmentWeight: 6,
|
|
shipmentHistory: statusHistory,
|
|
packages: packages,
|
|
receiverAddress: '3 Kambzwza St, Bahan Tsp, Yangon',
|
|
deliveryAddress: DeliveryAddress(
|
|
fullName: 'U Nyi Nyi',
|
|
addressLine1: '154-19 64th Ave.',
|
|
addressLine2: 'Flushing',
|
|
city: 'NY',
|
|
state: 'NY',
|
|
phoneNumber: '+1 (292)215-2247'),
|
|
cargoTypes: [
|
|
CargoType(name: 'General', weight: 25),
|
|
CargoType(name: 'Medicine', weight: 20),
|
|
CargoType(name: 'Dangerous', weight: 30)
|
|
]),
|
|
Carton(
|
|
shipmentNumber: "A201",
|
|
receiverNumber: "1",
|
|
receiverName: "Ko Wai",
|
|
boxNumber: "2",
|
|
rate: 7,
|
|
packageType: "General",
|
|
cargoDesc: "Construction tools",
|
|
weight: 75,
|
|
status: "Delivered",
|
|
arrivedDate: DateTime(2020, 5, 21),
|
|
width: 10,
|
|
height: 10,
|
|
length: 10,
|
|
shipmentWeight: 6,
|
|
shipmentHistory: statusHistory,
|
|
packages: packages,
|
|
receiverAddress: '3 Kambzwza St, Bahan Tsp, Yangon',
|
|
deliveryAddress: DeliveryAddress(
|
|
fullName: 'U Nyi Nyi',
|
|
addressLine1: '154-19 64th Ave.',
|
|
addressLine2: 'Flushing',
|
|
city: 'NY',
|
|
state: 'NY',
|
|
phoneNumber: '+1 (292)215-2247'),
|
|
cargoTypes: [
|
|
CargoType(name: 'General', weight: 25),
|
|
CargoType(name: 'Medicine', weight: 20),
|
|
CargoType(name: 'Dangerous', weight: 30)
|
|
]),
|
|
];
|
|
|
|
List<Carton> get completed {
|
|
return boxes.where((e) => e.status == "Delivered").toList()
|
|
..sort((e1, e2) {
|
|
return e2.packageNumber.compareTo(e1.packageNumber);
|
|
});
|
|
}
|
|
|
|
List<Carton> get processed {
|
|
return boxes.where((e) => e.status == "Packed").toList()
|
|
..sort((e1, e2) {
|
|
return e2.packageNumber.compareTo(e1.packageNumber);
|
|
});
|
|
}
|
|
|
|
List<Carton> get upcoming {
|
|
return boxes
|
|
.where((e) =>
|
|
e.status == "Packed" ||
|
|
// e.status == "Received" ||
|
|
e.status == "Shipped" ||
|
|
e.status == "Arrived")
|
|
.toList()
|
|
..sort((e1, e2) {
|
|
return e2.packageNumber.compareTo(e1.packageNumber);
|
|
});
|
|
}
|
|
|
|
List<String> cartonTypes = [carton_from_packages, carton_mix_box];
|
|
|
|
set selectedIndex(int index) {
|
|
_selectedIndex = index;
|
|
notifyListeners();
|
|
}
|
|
|
|
get selectedIndex => _selectedIndex;
|
|
|
|
initData() {
|
|
_selectedIndex = 1;
|
|
_loadBoxes();
|
|
|
|
if (_delivered != null) _delivered.close();
|
|
// _delivered = _getDelivered();
|
|
_delivered = _getDeliveredExample();
|
|
_delivered.load();
|
|
}
|
|
|
|
int count = 0;
|
|
Paginator _getDeliveredExample() {
|
|
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 Carton(
|
|
id: m.id,
|
|
shipmentNumber: m.message,
|
|
boxNumber: "1",
|
|
receiverNumber: "3",
|
|
rate: 0,
|
|
weight: 0,
|
|
arrivedDate: m.date,
|
|
);
|
|
});
|
|
return paginator;
|
|
}
|
|
|
|
Future<void> _loadBoxes() async {
|
|
if (user == null || !user.hasCarton()) return;
|
|
String path = "/$cartons_collection/";
|
|
if (listener != null) listener.cancel();
|
|
_boxes = [];
|
|
try {
|
|
listener = Firestore.instance
|
|
.collection("$path")
|
|
.snapshots()
|
|
.listen((QuerySnapshot snapshot) {
|
|
_boxes.clear();
|
|
_boxes = snapshot.documents.map((documentSnapshot) {
|
|
var s = Carton.fromMap(
|
|
documentSnapshot.data, documentSnapshot.documentID);
|
|
return s;
|
|
}).toList();
|
|
notifyListeners();
|
|
});
|
|
} catch (e) {
|
|
log.warning("Error!! $e");
|
|
}
|
|
}
|
|
|
|
Paginator _getDelivered() {
|
|
if (user == null || !user.hasCarton()) return null;
|
|
|
|
var pageQuery = Firestore.instance
|
|
.collection("/$cartons_collection")
|
|
.where("is_delivered", isEqualTo: true)
|
|
.where("is_deleted", isEqualTo: false);
|
|
var paginator = new Paginator(pageQuery, rowPerLoad: 20, toObj: (data, id) {
|
|
return Carton.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();
|
|
});
|
|
}
|
|
|
|
void initUser(user) {
|
|
super.initUser(user);
|
|
}
|
|
|
|
@override
|
|
logout() async {
|
|
if (listener != null) await listener.cancel();
|
|
if (_delivered != null) _delivered.close();
|
|
_boxes = [];
|
|
}
|
|
|
|
Future<List<Carton>> getCartons(String shipmentID) async {
|
|
String path = "/$cartons_collection";
|
|
var querySnap = await Firestore.instance
|
|
.collection(path)
|
|
.where("shipment_id", isEqualTo: shipmentID)
|
|
.getDocuments();
|
|
return querySnap.documents
|
|
.map((e) => Carton.fromMap(e.data, e.documentID))
|
|
.toList();
|
|
}
|
|
|
|
Future<void> createCarton(Carton carton) {
|
|
return Services.instance.cartonService.createCarton(carton);
|
|
}
|
|
|
|
Future<void> updateCarton(Carton carton) {
|
|
return Services.instance.cartonService.updateCarton(carton);
|
|
}
|
|
}
|