add pagination for pages
This commit is contained in:
@@ -4,62 +4,20 @@ 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/vo/shipment_status.dart';
|
||||
import 'package:fcs/helpers/paginator.dart';
|
||||
import 'package:fcs/pages/main/model/base_model.dart';
|
||||
import 'package:fcs/pagination/paginator_listener.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
|
||||
class CartonModel extends BaseModel {
|
||||
List<Carton> _boxes = [];
|
||||
PaginatorListener<Carton>? cartonsByFilter;
|
||||
|
||||
final log = Logger('CartonModel');
|
||||
List<Carton> get boxes => _selectedIndex == 1
|
||||
? _boxes
|
||||
: List<Carton>.from(_delivered?.values ?? []);
|
||||
|
||||
Paginator? _delivered;
|
||||
int _selectedIndex = 1;
|
||||
PaginatorListener<Carton>? cartonsByFilter;
|
||||
PaginatorListener<Carton>? getBoxes;
|
||||
|
||||
int selectedIndex = 1;
|
||||
int _selectedIndexFilter = 1;
|
||||
bool isLoading = false;
|
||||
|
||||
StreamSubscription<QuerySnapshot>? listener;
|
||||
StreamSubscription<QuerySnapshot>? cartonListener;
|
||||
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)
|
||||
];
|
||||
|
||||
// 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_from_cartons,
|
||||
@@ -74,10 +32,7 @@ class CartonModel extends BaseModel {
|
||||
carton_small_bag
|
||||
];
|
||||
|
||||
set selectedIndex(int index) {
|
||||
_selectedIndex = index;
|
||||
notifyListeners();
|
||||
}
|
||||
int get selectedIndexFilter => _selectedIndexFilter;
|
||||
|
||||
set selectedIndexFilter(int index) {
|
||||
_selectedIndexFilter = index;
|
||||
@@ -86,19 +41,6 @@ class CartonModel extends BaseModel {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
int get selectedIndex => _selectedIndex;
|
||||
int get selectedIndexFilter => _selectedIndexFilter;
|
||||
|
||||
initData() async {
|
||||
_selectedIndex = 1;
|
||||
_selectedIndexFilter = 1;
|
||||
_loadBoxes();
|
||||
|
||||
if (_delivered != null) _delivered!.close();
|
||||
_delivered = _getDelivered();
|
||||
_delivered!.load();
|
||||
}
|
||||
|
||||
@override
|
||||
void privilegeChanged() {
|
||||
if (user != null || !user!.hasCarton()) {
|
||||
@@ -106,40 +48,54 @@ class CartonModel extends BaseModel {
|
||||
}
|
||||
}
|
||||
|
||||
void initUser(user) {
|
||||
super.initUser(user);
|
||||
}
|
||||
|
||||
@override
|
||||
logout() async {
|
||||
getBoxes?.close();
|
||||
cartonsByFilter?.close();
|
||||
}
|
||||
|
||||
Future<void> _initData() async {
|
||||
logout();
|
||||
_selectedIndexFilter = 1;
|
||||
|
||||
_loadPaginationCartons(
|
||||
_selectedIndexFilter == 1 ? "carton_weight" : "user_name");
|
||||
}
|
||||
|
||||
Future<void> _loadBoxes() async {
|
||||
onChanged(int index) {
|
||||
selectedIndex = index;
|
||||
loadPaginationBoxes(selectedIndex);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> loadPaginationBoxes(int index) async {
|
||||
if (user == null || !user!.hasCarton()) return;
|
||||
String path = "/$cartons_collection/";
|
||||
if (listener != null) listener!.cancel();
|
||||
_boxes = [];
|
||||
try {
|
||||
listener = FirebaseFirestore.instance
|
||||
.collection("$path")
|
||||
.where("status",
|
||||
whereIn: [carton_packed_status, carton_shipped_status])
|
||||
.where("is_deleted", isEqualTo: false)
|
||||
.orderBy("created_at", descending: true)
|
||||
.snapshots()
|
||||
.listen((QuerySnapshot snapshot) {
|
||||
_boxes.clear();
|
||||
_boxes = snapshot.docs.map((documentSnapshot) {
|
||||
var s = Carton.fromMap(
|
||||
documentSnapshot.data() as Map<String, dynamic>,
|
||||
documentSnapshot.id);
|
||||
return s;
|
||||
}).toList();
|
||||
notifyListeners();
|
||||
});
|
||||
} catch (e) {
|
||||
log.warning("Error!! $e");
|
||||
|
||||
String path = "/$cartons_collection";
|
||||
Query col = FirebaseFirestore.instance.collection(path);
|
||||
Query pageQuery = FirebaseFirestore.instance.collection(path);
|
||||
|
||||
if (index == 1) {
|
||||
col = col.where("status",
|
||||
whereIn: [carton_packed_status, carton_shipped_status]);
|
||||
pageQuery = pageQuery.where("status",
|
||||
whereIn: [carton_packed_status, carton_shipped_status]);
|
||||
}
|
||||
|
||||
if (index == 2) {
|
||||
col = col.where("is_delivered", isEqualTo: true);
|
||||
pageQuery = pageQuery.where("is_delivered", isEqualTo: true);
|
||||
}
|
||||
|
||||
pageQuery = pageQuery.orderBy("created_at", descending: true);
|
||||
|
||||
getBoxes?.close();
|
||||
getBoxes = PaginatorListener<Carton>(
|
||||
col, pageQuery, (data, id) => Carton.fromMap(data, id),
|
||||
rowPerLoad: 30);
|
||||
}
|
||||
|
||||
_loadPaginationCartons(String orderName) {
|
||||
@@ -164,49 +120,6 @@ class CartonModel extends BaseModel {
|
||||
rowPerLoad: 30);
|
||||
}
|
||||
|
||||
Paginator? _getDelivered() {
|
||||
if (user == null || !user!.hasCarton()) return null;
|
||||
|
||||
var pageQuery = FirebaseFirestore.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 == null && _delivered!.ended || selectedIndex == 1) return;
|
||||
isLoading = true;
|
||||
notifyListeners();
|
||||
await _delivered!.load(onFinished: () {
|
||||
isLoading = false;
|
||||
notifyListeners();
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> refresh() async {
|
||||
if (selectedIndex == 1) return;
|
||||
await _delivered?.refresh(onFinished: () {
|
||||
notifyListeners();
|
||||
});
|
||||
}
|
||||
|
||||
void initUser(user) {
|
||||
super.initUser(user);
|
||||
}
|
||||
|
||||
@override
|
||||
logout() async {
|
||||
if (listener != null) await listener!.cancel();
|
||||
if (cartonListener != null) await cartonListener!.cancel();
|
||||
if (_delivered != null) _delivered!.close();
|
||||
if (cartonsByFilter != null) cartonsByFilter!.close();
|
||||
_boxes = [];
|
||||
}
|
||||
|
||||
Future<List<Carton>> getCartons(String shipmentID) async {
|
||||
String path = "/$cartons_collection";
|
||||
var querySnap = await FirebaseFirestore.instance
|
||||
|
||||
Reference in New Issue
Block a user