add pagination for pages
This commit is contained in:
@@ -4,100 +4,61 @@ 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/helpers/paginator.dart';
|
||||
import 'package:fcs/pages/main/model/base_model.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
|
||||
import '../../../pagination/paginator_listener.dart';
|
||||
|
||||
class DeliveryModel extends BaseModel {
|
||||
final log = Logger('DeliveryModel');
|
||||
List<Carton> get cartons => _selectedIndex == 1
|
||||
? _cartons
|
||||
: List<Carton>.from(_delivered?.values ?? []);
|
||||
PaginatorListener<Carton>? getCartons;
|
||||
int selectedIndex = 1;
|
||||
|
||||
Paginator? _delivered;
|
||||
int _selectedIndex = 1;
|
||||
bool isLoading = false;
|
||||
List<Carton> _cartons = [];
|
||||
StreamSubscription<QuerySnapshot>? listener;
|
||||
|
||||
set selectedIndex(int index) {
|
||||
_selectedIndex = index;
|
||||
onChanged(int index) {
|
||||
selectedIndex = index;
|
||||
loadPaginationCartons(selectedIndex);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
int get selectedIndex => _selectedIndex;
|
||||
|
||||
initData() {
|
||||
_selectedIndex = 1;
|
||||
_loadCartons();
|
||||
|
||||
if (_delivered != null) _delivered!.close();
|
||||
_delivered = _getDelivered();
|
||||
_delivered!.load();
|
||||
}
|
||||
|
||||
Future<void> _loadCartons() async {
|
||||
loadPaginationCartons(int index) {
|
||||
if (user == null || !user!.hasDeliveries()) return;
|
||||
String path = "/$cartons_collection/";
|
||||
if (listener != null) listener!.cancel();
|
||||
_cartons = [];
|
||||
try {
|
||||
listener = FirebaseFirestore.instance
|
||||
.collection("$path")
|
||||
String path = "/$cartons_collection";
|
||||
|
||||
Query col = FirebaseFirestore.instance.collection(path);
|
||||
Query pageQuery = FirebaseFirestore.instance.collection(path);
|
||||
|
||||
if (index == 1) {
|
||||
col = col
|
||||
.where("status", isEqualTo: carton_shipped_status)
|
||||
.where("carton_type", whereIn: [
|
||||
carton_from_packages,
|
||||
carton_from_shipments,
|
||||
carton_small_bag
|
||||
])
|
||||
.where("is_deleted", isEqualTo: false)
|
||||
.orderBy("carton_number", descending: false)
|
||||
.snapshots()
|
||||
.listen((QuerySnapshot snapshot) {
|
||||
_cartons.clear();
|
||||
_cartons = 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");
|
||||
carton_from_packages,
|
||||
carton_from_shipments,
|
||||
carton_small_bag
|
||||
]);
|
||||
pageQuery = pageQuery
|
||||
.where("status", isEqualTo: carton_shipped_status)
|
||||
.where("carton_type", whereIn: [
|
||||
carton_from_packages,
|
||||
carton_from_shipments,
|
||||
carton_small_bag
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Paginator _getDelivered() {
|
||||
if (user == null || !user!.hasDeliveries()) throw "No Privilege";
|
||||
if (index == 2) {
|
||||
col = col
|
||||
.where("is_delivered", isEqualTo: true)
|
||||
.where("status", whereIn: [carton_delivered_status]);
|
||||
pageQuery = pageQuery
|
||||
.where("is_delivered", isEqualTo: true)
|
||||
.where("status", whereIn: [carton_delivered_status]);
|
||||
}
|
||||
|
||||
var pageQuery = FirebaseFirestore.instance
|
||||
.collection("/$cartons_collection")
|
||||
.where("is_delivered", isEqualTo: true)
|
||||
.where("status", whereIn: [carton_delivered_status]).where("is_deleted",
|
||||
isEqualTo: false);
|
||||
var paginator = new Paginator(pageQuery, rowPerLoad: 20, toObj: (data, id) {
|
||||
return Carton.fromMap(data, id);
|
||||
});
|
||||
return paginator;
|
||||
}
|
||||
pageQuery = pageQuery.orderBy("carton_number");
|
||||
|
||||
Future<void> loadMore() async {
|
||||
if (_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();
|
||||
});
|
||||
getCartons?.close();
|
||||
getCartons = PaginatorListener<Carton>(
|
||||
col, pageQuery, (data, id) => Carton.fromMap(data, id),
|
||||
rowPerLoad: 30);
|
||||
}
|
||||
|
||||
void initUser(user) {
|
||||
@@ -106,9 +67,7 @@ class DeliveryModel extends BaseModel {
|
||||
|
||||
@override
|
||||
logout() async {
|
||||
if (listener != null) await listener!.cancel();
|
||||
if (_delivered != null) _delivered!.close();
|
||||
_cartons = [];
|
||||
getCartons?.close();
|
||||
}
|
||||
|
||||
Future<void> deliver(Carton carton) {
|
||||
|
||||
Reference in New Issue
Block a user