add pagination for pages
This commit is contained in:
@@ -4,100 +4,48 @@ 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/fcs_shipment.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 FcsShipmentModel extends BaseModel {
|
||||
final log = Logger('FcsShipmentModel');
|
||||
|
||||
StreamSubscription<QuerySnapshot>? listener;
|
||||
List<FcsShipment> _fcsShipments = [];
|
||||
List<FcsShipment> get fcsShipments => _selectedIndex == 1
|
||||
? _fcsShipments
|
||||
: List<FcsShipment>.from(_shipped!.values);
|
||||
|
||||
Paginator? _shipped;
|
||||
PaginatorListener<FcsShipment>? fcsShipments;
|
||||
bool isLoading = false;
|
||||
int _selectedIndex = 1;
|
||||
set selectedIndex(int index) {
|
||||
_selectedIndex = index;
|
||||
int selectedIndex = 1;
|
||||
|
||||
onChanged(int index) {
|
||||
selectedIndex = index;
|
||||
loadFcsShipments(selectedIndex);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
int get selectedIndex => _selectedIndex;
|
||||
|
||||
@override
|
||||
void privilegeChanged() {
|
||||
super.privilegeChanged();
|
||||
_loadFcsShipments();
|
||||
}
|
||||
|
||||
initData() {
|
||||
_selectedIndex = 1;
|
||||
_loadFcsShipments();
|
||||
|
||||
if (_shipped != null) _shipped!.close();
|
||||
_shipped = _getShipped();
|
||||
_shipped!.load();
|
||||
}
|
||||
|
||||
Future<void> _loadFcsShipments() async {
|
||||
loadFcsShipments(int index) {
|
||||
if (user == null || !user!.hasFcsShipments()) return;
|
||||
String path = "/$fcs_shipment_collection/";
|
||||
if (listener != null) listener!.cancel();
|
||||
_fcsShipments = [];
|
||||
try {
|
||||
listener = FirebaseFirestore.instance
|
||||
.collection("$path")
|
||||
.where("status", isEqualTo: fcs_shipment_confirmed_status)
|
||||
.where("is_deleted", isEqualTo: false)
|
||||
.orderBy("shipment_number", descending: true)
|
||||
.snapshots()
|
||||
.listen((QuerySnapshot snapshot) {
|
||||
_fcsShipments.clear();
|
||||
_fcsShipments = snapshot.docs.map((documentSnapshot) {
|
||||
var s = FcsShipment.fromMap(
|
||||
documentSnapshot.data() as Map<String, dynamic>,
|
||||
documentSnapshot.id);
|
||||
return s;
|
||||
}).toList();
|
||||
notifyListeners();
|
||||
});
|
||||
} catch (e) {
|
||||
log.warning("Error!! $e");
|
||||
|
||||
String path = "/$fcs_shipment_collection";
|
||||
Query col = FirebaseFirestore.instance.collection(path);
|
||||
Query pageQuery = FirebaseFirestore.instance.collection(path);
|
||||
|
||||
if (index == 1) {
|
||||
col = col.where("status", isEqualTo: fcs_shipment_confirmed_status);
|
||||
pageQuery =
|
||||
pageQuery.where("status", isEqualTo: fcs_shipment_confirmed_status);
|
||||
}
|
||||
}
|
||||
|
||||
Paginator _getShipped() {
|
||||
if (user == null || !user!.hasFcsShipments()) throw "No Privilege";
|
||||
if (index == 2) {
|
||||
col = col.where("status", isEqualTo: fcs_shipment_shipped_status);
|
||||
pageQuery =
|
||||
pageQuery.where("status", isEqualTo: fcs_shipment_shipped_status);
|
||||
}
|
||||
|
||||
var pageQuery = FirebaseFirestore.instance
|
||||
.collection("/$fcs_shipment_collection")
|
||||
.where("status", isEqualTo: fcs_shipment_shipped_status)
|
||||
.where("is_deleted", isEqualTo: false)
|
||||
.orderBy("shipment_number", descending: true);
|
||||
var paginator = new Paginator(pageQuery, rowPerLoad: 20, toObj: (data, id) {
|
||||
return FcsShipment.fromMap(data, id);
|
||||
});
|
||||
return paginator;
|
||||
}
|
||||
pageQuery = pageQuery.orderBy("shipment_number", descending: true);
|
||||
|
||||
Future<void> loadMore() async {
|
||||
if (_shipped!.ended || _selectedIndex == 1) return;
|
||||
isLoading = true;
|
||||
notifyListeners();
|
||||
await _shipped!.load(onFinished: () {
|
||||
isLoading = false;
|
||||
notifyListeners();
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> refresh() async {
|
||||
if (_selectedIndex == 1) return;
|
||||
await _shipped!.refresh(onFinished: () {
|
||||
notifyListeners();
|
||||
});
|
||||
fcsShipments?.close();
|
||||
fcsShipments = PaginatorListener<FcsShipment>(
|
||||
col, pageQuery, (data, id) => FcsShipment.fromMap(data, id),
|
||||
rowPerLoad: 30);
|
||||
}
|
||||
|
||||
Future<List<FcsShipment>> getActiveFcsShipments() async {
|
||||
@@ -157,9 +105,7 @@ class FcsShipmentModel extends BaseModel {
|
||||
|
||||
@override
|
||||
logout() async {
|
||||
if (listener != null) await listener!.cancel();
|
||||
if (_shipped != null) _shipped!.close();
|
||||
_fcsShipments = [];
|
||||
fcsShipments?.close();
|
||||
}
|
||||
|
||||
Future<void> create(FcsShipment fcsShipment) {
|
||||
|
||||
Reference in New Issue
Block a user