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;
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import 'package:fcs/domain/entities/package.dart';
|
||||
import 'package:fcs/helpers/paginator.dart';
|
||||
import 'package:fcs/helpers/theme.dart';
|
||||
import 'package:fcs/pages/package/model/package_model.dart';
|
||||
import 'package:fcs/pages/package/package_info.dart';
|
||||
import 'package:fcs/pages/package/package_list_row.dart';
|
||||
import 'package:fcs/pages/package_search/package_serach.dart';
|
||||
import 'package:fcs/pages/widgets/bottom_up_page_route.dart';
|
||||
import 'package:fcs/pages/widgets/local_popup_menu_button.dart';
|
||||
import 'package:fcs/pages/widgets/local_popupmenu.dart';
|
||||
import 'package:fcs/pages/widgets/local_text.dart';
|
||||
@@ -23,43 +23,40 @@ class PackageList extends StatefulWidget {
|
||||
|
||||
class _PackageListState extends State<PackageList> {
|
||||
bool _isLoading = false;
|
||||
bool _showDelivered = false;
|
||||
var _controller = ScrollController();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
Provider.of<PackageModel>(context, listen: false)
|
||||
.initDeliveredPackages(widget.onlyFcs);
|
||||
_controller.addListener(() {
|
||||
if (_showDelivered &&
|
||||
_controller.position.pixels == _controller.position.maxScrollExtent) {
|
||||
|
||||
_controller.addListener(() async {
|
||||
if (_controller.position.pixels == _controller.position.maxScrollExtent) {
|
||||
Provider.of<PackageModel>(context, listen: false)
|
||||
.loadMoreDeliveredPackages();
|
||||
.loadMore(isCustomer: !widget.onlyFcs);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var packageModel = Provider.of<PackageModel>(context);
|
||||
bool onlyFcs = widget.onlyFcs;
|
||||
var packages = _showDelivered
|
||||
? packageModel.deliveredPackages
|
||||
: onlyFcs ? packageModel.packages : packageModel.customerPackages;
|
||||
var packages =
|
||||
onlyFcs ? packageModel.packages : packageModel.customerPackages;
|
||||
|
||||
final popupMenu = LocalPopupMenuButton(
|
||||
popmenus: [
|
||||
LocalPopupMenu(
|
||||
id: 1, textKey: "package.popupmenu.active", selected: true),
|
||||
LocalPopupMenu(id: 2, textKey: "package.popupmenu.delivered")
|
||||
id: 1,
|
||||
textKey: "package.popupmenu.active",
|
||||
selected: packageModel.selectedIndex == 1),
|
||||
LocalPopupMenu(
|
||||
id: 2,
|
||||
textKey: "package.popupmenu.delivered",
|
||||
selected: packageModel.selectedIndex == 2)
|
||||
],
|
||||
popupMenuCallback: (p) => this.setState(() {
|
||||
_showDelivered = p.id == 2;
|
||||
packageModel.selectedIndex = p.id;
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -97,21 +94,25 @@ class _PackageListState extends State<PackageList> {
|
||||
body: Column(
|
||||
children: [
|
||||
Expanded(
|
||||
child: ListView.separated(
|
||||
controller: _controller,
|
||||
separatorBuilder: (context, index) => Divider(
|
||||
color: Colors.black,
|
||||
),
|
||||
scrollDirection: Axis.vertical,
|
||||
padding: EdgeInsets.only(top: 15),
|
||||
shrinkWrap: true,
|
||||
itemCount: packages.length,
|
||||
itemBuilder: (BuildContext context, int index) {
|
||||
return PackageListRow(
|
||||
key: ValueKey(packages[index].id),
|
||||
package: packages[index],
|
||||
);
|
||||
}),
|
||||
child: RefreshIndicator(
|
||||
child: ListView.separated(
|
||||
controller: _controller,
|
||||
separatorBuilder: (context, index) => Divider(
|
||||
color: Colors.black,
|
||||
),
|
||||
scrollDirection: Axis.vertical,
|
||||
padding: EdgeInsets.only(top: 15),
|
||||
shrinkWrap: true,
|
||||
itemCount: packages.length,
|
||||
itemBuilder: (BuildContext context, int index) {
|
||||
return PackageListRow(
|
||||
key: ValueKey(packages[index].id),
|
||||
package: packages[index],
|
||||
);
|
||||
}),
|
||||
onRefresh: () =>
|
||||
packageModel.refresh(isCustomer: !widget.onlyFcs),
|
||||
),
|
||||
),
|
||||
packageModel.isLoading
|
||||
? Container(
|
||||
|
||||
Reference in New Issue
Block a user