add paginator
This commit is contained in:
@@ -1,102 +1,132 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
import 'package:fcs/data/services/services.dart';
|
||||
import 'package:fcs/domain/vo/delivery_address.dart';
|
||||
import 'package:fcs/domain/vo/message.dart';
|
||||
import 'package:fcs/helpers/pagination.dart';
|
||||
import 'package:path/path.dart' as Path;
|
||||
|
||||
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/package.dart';
|
||||
import 'package:fcs/domain/entities/user.dart';
|
||||
import 'package:fcs/domain/vo/delivery_address.dart';
|
||||
import 'package:fcs/domain/vo/message.dart';
|
||||
import 'package:fcs/helpers/firebase_helper.dart';
|
||||
import 'package:fcs/helpers/paginator.dart';
|
||||
import 'package:fcs/pages/main/model/base_model.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:path/path.dart' as Path;
|
||||
|
||||
class PackageModel extends BaseModel {
|
||||
final log = Logger('PackageModel');
|
||||
|
||||
StreamSubscription<QuerySnapshot> listener;
|
||||
StreamSubscription<QuerySnapshot> customerPackageListener;
|
||||
Pagination pagination;
|
||||
|
||||
List<Package> packages = [];
|
||||
List<Package> customerPackages = [];
|
||||
List<Package> deliveredPackages = [];
|
||||
List<Package> get packages =>
|
||||
_selectedIndex == 1 ? _packages : List<Package>.from(_delivered.values);
|
||||
List<Package> get customerPackages => _selectedIndex == 1
|
||||
? _customerPackages
|
||||
: List<Package>.from(_customerDelivered.values);
|
||||
|
||||
bool endOfDeliveredPackages = false;
|
||||
List<Package> _packages = [];
|
||||
List<Package> _customerPackages = [];
|
||||
|
||||
Paginator _delivered;
|
||||
Paginator _customerDelivered;
|
||||
bool isLoading = false;
|
||||
bool isPackagesEnded = false;
|
||||
bool isCustomerPackagesEnded = false;
|
||||
int _selectedIndex = 1;
|
||||
set selectedIndex(int index) {
|
||||
_selectedIndex = index;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
get selectedIndex => _selectedIndex;
|
||||
|
||||
@override
|
||||
void privilegeChanged() {
|
||||
super.privilegeChanged();
|
||||
_loadPackages();
|
||||
_loadCustomerPackages();
|
||||
|
||||
if (_delivered != null) _delivered.close();
|
||||
_delivered = _getDeliveredExample(false);
|
||||
loadMore(isCustomer: false);
|
||||
if (_customerDelivered != null) _customerDelivered.close();
|
||||
_customerDelivered = _getDeliveredExample(false);
|
||||
loadMore(isCustomer: true);
|
||||
}
|
||||
|
||||
@override
|
||||
logout() async {
|
||||
if (_delivered != null) _delivered.close();
|
||||
if (_customerDelivered != null) _customerDelivered.close();
|
||||
if (listener != null) await listener.cancel();
|
||||
if (customerPackageListener != null) await customerPackageListener.cancel();
|
||||
if (pagination != null) pagination.close();
|
||||
packages = [];
|
||||
customerPackages = [];
|
||||
deliveredPackages = [];
|
||||
_packages = [];
|
||||
_customerPackages = [];
|
||||
}
|
||||
|
||||
Future<void> initDeliveredPackages(bool onlyFcs) {
|
||||
if (onlyFcs) {
|
||||
Future<void> loadMore({bool isCustomer}) async {
|
||||
if (selectedIndex == 1)
|
||||
return; // when delivered menu is not selected return
|
||||
var p = isCustomer ? _customerDelivered : _delivered;
|
||||
if (p.ended) return;
|
||||
isLoading = true;
|
||||
notifyListeners();
|
||||
await p.load(onFinished: () {
|
||||
isLoading = false;
|
||||
notifyListeners();
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> refresh({bool isCustomer}) async {
|
||||
if (selectedIndex == 1)
|
||||
return; // when delivered menu is not selected return
|
||||
var p = isCustomer ? _customerDelivered : _delivered;
|
||||
await p.refresh(onFinished: () {
|
||||
notifyListeners();
|
||||
});
|
||||
}
|
||||
|
||||
Paginator _getDelivered(bool isCustomer) {
|
||||
if (!isCustomer) {
|
||||
if (user == null ||
|
||||
!((user.hasPackages() ||
|
||||
user.hasReceiving() ||
|
||||
user.hasProcessing()))) return null;
|
||||
user.hasProcessing()))) throw "No privilege";
|
||||
}
|
||||
if (pagination != null) pagination.close();
|
||||
deliveredPackages = [];
|
||||
endOfDeliveredPackages = false;
|
||||
isLoading = false;
|
||||
|
||||
var pageQuery = Firestore.instance
|
||||
.collection("/$packages_collection")
|
||||
// .collection(
|
||||
// "/users/8OTfsbVvsUOn1SLxy1OrKk7Y_yNKkVoGalPcIlcHnAY/messages")
|
||||
// .orderBy("date", descending: true);
|
||||
.where("is_delivered", isEqualTo: true)
|
||||
.where("is_deleted", isEqualTo: false);
|
||||
if (!onlyFcs) {
|
||||
if (isCustomer) {
|
||||
pageQuery = pageQuery.where("user_id", isEqualTo: user.id);
|
||||
}
|
||||
pageQuery = pageQuery.orderBy("current_status_date", descending: true);
|
||||
pagination = new Pagination(pageQuery, rowPerLoad: 20);
|
||||
pagination.stream.listen((doc) {
|
||||
if (doc == null) {
|
||||
endOfDeliveredPackages = true;
|
||||
} else {
|
||||
deliveredPackages.add(Package.fromMap(doc.data, doc.documentID));
|
||||
// var m = Message.fromMap(doc.data, doc.documentID);
|
||||
// deliveredPackages.add(Package(
|
||||
// id: m.id,
|
||||
// status: package_delivered_status,
|
||||
// currentStatus: package_delivered_status,
|
||||
// currentStatusDate: m.date,
|
||||
// trackingID: (count++).toString(),
|
||||
// market: m.message));
|
||||
}
|
||||
var paginator = new Paginator(pageQuery, rowPerLoad: 20, toObj: (data, id) {
|
||||
return Package.fromMap(data, id);
|
||||
});
|
||||
return null;
|
||||
return paginator;
|
||||
}
|
||||
|
||||
Future<bool> loadMoreDeliveredPackages() {
|
||||
if (pagination != null && !isLoading && !endOfDeliveredPackages) {
|
||||
isLoading = true;
|
||||
notifyListeners();
|
||||
pagination.load().then((value) {
|
||||
isLoading = false;
|
||||
notifyListeners();
|
||||
});
|
||||
}
|
||||
return null;
|
||||
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 Package(
|
||||
id: m.id,
|
||||
status: package_delivered_status,
|
||||
currentStatus: package_delivered_status,
|
||||
currentStatusDate: m.date,
|
||||
trackingID: (count++).toString(),
|
||||
market: m.message);
|
||||
});
|
||||
return paginator;
|
||||
}
|
||||
|
||||
Future<void> _loadPackages() async {
|
||||
@@ -105,7 +135,7 @@ class PackageModel extends BaseModel {
|
||||
return;
|
||||
String path = "/$packages_collection";
|
||||
if (listener != null) listener.cancel();
|
||||
packages = [];
|
||||
_packages = [];
|
||||
|
||||
try {
|
||||
var q = Firestore.instance
|
||||
@@ -114,8 +144,8 @@ class PackageModel extends BaseModel {
|
||||
.where("is_deleted", isEqualTo: false);
|
||||
|
||||
listener = q.snapshots().listen((QuerySnapshot snapshot) {
|
||||
packages.clear();
|
||||
packages = snapshot.documents.map((documentSnapshot) {
|
||||
_packages.clear();
|
||||
_packages = snapshot.documents.map((documentSnapshot) {
|
||||
var package = Package.fromMap(
|
||||
documentSnapshot.data, documentSnapshot.documentID);
|
||||
return package;
|
||||
@@ -131,7 +161,7 @@ class PackageModel extends BaseModel {
|
||||
if (user == null) return;
|
||||
String path = "/$packages_collection";
|
||||
if (customerPackageListener != null) customerPackageListener.cancel();
|
||||
customerPackages = [];
|
||||
_customerPackages = [];
|
||||
|
||||
try {
|
||||
var q = Firestore.instance
|
||||
@@ -141,8 +171,8 @@ class PackageModel extends BaseModel {
|
||||
.where("user_id", isEqualTo: user.id);
|
||||
|
||||
customerPackageListener = q.snapshots().listen((QuerySnapshot snapshot) {
|
||||
customerPackages.clear();
|
||||
customerPackages = snapshot.documents.map((documentSnapshot) {
|
||||
_customerPackages.clear();
|
||||
_customerPackages = snapshot.documents.map((documentSnapshot) {
|
||||
var package = Package.fromMap(
|
||||
documentSnapshot.data, documentSnapshot.documentID);
|
||||
return package;
|
||||
|
||||
Reference in New Issue
Block a user