add pagination for invoice and update ui for material 3
This commit is contained in:
@@ -7,131 +7,65 @@ import 'package:fcs/domain/constants.dart';
|
||||
import 'package:fcs/domain/entities/invoice.dart';
|
||||
import 'package:fcs/domain/entities/payment.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;
|
||||
|
||||
import '../../../pagination/paginator_listener.dart';
|
||||
|
||||
class InvoiceModel extends BaseModel {
|
||||
final log = Logger('InvoiceModel');
|
||||
|
||||
StreamSubscription<QuerySnapshot<Map<String, dynamic>>>? listener;
|
||||
|
||||
List<Invoice> _invoices = [];
|
||||
|
||||
List<Invoice> get invoices =>
|
||||
_selectedIndex == 1 ? _invoices : List<Invoice>.from(_paginator!.values);
|
||||
|
||||
Paginator? _paginator;
|
||||
|
||||
bool endOfPaidInvoices = false;
|
||||
bool isLoading = false;
|
||||
int _selectedIndex = 1;
|
||||
|
||||
set selectedIndex(int index) {
|
||||
_selectedIndex = index;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
int get selectedIndex => _selectedIndex;
|
||||
|
||||
@override
|
||||
void privilegeChanged() {
|
||||
super.privilegeChanged();
|
||||
}
|
||||
|
||||
initData(bool forCustomer, bool isCanceled, bool isPaid) {
|
||||
_loadInvoices(forCustomer);
|
||||
|
||||
if (_paginator != null) _paginator!.close();
|
||||
_paginator = _getPaginator(forCustomer, isCanceled, isPaid);
|
||||
_paginator!.load(onFinished: () {
|
||||
notifyListeners();
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _loadInvoices(bool forCustomer) async {
|
||||
if (user == null) return;
|
||||
if (!forCustomer && !user!.hasInvoices()) return;
|
||||
String path = "/$invoices_collection";
|
||||
if (listener != null) listener!.cancel();
|
||||
_invoices = [];
|
||||
|
||||
try {
|
||||
var q = FirebaseFirestore.instance
|
||||
.collection("$path")
|
||||
.where("status", isEqualTo: invoice_issued_status)
|
||||
.where("is_deleted", isEqualTo: false);
|
||||
|
||||
if (forCustomer) {
|
||||
q = q.where("user_id", isEqualTo: user!.id);
|
||||
}
|
||||
|
||||
listener = q.snapshots().listen((QuerySnapshot snapshot) {
|
||||
_invoices.clear();
|
||||
_invoices = snapshot.docs.map((documentSnapshot) {
|
||||
var s = Invoice.fromMap(
|
||||
documentSnapshot.data() as Map<String, dynamic>,
|
||||
documentSnapshot.id);
|
||||
return s;
|
||||
}).toList();
|
||||
notifyListeners();
|
||||
});
|
||||
} catch (e) {
|
||||
log.warning("Error!! $e");
|
||||
}
|
||||
}
|
||||
|
||||
Paginator _getPaginator(bool isCustomer, bool isCanceled, bool isPaid) {
|
||||
if (!isCustomer) {
|
||||
if (user == null || !(user!.hasInvoices())) throw "No privilege";
|
||||
}
|
||||
var pageQuery = FirebaseFirestore.instance
|
||||
.collection("/$invoices_collection")
|
||||
.where("is_deleted", isEqualTo: false);
|
||||
if (isCustomer) {
|
||||
pageQuery = pageQuery.where("user_id", isEqualTo: user!.id);
|
||||
}
|
||||
if (isCanceled) {
|
||||
pageQuery = pageQuery.where("status", isEqualTo: invoice_cancel_status);
|
||||
}
|
||||
if (isPaid) {
|
||||
pageQuery = pageQuery.where("status", isEqualTo: invoice_paid_status);
|
||||
}
|
||||
|
||||
pageQuery = pageQuery.orderBy("created_at", descending: true);
|
||||
var paginator = new Paginator(pageQuery, rowPerLoad: 20, toObj: (data, id) {
|
||||
return Invoice.fromMap(data, id);
|
||||
});
|
||||
return paginator;
|
||||
}
|
||||
|
||||
Future<void> loadMore({bool? isCustomer}) async {
|
||||
if (_paginator!.ended || _selectedIndex == 1)
|
||||
return; // when paid menu is not selected return
|
||||
isLoading = true;
|
||||
notifyListeners();
|
||||
await _paginator!.load(onFinished: () {
|
||||
isLoading = false;
|
||||
notifyListeners();
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> refresh({bool? isCustomer}) async {
|
||||
if (_selectedIndex == 1) return; // when paid menu is not selected return
|
||||
await _paginator!.refresh(onFinished: () {
|
||||
notifyListeners();
|
||||
});
|
||||
}
|
||||
PaginatorListener<Invoice>? getInvoices;
|
||||
int selectedIndex = 1;
|
||||
|
||||
void initUser(user) {
|
||||
super.initUser(user);
|
||||
}
|
||||
|
||||
logout() async {
|
||||
if (_paginator != null) _paginator!.close();
|
||||
if (listener != null) await listener!.cancel();
|
||||
_invoices = [];
|
||||
getInvoices?.close();
|
||||
}
|
||||
|
||||
onChanged(int index, bool isCustomer) {
|
||||
selectedIndex = index;
|
||||
loadPaginationInvoices(index, isCustomer);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> loadPaginationInvoices(int index, bool isCustomer) async {
|
||||
if (user == null) return;
|
||||
if (!isCustomer && !user!.hasInvoices()) return;
|
||||
|
||||
String path = "/$invoices_collection";
|
||||
Query col = FirebaseFirestore.instance.collection(path);
|
||||
Query pageQuery = FirebaseFirestore.instance.collection(path);
|
||||
|
||||
if (isCustomer) {
|
||||
col = col.where("user_id", isEqualTo: user!.id);
|
||||
pageQuery = pageQuery.where("user_id", isEqualTo: user!.id);
|
||||
}
|
||||
|
||||
if (index == 1) {
|
||||
col = col.where("status", isEqualTo: invoice_issued_status);
|
||||
pageQuery = pageQuery.where("status", isEqualTo: invoice_issued_status);
|
||||
}
|
||||
|
||||
if (index == 2) {
|
||||
col = col.where("status", isEqualTo: invoice_paid_status);
|
||||
pageQuery = pageQuery.where("status", isEqualTo: invoice_paid_status);
|
||||
}
|
||||
|
||||
if (index == 3) {
|
||||
col = col.where("status", isEqualTo: invoice_cancel_status);
|
||||
pageQuery = pageQuery.where("status", isEqualTo: invoice_cancel_status);
|
||||
}
|
||||
|
||||
pageQuery = pageQuery.orderBy("created_at", descending: true);
|
||||
|
||||
getInvoices?.close();
|
||||
getInvoices = PaginatorListener<Invoice>(
|
||||
col, pageQuery, (data, id) => Invoice.fromMap(data, id),
|
||||
rowPerLoad: 30);
|
||||
}
|
||||
|
||||
Future<Invoice?> getInvoice(String id) async {
|
||||
@@ -140,7 +74,7 @@ class InvoiceModel extends BaseModel {
|
||||
var ref = FirebaseFirestore.instance.collection("$path").doc(id);
|
||||
var snap = await ref.get(const GetOptions(source: Source.server));
|
||||
if (snap.exists) {
|
||||
var s = Invoice.fromMap(snap.data as Map<String, dynamic>, snap.id);
|
||||
var s = Invoice.fromMap(snap.data() as Map<String, dynamic>, snap.id);
|
||||
return s;
|
||||
}
|
||||
} catch (e) {
|
||||
|
||||
Reference in New Issue
Block a user